mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Merge pull request #27679 from hashicorp/alisdair/apply-destroy-test-fixes
cli: Better diagnostics for apply positional args
This commit is contained in:
commit
32d2084387
@ -74,10 +74,21 @@ func (c *ApplyCommand) Run(args []string) int {
|
||||
c.Ui.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
}
|
||||
if c.Destroy && planFile != nil {
|
||||
c.Ui.Error("Destroy can't be called with a plan file.")
|
||||
return 1
|
||||
|
||||
// If the path doesn't look like a plan, both planFile and err will be
|
||||
// nil. In that case, the user is probably trying to use the positional
|
||||
// argument to specify a configuration path. Point them at -chdir.
|
||||
if planFile == nil {
|
||||
c.Ui.Error(fmt.Sprintf("Failed to load %q as a plan file. Did you mean to use -chdir?", planPath))
|
||||
return 1
|
||||
}
|
||||
|
||||
// If we successfully loaded a plan but this is a destroy operation,
|
||||
// explain that this is not supported.
|
||||
if c.Destroy {
|
||||
c.Ui.Error("Destroy can't be called with a plan file.")
|
||||
return 1
|
||||
}
|
||||
}
|
||||
if planFile != nil {
|
||||
// Reset the config path for backend loading
|
||||
|
@ -18,6 +18,12 @@ import (
|
||||
)
|
||||
|
||||
func TestApply_destroy(t *testing.T) {
|
||||
// Create a temporary working directory that is empty
|
||||
td := tempDir(t)
|
||||
testCopyDir(t, testFixturePath("apply"), td)
|
||||
defer os.RemoveAll(td)
|
||||
defer testChdir(t, td)()
|
||||
|
||||
originalState := states.BuildState(func(s *states.SyncState) {
|
||||
s.SetResourceInstanceCurrent(
|
||||
addrs.Resource{
|
||||
@ -64,7 +70,6 @@ func TestApply_destroy(t *testing.T) {
|
||||
args := []string{
|
||||
"-auto-approve",
|
||||
"-state", statePath,
|
||||
testFixturePath("apply"),
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Log(ui.OutputWriter.String())
|
||||
@ -233,6 +238,12 @@ func TestApply_destroyApproveYes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApply_destroyLockedState(t *testing.T) {
|
||||
// Create a temporary working directory that is empty
|
||||
td := tempDir(t)
|
||||
testCopyDir(t, testFixturePath("apply"), td)
|
||||
defer os.RemoveAll(td)
|
||||
defer testChdir(t, td)()
|
||||
|
||||
originalState := states.BuildState(func(s *states.SyncState) {
|
||||
s.SetResourceInstanceCurrent(
|
||||
addrs.Resource{
|
||||
@ -272,7 +283,6 @@ func TestApply_destroyLockedState(t *testing.T) {
|
||||
args := []string{
|
||||
"-auto-approve",
|
||||
"-state", statePath,
|
||||
testFixturePath("apply"),
|
||||
}
|
||||
|
||||
if code := c.Run(args); code == 0 {
|
||||
@ -286,6 +296,12 @@ func TestApply_destroyLockedState(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApply_destroyPlan(t *testing.T) {
|
||||
// Create a temporary working directory that is empty
|
||||
td := tempDir(t)
|
||||
testCopyDir(t, testFixturePath("apply"), td)
|
||||
defer os.RemoveAll(td)
|
||||
defer testChdir(t, td)()
|
||||
|
||||
planPath := testPlanFileNoop(t)
|
||||
|
||||
p := testProvider()
|
||||
@ -305,9 +321,50 @@ func TestApply_destroyPlan(t *testing.T) {
|
||||
if code := c.Run(args); code != 1 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
output := ui.ErrorWriter.String()
|
||||
if !strings.Contains(output, "plan file") {
|
||||
t.Fatal("expected command output to refer to plan file, but got:", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApply_destroyPath(t *testing.T) {
|
||||
// Create a temporary working directory that is empty
|
||||
td := tempDir(t)
|
||||
testCopyDir(t, testFixturePath("apply"), td)
|
||||
defer os.RemoveAll(td)
|
||||
defer testChdir(t, td)()
|
||||
|
||||
p := applyFixtureProvider()
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &ApplyCommand{
|
||||
Destroy: true,
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-auto-approve",
|
||||
testFixturePath("apply"),
|
||||
}
|
||||
if code := c.Run(args); code != 1 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
output := ui.ErrorWriter.String()
|
||||
if !strings.Contains(output, "-chdir") {
|
||||
t.Fatal("expected command output to refer to -chdir flag, but got:", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApply_destroyTargeted(t *testing.T) {
|
||||
// Create a temporary working directory that is empty
|
||||
td := tempDir(t)
|
||||
testCopyDir(t, testFixturePath("apply-destroy-targeted"), td)
|
||||
defer os.RemoveAll(td)
|
||||
defer testChdir(t, td)()
|
||||
|
||||
originalState := states.BuildState(func(s *states.SyncState) {
|
||||
s.SetResourceInstanceCurrent(
|
||||
addrs.Resource{
|
||||
@ -383,7 +440,6 @@ func TestApply_destroyTargeted(t *testing.T) {
|
||||
"-auto-approve",
|
||||
"-target", "test_instance.foo",
|
||||
"-state", statePath,
|
||||
testFixturePath("apply-destroy-targeted"),
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
|
@ -63,6 +63,36 @@ func TestApply(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestApply_path(t *testing.T) {
|
||||
// Create a temporary working directory that is empty
|
||||
td := tempDir(t)
|
||||
testCopyDir(t, testFixturePath("apply"), td)
|
||||
defer os.RemoveAll(td)
|
||||
defer testChdir(t, td)()
|
||||
|
||||
p := applyFixtureProvider()
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &ApplyCommand{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-auto-approve",
|
||||
testFixturePath("apply"),
|
||||
}
|
||||
if code := c.Run(args); code != 1 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
output := ui.ErrorWriter.String()
|
||||
if !strings.Contains(output, "-chdir") {
|
||||
t.Fatal("expected command output to refer to -chdir flag, but got:", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApply_approveNo(t *testing.T) {
|
||||
// Create a temporary working directory that is empty
|
||||
td := tempDir(t)
|
||||
|
Loading…
Reference in New Issue
Block a user