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