terraform/config/variable_test.go

140 lines
2.7 KiB
Go
Raw Normal View History

2014-05-24 07:07:33 +02:00
package config
import (
"reflect"
"testing"
"github.com/mitchellh/reflectwalk"
2014-05-24 07:07:33 +02:00
)
2014-05-24 07:10:32 +02:00
func BenchmarkVariableDetectWalker(b *testing.B) {
w := new(variableDetectWalker)
str := reflect.ValueOf(`foo ${var.bar} bar ${bar.baz.bing} $${escaped}`)
b.ResetTimer()
2014-05-24 07:10:32 +02:00
for i := 0; i < b.N; i++ {
w.Variables = nil
w.Primitive(str)
}
}
2014-05-24 07:07:33 +02:00
func TestVariableDetectWalker(t *testing.T) {
w := new(variableDetectWalker)
str := `foo ${var.bar}`
if err := w.Primitive(reflect.ValueOf(str)); err != nil {
t.Fatalf("err: %s", err)
}
if len(w.Variables) != 1 {
t.Fatalf("bad: %#v", w.Variables)
}
if w.Variables["var.bar"].(*UserVariable).FullKey() != "var.bar" {
t.Fatalf("bad: %#v", w.Variables)
}
}
func TestVariableDetectWalker_resource(t *testing.T) {
w := new(variableDetectWalker)
str := `foo ${ec2.foo.bar}`
if err := w.Primitive(reflect.ValueOf(str)); err != nil {
t.Fatalf("err: %s", err)
}
if len(w.Variables) != 1 {
t.Fatalf("bad: %#v", w.Variables)
}
if w.Variables["ec2.foo.bar"].(*ResourceVariable).FullKey() != "ec2.foo.bar" {
t.Fatalf("bad: %#v", w.Variables)
}
}
2014-05-24 07:07:33 +02:00
func TestVariableDetectWalker_bad(t *testing.T) {
w := new(variableDetectWalker)
str := `foo ${bar}`
if err := w.Primitive(reflect.ValueOf(str)); err == nil {
t.Fatal("should error")
}
}
func TestVariableDetectWalker_escaped(t *testing.T) {
w := new(variableDetectWalker)
str := `foo $${var.bar}`
if err := w.Primitive(reflect.ValueOf(str)); err != nil {
t.Fatalf("err: %s", err)
}
if len(w.Variables) > 0 {
t.Fatalf("bad: %#v", w.Variables)
}
}
func TestVariableDetectWalker_empty(t *testing.T) {
w := new(variableDetectWalker)
str := `foo`
if err := w.Primitive(reflect.ValueOf(str)); err != nil {
t.Fatalf("err: %s", err)
}
if len(w.Variables) > 0 {
t.Fatalf("bad: %#v", w.Variables)
}
}
func TestVariableReplaceWalker(t *testing.T) {
w := &variableReplaceWalker{
Values: map[string]string{
"var.bar": "bar",
},
}
cases := []struct {
Input interface{}
Output interface{}
}{
{
`foo ${var.bar}`,
"foo bar",
},
{
[]string{"foo", "${var.bar}"},
[]string{"foo", "bar"},
},
{
map[string]interface{}{
"ami": "${var.bar}",
"security_groups": []interface{}{
"foo",
"${var.bar}",
},
},
map[string]interface{}{
"ami": "bar",
"security_groups": []interface{}{
"foo",
"bar",
},
},
},
}
for i, tc := range cases {
var input interface{} = tc.Input
if reflect.ValueOf(tc.Input).Kind() == reflect.String {
input = &tc.Input
}
if err := reflectwalk.Walk(input, w); err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(tc.Input, tc.Output) {
t.Fatalf("bad %d: %#v", i, tc.Input)
}
}
}