Skip to content

Commit

Permalink
Fix panic during "terraform show" with empty state
Browse files Browse the repository at this point in the history
It's possible to have > 1 result from the StateFilter, and not have any
instances to show.
  • Loading branch information
jbardin committed Oct 26, 2016
1 parent 68d865f commit 43f860d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions command/state_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion command/state_show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 43f860d

Please sign in to comment.