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-07-22 15:43:04 +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
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-07-22 15:43:04 +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))",
|
2014-07-22 15:43:04 +02:00
|
|
|
},
|
|
|
|
},
|
2014-08-05 19:28:20 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
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-05 19:28:20 +02:00
|
|
|
},
|
|
|
|
},
|
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
|
|
|
},
|
|
|
|
},
|
2014-12-20 13:54:58 +01: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-12-20 13:54:58 +01:00
|
|
|
},
|
|
|
|
},
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-21 20:30:43 +02:00
|
|
|
|
|
|
|
func TestInterpolationWalker_replace(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input interface{}
|
|
|
|
Output interface{}
|
2014-10-10 00:55:22 +02:00
|
|
|
Value string
|
2014-07-21 20:30:43 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: map[string]interface{}{
|
|
|
|
"foo": "$${var.foo}",
|
|
|
|
},
|
|
|
|
Output: map[string]interface{}{
|
|
|
|
"foo": "$${var.foo}",
|
|
|
|
},
|
2014-10-10 00:55:22 +02:00
|
|
|
Value: "bar",
|
2014-07-21 20:30:43 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Input: map[string]interface{}{
|
2014-07-21 20:45:56 +02:00
|
|
|
"foo": "hello, ${var.foo}",
|
2014-07-21 20:30:43 +02:00
|
|
|
},
|
|
|
|
Output: map[string]interface{}{
|
2015-01-13 19:27:57 +01:00
|
|
|
"foo": "bar",
|
2014-07-21 20:30:43 +02:00
|
|
|
},
|
2014-10-10 00:55:22 +02:00
|
|
|
Value: "bar",
|
2014-07-21 20:30:43 +02:00
|
|
|
},
|
2014-08-27 22:29:02 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Input: map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{
|
|
|
|
"${var.foo}": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Output: map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{
|
|
|
|
"bar": "bar",
|
|
|
|
},
|
|
|
|
},
|
2014-10-10 00:55:22 +02:00
|
|
|
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",
|
2014-08-27 22:29:02 +02:00
|
|
|
},
|
2014-10-10 02:23:10 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
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",
|
2014-10-10 02:23:10 +02:00
|
|
|
},
|
2014-07-21 20:30:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
2015-01-13 19:27:57 +01:00
|
|
|
fn := func(ast.Node) (string, error) {
|
2014-10-10 00:55:22 +02:00
|
|
|
return tc.Value, nil
|
2014-07-21 20:30:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|