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