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