2015-05-04 19:26:17 +02:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-10 19:34:56 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2016-01-15 22:28:47 +01:00
|
|
|
"sync"
|
2015-05-04 19:26:17 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
r "github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testProviders = map[string]terraform.ResourceProvider{
|
|
|
|
"template": Provider(),
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateRendering(t *testing.T) {
|
|
|
|
var cases = []struct {
|
|
|
|
vars string
|
|
|
|
template string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{`{}`, `ABC`, `ABC`},
|
2016-05-22 00:09:55 +02:00
|
|
|
{`{a="foo"}`, `$${a}`, `foo`},
|
|
|
|
{`{a="hello"}`, `$${replace(a, "ello", "i")}`, `hi`},
|
2015-05-04 19:26:17 +02:00
|
|
|
{`{}`, `${1+2+3}`, `6`},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range cases {
|
2016-05-22 00:09:55 +02:00
|
|
|
r.UnitTest(t, r.TestCase{
|
2015-05-04 19:26:17 +02:00
|
|
|
Providers: testProviders,
|
|
|
|
Steps: []r.TestStep{
|
|
|
|
r.TestStep{
|
2015-11-13 18:07:02 +01:00
|
|
|
Config: testTemplateConfig(tt.template, tt.vars),
|
2015-05-04 19:26:17 +02:00
|
|
|
Check: func(s *terraform.State) error {
|
|
|
|
got := s.RootModule().Outputs["rendered"]
|
2016-05-12 02:05:02 +02:00
|
|
|
if tt.want != got.Value {
|
2015-05-04 19:26:17 +02:00
|
|
|
return fmt.Errorf("template:\n%s\nvars:\n%s\ngot:\n%s\nwant:\n%s\n", tt.template, tt.vars, got, tt.want)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-06-17 20:58:01 +02:00
|
|
|
|
2016-03-10 19:34:56 +01:00
|
|
|
func TestValidateTemplateAttribute(t *testing.T) {
|
|
|
|
file, err := ioutil.TempFile("", "testtemplate")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
file.WriteString("Hello world.")
|
|
|
|
file.Close()
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
ws, es := validateTemplateAttribute(file.Name(), "test")
|
|
|
|
|
|
|
|
if len(es) != 0 {
|
|
|
|
t.Fatalf("Unexpected errors: %#v", es)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ws) != 1 {
|
|
|
|
t.Fatalf("Expected 1 warning, got %d", len(ws))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(ws[0], "Specifying a path directly is deprecated") {
|
|
|
|
t.Fatalf("Expected warning about path, got: %s", ws[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 22:39:34 +02:00
|
|
|
func TestValidateVarsAttribute(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Vars map[string]interface{}
|
|
|
|
ExpectErr string
|
|
|
|
}{
|
|
|
|
"lists are invalid": {
|
|
|
|
map[string]interface{}{
|
|
|
|
"list": []interface{}{},
|
|
|
|
},
|
|
|
|
`vars: cannot contain non-primitives`,
|
|
|
|
},
|
|
|
|
"maps are invalid": {
|
|
|
|
map[string]interface{}{
|
|
|
|
"map": map[string]interface{}{},
|
|
|
|
},
|
|
|
|
`vars: cannot contain non-primitives`,
|
|
|
|
},
|
|
|
|
"strings, integers, floats, and bools are AOK": {
|
|
|
|
map[string]interface{}{
|
|
|
|
"string": "foo",
|
|
|
|
"int": 1,
|
|
|
|
"bool": true,
|
|
|
|
"float": float64(1.0),
|
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for tn, tc := range cases {
|
|
|
|
_, es := validateVarsAttribute(tc.Vars, "vars")
|
|
|
|
if len(es) > 0 {
|
|
|
|
if tc.ExpectErr == "" {
|
|
|
|
t.Fatalf("%s: expected no err, got: %#v", tn, es)
|
|
|
|
}
|
|
|
|
if !strings.Contains(es[0].Error(), tc.ExpectErr) {
|
|
|
|
t.Fatalf("%s: expected\n%s\nto contain\n%s", tn, es[0], tc.ExpectErr)
|
|
|
|
}
|
|
|
|
} else if tc.ExpectErr != "" {
|
|
|
|
t.Fatalf("%s: expected err containing %q, got none!", tn, tc.ExpectErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 22:28:47 +01:00
|
|
|
// This test covers a panic due to config.Func formerly being a
|
|
|
|
// shared map, causing multiple template_file resources to try and
|
|
|
|
// accessing it parallel during their lang.Eval() runs.
|
|
|
|
//
|
|
|
|
// Before fix, test fails under `go test -race`
|
|
|
|
func TestTemplateSharedMemoryRace(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
go func(wg sync.WaitGroup, t *testing.T, i int) {
|
|
|
|
wg.Add(1)
|
|
|
|
out, err := execute("don't panic!", map[string]interface{}{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if out != "don't panic!" {
|
|
|
|
t.Fatalf("bad output: %s", out)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}(wg, t, i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2015-11-13 18:07:02 +01:00
|
|
|
func testTemplateConfig(template, vars string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-05-22 00:09:55 +02:00
|
|
|
data "template_file" "t0" {
|
2015-11-13 18:07:02 +01:00
|
|
|
template = "%s"
|
|
|
|
vars = %s
|
|
|
|
}
|
|
|
|
output "rendered" {
|
2016-05-22 00:09:55 +02:00
|
|
|
value = "${data.template_file.t0.rendered}"
|
2015-11-13 18:07:02 +01:00
|
|
|
}`, template, vars)
|
2015-06-17 20:58:01 +02:00
|
|
|
}
|