Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic on show plan #23581

Merged
merged 2 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions command/format/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,9 @@ func (p *blockBodyDiffPrinter) writeActionSymbol(action plans.Action) {
}

func (p *blockBodyDiffPrinter) pathForcesNewResource(path cty.Path) bool {
if !p.action.IsReplace() {
// "requiredReplace" only applies when the instance is being replaced
if !p.action.IsReplace() || p.requiredReplace.Empty() {
// "requiredReplace" only applies when the instance is being replaced,
// and we should only inspect that set if it is not empty
return false
}
return p.requiredReplace.Has(path)
Expand Down
35 changes: 31 additions & 4 deletions command/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,34 @@ func TestShow_plan(t *testing.T) {
}
}

func TestShow_planWithChanges(t *testing.T) {
planPathWithChanges := showFixturePlanFile(t, plans.DeleteThenCreate)

ui := cli.NewMockUi()
c := &ShowCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(showFixtureProvider()),
Ui: ui,
},
}

args := []string{
planPathWithChanges,
}

if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}

want := `test_instance.foo must be replaced`
got := ui.OutputWriter.String()
if !strings.Contains(got, want) {
t.Errorf("missing expected output\nwant: %s\ngot:\n%s", want, got)
}
}

func TestShow_plan_json(t *testing.T) {
planPath := showFixturePlanFile(t)
planPath := showFixturePlanFile(t, plans.Create)

ui := new(cli.MockUi)
c := &ShowCommand{
Expand Down Expand Up @@ -377,9 +403,10 @@ func showFixtureProvider() *terraform.MockProvider {
}

// showFixturePlanFile creates a plan file at a temporary location containing a
// single change to create the test_instance.foo that is included in the "show"
// single change to create or update the test_instance.foo that is included in the "show"
// test fixture, returning the location of that plan file.
func showFixturePlanFile(t *testing.T) string {
// `action` is the planned change you would like to elicit
func showFixturePlanFile(t *testing.T, action plans.Action) string {
_, snap := testModuleWithSnapshot(t, "show")
plannedVal := cty.ObjectVal(map[string]cty.Value{
"id": cty.UnknownVal(cty.String),
Expand All @@ -402,7 +429,7 @@ func showFixturePlanFile(t *testing.T) string {
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
ProviderAddr: addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
ChangeSrc: plans.ChangeSrc{
Action: plans.Create,
Action: action,
Before: priorValRaw,
After: plannedValRaw,
},
Expand Down