From e4e50617aa86097d694d68662ae2a31c8f2a57b0 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 21 Jan 2021 12:43:50 -0500 Subject: [PATCH] add failing test, and start new test files The existing context test files are becoming quite unwieldy. Start new ones both to make editing easier, and to help discourage the copy+pasting of older test patterns we no longer need. --- terraform/context_apply2_test.go | 1 + terraform/context_apply_test.go | 5 +++ terraform/context_plan2_test.go | 61 ++++++++++++++++++++++++++++++++ terraform/context_plan_test.go | 5 +++ 4 files changed, 72 insertions(+) create mode 100644 terraform/context_apply2_test.go create mode 100644 terraform/context_plan2_test.go diff --git a/terraform/context_apply2_test.go b/terraform/context_apply2_test.go new file mode 100644 index 000000000..cc3ee2f47 --- /dev/null +++ b/terraform/context_apply2_test.go @@ -0,0 +1 @@ +package terraform diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 537bd289b..1f80ae7d3 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -12564,3 +12564,8 @@ resource "test_object" "a" { t.Fatalf("error missing expected info: %q", errString) } } + +//////////////////////////////////////////////////////////////////////////////// +// NOTE: Due to the size of this file, new tests should be added to +// context_apply2_test.go. +//////////////////////////////////////////////////////////////////////////////// diff --git a/terraform/context_plan2_test.go b/terraform/context_plan2_test.go new file mode 100644 index 000000000..ecf2a36f8 --- /dev/null +++ b/terraform/context_plan2_test.go @@ -0,0 +1,61 @@ +package terraform + +import ( + "testing" + + "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/plans" + "github.com/hashicorp/terraform/providers" + "github.com/hashicorp/terraform/states" + "github.com/zclconf/go-cty/cty" +) + +func TestContext2Plan_removedDuringRefresh(t *testing.T) { + // The resource was added to state but actually failed to create and was + // left tainted. This should be removed during plan and result in a Create + // action. + m := testModuleInline(t, map[string]string{ + "main.tf": ` +resource "test_object" "a" { +} +`, + }) + + p := simpleMockProvider() + p.ReadResourceFn = func(req providers.ReadResourceRequest) (resp providers.ReadResourceResponse) { + resp.NewState = cty.NullVal(req.PriorState.Type()) + return resp + } + + addr := mustResourceInstanceAddr("test_object.a") + state := states.BuildState(func(s *states.SyncState) { + s.SetResourceInstanceCurrent(addr, &states.ResourceInstanceObjectSrc{ + AttrsJSON: []byte(`{"test_string":"foo"}`), + Status: states.ObjectTainted, + }, mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`)) + }) + + ctx := testContext2(t, &ContextOpts{ + Config: m, + State: state, + Providers: map[addrs.Provider]providers.Factory{ + addrs.NewDefaultProvider("test"): testProviderFuncFixed(p), + }, + }) + + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatal(diags.Err()) + } + + for _, c := range plan.Changes.Resources { + if c.Action != plans.Create { + t.Fatalf("expected Create action for missing %s, got %s", c.Addr, c.Action) + } + } + + _, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatal(diags.Err()) + } +} diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 099fff1c1..5507cc08d 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -6732,3 +6732,8 @@ output "planned" { } } } + +//////////////////////////////////////////////////////////////////////////////// +// NOTE: Due to the size of this file, new tests should be added to +// context_plan2_test.go. +////////////////////////////////////////////////////////////////////////////////