@@ -17,6 +17,10 @@ import (
1717 "github.com/hashicorp/terraform/internal/terraform"
1818)
1919
20+ func ptrOf [T any ](v T ) * T {
21+ return & v
22+ }
23+
2024func TestMarshalAttributeValues (t * testing.T ) {
2125 tests := []struct {
2226 Attr cty.Value
@@ -105,7 +109,7 @@ func TestMarshalAttributeValues(t *testing.T) {
105109 }
106110
107111 for _ , test := range tests {
108- got := marshalAttributeValues (test .Attr , test . Schema )
112+ got := marshalAttributeValues (test .Attr )
109113 eq := reflect .DeepEqual (got , test .Want )
110114 if ! eq {
111115 t .Fatalf ("wrong result:\n Got: %#v\n Want: %#v\n " , got , test .Want )
@@ -185,11 +189,13 @@ func TestMarshalPlannedOutputs(t *testing.T) {
185189
186190func TestMarshalPlanResources (t * testing.T ) {
187191 tests := map [string ]struct {
188- Action plans.Action
189- Before cty.Value
190- After cty.Value
191- Want []resource
192- Err bool
192+ Action plans.Action
193+ Before cty.Value
194+ After cty.Value
195+ Want []resource
196+ Err bool
197+ BeforeIdentity cty.Value
198+ AfterIdentity cty.Value
193199 }{
194200 "create with unknowns" : {
195201 Action : plans .Create ,
@@ -257,6 +263,37 @@ func TestMarshalPlanResources(t *testing.T) {
257263 }},
258264 Err : false ,
259265 },
266+ "with identity" : {
267+ Action : plans .Create ,
268+ Before : cty .NullVal (cty .EmptyObject ),
269+ After : cty .ObjectVal (map [string ]cty.Value {
270+ "woozles" : cty .StringVal ("woo" ),
271+ "foozles" : cty .NullVal (cty .String ),
272+ }),
273+ BeforeIdentity : cty .NullVal (cty .EmptyObject ),
274+ AfterIdentity : cty .ObjectVal (map [string ]cty.Value {
275+ "id" : cty .StringVal ("someId" ),
276+ }),
277+ Want : []resource {{
278+ Address : "test_thing.example" ,
279+ Mode : "managed" ,
280+ Type : "test_thing" ,
281+ Name : "example" ,
282+ Index : addrs .InstanceKey (nil ),
283+ ProviderName : "registry.terraform.io/hashicorp/test" ,
284+ SchemaVersion : 1 ,
285+ AttributeValues : attributeValues {
286+ "woozles" : json .RawMessage (`"woo"` ),
287+ "foozles" : json .RawMessage (`null` ),
288+ },
289+ SensitiveValues : json .RawMessage ("{}" ),
290+ IdentitySchemaVersion : ptrOf [uint64 ](2 ),
291+ IdentityValues : attributeValues {
292+ "id" : json .RawMessage (`"someId"` ),
293+ },
294+ }},
295+ Err : false ,
296+ },
260297 }
261298
262299 for name , test := range tests {
@@ -270,6 +307,23 @@ func TestMarshalPlanResources(t *testing.T) {
270307 if err != nil {
271308 t .Fatal (err )
272309 }
310+
311+ var beforeIdentity , afterIdentity plans.DynamicValue
312+ if ! test .BeforeIdentity .IsNull () {
313+ var err error
314+ beforeIdentity , err = plans .NewDynamicValue (test .BeforeIdentity , test .BeforeIdentity .Type ())
315+ if err != nil {
316+ t .Fatal (err )
317+ }
318+ }
319+ if ! test .AfterIdentity .IsNull () {
320+ var err error
321+ afterIdentity , err = plans .NewDynamicValue (test .AfterIdentity , test .AfterIdentity .Type ())
322+ if err != nil {
323+ t .Fatal (err )
324+ }
325+ }
326+
273327 testChange := & plans.ChangesSrc {
274328 Resources : []* plans.ResourceInstanceChangeSrc {
275329 {
@@ -283,9 +337,11 @@ func TestMarshalPlanResources(t *testing.T) {
283337 Module : addrs .RootModule ,
284338 },
285339 ChangeSrc : plans.ChangeSrc {
286- Action : test .Action ,
287- Before : before ,
288- After : after ,
340+ Action : test .Action ,
341+ Before : before ,
342+ After : after ,
343+ BeforeIdentity : beforeIdentity ,
344+ AfterIdentity : afterIdentity ,
289345 },
290346 },
291347 },
@@ -357,6 +413,13 @@ func testSchemas() *terraform.Schemas {
357413 "foozles" : {Type : cty .String , Optional : true },
358414 },
359415 },
416+ IdentityVersion : 2 ,
417+ Identity : & configschema.Object {
418+ Attributes : map [string ]* configschema.Attribute {
419+ "id" : {Type : cty .String , Required : true },
420+ },
421+ Nesting : configschema .NestingSingle ,
422+ },
360423 },
361424 },
362425 },
0 commit comments