terraform/config/interpolate_walk_test.go

190 lines
3.3 KiB
Go
Raw Normal View History

2014-07-21 20:24:44 +02:00
package config
import (
2015-01-13 19:27:57 +01:00
"fmt"
2014-07-21 20:24:44 +02:00
"reflect"
"testing"
2015-01-13 19:27:57 +01:00
"github.com/hashicorp/terraform/config/lang/ast"
2014-07-21 20:24:44 +02:00
"github.com/mitchellh/reflectwalk"
)
func TestInterpolationWalker_detect(t *testing.T) {
cases := []struct {
Input interface{}
2015-01-13 19:27:57 +01:00
Result []string
2014-07-21 20:24:44 +02:00
}{
{
Input: map[string]interface{}{
"foo": "$${var.foo}",
},
Result: nil,
},
{
Input: map[string]interface{}{
"foo": "${var.foo}",
},
2015-01-13 19:27:57 +01:00
Result: []string{
"Variable(var.foo)",
2014-07-21 20:24:44 +02:00
},
},
2014-08-05 19:11:56 +02:00
{
Input: map[string]interface{}{
"foo": "${aws_instance.foo.*.num}",
},
2015-01-13 19:27:57 +01:00
Result: []string{
"Variable(aws_instance.foo.*.num)",
2014-08-05 19:11:56 +02:00
},
},
{
Input: map[string]interface{}{
"foo": "${lookup(var.foo)}",
},
2015-01-13 19:27:57 +01:00
Result: []string{
"Call(lookup, Variable(var.foo))",
},
},
{
Input: map[string]interface{}{
"foo": `${file("test.txt")}`,
},
2015-01-13 19:27:57 +01:00
Result: []string{
"Call(file, Literal(TypeString, test.txt))",
},
},
2014-08-21 20:33:52 +02:00
{
Input: map[string]interface{}{
"foo": `${file("foo/bar.txt")}`,
},
2015-01-13 19:27:57 +01:00
Result: []string{
"Call(file, Literal(TypeString, foo/bar.txt))",
2014-08-21 20:33:52 +02:00
},
},
2014-10-10 06:22:35 +02:00
{
Input: map[string]interface{}{
"foo": `${join(",", foo.bar.*.id)}`,
},
2015-01-13 19:27:57 +01:00
Result: []string{
"Call(join, Literal(TypeString, ,), Variable(foo.bar.*.id))",
2014-10-10 06:22:35 +02:00
},
},
{
Input: map[string]interface{}{
"foo": `${concat("localhost", ":8080")}`,
},
2015-01-13 19:27:57 +01:00
Result: []string{
"Call(concat, Literal(TypeString, localhost), Literal(TypeString, :8080))",
},
},
2014-07-21 20:24:44 +02:00
}
for i, tc := range cases {
2015-01-13 19:27:57 +01:00
var actual []string
detectFn := func(root ast.Node) (string, error) {
actual = append(actual, fmt.Sprintf("%s", root))
2014-07-21 20:24:44 +02:00
return "", nil
}
w := &interpolationWalker{F: detectFn}
if err := reflectwalk.Walk(tc.Input, w); err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(actual, tc.Result) {
t.Fatalf("%d: bad:\n\n%#v", i, actual)
}
}
}
func TestInterpolationWalker_replace(t *testing.T) {
cases := []struct {
Input interface{}
Output interface{}
Value string
}{
{
Input: map[string]interface{}{
"foo": "$${var.foo}",
},
Output: map[string]interface{}{
"foo": "$${var.foo}",
},
Value: "bar",
},
{
Input: map[string]interface{}{
"foo": "hello, ${var.foo}",
},
Output: map[string]interface{}{
2015-01-13 19:27:57 +01:00
"foo": "bar",
},
Value: "bar",
},
{
Input: map[string]interface{}{
"foo": map[string]interface{}{
"${var.foo}": "bar",
},
},
Output: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "bar",
},
},
Value: "bar",
},
{
Input: map[string]interface{}{
"foo": []interface{}{
"${var.foo}",
"bing",
},
},
Output: map[string]interface{}{
"foo": []interface{}{
"bar",
"baz",
"bing",
},
},
2014-10-10 01:13:43 +02:00
Value: "bar" + InterpSplitDelim + "baz",
},
{
Input: map[string]interface{}{
"foo": []interface{}{
"${var.foo}",
"bing",
},
},
Output: map[string]interface{}{},
2014-10-10 06:22:35 +02:00
Value: UnknownVariableValue + InterpSplitDelim + "baz",
},
}
for i, tc := range cases {
2015-01-13 19:27:57 +01:00
fn := func(ast.Node) (string, error) {
return tc.Value, nil
}
w := &interpolationWalker{F: fn, Replace: true}
if err := reflectwalk.Walk(tc.Input, w); err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(tc.Input, tc.Output) {
t.Fatalf("%d: bad:\n\n%#v", i, tc.Input)
}
}
}