From 66bec6c3c764fa4ea680a588a1502d7ff58c3bb2 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 7 Mar 2024 17:46:07 -0800 Subject: [PATCH] command: TestWorkspace_deleteWithState uses modern state format This test is intending to test behavior in the presence of a state snapshot but was achieving that using the legacy state snapshot format that we preserved only for its previous (now eliminated, in an earlier commit) purpose of implementing the CLI backend state format. Instead, we'll construct a state snapshot format using the modern API for doing that, which therefore constructs a modern state snapshot file as would be created by today's Terraform. This eliminates the last use of the "legacy/terraform" package aside from the calls from other packages under "legacy". --- internal/command/workspace_command_test.go | 36 ++++++++++++++-------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/internal/command/workspace_command_test.go b/internal/command/workspace_command_test.go index 88a707242a..ede8bbff8d 100644 --- a/internal/command/workspace_command_test.go +++ b/internal/command/workspace_command_test.go @@ -11,14 +11,14 @@ import ( "testing" "github.com/hashicorp/cli" + "github.com/hashicorp/terraform/internal/addrs" "github.com/hashicorp/terraform/internal/backend" "github.com/hashicorp/terraform/internal/backend/local" "github.com/hashicorp/terraform/internal/backend/remote-state/inmem" "github.com/hashicorp/terraform/internal/states" + "github.com/hashicorp/terraform/internal/states/statefile" "github.com/hashicorp/terraform/internal/states/statemgr" - - legacy "github.com/hashicorp/terraform/internal/legacy/terraform" ) func TestWorkspace_createAndChange(t *testing.T) { @@ -384,20 +384,30 @@ func TestWorkspace_deleteWithState(t *testing.T) { } // create a non-empty state - originalState := &legacy.State{ - Modules: []*legacy.ModuleState{ - { - Path: []string{"root"}, - Resources: map[string]*legacy.ResourceState{ - "test_instance.foo": { + originalState := states.BuildState(func(ss *states.SyncState) { + ss.SetResourceInstanceCurrent( + addrs.AbsResourceInstance{ + Resource: addrs.ResourceInstance{ + Resource: addrs.Resource{ + Mode: addrs.ManagedResourceMode, Type: "test_instance", - Primary: &legacy.InstanceState{ - ID: "bar", - }, + Name: "foo", }, }, }, - }, + &states.ResourceInstanceObjectSrc{ + AttrsJSON: []byte("{}"), + Status: states.ObjectReady, + }, + addrs.AbsProviderConfig{ + Provider: addrs.NewBuiltInProvider("test"), + }, + ) + }) + originalStateFile := &statefile.File{ + Serial: 1, + Lineage: "whatever", + State: originalState, } f, err := os.Create(filepath.Join(local.DefaultWorkspaceDir, "test", "terraform.tfstate")) @@ -405,7 +415,7 @@ func TestWorkspace_deleteWithState(t *testing.T) { t.Fatal(err) } defer f.Close() - if err := legacy.WriteState(originalState, f); err != nil { + if err := statefile.Write(originalStateFile, f); err != nil { t.Fatal(err) }