From 43c05252774e7cece81a07443961f07fd64c9bd7 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 2 Oct 2020 08:50:24 -0400 Subject: [PATCH] fix the test that was supposed to break The test for this behavior did not work, because the old mock diff function does not work correctly. Write a PlanResourceChange function to return a correct plan. --- terraform/context_apply_test.go | 43 +++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 4596ae09d..8ce875a63 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -11389,11 +11389,34 @@ locals { return testApplyFn(info, s, d) } - p.DiffFn = testDiffFn + p.PlanResourceChangeFn = func(r providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) { + n := r.ProposedNewState.AsValueMap() + + if r.PriorState.IsNull() { + n["id"] = cty.UnknownVal(cty.String) + resp.PlannedState = cty.ObjectVal(n) + return resp + } + + p := r.PriorState.AsValueMap() + + priorRN := p["require_new"] + newRN := n["require_new"] + + if eq := priorRN.Equals(newRN); !eq.IsKnown() || eq.False() { + resp.RequiresReplace = []cty.Path{{cty.GetAttrStep{Name: "require_new"}}} + n["id"] = cty.UnknownVal(cty.String) + } + + resp.PlannedState = cty.ObjectVal(n) + return resp + } + + // reduce the count to 1 ctx := testContext2(t, &ContextOpts{ Variables: InputValues{ "ct": &InputValue{ - Value: cty.NumberIntVal(0), + Value: cty.NumberIntVal(1), SourceType: ValueFromCaller, }, }, @@ -11409,21 +11432,11 @@ locals { t.Fatal(diags.ErrWithWarnings()) } - // if resource b isn't going to apply correctly, we will get an error about - // an invalid plan value state, diags = ctx.Apply() - errMsg := diags.ErrWithWarnings().Error() - if strings.Contains(errMsg, "Cycle") { - t.Fatal("test should not produce a cycle:\n", errMsg) + if diags.HasErrors() { + log.Fatal(diags.ErrWithWarnings()) } - if !diags.HasErrors() { - // FIXME: this test is correct, but needs to wait until we no longer - // evaluate resourced that are pending destruction. - t.Fatal("used to error, but now it's fixed!") - } - return - // check the output, as those can't cause an error planning the value out := state.RootModule().OutputValues["out"].Value.AsString() if out != "a0" { @@ -11450,8 +11463,6 @@ locals { t.Fatal(diags.ErrWithWarnings()) } - // if resource b isn't going to apply correctly, we will get an error about - // an invalid plan value state, diags = ctx.Apply() if diags.HasErrors() { t.Fatal(diags.ErrWithWarnings())