From 43f860ddfd837984142fd51aebe15c2d2e80577e Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 26 Oct 2016 17:18:36 -0400 Subject: [PATCH] Fix panic during "terraform show" with empty state It's possible to have > 1 result from the StateFilter, and not have any instances to show. --- command/state_show.go | 4 ++++ command/state_show_test.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/command/state_show.go b/command/state_show.go index 1f5096e63d92..d0f7517af6ee 100644 --- a/command/state_show.go +++ b/command/state_show.go @@ -55,6 +55,10 @@ func (c *StateShowCommand) Run(args []string) int { return 1 } + if instance == nil { + return 0 + } + is := instance.Value.(*terraform.InstanceState) // Sort the keys diff --git a/command/state_show_test.go b/command/state_show_test.go index cbcfb46d8ce2..9e0ede3966b5 100644 --- a/command/state_show_test.go +++ b/command/state_show_test.go @@ -127,7 +127,7 @@ func TestStateShow_noState(t *testing.T) { } func TestStateShow_emptyState(t *testing.T) { - state := &terraform.State{} + state := terraform.NewState() statePath := testStateFile(t, state) @@ -149,6 +149,34 @@ func TestStateShow_emptyState(t *testing.T) { } } +func TestStateShow_emptyStateWithModule(t *testing.T) { + // empty state with empty module + state := terraform.NewState() + + mod := &terraform.ModuleState{ + Path: []string{"root", "mod"}, + } + state.Modules = append(state.Modules, mod) + + statePath := testStateFile(t, state) + + p := testProvider() + ui := new(cli.MockUi) + c := &StateShowCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, + }, + } + + args := []string{ + "-state", statePath, + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } +} + const testStateShowOutput = ` id = bar bar = value