github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/builtin/providers/template/resource_template_file_test.go (about)

     1  package template
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"testing"
     7  
     8  	r "github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  var testProviders = map[string]terraform.ResourceProvider{
    13  	"template": Provider(),
    14  }
    15  
    16  func TestTemplateRendering(t *testing.T) {
    17  	var cases = []struct {
    18  		vars     string
    19  		template string
    20  		want     string
    21  	}{
    22  		{`{}`, `ABC`, `ABC`},
    23  		{`{a="foo"}`, `${a}`, `foo`},
    24  		{`{a="hello"}`, `${replace(a, "ello", "i")}`, `hi`},
    25  		{`{}`, `${1+2+3}`, `6`},
    26  	}
    27  
    28  	for _, tt := range cases {
    29  		r.Test(t, r.TestCase{
    30  			Providers: testProviders,
    31  			Steps: []r.TestStep{
    32  				r.TestStep{
    33  					Config: testTemplateConfig(tt.template, tt.vars),
    34  					Check: func(s *terraform.State) error {
    35  						got := s.RootModule().Outputs["rendered"]
    36  						if tt.want != got {
    37  							return fmt.Errorf("template:\n%s\nvars:\n%s\ngot:\n%s\nwant:\n%s\n", tt.template, tt.vars, got, tt.want)
    38  						}
    39  						return nil
    40  					},
    41  				},
    42  			},
    43  		})
    44  	}
    45  }
    46  
    47  // https://github.com/hashicorp/terraform/issues/2344
    48  func TestTemplateVariableChange(t *testing.T) {
    49  	steps := []struct {
    50  		vars     string
    51  		template string
    52  		want     string
    53  	}{
    54  		{`{a="foo"}`, `${a}`, `foo`},
    55  		{`{b="bar"}`, `${b}`, `bar`},
    56  	}
    57  
    58  	var testSteps []r.TestStep
    59  	for i, step := range steps {
    60  		testSteps = append(testSteps, r.TestStep{
    61  			Config: testTemplateConfig(step.template, step.vars),
    62  			Check: func(i int, want string) r.TestCheckFunc {
    63  				return func(s *terraform.State) error {
    64  					got := s.RootModule().Outputs["rendered"]
    65  					if want != got {
    66  						return fmt.Errorf("[%d] got:\n%q\nwant:\n%q\n", i, got, want)
    67  					}
    68  					return nil
    69  				}
    70  			}(i, step.want),
    71  		})
    72  	}
    73  
    74  	r.Test(t, r.TestCase{
    75  		Providers: testProviders,
    76  		Steps:     testSteps,
    77  	})
    78  }
    79  
    80  // This test covers a panic due to config.Func formerly being a
    81  // shared map, causing multiple template_file resources to try and
    82  // accessing it parallel during their lang.Eval() runs.
    83  //
    84  // Before fix, test fails under `go test -race`
    85  func TestTemplateSharedMemoryRace(t *testing.T) {
    86  	var wg sync.WaitGroup
    87  	for i := 0; i < 100; i++ {
    88  		go func(wg sync.WaitGroup, t *testing.T, i int) {
    89  			wg.Add(1)
    90  			out, err := execute("don't panic!", map[string]interface{}{})
    91  			if err != nil {
    92  				t.Fatalf("err: %s", err)
    93  			}
    94  			if out != "don't panic!" {
    95  				t.Fatalf("bad output: %s", out)
    96  			}
    97  			wg.Done()
    98  		}(wg, t, i)
    99  	}
   100  	wg.Wait()
   101  }
   102  
   103  func testTemplateConfig(template, vars string) string {
   104  	return fmt.Sprintf(`
   105  		resource "template_file" "t0" {
   106  			template = "%s"
   107  			vars = %s
   108  		}
   109  		output "rendered" {
   110  				value = "${template_file.t0.rendered}"
   111  		}`, template, vars)
   112  }