From c6c851eb3f9835bccd311063122a702ae3ad03d8 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Sat, 9 May 2020 10:41:24 -0400 Subject: [PATCH] add test for using a data source with depends_on Ensure that a data source with depends_on not only plans to update during refresh, but evaluates correctly in the plan ensuring dependencies are planned accordingly. --- terraform/context_apply_test.go | 62 ++++++++++++++++++- .../testdata/apply-data-depends-on/main.tf | 7 --- 2 files changed, 61 insertions(+), 8 deletions(-) delete mode 100644 terraform/testdata/apply-data-depends-on/main.tf diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index b26d2c481..6e910a69b 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -8724,7 +8724,20 @@ func TestContext2Apply_destroyNestedModuleWithAttrsReferencingResource(t *testin // that resource to be applied first. func TestContext2Apply_dataDependsOn(t *testing.T) { p := testProvider("null") - m := testModule(t, "apply-data-depends-on") + m := testModuleInline(t, map[string]string{ + "main.tf": ` +resource "null_instance" "write" { + foo = "attribute" +} + +data "null_data_source" "read" { + depends_on = ["null_instance.write"] +} + +resource "null_instance" "depends" { + foo = data.null_data_source.read.foo +} +`}) ctx := testContext2(t, &ContextOpts{ Config: m, @@ -8792,6 +8805,53 @@ func TestContext2Apply_dataDependsOn(t *testing.T) { t.Fatalf("unexpected change for %s", c.Addr) } } + + // now we cause a change in the first resource, which should trigger a plan + // in the data source, and the resource that depends on the data source + // must plan a change as well. + m = testModuleInline(t, map[string]string{ + "main.tf": ` +resource "null_instance" "write" { + foo = "new" +} + +data "null_data_source" "read" { + depends_on = ["null_instance.write"] +} + +resource "null_instance" "depends" { + foo = data.null_data_source.read.foo +} +`}) + + p.ApplyFn = func(info *InstanceInfo, s *InstanceState, d *InstanceDiff) (*InstanceState, error) { + // the side effect of the resource being applied + provisionerOutput = "APPLIED_AGAIN" + return testApplyFn(info, s, d) + } + + ctx = testContext2(t, &ContextOpts{ + Config: m, + State: state, + Providers: map[addrs.Provider]providers.Factory{ + addrs.NewDefaultProvider("null"): testProviderFuncFixed(p), + }, + }) + + plan, diags = ctx.Plan() + assertNoErrors(t, diags) + + expectedChanges := map[string]plans.Action{ + "null_instance.write": plans.Update, + "data.null_data_source.read": plans.Read, + "null_instance.depends": plans.Update, + } + + for _, c := range plan.Changes.Resources { + if c.Action != expectedChanges[c.Addr.String()] { + t.Errorf("unexpected %s for %s", c.Action, c.Addr) + } + } } func TestContext2Apply_terraformWorkspace(t *testing.T) { diff --git a/terraform/testdata/apply-data-depends-on/main.tf b/terraform/testdata/apply-data-depends-on/main.tf deleted file mode 100644 index f48b48e40..000000000 --- a/terraform/testdata/apply-data-depends-on/main.tf +++ /dev/null @@ -1,7 +0,0 @@ -resource "null_instance" "write" { - foo = "attribute" -} - -data "null_data_source" "read" { - depends_on = ["null_instance.write"] -}