From 45eb3bb1a8936665b399f803df67f6a521dd2d9f Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Fri, 5 Apr 2024 11:02:34 +0200 Subject: [PATCH] stacks: add read resource test case without refresh opts --- .../terraform/context_apply_deferred_test.go | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/internal/terraform/context_apply_deferred_test.go b/internal/terraform/context_apply_deferred_test.go index cfbe9cd30384..ae25fc3757e3 100644 --- a/internal/terraform/context_apply_deferred_test.go +++ b/internal/terraform/context_apply_deferred_test.go @@ -4,6 +4,7 @@ package terraform import ( + "encoding/json" "fmt" "sync" "testing" @@ -362,8 +363,23 @@ output "a" { } `, }, + state: states.BuildState(func(state *states.SyncState) { + state.SetResourceInstanceCurrent( + mustResourceInstanceAddr("test.a"), + &states.ResourceInstanceObjectSrc{ + Status: states.ObjectReady, + AttrsJSON: mustParseJson(map[string]interface{}{ + "name": "a", // this is different from the config + }), + }, + addrs.AbsProviderConfig{ + Provider: addrs.NewDefaultProvider("test"), + Module: addrs.RootModule, + }) + }), stages: []deferredActionsTestStage{ { + buildOpts: func(opts *PlanOpts) { opts.Mode = plans.RefreshOnlyMode }, @@ -387,6 +403,36 @@ output "a" { wantDeferred: make(map[string]providers.DeferredReason), complete: true, }, + + { + inputs: map[string]cty.Value{}, + wantPlanned: map[string]cty.Value{ + // No resource is deferred since the refresh is not mandatory. + "deferred_read": cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("deferred_read"), + "upstream_names": cty.NullVal(cty.Set(cty.String)), + }), + }, + + wantActions: map[string]plans.Action{ + `test.a`: plans.Update, + }, + wantApplied: map[string]cty.Value{ + // No resource is deferred since the refresh is not mandatory. + "deferred_read": cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("deferred_read"), + "upstream_names": cty.NullVal(cty.Set(cty.String)), + }), + }, + wantOutputs: map[string]cty.Value{ + "a": cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("deferred_read"), + "upstream_names": cty.NullVal(cty.Set(cty.String)), + }), + }, + wantDeferred: make(map[string]providers.DeferredReason), + complete: true, + }, }, } ) @@ -448,12 +494,16 @@ func TestContextApply_deferredActions(t *testing.T) { } plan, diags := ctx.Plan(cfg, state, opts) + + // We expect no diagnostics. + assertNoDiagnostics(t, diags) + if plan.Complete != stage.complete { t.Errorf("wrong completion status in plan: got %v, want %v", plan.Complete, stage.complete) } - // We expect the correct planned changes and no diagnostics. - assertNoDiagnostics(t, diags) + // We expect the correct planned change + provider.plannedChanges.Test(t, stage.wantPlanned) // We expect the correct actions. @@ -598,3 +648,11 @@ func (provider *deferredActionsProvider) Provider() providers.Interface { }, } } + +func mustParseJson(values map[string]interface{}) []byte { + data, err := json.Marshal(values) + if err != nil { + panic(err) + } + return data +}