github.com/jrasell/terraform@v0.6.17-0.20160523115548-2652f5232949/terraform/state_add_test.go (about) 1 package terraform 2 3 import ( 4 "testing" 5 ) 6 7 func TestStateAdd(t *testing.T) { 8 cases := map[string]struct { 9 Err bool 10 From, To string 11 Value interface{} 12 One, Two *State 13 }{ 14 "ModuleState => Module Addr (new)": { 15 false, 16 "", 17 "module.foo", 18 &ModuleState{ 19 Path: rootModulePath, 20 Resources: map[string]*ResourceState{ 21 "test_instance.foo": &ResourceState{ 22 Type: "test_instance", 23 Primary: &InstanceState{ 24 ID: "foo", 25 }, 26 }, 27 28 "test_instance.bar": &ResourceState{ 29 Type: "test_instance", 30 Primary: &InstanceState{ 31 ID: "foo", 32 }, 33 }, 34 }, 35 }, 36 37 &State{}, 38 &State{ 39 Modules: []*ModuleState{ 40 &ModuleState{ 41 Path: []string{"root", "foo"}, 42 Resources: map[string]*ResourceState{ 43 "test_instance.foo": &ResourceState{ 44 Type: "test_instance", 45 Primary: &InstanceState{ 46 ID: "foo", 47 }, 48 }, 49 50 "test_instance.bar": &ResourceState{ 51 Type: "test_instance", 52 Primary: &InstanceState{ 53 ID: "foo", 54 }, 55 }, 56 }, 57 }, 58 }, 59 }, 60 }, 61 62 "ModuleState => Nested Module Addr (new)": { 63 false, 64 "", 65 "module.foo.module.bar", 66 &ModuleState{ 67 Path: rootModulePath, 68 Resources: map[string]*ResourceState{ 69 "test_instance.foo": &ResourceState{ 70 Type: "test_instance", 71 Primary: &InstanceState{ 72 ID: "foo", 73 }, 74 }, 75 76 "test_instance.bar": &ResourceState{ 77 Type: "test_instance", 78 Primary: &InstanceState{ 79 ID: "foo", 80 }, 81 }, 82 }, 83 }, 84 85 &State{}, 86 &State{ 87 Modules: []*ModuleState{ 88 &ModuleState{ 89 Path: []string{"root", "foo", "bar"}, 90 Resources: map[string]*ResourceState{ 91 "test_instance.foo": &ResourceState{ 92 Type: "test_instance", 93 Primary: &InstanceState{ 94 ID: "foo", 95 }, 96 }, 97 98 "test_instance.bar": &ResourceState{ 99 Type: "test_instance", 100 Primary: &InstanceState{ 101 ID: "foo", 102 }, 103 }, 104 }, 105 }, 106 }, 107 }, 108 }, 109 110 "ModuleState w/ outputs and deps => Module Addr (new)": { 111 false, 112 "", 113 "module.foo", 114 &ModuleState{ 115 Path: rootModulePath, 116 Outputs: map[string]*OutputState{ 117 "foo": &OutputState{ 118 Type: "string", 119 Sensitive: false, 120 Value: "bar", 121 }, 122 }, 123 Dependencies: []string{"foo"}, 124 Resources: map[string]*ResourceState{ 125 "test_instance.foo": &ResourceState{ 126 Type: "test_instance", 127 Primary: &InstanceState{ 128 ID: "foo", 129 }, 130 }, 131 132 "test_instance.bar": &ResourceState{ 133 Type: "test_instance", 134 Primary: &InstanceState{ 135 ID: "foo", 136 }, 137 }, 138 }, 139 }, 140 141 &State{}, 142 &State{ 143 Modules: []*ModuleState{ 144 &ModuleState{ 145 Path: []string{"root", "foo"}, 146 Outputs: map[string]*OutputState{ 147 "foo": &OutputState{ 148 Type: "string", 149 Sensitive: false, 150 Value: "bar", 151 }, 152 }, 153 Dependencies: []string{"foo"}, 154 Resources: map[string]*ResourceState{ 155 "test_instance.foo": &ResourceState{ 156 Type: "test_instance", 157 Primary: &InstanceState{ 158 ID: "foo", 159 }, 160 }, 161 162 "test_instance.bar": &ResourceState{ 163 Type: "test_instance", 164 Primary: &InstanceState{ 165 ID: "foo", 166 }, 167 }, 168 }, 169 }, 170 }, 171 }, 172 }, 173 174 "ModuleState => Module Addr (existing)": { 175 true, 176 "", 177 "module.foo", 178 &ModuleState{}, 179 &State{ 180 Modules: []*ModuleState{ 181 &ModuleState{ 182 Path: []string{"root", "foo"}, 183 Resources: map[string]*ResourceState{ 184 "test_instance.baz": &ResourceState{ 185 Type: "test_instance", 186 Primary: &InstanceState{ 187 ID: "foo", 188 }, 189 }, 190 }, 191 }, 192 }, 193 }, 194 nil, 195 }, 196 197 "ResourceState => Resource Addr (new)": { 198 false, 199 "aws_instance.bar", 200 "aws_instance.foo", 201 &ResourceState{ 202 Type: "test_instance", 203 Primary: &InstanceState{ 204 ID: "foo", 205 }, 206 }, 207 208 &State{}, 209 &State{ 210 Modules: []*ModuleState{ 211 &ModuleState{ 212 Path: []string{"root"}, 213 Resources: map[string]*ResourceState{ 214 "aws_instance.foo": &ResourceState{ 215 Type: "test_instance", 216 Primary: &InstanceState{ 217 ID: "foo", 218 }, 219 }, 220 }, 221 }, 222 }, 223 }, 224 }, 225 226 "ResourceState w/ deps, provider => Resource Addr (new)": { 227 false, 228 "aws_instance.bar", 229 "aws_instance.foo", 230 &ResourceState{ 231 Type: "test_instance", 232 Provider: "foo", 233 Dependencies: []string{"bar"}, 234 Primary: &InstanceState{ 235 ID: "foo", 236 }, 237 }, 238 239 &State{}, 240 &State{ 241 Modules: []*ModuleState{ 242 &ModuleState{ 243 Path: []string{"root"}, 244 Resources: map[string]*ResourceState{ 245 "aws_instance.foo": &ResourceState{ 246 Type: "test_instance", 247 Provider: "foo", 248 Dependencies: []string{"bar"}, 249 Primary: &InstanceState{ 250 ID: "foo", 251 }, 252 }, 253 }, 254 }, 255 }, 256 }, 257 }, 258 259 "ResourceState w/ tainted => Resource Addr (new)": { 260 false, 261 "aws_instance.bar", 262 "aws_instance.foo", 263 &ResourceState{ 264 Type: "test_instance", 265 Tainted: []*InstanceState{ 266 &InstanceState{ 267 ID: "foo", 268 }, 269 }, 270 }, 271 272 &State{}, 273 &State{ 274 Modules: []*ModuleState{ 275 &ModuleState{ 276 Path: []string{"root"}, 277 Resources: map[string]*ResourceState{ 278 "aws_instance.foo": &ResourceState{ 279 Type: "test_instance", 280 Primary: &InstanceState{}, 281 Tainted: []*InstanceState{ 282 &InstanceState{ 283 ID: "foo", 284 }, 285 }, 286 }, 287 }, 288 }, 289 }, 290 }, 291 }, 292 293 "ResourceState => Resource Addr (existing)": { 294 true, 295 "aws_instance.bar", 296 "aws_instance.foo", 297 &ResourceState{ 298 Type: "test_instance", 299 Primary: &InstanceState{ 300 ID: "foo", 301 }, 302 }, 303 304 &State{ 305 Modules: []*ModuleState{ 306 &ModuleState{ 307 Path: []string{"root"}, 308 Resources: map[string]*ResourceState{ 309 "aws_instance.foo": &ResourceState{ 310 Type: "test_instance", 311 Primary: &InstanceState{ 312 ID: "foo", 313 }, 314 }, 315 }, 316 }, 317 }, 318 }, 319 nil, 320 }, 321 322 "ResourceState => Module (new)": { 323 false, 324 "aws_instance.bar", 325 "module.foo", 326 &ResourceState{ 327 Type: "test_instance", 328 Primary: &InstanceState{ 329 ID: "foo", 330 }, 331 }, 332 333 &State{}, 334 &State{ 335 Modules: []*ModuleState{ 336 &ModuleState{ 337 Path: []string{"root", "foo"}, 338 Resources: map[string]*ResourceState{ 339 "aws_instance.bar": &ResourceState{ 340 Type: "test_instance", 341 Primary: &InstanceState{ 342 ID: "foo", 343 }, 344 }, 345 }, 346 }, 347 }, 348 }, 349 }, 350 351 "InstanceState => Resource (new)": { 352 false, 353 "aws_instance.bar.primary", 354 "aws_instance.baz", 355 &InstanceState{ 356 ID: "foo", 357 }, 358 359 &State{}, 360 &State{ 361 Modules: []*ModuleState{ 362 &ModuleState{ 363 Path: []string{"root"}, 364 Resources: map[string]*ResourceState{ 365 "aws_instance.baz": &ResourceState{ 366 Type: "aws_instance", 367 Primary: &InstanceState{ 368 ID: "foo", 369 }, 370 }, 371 }, 372 }, 373 }, 374 }, 375 }, 376 377 "InstanceState => Module (new)": { 378 false, 379 "aws_instance.bar.primary", 380 "module.foo", 381 &InstanceState{ 382 ID: "foo", 383 }, 384 385 &State{}, 386 &State{ 387 Modules: []*ModuleState{ 388 &ModuleState{ 389 Path: []string{"root", "foo"}, 390 Resources: map[string]*ResourceState{ 391 "aws_instance.bar": &ResourceState{ 392 Type: "aws_instance", 393 Primary: &InstanceState{ 394 ID: "foo", 395 }, 396 }, 397 }, 398 }, 399 }, 400 }, 401 }, 402 } 403 404 for k, tc := range cases { 405 // Make sure they're both initialized as normal 406 tc.One.init() 407 if tc.Two != nil { 408 tc.Two.init() 409 } 410 411 // Add the value 412 err := tc.One.Add(tc.From, tc.To, tc.Value) 413 if (err != nil) != tc.Err { 414 t.Fatalf("bad: %s\n\n%s", k, err) 415 } 416 if tc.Err { 417 continue 418 } 419 420 // Prune them both to be sure 421 tc.One.prune() 422 tc.Two.prune() 423 424 // Verify equality 425 if !tc.One.Equal(tc.Two) { 426 t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two) 427 //t.Fatalf("Bad: %s\n\n%s\n\n%s", k, tc.One.String(), tc.Two.String()) 428 } 429 } 430 }