@@ -21,13 +21,13 @@ import (
21
21
"github.com/zclconf/go-cty/cty"
22
22
23
23
"github.com/hashicorp/terraform/internal/addrs"
24
- "github.com/hashicorp/terraform/internal/backend/local"
25
24
testing_command "github.com/hashicorp/terraform/internal/command/testing"
26
25
"github.com/hashicorp/terraform/internal/command/views"
27
26
"github.com/hashicorp/terraform/internal/moduletest/graph"
28
27
"github.com/hashicorp/terraform/internal/providers"
29
28
"github.com/hashicorp/terraform/internal/states"
30
29
"github.com/hashicorp/terraform/internal/states/statefile"
30
+ "github.com/hashicorp/terraform/internal/states/statemgr"
31
31
"github.com/hashicorp/terraform/internal/terminal"
32
32
)
33
33
@@ -487,6 +487,8 @@ func TestTest_Interrupt(t *testing.T) {
487
487
}
488
488
489
489
func TestTest_DestroyFail (t * testing.T ) {
490
+ // Testing that when a cleanup fails, we leave behind state files of the failed
491
+ // resources, and that the test command fails with a non-zero exit code.
490
492
td := t .TempDir ()
491
493
testCopyDir (t , testFixturePath (path .Join ("test" , "destroy_fail" )), td )
492
494
defer testChdir (t , td )()
@@ -593,29 +595,6 @@ main.tftest.hcl/single, and they need to be cleaned up manually:
593
595
t .Fatalf ("failed to unmarshal manifest.json: %s" , err )
594
596
}
595
597
596
- // function to get the state for a given state key within the manifest
597
- stateGetter := func (stateKey string ) (* states.State , error ) {
598
- backend := local .New ()
599
- dir := filepath .Dir (stateKey )
600
- backendConfig := cty .ObjectVal (map [string ]cty.Value {
601
- "path" : cty .StringVal (stateKey ),
602
- "workspace_dir" : cty .StringVal (dir ),
603
- })
604
- diags := backend .Configure (backendConfig )
605
- if diags .HasErrors () {
606
- return nil , fmt .Errorf ("failed to configure backend: %s" , diags )
607
- }
608
-
609
- mgr , err := backend .StateMgr ("default" )
610
- if err != nil {
611
- return nil , fmt .Errorf ("failed to create state manager: %s" , err )
612
- }
613
- if err := mgr .RefreshState (); err != nil {
614
- return nil , fmt .Errorf ("failed to refresh state: %s" , err )
615
- }
616
- return mgr .State (), nil
617
- }
618
-
619
598
expectedStates := map [string ][]string {
620
599
"main." : {"test_resource.another" , "test_resource.resource" },
621
600
"main.double" : {"test_resource.another" , "test_resource.resource" },
@@ -625,10 +604,11 @@ main.tftest.hcl/single, and they need to be cleaned up manually:
625
604
// Verify the states in the manifest
626
605
for fileName , file := range manifest .Files {
627
606
for name , state := range file .States {
628
- state , err := stateGetter ( state .Path )
629
- if err != nil {
630
- t .Fatalf ("failed to get state: %s" , err )
607
+ sm := statemgr . NewFilesystem ( filepath . Join ( td , state .Path ) )
608
+ if err := sm . RefreshState (); err != nil {
609
+ t .Fatalf ("error when reading state file : %s" , err )
631
610
}
611
+ state := sm .State ()
632
612
633
613
// If the state is nil, then the test cleaned up the state
634
614
if state == nil {
@@ -2313,29 +2293,6 @@ Success! 5 passed, 0 failed.
2313
2293
t .Fatalf ("failed to unmarshal manifest.json: %s" , err )
2314
2294
}
2315
2295
2316
- // function to get the state for a given state key within the manifest
2317
- stateGetter := func (stateKey string ) (* states.State , error ) {
2318
- backend := local .New ()
2319
- dir := filepath .Dir (stateKey )
2320
- backendConfig := cty .ObjectVal (map [string ]cty.Value {
2321
- "path" : cty .StringVal (stateKey ),
2322
- "workspace_dir" : cty .StringVal (dir ),
2323
- })
2324
- diags := backend .Configure (backendConfig )
2325
- if diags .HasErrors () {
2326
- return nil , fmt .Errorf ("failed to configure backend: %s" , diags )
2327
- }
2328
-
2329
- mgr , err := backend .StateMgr ("default" )
2330
- if err != nil {
2331
- return nil , fmt .Errorf ("failed to create state manager: %s" , err )
2332
- }
2333
- if err := mgr .RefreshState (); err != nil {
2334
- return nil , fmt .Errorf ("failed to refresh state: %s" , err )
2335
- }
2336
- return mgr .State (), nil
2337
- }
2338
-
2339
2296
expectedStates := map [string ][]string {
2340
2297
"main." : {"test_resource.resource" },
2341
2298
}
@@ -2344,10 +2301,11 @@ Success! 5 passed, 0 failed.
2344
2301
// Verify the states in the manifest
2345
2302
for fileName , file := range manifest .Files {
2346
2303
for name , state := range file .States {
2347
- state , err := stateGetter ( state .Path )
2348
- if err != nil {
2349
- t .Fatalf ("failed to get state: %s" , err )
2304
+ sm := statemgr . NewFilesystem ( filepath . Join ( td , state .Path ) )
2305
+ if err := sm . RefreshState (); err != nil {
2306
+ t .Fatalf ("error when reading state file : %s" , err )
2350
2307
}
2308
+ state := sm .State ()
2351
2309
2352
2310
// If the state is nil, then the test cleaned up the state
2353
2311
if state == nil {
0 commit comments