github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/builtin/providers/template/resource_test.go (about) 1 package template 2 3 import ( 4 "fmt" 5 "testing" 6 7 r "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 var testProviders = map[string]terraform.ResourceProvider{ 12 "template": Provider(), 13 } 14 15 func TestTemplateRendering(t *testing.T) { 16 var cases = []struct { 17 vars string 18 template string 19 want string 20 }{ 21 {`{}`, `ABC`, `ABC`}, 22 {`{a="foo"}`, `${a}`, `foo`}, 23 {`{a="hello"}`, `${replace(a, "ello", "i")}`, `hi`}, 24 {`{}`, `${1+2+3}`, `6`}, 25 } 26 27 for _, tt := range cases { 28 r.Test(t, r.TestCase{ 29 PreCheck: func() { 30 readfile = func(string) ([]byte, error) { 31 return []byte(tt.template), nil 32 } 33 }, 34 Providers: testProviders, 35 Steps: []r.TestStep{ 36 r.TestStep{ 37 Config: testTemplateConfig(tt.vars), 38 Check: func(s *terraform.State) error { 39 got := s.RootModule().Outputs["rendered"] 40 if tt.want != got { 41 return fmt.Errorf("template:\n%s\nvars:\n%s\ngot:\n%s\nwant:\n%s\n", tt.template, tt.vars, got, tt.want) 42 } 43 return nil 44 }, 45 }, 46 }, 47 }) 48 } 49 } 50 51 // https://github.com/hashicorp/terraform/issues/2344 52 func TestTemplateVariableChange(t *testing.T) { 53 steps := []struct { 54 vars string 55 template string 56 want string 57 }{ 58 {`{a="foo"}`, `${a}`, `foo`}, 59 {`{b="bar"}`, `${b}`, `bar`}, 60 } 61 62 var testSteps []r.TestStep 63 for i, step := range steps { 64 testSteps = append(testSteps, r.TestStep{ 65 PreConfig: func(template string) func() { 66 return func() { 67 readfile = func(string) ([]byte, error) { 68 return []byte(template), nil 69 } 70 } 71 }(step.template), 72 Config: testTemplateConfig(step.vars), 73 Check: func(i int, want string) r.TestCheckFunc { 74 return func(s *terraform.State) error { 75 got := s.RootModule().Outputs["rendered"] 76 if want != got { 77 return fmt.Errorf("[%d] got:\n%q\nwant:\n%q\n", i, got, want) 78 } 79 return nil 80 } 81 }(i, step.want), 82 }) 83 } 84 85 r.Test(t, r.TestCase{ 86 Providers: testProviders, 87 Steps: testSteps, 88 }) 89 } 90 91 func testTemplateConfig(vars string) string { 92 return ` 93 resource "template_file" "t0" { 94 filename = "mock" 95 vars = ` + vars + ` 96 } 97 output "rendered" { 98 value = "${template_file.t0.rendered}" 99 } 100 ` 101 }