From 94f43ff1458d573cfdf7fc9f17ba8610090898c0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 3 Oct 2014 15:57:43 -0700 Subject: [PATCH] terraform: fix crash if outputs with no state [GH-358] --- terraform/context.go | 2 +- terraform/context_test.go | 21 +++++++++++++++++++ .../test-fixtures/refresh-no-state/main.tf | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/refresh-no-state/main.tf diff --git a/terraform/context.go b/terraform/context.go index 2823fdc4f..132a9f48d 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -498,7 +498,7 @@ func (c *walkContext) Walk() error { // On Apply, we prune so that we don't do outputs if we destroyed mod.prune() } - if len(mod.Resources) == 0 { + if mod == nil || len(mod.Resources) == 0 { return nil } diff --git a/terraform/context_test.go b/terraform/context_test.go index dded2d987..3d2c1cf3a 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -3336,6 +3336,27 @@ func TestContextRefresh_modules(t *testing.T) { } } +// GH-70 +func TestContextRefresh_noState(t *testing.T) { + p := testProvider("aws") + m := testModule(t, "refresh-no-state") + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + p.RefreshFn = nil + p.RefreshReturn = &InstanceState{ + ID: "foo", + } + + if _, err := ctx.Refresh(); err != nil { + t.Fatalf("err: %s", err) + } +} + func TestContextRefresh_state(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-basic") diff --git a/terraform/test-fixtures/refresh-no-state/main.tf b/terraform/test-fixtures/refresh-no-state/main.tf new file mode 100644 index 000000000..13edd5f5e --- /dev/null +++ b/terraform/test-fixtures/refresh-no-state/main.tf @@ -0,0 +1 @@ +output "foo" {}