From dc036a0dd057284fb288ce46587194b11eec0b46 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 16 Jan 2015 09:56:51 -0800 Subject: [PATCH] terraform: fix bug with crash with no providers [GH-786] --- terraform/context_test.go | 19 +++++++++++++++++++ terraform/graph.go | 2 +- .../refresh-unknown-provider/main.tf | 4 ++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/refresh-unknown-provider/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index e5c2794a5..b464fed90 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -4044,6 +4044,25 @@ func TestContextPlan_taint(t *testing.T) { } } +// Doing a Refresh (or any operation really, but Refresh usually +// happens first) with a config with an unknown provider should result in +// an error. The key bug this found was that this wasn't happening if +// Providers was _empty_. +func TestContextRefresh_unknownProvider(t *testing.T) { + m := testModule(t, "refresh-unknown-provider") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{}, + }) + + if _, err := ctx.Refresh(); err == nil { + t.Fatal("should error") + } +} + func TestContextPlan_multiple_taint(t *testing.T) { m := testModule(t, "plan-taint") p := testProvider("aws") diff --git a/terraform/graph.go b/terraform/graph.go index d86493015..90db2f35a 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -214,7 +214,7 @@ func Graph(opts *GraphOpts) (*depgraph.Graph, error) { // determine what providers are missing. graphMapResourceProviderId(g) - if len(opts.Providers) > 0 { + if opts.Providers != nil { // Add missing providers from the mapping. if err := graphAddMissingResourceProviders(g, opts.Providers); err != nil { return nil, err diff --git a/terraform/test-fixtures/refresh-unknown-provider/main.tf b/terraform/test-fixtures/refresh-unknown-provider/main.tf new file mode 100644 index 000000000..8a29fddd0 --- /dev/null +++ b/terraform/test-fixtures/refresh-unknown-provider/main.tf @@ -0,0 +1,4 @@ +resource "unknown_instance" "foo" { + num = "2" + compute = "foo" +}