github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/config/interpolate_test.go (about) 1 package config 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/hil" 9 ) 10 11 func TestNewInterpolatedVariable(t *testing.T) { 12 cases := []struct { 13 Input string 14 Result InterpolatedVariable 15 Error bool 16 }{ 17 { 18 "var.foo", 19 &UserVariable{ 20 Name: "foo", 21 key: "var.foo", 22 }, 23 false, 24 }, 25 { 26 "module.foo.bar", 27 &ModuleVariable{ 28 Name: "foo", 29 Field: "bar", 30 key: "module.foo.bar", 31 }, 32 false, 33 }, 34 { 35 "count.index", 36 &CountVariable{ 37 Type: CountValueIndex, 38 key: "count.index", 39 }, 40 false, 41 }, 42 { 43 "count.nope", 44 &CountVariable{ 45 Type: CountValueInvalid, 46 key: "count.nope", 47 }, 48 false, 49 }, 50 { 51 "path.module", 52 &PathVariable{ 53 Type: PathValueModule, 54 key: "path.module", 55 }, 56 false, 57 }, 58 { 59 "self.address", 60 &SelfVariable{ 61 Field: "address", 62 key: "self.address", 63 }, 64 false, 65 }, 66 { 67 "terraform.env", 68 &TerraformVariable{ 69 Field: "env", 70 key: "terraform.env", 71 }, 72 false, 73 }, 74 } 75 76 for i, tc := range cases { 77 actual, err := NewInterpolatedVariable(tc.Input) 78 if err != nil != tc.Error { 79 t.Fatalf("%d. Error: %s", i, err) 80 } 81 if !reflect.DeepEqual(actual, tc.Result) { 82 t.Fatalf("%d bad: %#v", i, actual) 83 } 84 } 85 } 86 87 func TestNewResourceVariable(t *testing.T) { 88 v, err := NewResourceVariable("foo.bar.baz") 89 if err != nil { 90 t.Fatalf("err: %s", err) 91 } 92 93 if v.Mode != ManagedResourceMode { 94 t.Fatalf("bad: %#v", v) 95 } 96 if v.Type != "foo" { 97 t.Fatalf("bad: %#v", v) 98 } 99 if v.Name != "bar" { 100 t.Fatalf("bad: %#v", v) 101 } 102 if v.Field != "baz" { 103 t.Fatalf("bad: %#v", v) 104 } 105 if v.Multi { 106 t.Fatal("should not be multi") 107 } 108 109 if v.FullKey() != "foo.bar.baz" { 110 t.Fatalf("bad: %#v", v) 111 } 112 } 113 114 func TestNewResourceVariableData(t *testing.T) { 115 v, err := NewResourceVariable("data.foo.bar.baz") 116 if err != nil { 117 t.Fatalf("err: %s", err) 118 } 119 120 if v.Mode != DataResourceMode { 121 t.Fatalf("bad: %#v", v) 122 } 123 if v.Type != "foo" { 124 t.Fatalf("bad: %#v", v) 125 } 126 if v.Name != "bar" { 127 t.Fatalf("bad: %#v", v) 128 } 129 if v.Field != "baz" { 130 t.Fatalf("bad: %#v", v) 131 } 132 if v.Multi { 133 t.Fatal("should not be multi") 134 } 135 136 if v.FullKey() != "data.foo.bar.baz" { 137 t.Fatalf("bad: %#v", v) 138 } 139 } 140 141 func TestNewUserVariable(t *testing.T) { 142 v, err := NewUserVariable("var.bar") 143 if err != nil { 144 t.Fatalf("err: %s", err) 145 } 146 147 if v.Name != "bar" { 148 t.Fatalf("bad: %#v", v.Name) 149 } 150 if v.FullKey() != "var.bar" { 151 t.Fatalf("bad: %#v", v) 152 } 153 } 154 155 func TestNewUserVariable_oldMapDotIndexErr(t *testing.T) { 156 _, err := NewUserVariable("var.bar.baz") 157 if err == nil || !strings.Contains(err.Error(), "Invalid dot index") { 158 t.Fatalf("Expected dot index err, got: %#v", err) 159 } 160 } 161 162 func TestResourceVariable_impl(t *testing.T) { 163 var _ InterpolatedVariable = new(ResourceVariable) 164 } 165 166 func TestResourceVariable_Multi(t *testing.T) { 167 v, err := NewResourceVariable("foo.bar.*.baz") 168 if err != nil { 169 t.Fatalf("err: %s", err) 170 } 171 172 if v.Type != "foo" { 173 t.Fatalf("bad: %#v", v) 174 } 175 if v.Name != "bar" { 176 t.Fatalf("bad: %#v", v) 177 } 178 if v.Field != "baz" { 179 t.Fatalf("bad: %#v", v) 180 } 181 if !v.Multi { 182 t.Fatal("should be multi") 183 } 184 } 185 186 func TestResourceVariable_MultiIndex(t *testing.T) { 187 cases := []struct { 188 Input string 189 Index int 190 Field string 191 }{ 192 {"foo.bar.*.baz", -1, "baz"}, 193 {"foo.bar.0.baz", 0, "baz"}, 194 {"foo.bar.5.baz", 5, "baz"}, 195 } 196 197 for _, tc := range cases { 198 v, err := NewResourceVariable(tc.Input) 199 if err != nil { 200 t.Fatalf("err: %s", err) 201 } 202 if !v.Multi { 203 t.Fatalf("should be multi: %s", tc.Input) 204 } 205 if v.Index != tc.Index { 206 t.Fatalf("bad: %d\n\n%s", v.Index, tc.Input) 207 } 208 if v.Field != tc.Field { 209 t.Fatalf("bad: %s\n\n%s", v.Field, tc.Input) 210 } 211 } 212 } 213 214 func TestUserVariable_impl(t *testing.T) { 215 var _ InterpolatedVariable = new(UserVariable) 216 } 217 218 func TestDetectVariables(t *testing.T) { 219 cases := []struct { 220 Input string 221 Result []InterpolatedVariable 222 }{ 223 { 224 "foo $${var.foo}", 225 nil, 226 }, 227 228 { 229 "foo ${var.foo}", 230 []InterpolatedVariable{ 231 &UserVariable{ 232 Name: "foo", 233 key: "var.foo", 234 }, 235 }, 236 }, 237 238 { 239 "foo ${var.foo} ${var.bar}", 240 []InterpolatedVariable{ 241 &UserVariable{ 242 Name: "foo", 243 key: "var.foo", 244 }, 245 &UserVariable{ 246 Name: "bar", 247 key: "var.bar", 248 }, 249 }, 250 }, 251 252 { 253 `foo ${module.foo.output["key"]}`, 254 []InterpolatedVariable{ 255 &ModuleVariable{ 256 Name: "foo", 257 Field: "output", 258 key: "module.foo.output", 259 }, 260 &ModuleVariable{ 261 Name: "foo", 262 Field: "output", 263 key: "module.foo.output", 264 }, 265 }, 266 }, 267 } 268 269 for _, tc := range cases { 270 ast, err := hil.Parse(tc.Input) 271 if err != nil { 272 t.Fatalf("%s\n\nInput: %s", err, tc.Input) 273 } 274 275 actual, err := DetectVariables(ast) 276 if err != nil { 277 t.Fatalf("err: %s", err) 278 } 279 if !reflect.DeepEqual(actual, tc.Result) { 280 t.Fatalf("bad: %#v\n\nInput: %s", actual, tc.Input) 281 } 282 } 283 }