Skip to content

Commit

Permalink
stacks: add read resource test case without refresh opts
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Apr 5, 2024
1 parent 9b1bbae commit 45eb3bb
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions internal/terraform/context_apply_deferred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package terraform

import (
"encoding/json"
"fmt"
"sync"
"testing"
Expand Down Expand Up @@ -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
},
Expand All @@ -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,
},
},
}
)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

0 comments on commit 45eb3bb

Please sign in to comment.