Skip to content

Commit a175b17

Browse files
committed
Test identity decoding from state without schema
1 parent 87ee7e3 commit a175b17

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

internal/terraform/context_plan_identity_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,76 @@ func TestContext2Plan_resource_identity_refresh(t *testing.T) {
309309
}
310310
}
311311

312+
func TestContext2Plan_resource_identity_refresh_downgrade(t *testing.T) {
313+
p := testProvider("aws")
314+
m := testModule(t, "refresh-basic")
315+
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
316+
ResourceTypes: map[string]*configschema.Block{
317+
"aws_instance": {
318+
Attributes: map[string]*configschema.Attribute{
319+
"id": {
320+
Type: cty.String,
321+
Computed: true,
322+
},
323+
"foo": {
324+
Type: cty.String,
325+
Optional: true,
326+
Computed: true,
327+
},
328+
},
329+
},
330+
},
331+
})
332+
333+
state := states.NewState()
334+
root := state.EnsureModule(addrs.RootModuleInstance)
335+
336+
root.SetResourceInstanceCurrent(
337+
mustResourceInstanceAddr("aws_instance.web").Resource,
338+
&states.ResourceInstanceObjectSrc{
339+
Status: states.ObjectReady,
340+
AttrsJSON: []byte(`{"id":"foo","foo":"bar"}`),
341+
IdentitySchemaVersion: 0,
342+
IdentityJSON: []byte(`{"id": "foo"}`),
343+
},
344+
mustProviderConfig(`provider["registry.terraform.io/hashicorp/aws"]`),
345+
)
346+
347+
ctx := testContext2(t, &ContextOpts{
348+
Providers: map[addrs.Provider]providers.Factory{
349+
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
350+
},
351+
})
352+
353+
schema := p.GetProviderSchemaResponse.ResourceTypes["aws_instance"]
354+
355+
p.ReadResourceFn = func(req providers.ReadResourceRequest) providers.ReadResourceResponse {
356+
return providers.ReadResourceResponse{
357+
NewState: req.PriorState,
358+
}
359+
}
360+
361+
s, diags := ctx.Plan(m, state, &PlanOpts{Mode: plans.RefreshOnlyMode})
362+
363+
if diags.HasErrors() {
364+
t.Fatal(diags.Err())
365+
}
366+
367+
if !p.ReadResourceCalled {
368+
t.Fatal("ReadResource should be called")
369+
}
370+
371+
mod := s.PriorState.RootModule()
372+
fromState, err := mod.Resources["aws_instance.web"].Instances[addrs.NoKey].Current.Decode(schema)
373+
if err != nil {
374+
t.Fatal(err)
375+
}
376+
377+
if !fromState.Identity.IsNull() {
378+
t.Fatalf("wrong identity\nwant: null\ngot: %s", fromState.Identity.GoString())
379+
}
380+
}
381+
312382
// This test validates if a resource identity that is deposed and will be destroyed
313383
// can be refreshed with an identity during the plan.
314384
func TestContext2Plan_resource_identity_refresh_destroy_deposed(t *testing.T) {

0 commit comments

Comments
 (0)