Skip to content

Commit ef3d3a7

Browse files
committed
use simple state fns
1 parent 7f4cb14 commit ef3d3a7

File tree

1 file changed

+11
-53
lines changed

1 file changed

+11
-53
lines changed

internal/command/test_test.go

+11-53
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import (
2121
"github.com/zclconf/go-cty/cty"
2222

2323
"github.com/hashicorp/terraform/internal/addrs"
24-
"github.com/hashicorp/terraform/internal/backend/local"
2524
testing_command "github.com/hashicorp/terraform/internal/command/testing"
2625
"github.com/hashicorp/terraform/internal/command/views"
2726
"github.com/hashicorp/terraform/internal/moduletest/graph"
2827
"github.com/hashicorp/terraform/internal/providers"
2928
"github.com/hashicorp/terraform/internal/states"
3029
"github.com/hashicorp/terraform/internal/states/statefile"
30+
"github.com/hashicorp/terraform/internal/states/statemgr"
3131
"github.com/hashicorp/terraform/internal/terminal"
3232
)
3333

@@ -487,6 +487,8 @@ func TestTest_Interrupt(t *testing.T) {
487487
}
488488

489489
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.
490492
td := t.TempDir()
491493
testCopyDir(t, testFixturePath(path.Join("test", "destroy_fail")), td)
492494
defer testChdir(t, td)()
@@ -593,29 +595,6 @@ main.tftest.hcl/single, and they need to be cleaned up manually:
593595
t.Fatalf("failed to unmarshal manifest.json: %s", err)
594596
}
595597

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-
619598
expectedStates := map[string][]string{
620599
"main.": {"test_resource.another", "test_resource.resource"},
621600
"main.double": {"test_resource.another", "test_resource.resource"},
@@ -625,10 +604,11 @@ main.tftest.hcl/single, and they need to be cleaned up manually:
625604
// Verify the states in the manifest
626605
for fileName, file := range manifest.Files {
627606
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)
631610
}
611+
state := sm.State()
632612

633613
// If the state is nil, then the test cleaned up the state
634614
if state == nil {
@@ -2313,29 +2293,6 @@ Success! 5 passed, 0 failed.
23132293
t.Fatalf("failed to unmarshal manifest.json: %s", err)
23142294
}
23152295

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-
23392296
expectedStates := map[string][]string{
23402297
"main.": {"test_resource.resource"},
23412298
}
@@ -2344,10 +2301,11 @@ Success! 5 passed, 0 failed.
23442301
// Verify the states in the manifest
23452302
for fileName, file := range manifest.Files {
23462303
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)
23502307
}
2308+
state := sm.State()
23512309

23522310
// If the state is nil, then the test cleaned up the state
23532311
if state == nil {

0 commit comments

Comments
 (0)