github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/ignition/resource_ignition_config_test.go (about)

     1  package ignition
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/coreos/ignition/config/types"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestIngnitionFileReplace(t *testing.T) {
    14  	testIgnition(t, `
    15  		data "ignition_config" "test" {
    16  			replace {
    17  				source = "foo"
    18  				verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    19  			}
    20  		}
    21  	`, func(c *types.Config) error {
    22  		r := c.Ignition.Config.Replace
    23  		if r == nil {
    24  			return fmt.Errorf("unable to find replace config")
    25  		}
    26  
    27  		if r.Source.String() != "foo" {
    28  			return fmt.Errorf("config.replace.source, found %q", r.Source)
    29  		}
    30  
    31  		if r.Verification.Hash.Sum != "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" {
    32  			return fmt.Errorf("config.replace.verification, found %q", r.Verification.Hash)
    33  		}
    34  
    35  		return nil
    36  	})
    37  }
    38  
    39  func TestIngnitionFileAppend(t *testing.T) {
    40  	testIgnition(t, `
    41  		data "ignition_config" "test" {
    42  			append {
    43  				source = "foo"
    44  				verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    45  			}
    46  
    47  		    append {
    48  		    	source = "foo"
    49  		    	verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    50  			}
    51  		}
    52  	`, func(c *types.Config) error {
    53  		a := c.Ignition.Config.Append
    54  		if len(a) != 2 {
    55  			return fmt.Errorf("unable to find append config, expected 2")
    56  		}
    57  
    58  		if a[0].Source.String() != "foo" {
    59  			return fmt.Errorf("config.replace.source, found %q", a[0].Source)
    60  		}
    61  
    62  		if a[0].Verification.Hash.Sum != "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" {
    63  			return fmt.Errorf("config.replace.verification, found %q", a[0].Verification.Hash)
    64  		}
    65  
    66  		return nil
    67  	})
    68  }
    69  
    70  func testIgnition(t *testing.T, input string, assert func(*types.Config) error) {
    71  	check := func(s *terraform.State) error {
    72  		got := s.RootModule().Outputs["rendered"].Value.(string)
    73  
    74  		c := &types.Config{}
    75  		err := json.Unmarshal([]byte(got), c)
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		return assert(c)
    81  	}
    82  
    83  	resource.Test(t, resource.TestCase{
    84  		Providers: testProviders,
    85  		Steps: []resource.TestStep{
    86  			{
    87  				Config: fmt.Sprintf(testTemplate, input),
    88  				Check:  check,
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  var testTemplate = `
    95  %s
    96  
    97  output "rendered" {
    98  	value = "${data.ignition_config.test.rendered}"
    99  }
   100  
   101  `