2014-07-21 20:24:44 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/reflectwalk"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInterpolationWalker_detect(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input interface{}
|
|
|
|
Result []Interpolation
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: map[string]interface{}{
|
|
|
|
"foo": "$${var.foo}",
|
|
|
|
},
|
|
|
|
Result: nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Input: map[string]interface{}{
|
|
|
|
"foo": "${var.foo}",
|
|
|
|
},
|
|
|
|
Result: []Interpolation{
|
|
|
|
&VariableInterpolation{
|
|
|
|
Variable: &UserVariable{
|
|
|
|
Name: "foo",
|
|
|
|
key: "var.foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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}",
|
|
|
|
},
|
|
|
|
Result: []Interpolation{
|
|
|
|
&VariableInterpolation{
|
|
|
|
Variable: &ResourceVariable{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
Field: "num",
|
|
|
|
|
|
|
|
Multi: true,
|
|
|
|
Index: -1,
|
|
|
|
|
|
|
|
key: "aws_instance.foo.*.num",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-07-22 15:43:04 +02:00
|
|
|
{
|
|
|
|
Input: map[string]interface{}{
|
|
|
|
"foo": "${lookup(var.foo)}",
|
|
|
|
},
|
|
|
|
Result: []Interpolation{
|
|
|
|
&FunctionInterpolation{
|
|
|
|
Func: nil,
|
2014-07-23 00:59:53 +02:00
|
|
|
Args: []Interpolation{
|
|
|
|
&VariableInterpolation{
|
|
|
|
Variable: &UserVariable{
|
|
|
|
Name: "foo",
|
|
|
|
key: "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")}`,
|
|
|
|
},
|
|
|
|
Result: []Interpolation{
|
|
|
|
&FunctionInterpolation{
|
|
|
|
Func: nil,
|
|
|
|
Args: []Interpolation{
|
|
|
|
&LiteralInterpolation{
|
|
|
|
Literal: "test.txt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-08-21 20:33:52 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Input: map[string]interface{}{
|
|
|
|
"foo": `${file("foo/bar.txt")}`,
|
|
|
|
},
|
|
|
|
Result: []Interpolation{
|
|
|
|
&FunctionInterpolation{
|
|
|
|
Func: nil,
|
|
|
|
Args: []Interpolation{
|
|
|
|
&LiteralInterpolation{
|
|
|
|
Literal: "foo/bar.txt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-07-21 20:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
var actual []Interpolation
|
|
|
|
|
|
|
|
detectFn := func(i Interpolation) (string, error) {
|
|
|
|
actual = append(actual, i)
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &interpolationWalker{F: detectFn}
|
|
|
|
if err := reflectwalk.Walk(tc.Input, w); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-07-22 15:43:04 +02:00
|
|
|
for _, a := range actual {
|
|
|
|
// This is jank, but reflect.DeepEqual never has functions
|
|
|
|
// being the same.
|
|
|
|
if f, ok := a.(*FunctionInterpolation); ok {
|
|
|
|
f.Func = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 20:24:44 +02:00
|
|
|
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{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: map[string]interface{}{
|
|
|
|
"foo": "$${var.foo}",
|
|
|
|
},
|
|
|
|
Output: map[string]interface{}{
|
|
|
|
"foo": "$${var.foo}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
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{}{
|
2014-07-21 20:45:56 +02:00
|
|
|
"foo": "hello, 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-07-21 20:30:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
fn := func(i Interpolation) (string, error) {
|
|
|
|
return "bar", 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|