From 58babccc7a44dcfc5859063c4c0363c34d3447eb Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 3 Jun 2020 16:22:09 -0400 Subject: [PATCH] improve depends_on test to check ordering --- terraform/context_apply_test.go | 44 +++++++++++++++---- .../apply-module-depends-on/moda/main.tf | 1 + .../apply-module-depends-on/modb/main.tf | 1 + 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index f7b1130a1..a99aae428 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -11121,17 +11121,36 @@ func TestContext2Apply_moduleDependsOn(t *testing.T) { m := testModule(t, "apply-module-depends-on") p := testProvider("test") - p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ - State: cty.ObjectVal(map[string]cty.Value{ - "id": cty.StringVal("data"), - "foo": cty.NullVal(cty.String), - }), - } - p.DiffFn = testDiffFn // each instance being applied should happen in sequential order applied := int64(0) + p.ReadDataSourceFn = func(req providers.ReadDataSourceRequest) providers.ReadDataSourceResponse { + cfg := req.Config.AsValueMap() + foo := cfg["foo"].AsString() + ord := atomic.LoadInt64(&applied) + + resp := providers.ReadDataSourceResponse{ + State: cty.ObjectVal(map[string]cty.Value{ + "id": cty.StringVal("data"), + "foo": cfg["foo"], + }), + } + + if foo == "a" && ord < 4 { + // due to data source "a"'s module depending on instance 4, this + // should not be less than 4 + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("data source a read too early")) + } + if foo == "b" && ord < 1 { + // due to data source "b"'s module depending on instance 1, this + // should not be less than 1 + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("data source b read too early")) + } + return resp + } + p.DiffFn = testDiffFn + p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) { state := req.PlannedState.AsValueMap() num, _ := state["num"].AsBigFloat().Float64() @@ -11154,7 +11173,12 @@ func TestContext2Apply_moduleDependsOn(t *testing.T) { }, }) - _, diags := ctx.Plan() + _, diags := ctx.Refresh() + if diags.HasErrors() { + t.Fatal(diags.ErrWithWarnings()) + } + + _, diags = ctx.Plan() if diags.HasErrors() { t.Fatal(diags.ErrWithWarnings()) } @@ -11165,6 +11189,10 @@ func TestContext2Apply_moduleDependsOn(t *testing.T) { } // run the plan again to ensure that data sources are not going to be re-read + _, diags = ctx.Refresh() + if diags.HasErrors() { + t.Fatal(diags.ErrWithWarnings()) + } plan, diags := ctx.Plan() if diags.HasErrors() { t.Fatal(diags.ErrWithWarnings()) diff --git a/terraform/testdata/apply-module-depends-on/moda/main.tf b/terraform/testdata/apply-module-depends-on/moda/main.tf index 914e545c8..e60d300ba 100644 --- a/terraform/testdata/apply-module-depends-on/moda/main.tf +++ b/terraform/testdata/apply-module-depends-on/moda/main.tf @@ -3,6 +3,7 @@ resource "test_instance" "a" { } data "test_data_source" "a" { + foo = "a" } output "out" { diff --git a/terraform/testdata/apply-module-depends-on/modb/main.tf b/terraform/testdata/apply-module-depends-on/modb/main.tf index 17192b039..961c5d560 100644 --- a/terraform/testdata/apply-module-depends-on/modb/main.tf +++ b/terraform/testdata/apply-module-depends-on/modb/main.tf @@ -3,6 +3,7 @@ resource "test_instance" "b" { } data "test_data_source" "b" { + foo = "b" } output "out" {