Skip to content

Commit 7121af8

Browse files
committed
use simple state fns
1 parent fa1f5d3 commit 7121af8

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,12 +21,12 @@ 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"
29+
"github.com/hashicorp/terraform/internal/states/statemgr"
3030
"github.com/hashicorp/terraform/internal/terminal"
3131
)
3232

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

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

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

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

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-
23382295
expectedStates := map[string][]string{
23392296
"main.": {"test_resource.resource"},
23402297
}
@@ -2343,10 +2300,11 @@ Success! 5 passed, 0 failed.
23432300
// Verify the states in the manifest
23442301
for fileName, file := range manifest.Files {
23452302
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)
23492306
}
2307+
state := sm.State()
23502308

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

0 commit comments

Comments
 (0)