Merge pull request #12178 from hashicorp/b-refresh-nil-module

backend/local: refresh with no config should not crash on input
This commit is contained in:
Mitchell Hashimoto 2017-02-22 18:40:08 -08:00 committed by GitHub
commit d719ba5155
2 changed files with 64 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/backend"
clistate "github.com/hashicorp/terraform/command/state"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/state"
)
@ -42,6 +43,12 @@ func (b *Local) opRefresh(
}
}
// If we have no config module given to use, create an empty tree to
// avoid crashes when Terraform.Context is initialized.
if op.Module == nil {
op.Module = module.NewEmptyTree()
}
// Get our context
tfCtx, opState, err := b.context(op)
if err != nil {

View File

@ -40,6 +40,63 @@ test_instance.foo:
`)
}
func TestLocal_refreshNilModule(t *testing.T) {
b := TestLocal(t)
p := TestLocalProvider(t, b, "test")
terraform.TestStateFile(t, b.StatePath, testRefreshState())
p.RefreshFn = nil
p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
op := testOperationRefresh()
op.Module = nil
run, err := b.Operation(context.Background(), op)
if err != nil {
t.Fatalf("bad: %s", err)
}
<-run.Done()
if !p.RefreshCalled {
t.Fatal("refresh should be called")
}
checkState(t, b.StateOutPath, `
test_instance.foo:
ID = yes
`)
}
// GH-12174
func TestLocal_refreshNilModuleWithInput(t *testing.T) {
b := TestLocal(t)
p := TestLocalProvider(t, b, "test")
terraform.TestStateFile(t, b.StatePath, testRefreshState())
p.RefreshFn = nil
p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
b.OpInput = true
op := testOperationRefresh()
op.Module = nil
run, err := b.Operation(context.Background(), op)
if err != nil {
t.Fatalf("bad: %s", err)
}
<-run.Done()
if !p.RefreshCalled {
t.Fatal("refresh should be called")
}
checkState(t, b.StateOutPath, `
test_instance.foo:
ID = yes
`)
}
func TestLocal_refreshInput(t *testing.T) {
b := TestLocal(t)
p := TestLocalProvider(t, b, "test")