mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 08:21:07 -06:00
47a16b0937
a large refactor to addrs.AbsProviderConfig, embedding the addrs.Provider instead of a Type string. I've added and updated tests, added some Legacy functions to support older state formats and shims, and added a normalization step when reading v4 (current) state files (not the added tests under states/statefile/roundtrip which work with both current and legacy-style AbsProviderConfig strings). The remaining 'fixme' and 'todo' comments are mostly going to be addressed in a subsequent PR and involve looking up a given local provider config's FQN. This is fine for now as we are only working with default assumption.
499 lines
12 KiB
Go
499 lines
12 KiB
Go
package command
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/helper/copy"
|
|
"github.com/hashicorp/terraform/states"
|
|
)
|
|
|
|
func TestStateRm(t *testing.T) {
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "bar",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
})
|
|
statePath := testStateFile(t, state)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StateRmCommand{
|
|
StateMeta{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"test_instance.foo",
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// Test it is correct
|
|
testStateOutput(t, statePath, testStateRmOutput)
|
|
|
|
// Test we have backups
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
if len(backups) != 1 {
|
|
t.Fatalf("bad: %#v", backups)
|
|
}
|
|
testStateOutput(t, backups[0], testStateRmOutputOriginal)
|
|
}
|
|
|
|
func TestStateRmNotChildModule(t *testing.T) {
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
// This second instance has the same local address as the first but
|
|
// is in a child module. Older versions of Terraform would incorrectly
|
|
// remove this one too, since they failed to check the module address.
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("child", addrs.NoKey)),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
})
|
|
statePath := testStateFile(t, state)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StateRmCommand{
|
|
StateMeta{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"test_instance.foo",
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// Test it is correct
|
|
testStateOutput(t, statePath, `
|
|
<no state>
|
|
module.child:
|
|
test_instance.foo:
|
|
ID = foo
|
|
provider = provider["registry.terraform.io/-/test"]
|
|
bar = value
|
|
foo = value
|
|
`)
|
|
|
|
// Test we have backups
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
if len(backups) != 1 {
|
|
t.Fatalf("bad: %#v", backups)
|
|
}
|
|
testStateOutput(t, backups[0], `
|
|
test_instance.foo:
|
|
ID = bar
|
|
provider = provider["registry.terraform.io/-/test"]
|
|
bar = value
|
|
foo = value
|
|
|
|
module.child:
|
|
test_instance.foo:
|
|
ID = foo
|
|
provider = provider["registry.terraform.io/-/test"]
|
|
bar = value
|
|
foo = value
|
|
`)
|
|
}
|
|
|
|
func TestStateRmNoArgs(t *testing.T) {
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "bar",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
})
|
|
statePath := testStateFile(t, state)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StateRmCommand{
|
|
StateMeta{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
}
|
|
if code := c.Run(args); code == 0 {
|
|
t.Errorf("expected non-zero exit code, got: %d", code)
|
|
}
|
|
|
|
if msg := ui.ErrorWriter.String(); !strings.Contains(msg, "At least one address") {
|
|
t.Errorf("not the error we were looking for:\n%s", msg)
|
|
}
|
|
|
|
}
|
|
|
|
func TestStateRmNonExist(t *testing.T) {
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "bar",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
})
|
|
statePath := testStateFile(t, state)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StateRmCommand{
|
|
StateMeta{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-state", statePath,
|
|
"test_instance.baz", // doesn't exist in the state constructed above
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("expected exit status %d, got: %d", 0, code)
|
|
}
|
|
|
|
if msg := ui.OutputWriter.String(); !strings.Contains(msg, "No matching resource instances found") {
|
|
t.Fatalf("unexpected output:\n%s", msg)
|
|
}
|
|
|
|
}
|
|
|
|
func TestStateRm_backupExplicit(t *testing.T) {
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "bar",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
})
|
|
statePath := testStateFile(t, state)
|
|
backupPath := statePath + ".backup.test"
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StateRmCommand{
|
|
StateMeta{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-backup", backupPath,
|
|
"-state", statePath,
|
|
"test_instance.foo",
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// Test it is correct
|
|
testStateOutput(t, statePath, testStateRmOutput)
|
|
|
|
// Test backup
|
|
testStateOutput(t, backupPath, testStateRmOutputOriginal)
|
|
}
|
|
|
|
func TestStateRm_noState(t *testing.T) {
|
|
tmp, cwd := testCwd(t)
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StateRmCommand{
|
|
StateMeta{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{"foo"}
|
|
if code := c.Run(args); code != 1 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestStateRm_needsInit(t *testing.T) {
|
|
td := tempDir(t)
|
|
copy.CopyDir(testFixturePath("backend-change"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StateRmCommand{
|
|
StateMeta{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{"foo"}
|
|
if code := c.Run(args); code == 0 {
|
|
t.Fatalf("expected error output, got:\n%s", ui.OutputWriter.String())
|
|
}
|
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "Initialization") {
|
|
t.Fatalf("expected initialization error, got:\n%s", ui.ErrorWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestStateRm_backendState(t *testing.T) {
|
|
td := tempDir(t)
|
|
copy.CopyDir(testFixturePath("backend-unchanged"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "bar",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Module: addrs.RootModuleInstance,
|
|
},
|
|
)
|
|
})
|
|
|
|
statePath := "local-state.tfstate"
|
|
backupPath := "local-state.backup"
|
|
|
|
f, err := os.Create(statePath)
|
|
if err != nil {
|
|
t.Fatalf("failed to create state file %s: %s", statePath, err)
|
|
}
|
|
defer f.Close()
|
|
|
|
err = writeStateForTesting(state, f)
|
|
if err != nil {
|
|
t.Fatalf("failed to write state to file %s: %s", statePath, err)
|
|
}
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StateRmCommand{
|
|
StateMeta{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-backup", backupPath,
|
|
"test_instance.foo",
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// Test it is correct
|
|
testStateOutput(t, statePath, testStateRmOutput)
|
|
|
|
// Test backup
|
|
testStateOutput(t, backupPath, testStateRmOutputOriginal)
|
|
}
|
|
|
|
const testStateRmOutputOriginal = `
|
|
test_instance.bar:
|
|
ID = foo
|
|
provider = provider["registry.terraform.io/-/test"]
|
|
bar = value
|
|
foo = value
|
|
test_instance.foo:
|
|
ID = bar
|
|
provider = provider["registry.terraform.io/-/test"]
|
|
bar = value
|
|
foo = value
|
|
`
|
|
|
|
const testStateRmOutput = `
|
|
test_instance.bar:
|
|
ID = foo
|
|
provider = provider["registry.terraform.io/-/test"]
|
|
bar = value
|
|
foo = value
|
|
`
|