From ba749db9ed622afba1f4cc2793cf0daead32a467 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 4 Jan 2018 15:00:09 -0500 Subject: [PATCH] add test checking CloseProvider There was no test checking that Close wsa called on the mock provider. This fails now since the CloseProviderTransformer isn't using the fully resolved provider name. --- terraform/context_plan_test.go | 26 +++++++++++++++++++ terraform/eval_provider_test.go | 5 ++-- .../plan-close-module-provider/main.tf | 3 +++ .../plan-close-module-provider/mod/main.tf | 7 +++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 terraform/test-fixtures/plan-close-module-provider/main.tf create mode 100644 terraform/test-fixtures/plan-close-module-provider/mod/main.tf diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 00d815901..8b680879d 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -2401,6 +2401,32 @@ func TestContext2Plan_hook(t *testing.T) { } } +func TestContext2Plan_closeProvider(t *testing.T) { + // this fixture only has an aliased provider located in the module, to make + // sure that the provier name contains a path more complex than + // "provider.aws". + m := testModule(t, "plan-close-module-provider") + p := testProvider("aws") + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + ProviderResolver: ResourceProviderResolverFixed( + map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + ), + }) + + _, err := ctx.Plan() + if err != nil { + t.Fatalf("err: %s", err) + } + + if !p.CloseCalled { + t.Fatal("provider not closed") + } +} + func TestContext2Plan_orphan(t *testing.T) { m := testModule(t, "plan-orphan") p := testProvider("aws") diff --git a/terraform/eval_provider_test.go b/terraform/eval_provider_test.go index cca02a76b..8722b3e51 100644 --- a/terraform/eval_provider_test.go +++ b/terraform/eval_provider_test.go @@ -90,7 +90,8 @@ func TestEvalInitProvider(t *testing.T) { } func TestEvalCloseProvider(t *testing.T) { - n := &EvalCloseProvider{Name: "foo"} + providerName := ResolveProviderName("foo", nil) + n := &EvalCloseProvider{Name: providerName} provider := &MockResourceProvider{} ctx := &MockEvalContext{CloseProviderProvider: provider} if _, err := n.Eval(ctx); err != nil { @@ -100,7 +101,7 @@ func TestEvalCloseProvider(t *testing.T) { if !ctx.CloseProviderCalled { t.Fatal("should be called") } - if ctx.CloseProviderName != "foo" { + if ctx.CloseProviderName != providerName { t.Fatalf("bad: %#v", ctx.CloseProviderName) } } diff --git a/terraform/test-fixtures/plan-close-module-provider/main.tf b/terraform/test-fixtures/plan-close-module-provider/main.tf new file mode 100644 index 000000000..ba8468469 --- /dev/null +++ b/terraform/test-fixtures/plan-close-module-provider/main.tf @@ -0,0 +1,3 @@ +module "mod" { + source = "./mod" +} diff --git a/terraform/test-fixtures/plan-close-module-provider/mod/main.tf b/terraform/test-fixtures/plan-close-module-provider/mod/main.tf new file mode 100644 index 000000000..3ce1991f2 --- /dev/null +++ b/terraform/test-fixtures/plan-close-module-provider/mod/main.tf @@ -0,0 +1,7 @@ +provider "aws" { + alias = "mod" +} + +resource "aws_instance" "bar" { + provider = "aws.mod" +}