github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/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: ` 38 resource "template_file" "t0" { 39 filename = "mock" 40 vars = ` + tt.vars + ` 41 } 42 output "rendered" { 43 value = "${template_file.t0.rendered}" 44 } 45 `, 46 Check: func(s *terraform.State) error { 47 got := s.RootModule().Outputs["rendered"] 48 if tt.want != got { 49 return fmt.Errorf("template:\n%s\nvars:\n%s\ngot:\n%s\nwant:\n%s\n", tt.template, tt.vars, got, tt.want) 50 } 51 return nil 52 }, 53 }, 54 }, 55 }) 56 } 57 }