github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/template/resource_template_dir_test.go (about)

     1  package template
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"errors"
     8  	r "github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  )
    14  
    15  const templateDirRenderingConfig = `
    16  resource "template_dir" "dir" {
    17  	source_dir = "%s"
    18    destination_dir = "%s"
    19    vars = %s
    20  }`
    21  
    22  type testTemplate struct {
    23  	template string
    24  	want     string
    25  }
    26  
    27  func testTemplateDirWriteFiles(files map[string]testTemplate) (in, out string, err error) {
    28  	in, err = ioutil.TempDir(os.TempDir(), "terraform_template_dir")
    29  	if err != nil {
    30  		return
    31  	}
    32  
    33  	for name, file := range files {
    34  		path := filepath.Join(in, name)
    35  
    36  		err = os.MkdirAll(filepath.Dir(path), 0777)
    37  		if err != nil {
    38  			return
    39  		}
    40  
    41  		err = ioutil.WriteFile(path, []byte(file.template), 0777)
    42  		if err != nil {
    43  			return
    44  		}
    45  	}
    46  
    47  	out = fmt.Sprintf("%s.out", in)
    48  	return
    49  }
    50  
    51  func TestTemplateDirRendering(t *testing.T) {
    52  	var cases = []struct {
    53  		vars  string
    54  		files map[string]testTemplate
    55  	}{
    56  		{
    57  			files: map[string]testTemplate{
    58  				"foo.txt":           {"${bar}", "bar"},
    59  				"nested/monkey.txt": {"ooh-ooh-ooh-eee-eee", "ooh-ooh-ooh-eee-eee"},
    60  				"maths.txt":         {"${1+2+3}", "6"},
    61  			},
    62  			vars: `{bar = "bar"}`,
    63  		},
    64  	}
    65  
    66  	for _, tt := range cases {
    67  		// Write the desired templates in a temporary directory.
    68  		in, out, err := testTemplateDirWriteFiles(tt.files)
    69  		if err != nil {
    70  			t.Skipf("could not write templates to temporary directory: %s", err)
    71  			continue
    72  		}
    73  		defer os.RemoveAll(in)
    74  		defer os.RemoveAll(out)
    75  
    76  		// Run test case.
    77  		r.UnitTest(t, r.TestCase{
    78  			Providers: testProviders,
    79  			Steps: []r.TestStep{
    80  				{
    81  					Config: fmt.Sprintf(templateDirRenderingConfig, in, out, tt.vars),
    82  					Check: func(s *terraform.State) error {
    83  						for name, file := range tt.files {
    84  							content, err := ioutil.ReadFile(filepath.Join(out, name))
    85  							if err != nil {
    86  								return fmt.Errorf("template:\n%s\nvars:\n%s\ngot:\n%s\nwant:\n%s\n", file.template, tt.vars, err, file.want)
    87  							}
    88  							if string(content) != file.want {
    89  								return fmt.Errorf("template:\n%s\nvars:\n%s\ngot:\n%s\nwant:\n%s\n", file.template, tt.vars, content, file.want)
    90  							}
    91  						}
    92  						return nil
    93  					},
    94  				},
    95  			},
    96  			CheckDestroy: func(*terraform.State) error {
    97  				if _, err := os.Stat(out); os.IsNotExist(err) {
    98  					return nil
    99  				}
   100  				return errors.New("template_dir did not get destroyed")
   101  			},
   102  		})
   103  	}
   104  }