github.com/sfdevops1/terrra4orm@v0.11.12-beta1/helper/resource/testing_import_state_test.go (about) 1 package resource 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestTest_importState(t *testing.T) { 12 mp := testProvider() 13 mp.ImportStateReturn = []*terraform.InstanceState{ 14 &terraform.InstanceState{ 15 ID: "foo", 16 Ephemeral: terraform.EphemeralState{Type: "test_instance"}, 17 }, 18 } 19 mp.RefreshFn = func( 20 i *terraform.InstanceInfo, 21 s *terraform.InstanceState) (*terraform.InstanceState, error) { 22 return s, nil 23 } 24 25 checked := false 26 checkFn := func(s []*terraform.InstanceState) error { 27 checked = true 28 29 if s[0].ID != "foo" { 30 return fmt.Errorf("bad: %#v", s) 31 } 32 33 return nil 34 } 35 36 mt := new(mockT) 37 Test(mt, TestCase{ 38 Providers: map[string]terraform.ResourceProvider{ 39 "test": mp, 40 }, 41 42 Steps: []TestStep{ 43 TestStep{ 44 Config: testConfigStrProvider, 45 ResourceName: "test_instance.foo", 46 ImportState: true, 47 ImportStateId: "foo", 48 ImportStateCheck: checkFn, 49 }, 50 }, 51 }) 52 53 if mt.failed() { 54 t.Fatalf("test failed: %s", mt.failMessage()) 55 } 56 if !checked { 57 t.Fatal("didn't call check") 58 } 59 } 60 61 func TestTest_importStateFail(t *testing.T) { 62 mp := testProvider() 63 mp.ImportStateReturn = []*terraform.InstanceState{ 64 &terraform.InstanceState{ 65 ID: "bar", 66 Ephemeral: terraform.EphemeralState{Type: "test_instance"}, 67 }, 68 } 69 mp.RefreshFn = func( 70 i *terraform.InstanceInfo, 71 s *terraform.InstanceState) (*terraform.InstanceState, error) { 72 return s, nil 73 } 74 75 checked := false 76 checkFn := func(s []*terraform.InstanceState) error { 77 checked = true 78 79 if s[0].ID != "foo" { 80 return fmt.Errorf("bad: %#v", s) 81 } 82 83 return nil 84 } 85 86 mt := new(mockT) 87 Test(mt, TestCase{ 88 Providers: map[string]terraform.ResourceProvider{ 89 "test": mp, 90 }, 91 92 Steps: []TestStep{ 93 TestStep{ 94 Config: testConfigStrProvider, 95 ResourceName: "test_instance.foo", 96 ImportState: true, 97 ImportStateId: "foo", 98 ImportStateCheck: checkFn, 99 }, 100 }, 101 }) 102 103 if !mt.failed() { 104 t.Fatal("should fail") 105 } 106 if !checked { 107 t.Fatal("didn't call check") 108 } 109 } 110 111 func TestTest_importStateDetectId(t *testing.T) { 112 mp := testProvider() 113 mp.DiffReturn = nil 114 mp.ApplyFn = func( 115 info *terraform.InstanceInfo, 116 state *terraform.InstanceState, 117 diff *terraform.InstanceDiff) (*terraform.InstanceState, error) { 118 if !diff.Destroy { 119 return &terraform.InstanceState{ 120 ID: "foo", 121 }, nil 122 } 123 124 return nil, nil 125 } 126 127 mp.RefreshFn = func( 128 i *terraform.InstanceInfo, 129 s *terraform.InstanceState) (*terraform.InstanceState, error) { 130 return s, nil 131 } 132 133 mp.ImportStateFn = func( 134 info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) { 135 if id != "foo" { 136 return nil, fmt.Errorf("bad import ID: %s", id) 137 } 138 139 return []*terraform.InstanceState{ 140 &terraform.InstanceState{ 141 ID: "bar", 142 Ephemeral: terraform.EphemeralState{Type: "test_instance"}, 143 }, 144 }, nil 145 } 146 147 checked := false 148 checkFn := func(s []*terraform.InstanceState) error { 149 checked = true 150 151 if s[0].ID != "bar" { 152 return fmt.Errorf("bad: %#v", s) 153 } 154 155 return nil 156 } 157 158 mt := new(mockT) 159 Test(mt, TestCase{ 160 Providers: map[string]terraform.ResourceProvider{ 161 "test": mp, 162 }, 163 164 Steps: []TestStep{ 165 TestStep{ 166 Config: testConfigStr, 167 }, 168 TestStep{ 169 Config: testConfigStr, 170 ResourceName: "test_instance.foo", 171 ImportState: true, 172 ImportStateCheck: checkFn, 173 }, 174 }, 175 }) 176 177 if mt.failed() { 178 t.Fatalf("test failed: %s", mt.failMessage()) 179 } 180 if !checked { 181 t.Fatal("didn't call check") 182 } 183 } 184 185 func TestTest_importStateIdPrefix(t *testing.T) { 186 mp := testProvider() 187 mp.DiffReturn = nil 188 mp.ApplyFn = func( 189 info *terraform.InstanceInfo, 190 state *terraform.InstanceState, 191 diff *terraform.InstanceDiff) (*terraform.InstanceState, error) { 192 if !diff.Destroy { 193 return &terraform.InstanceState{ 194 ID: "foo", 195 }, nil 196 } 197 198 return nil, nil 199 } 200 201 mp.RefreshFn = func( 202 i *terraform.InstanceInfo, 203 s *terraform.InstanceState) (*terraform.InstanceState, error) { 204 return s, nil 205 } 206 207 mp.ImportStateFn = func( 208 info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) { 209 if id != "bazfoo" { 210 return nil, fmt.Errorf("bad import ID: %s", id) 211 } 212 213 return []*terraform.InstanceState{ 214 { 215 ID: "bar", 216 Ephemeral: terraform.EphemeralState{Type: "test_instance"}, 217 }, 218 }, nil 219 } 220 221 checked := false 222 checkFn := func(s []*terraform.InstanceState) error { 223 checked = true 224 225 if s[0].ID != "bar" { 226 return fmt.Errorf("bad: %#v", s) 227 } 228 229 return nil 230 } 231 232 mt := new(mockT) 233 Test(mt, TestCase{ 234 Providers: map[string]terraform.ResourceProvider{ 235 "test": mp, 236 }, 237 238 Steps: []TestStep{ 239 { 240 Config: testConfigStr, 241 }, 242 { 243 Config: testConfigStr, 244 ResourceName: "test_instance.foo", 245 ImportState: true, 246 ImportStateCheck: checkFn, 247 ImportStateIdPrefix: "baz", 248 }, 249 }, 250 }) 251 252 if mt.failed() { 253 t.Fatalf("test failed: %s", mt.failMessage()) 254 } 255 if !checked { 256 t.Fatal("didn't call check") 257 } 258 } 259 260 func TestTest_importStateVerify(t *testing.T) { 261 mp := testProvider() 262 mp.DiffReturn = nil 263 mp.ApplyFn = func( 264 info *terraform.InstanceInfo, 265 state *terraform.InstanceState, 266 diff *terraform.InstanceDiff) (*terraform.InstanceState, error) { 267 if !diff.Destroy { 268 return &terraform.InstanceState{ 269 ID: "foo", 270 Attributes: map[string]string{ 271 "foo": "bar", 272 }, 273 }, nil 274 } 275 276 return nil, nil 277 } 278 279 mp.RefreshFn = func( 280 i *terraform.InstanceInfo, 281 s *terraform.InstanceState) (*terraform.InstanceState, error) { 282 if len(s.Attributes) == 0 { 283 s.Attributes = map[string]string{ 284 "id": s.ID, 285 "foo": "bar", 286 } 287 } 288 289 return s, nil 290 } 291 292 mp.ImportStateFn = func( 293 info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) { 294 if id != "foo" { 295 return nil, fmt.Errorf("bad import ID: %s", id) 296 } 297 298 return []*terraform.InstanceState{ 299 &terraform.InstanceState{ 300 ID: "foo", 301 Ephemeral: terraform.EphemeralState{Type: "test_instance"}, 302 }, 303 }, nil 304 } 305 306 mt := new(mockT) 307 Test(mt, TestCase{ 308 Providers: map[string]terraform.ResourceProvider{ 309 "test": mp, 310 }, 311 312 Steps: []TestStep{ 313 TestStep{ 314 Config: testConfigStr, 315 }, 316 TestStep{ 317 Config: testConfigStr, 318 ResourceName: "test_instance.foo", 319 ImportState: true, 320 ImportStateVerify: true, 321 }, 322 }, 323 }) 324 325 if mt.failed() { 326 t.Fatalf("test failed: %s", mt.failMessage()) 327 } 328 } 329 330 func TestTest_importStateVerifyFail(t *testing.T) { 331 mp := testProvider() 332 mp.DiffReturn = nil 333 mp.ApplyFn = func( 334 info *terraform.InstanceInfo, 335 state *terraform.InstanceState, 336 diff *terraform.InstanceDiff) (*terraform.InstanceState, error) { 337 if !diff.Destroy { 338 return &terraform.InstanceState{ 339 ID: "foo", 340 Attributes: map[string]string{ 341 "foo": "bar", 342 }, 343 }, nil 344 } 345 346 return nil, nil 347 } 348 349 mp.RefreshFn = func( 350 i *terraform.InstanceInfo, 351 s *terraform.InstanceState) (*terraform.InstanceState, error) { 352 return s, nil 353 } 354 355 mp.ImportStateFn = func( 356 info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) { 357 if id != "foo" { 358 return nil, fmt.Errorf("bad import ID: %s", id) 359 } 360 361 return []*terraform.InstanceState{ 362 &terraform.InstanceState{ 363 ID: "foo", 364 Ephemeral: terraform.EphemeralState{Type: "test_instance"}, 365 }, 366 }, nil 367 } 368 369 mt := new(mockT) 370 Test(mt, TestCase{ 371 Providers: map[string]terraform.ResourceProvider{ 372 "test": mp, 373 }, 374 375 Steps: []TestStep{ 376 TestStep{ 377 Config: testConfigStr, 378 }, 379 TestStep{ 380 Config: testConfigStr, 381 ResourceName: "test_instance.foo", 382 ImportState: true, 383 ImportStateVerify: true, 384 }, 385 }, 386 }) 387 388 if !mt.failed() { 389 t.Fatalf("test should fail") 390 } 391 } 392 393 func TestTest_importStateIdFunc(t *testing.T) { 394 mp := testProvider() 395 mp.ImportStateFn = func( 396 info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) { 397 if id != "foo:bar" { 398 return nil, fmt.Errorf("bad import ID: %s", id) 399 } 400 401 return []*terraform.InstanceState{ 402 { 403 ID: "foo", 404 Ephemeral: terraform.EphemeralState{Type: "test_instance"}, 405 }, 406 }, nil 407 } 408 409 mp.RefreshFn = func( 410 i *terraform.InstanceInfo, 411 s *terraform.InstanceState) (*terraform.InstanceState, error) { 412 return s, nil 413 } 414 415 checked := false 416 checkFn := func(s []*terraform.InstanceState) error { 417 checked = true 418 419 if s[0].ID != "foo" { 420 return fmt.Errorf("bad: %#v", s) 421 } 422 423 return nil 424 } 425 426 mt := new(mockT) 427 Test(mt, TestCase{ 428 Providers: map[string]terraform.ResourceProvider{ 429 "test": mp, 430 }, 431 432 Steps: []TestStep{ 433 TestStep{ 434 Config: testConfigStrProvider, 435 ResourceName: "test_instance.foo", 436 ImportState: true, 437 ImportStateIdFunc: func(*terraform.State) (string, error) { return "foo:bar", nil }, 438 ImportStateCheck: checkFn, 439 }, 440 }, 441 }) 442 443 if mt.failed() { 444 t.Fatalf("test failed: %s", mt.failMessage()) 445 } 446 if !checked { 447 t.Fatal("didn't call check") 448 } 449 } 450 451 func TestTest_importStateIdFuncFail(t *testing.T) { 452 mp := testProvider() 453 mp.ImportStateFn = func( 454 info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) { 455 if id != "foo:bar" { 456 return nil, fmt.Errorf("bad import ID: %s", id) 457 } 458 459 return []*terraform.InstanceState{ 460 { 461 ID: "foo", 462 Ephemeral: terraform.EphemeralState{Type: "test_instance"}, 463 }, 464 }, nil 465 } 466 467 mp.RefreshFn = func( 468 i *terraform.InstanceInfo, 469 s *terraform.InstanceState) (*terraform.InstanceState, error) { 470 return s, nil 471 } 472 473 checkFn := func(s []*terraform.InstanceState) error { 474 if s[0].ID != "foo" { 475 return fmt.Errorf("bad: %#v", s) 476 } 477 478 return nil 479 } 480 481 mt := new(mockT) 482 Test(mt, TestCase{ 483 Providers: map[string]terraform.ResourceProvider{ 484 "test": mp, 485 }, 486 487 Steps: []TestStep{ 488 TestStep{ 489 Config: testConfigStrProvider, 490 ResourceName: "test_instance.foo", 491 ImportState: true, 492 ImportStateIdFunc: func(*terraform.State) (string, error) { return "foo:bar", errors.New("foobar") }, 493 ImportStateCheck: checkFn, 494 }, 495 }, 496 }) 497 498 if !mt.failed() { 499 t.Fatalf("test should fail") 500 } 501 }