github.com/federicobaldo/terraform@v0.6.15-0.20160323222747-b20f680cbf05/terraform/resource_address_test.go (about) 1 package terraform 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestParseResourceAddress(t *testing.T) { 9 cases := map[string]struct { 10 Input string 11 Expected *ResourceAddress 12 }{ 13 "implicit primary, no specific index": { 14 Input: "aws_instance.foo", 15 Expected: &ResourceAddress{ 16 Type: "aws_instance", 17 Name: "foo", 18 InstanceType: TypePrimary, 19 Index: -1, 20 }, 21 }, 22 "implicit primary, explicit index": { 23 Input: "aws_instance.foo[2]", 24 Expected: &ResourceAddress{ 25 Type: "aws_instance", 26 Name: "foo", 27 InstanceType: TypePrimary, 28 Index: 2, 29 }, 30 }, 31 "implicit primary, explicit index over ten": { 32 Input: "aws_instance.foo[12]", 33 Expected: &ResourceAddress{ 34 Type: "aws_instance", 35 Name: "foo", 36 InstanceType: TypePrimary, 37 Index: 12, 38 }, 39 }, 40 "explicit primary, explicit index": { 41 Input: "aws_instance.foo.primary[2]", 42 Expected: &ResourceAddress{ 43 Type: "aws_instance", 44 Name: "foo", 45 InstanceType: TypePrimary, 46 Index: 2, 47 }, 48 }, 49 "tainted": { 50 Input: "aws_instance.foo.tainted", 51 Expected: &ResourceAddress{ 52 Type: "aws_instance", 53 Name: "foo", 54 InstanceType: TypeTainted, 55 Index: -1, 56 }, 57 }, 58 "deposed": { 59 Input: "aws_instance.foo.deposed", 60 Expected: &ResourceAddress{ 61 Type: "aws_instance", 62 Name: "foo", 63 InstanceType: TypeDeposed, 64 Index: -1, 65 }, 66 }, 67 "with a hyphen": { 68 Input: "aws_instance.foo-bar", 69 Expected: &ResourceAddress{ 70 Type: "aws_instance", 71 Name: "foo-bar", 72 InstanceType: TypePrimary, 73 Index: -1, 74 }, 75 }, 76 "in a module": { 77 Input: "module.child.aws_instance.foo", 78 Expected: &ResourceAddress{ 79 Path: []string{"child"}, 80 Type: "aws_instance", 81 Name: "foo", 82 InstanceType: TypePrimary, 83 Index: -1, 84 }, 85 }, 86 "nested modules": { 87 Input: "module.a.module.b.module.forever.aws_instance.foo", 88 Expected: &ResourceAddress{ 89 Path: []string{"a", "b", "forever"}, 90 Type: "aws_instance", 91 Name: "foo", 92 InstanceType: TypePrimary, 93 Index: -1, 94 }, 95 }, 96 "just a module": { 97 Input: "module.a", 98 Expected: &ResourceAddress{ 99 Path: []string{"a"}, 100 Type: "", 101 Name: "", 102 InstanceType: TypePrimary, 103 Index: -1, 104 }, 105 }, 106 "just a nested module": { 107 Input: "module.a.module.b", 108 Expected: &ResourceAddress{ 109 Path: []string{"a", "b"}, 110 Type: "", 111 Name: "", 112 InstanceType: TypePrimary, 113 Index: -1, 114 }, 115 }, 116 } 117 118 for tn, tc := range cases { 119 out, err := ParseResourceAddress(tc.Input) 120 if err != nil { 121 t.Fatalf("unexpected err: %#v", err) 122 } 123 124 if !reflect.DeepEqual(out, tc.Expected) { 125 t.Fatalf("bad: %q\n\nexpected:\n%#v\n\ngot:\n%#v", tn, tc.Expected, out) 126 } 127 } 128 } 129 130 func TestResourceAddressEquals(t *testing.T) { 131 cases := map[string]struct { 132 Address *ResourceAddress 133 Other interface{} 134 Expect bool 135 }{ 136 "basic match": { 137 Address: &ResourceAddress{ 138 Type: "aws_instance", 139 Name: "foo", 140 InstanceType: TypePrimary, 141 Index: 0, 142 }, 143 Other: &ResourceAddress{ 144 Type: "aws_instance", 145 Name: "foo", 146 InstanceType: TypePrimary, 147 Index: 0, 148 }, 149 Expect: true, 150 }, 151 "address does not set index": { 152 Address: &ResourceAddress{ 153 Type: "aws_instance", 154 Name: "foo", 155 InstanceType: TypePrimary, 156 Index: -1, 157 }, 158 Other: &ResourceAddress{ 159 Type: "aws_instance", 160 Name: "foo", 161 InstanceType: TypePrimary, 162 Index: 3, 163 }, 164 Expect: true, 165 }, 166 "other does not set index": { 167 Address: &ResourceAddress{ 168 Type: "aws_instance", 169 Name: "foo", 170 InstanceType: TypePrimary, 171 Index: 3, 172 }, 173 Other: &ResourceAddress{ 174 Type: "aws_instance", 175 Name: "foo", 176 InstanceType: TypePrimary, 177 Index: -1, 178 }, 179 Expect: true, 180 }, 181 "neither sets index": { 182 Address: &ResourceAddress{ 183 Type: "aws_instance", 184 Name: "foo", 185 InstanceType: TypePrimary, 186 Index: -1, 187 }, 188 Other: &ResourceAddress{ 189 Type: "aws_instance", 190 Name: "foo", 191 InstanceType: TypePrimary, 192 Index: -1, 193 }, 194 Expect: true, 195 }, 196 "index over ten": { 197 Address: &ResourceAddress{ 198 Type: "aws_instance", 199 Name: "foo", 200 InstanceType: TypePrimary, 201 Index: 1, 202 }, 203 Other: &ResourceAddress{ 204 Type: "aws_instance", 205 Name: "foo", 206 InstanceType: TypePrimary, 207 Index: 13, 208 }, 209 Expect: false, 210 }, 211 "different type": { 212 Address: &ResourceAddress{ 213 Type: "aws_instance", 214 Name: "foo", 215 InstanceType: TypePrimary, 216 Index: 0, 217 }, 218 Other: &ResourceAddress{ 219 Type: "aws_vpc", 220 Name: "foo", 221 InstanceType: TypePrimary, 222 Index: 0, 223 }, 224 Expect: false, 225 }, 226 "different name": { 227 Address: &ResourceAddress{ 228 Type: "aws_instance", 229 Name: "foo", 230 InstanceType: TypePrimary, 231 Index: 0, 232 }, 233 Other: &ResourceAddress{ 234 Type: "aws_instance", 235 Name: "bar", 236 InstanceType: TypePrimary, 237 Index: 0, 238 }, 239 Expect: false, 240 }, 241 "different instance type": { 242 Address: &ResourceAddress{ 243 Type: "aws_instance", 244 Name: "foo", 245 InstanceType: TypePrimary, 246 Index: 0, 247 }, 248 Other: &ResourceAddress{ 249 Type: "aws_instance", 250 Name: "foo", 251 InstanceType: TypeTainted, 252 Index: 0, 253 }, 254 Expect: false, 255 }, 256 "different index": { 257 Address: &ResourceAddress{ 258 Type: "aws_instance", 259 Name: "foo", 260 InstanceType: TypePrimary, 261 Index: 0, 262 }, 263 Other: &ResourceAddress{ 264 Type: "aws_instance", 265 Name: "foo", 266 InstanceType: TypePrimary, 267 Index: 1, 268 }, 269 Expect: false, 270 }, 271 "module address matches address of resource inside module": { 272 Address: &ResourceAddress{ 273 Path: []string{"a", "b"}, 274 Type: "", 275 Name: "", 276 InstanceType: TypePrimary, 277 Index: -1, 278 }, 279 Other: &ResourceAddress{ 280 Path: []string{"a", "b"}, 281 Type: "aws_instance", 282 Name: "foo", 283 InstanceType: TypePrimary, 284 Index: 0, 285 }, 286 Expect: true, 287 }, 288 "module address doesn't match resource outside module": { 289 Address: &ResourceAddress{ 290 Path: []string{"a", "b"}, 291 Type: "", 292 Name: "", 293 InstanceType: TypePrimary, 294 Index: -1, 295 }, 296 Other: &ResourceAddress{ 297 Path: []string{"a"}, 298 Type: "aws_instance", 299 Name: "foo", 300 InstanceType: TypePrimary, 301 Index: 0, 302 }, 303 Expect: false, 304 }, 305 "nil path vs empty path should match": { 306 Address: &ResourceAddress{ 307 Path: []string{}, 308 Type: "aws_instance", 309 Name: "foo", 310 InstanceType: TypePrimary, 311 Index: -1, 312 }, 313 Other: &ResourceAddress{ 314 Path: nil, 315 Type: "aws_instance", 316 Name: "foo", 317 InstanceType: TypePrimary, 318 Index: 0, 319 }, 320 Expect: true, 321 }, 322 } 323 324 for tn, tc := range cases { 325 actual := tc.Address.Equals(tc.Other) 326 if actual != tc.Expect { 327 t.Fatalf("%q: expected equals: %t, got %t for:\n%#v\n%#v", 328 tn, tc.Expect, actual, tc.Address, tc.Other) 329 } 330 } 331 }