github.com/shvar/terraform@v0.6.9-0.20151215234924-3365cd2231df/config/interpolate_walk_test.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/hashicorp/terraform/config/lang/ast" 9 "github.com/mitchellh/reflectwalk" 10 ) 11 12 func TestInterpolationWalker_detect(t *testing.T) { 13 cases := []struct { 14 Input interface{} 15 Result []string 16 }{ 17 { 18 Input: map[string]interface{}{ 19 "foo": "$${var.foo}", 20 }, 21 Result: nil, 22 }, 23 24 { 25 Input: map[string]interface{}{ 26 "foo": "${var.foo}", 27 }, 28 Result: []string{ 29 "Variable(var.foo)", 30 }, 31 }, 32 33 { 34 Input: map[string]interface{}{ 35 "foo": "${aws_instance.foo.*.num}", 36 }, 37 Result: []string{ 38 "Variable(aws_instance.foo.*.num)", 39 }, 40 }, 41 42 { 43 Input: map[string]interface{}{ 44 "foo": "${lookup(var.foo)}", 45 }, 46 Result: []string{ 47 "Call(lookup, Variable(var.foo))", 48 }, 49 }, 50 51 { 52 Input: map[string]interface{}{ 53 "foo": `${file("test.txt")}`, 54 }, 55 Result: []string{ 56 "Call(file, Literal(TypeString, test.txt))", 57 }, 58 }, 59 60 { 61 Input: map[string]interface{}{ 62 "foo": `${file("foo/bar.txt")}`, 63 }, 64 Result: []string{ 65 "Call(file, Literal(TypeString, foo/bar.txt))", 66 }, 67 }, 68 69 { 70 Input: map[string]interface{}{ 71 "foo": `${join(",", foo.bar.*.id)}`, 72 }, 73 Result: []string{ 74 "Call(join, Literal(TypeString, ,), Variable(foo.bar.*.id))", 75 }, 76 }, 77 78 { 79 Input: map[string]interface{}{ 80 "foo": `${concat("localhost", ":8080")}`, 81 }, 82 Result: []string{ 83 "Call(concat, Literal(TypeString, localhost), Literal(TypeString, :8080))", 84 }, 85 }, 86 } 87 88 for i, tc := range cases { 89 var actual []string 90 detectFn := func(root ast.Node) (string, error) { 91 actual = append(actual, fmt.Sprintf("%s", root)) 92 return "", nil 93 } 94 95 w := &interpolationWalker{F: detectFn} 96 if err := reflectwalk.Walk(tc.Input, w); err != nil { 97 t.Fatalf("err: %s", err) 98 } 99 100 if !reflect.DeepEqual(actual, tc.Result) { 101 t.Fatalf("%d: bad:\n\n%#v", i, actual) 102 } 103 } 104 } 105 106 func TestInterpolationWalker_replace(t *testing.T) { 107 cases := []struct { 108 Input interface{} 109 Output interface{} 110 Value string 111 }{ 112 { 113 Input: map[string]interface{}{ 114 "foo": "$${var.foo}", 115 }, 116 Output: map[string]interface{}{ 117 "foo": "$${var.foo}", 118 }, 119 Value: "bar", 120 }, 121 122 { 123 Input: map[string]interface{}{ 124 "foo": "hello, ${var.foo}", 125 }, 126 Output: map[string]interface{}{ 127 "foo": "bar", 128 }, 129 Value: "bar", 130 }, 131 132 { 133 Input: map[string]interface{}{ 134 "foo": map[string]interface{}{ 135 "${var.foo}": "bar", 136 }, 137 }, 138 Output: map[string]interface{}{ 139 "foo": map[string]interface{}{ 140 "bar": "bar", 141 }, 142 }, 143 Value: "bar", 144 }, 145 146 { 147 Input: map[string]interface{}{ 148 "foo": []interface{}{ 149 "${var.foo}", 150 "bing", 151 }, 152 }, 153 Output: map[string]interface{}{ 154 "foo": []interface{}{ 155 "bar", 156 "baz", 157 "bing", 158 }, 159 }, 160 Value: NewStringList([]string{"bar", "baz"}).String(), 161 }, 162 163 { 164 Input: map[string]interface{}{ 165 "foo": []interface{}{ 166 "${var.foo}", 167 "bing", 168 }, 169 }, 170 Output: map[string]interface{}{}, 171 Value: NewStringList([]string{UnknownVariableValue, "baz"}).String(), 172 }, 173 } 174 175 for i, tc := range cases { 176 fn := func(ast.Node) (string, error) { 177 return tc.Value, nil 178 } 179 180 w := &interpolationWalker{F: fn, Replace: true} 181 if err := reflectwalk.Walk(tc.Input, w); err != nil { 182 t.Fatalf("err: %s", err) 183 } 184 185 if !reflect.DeepEqual(tc.Input, tc.Output) { 186 t.Fatalf("%d: bad:\n\nexpected:%#v\ngot:%#v", i, tc.Output, tc.Input) 187 } 188 } 189 }