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.
This commit is contained in:
James Bardin 2016-10-26 17:18:36 -04:00
parent 68d865f46e
commit 43f860ddfd
2 changed files with 33 additions and 1 deletions

View File

@ -55,6 +55,10 @@ func (c *StateShowCommand) Run(args []string) int {
return 1 return 1
} }
if instance == nil {
return 0
}
is := instance.Value.(*terraform.InstanceState) is := instance.Value.(*terraform.InstanceState)
// Sort the keys // Sort the keys

View File

@ -127,7 +127,7 @@ func TestStateShow_noState(t *testing.T) {
} }
func TestStateShow_emptyState(t *testing.T) { func TestStateShow_emptyState(t *testing.T) {
state := &terraform.State{} state := terraform.NewState()
statePath := testStateFile(t, state) 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 = ` const testStateShowOutput = `
id = bar id = bar
bar = value bar = value