Skip to content

Commit 6ad5c31

Browse files
committed
Trying to start testing
1 parent 6def5ff commit 6ad5c31

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

internal/fromproto5/moveresourcestate_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ func TestMoveResourceStateRequest(t *testing.T) {
162162
TargetTypeName: "examplecloud_thing",
163163
},
164164
},
165+
"SourceIdentity": {
166+
input: &tfprotov5.MoveResourceStateRequest{
167+
SourceIdentity: testNewTfprotov5RawState(t, map[string]interface{}{
168+
"test_identity_attribute": "test-value",
169+
}),
170+
},
171+
resourceSchema: testFwSchema,
172+
expected: &fwserver.MoveResourceStateRequest{
173+
SourceIdentity: testNewTfprotov6RawState(t, map[string]interface{}{
174+
"test_identity_attribute": "test-value",
175+
}),
176+
TargetResourceSchema: testFwSchema,
177+
},
178+
},
179+
"SourceIdentitySchemaVersion": {
180+
input: &tfprotov5.MoveResourceStateRequest{
181+
SourceIdentitySchemaVersion: 123,
182+
},
183+
resourceSchema: testFwSchema,
184+
expected: &fwserver.MoveResourceStateRequest{
185+
SourceIdentitySchemaVersion: 123,
186+
TargetResourceSchema: testFwSchema,
187+
},
188+
},
165189
}
166190

167191
for name, testCase := range testCases {

internal/fromproto6/moveresourcestate_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ func TestMoveResourceStateRequest(t *testing.T) {
162162
TargetTypeName: "examplecloud_thing",
163163
},
164164
},
165+
"SourceIdentity": {
166+
input: &tfprotov6.MoveResourceStateRequest{
167+
SourceIdentity: testNewRawState(t, map[string]interface{}{
168+
"test_identity_attribute": "test-value",
169+
}),
170+
},
171+
resourceSchema: testFwSchema,
172+
expected: &fwserver.MoveResourceStateRequest{
173+
SourceIdentity: testNewRawState(t, map[string]interface{}{
174+
"test_identity_attribute": "test-value",
175+
}),
176+
TargetResourceSchema: testFwSchema,
177+
},
178+
},
179+
"SourceIdentitySchemaVersion": {
180+
input: &tfprotov6.MoveResourceStateRequest{
181+
SourceIdentitySchemaVersion: 123,
182+
},
183+
resourceSchema: testFwSchema,
184+
expected: &fwserver.MoveResourceStateRequest{
185+
SourceIdentitySchemaVersion: 123,
186+
TargetResourceSchema: testFwSchema,
187+
},
188+
},
165189
}
166190

167191
for name, testCase := range testCases {

internal/fwserver/server_moveresourcestate_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package fwserver_test
66
import (
77
"context"
88
"fmt"
9+
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
910
"testing"
1011

1112
"github.com/google/go-cmp/cmp"
@@ -39,8 +40,21 @@ func TestServerMoveResourceState(t *testing.T) {
3940
},
4041
},
4142
}
43+
44+
testIdentitySchema := identityschema.Schema{
45+
Attributes: map[string]identityschema.Attribute{
46+
"optionalforimport_attribute": identityschema.StringAttribute{
47+
OptionalForImport: true,
48+
},
49+
"requiredforimport_attribute": identityschema.StringAttribute{
50+
RequiredForImport: true,
51+
},
52+
},
53+
}
4254
schemaType := testSchema.Type().TerraformType(ctx)
4355

56+
schemaIdentityType := testIdentitySchema.Type().TerraformType(ctx)
57+
4458
testSchemaWriteOnly := schema.Schema{
4559
Attributes: map[string]schema.Attribute{
4660
"id": schema.StringAttribute{
@@ -812,6 +826,112 @@ func TestServerMoveResourceState(t *testing.T) {
812826
},
813827
},
814828
},
829+
"request-SourceIdentitySchemaVersion": {
830+
server: &fwserver.Server{
831+
Provider: &testprovider.Provider{},
832+
},
833+
request: &fwserver.MoveResourceStateRequest{
834+
SourceIdentitySchemaVersion: 123,
835+
// SourceRawState required to prevent error
836+
SourceRawState: testNewRawState(t, map[string]interface{}{
837+
"id": "test-id-value",
838+
"required_attribute": true,
839+
}),
840+
TargetResource: &testprovider.ResourceWithMoveState{
841+
MoveStateMethod: func(ctx context.Context) []resource.StateMover {
842+
return []resource.StateMover{
843+
{
844+
StateMover: func(_ context.Context, req resource.MoveStateRequest, resp *resource.MoveStateResponse) {
845+
expected := int64(123)
846+
847+
if diff := cmp.Diff(req.SourceIdentitySchemaVersion, expected); diff != "" {
848+
resp.Diagnostics.AddError("Unexpected req.SourceIdentitySchemaVersion difference", diff)
849+
}
850+
851+
// Prevent missing implementation error, the values do not matter except for response assertion
852+
resp.Diagnostics.Append(resp.TargetState.SetAttribute(ctx, path.Root("id"), "test-id-value")...)
853+
resp.Diagnostics.Append(resp.TargetState.SetAttribute(ctx, path.Root("required_attribute"), "true")...)
854+
},
855+
},
856+
}
857+
},
858+
},
859+
TargetResourceSchema: testSchema,
860+
TargetTypeName: "test_resource",
861+
},
862+
expectedResponse: &fwserver.MoveResourceStateResponse{
863+
TargetPrivate: privatestate.EmptyData(ctx),
864+
TargetState: &tfsdk.State{
865+
Raw: tftypes.NewValue(schemaType, map[string]tftypes.Value{
866+
"id": tftypes.NewValue(tftypes.String, "test-id-value"),
867+
"optional_attribute": tftypes.NewValue(tftypes.String, nil),
868+
"required_attribute": tftypes.NewValue(tftypes.String, "true"),
869+
}),
870+
Schema: testSchema,
871+
},
872+
},
873+
},
874+
"request-SourceIdentity": {
875+
server: &fwserver.Server{
876+
Provider: &testprovider.Provider{},
877+
},
878+
request: &fwserver.MoveResourceStateRequest{
879+
// SourceRawState required to prevent error
880+
SourceIdentity: testNewRawState(t, map[string]interface{}{
881+
"optionalforimport_attribute": false,
882+
"requiredforimport_attribute": true,
883+
}),
884+
SourceRawState: testNewRawState(t, map[string]interface{}{
885+
"id": "test-id-value",
886+
"required_attribute": true,
887+
}),
888+
TargetResource: &testprovider.ResourceWithMoveState{
889+
MoveStateMethod: func(ctx context.Context) []resource.StateMover {
890+
return []resource.StateMover{
891+
{
892+
StateMover: func(_ context.Context, req resource.MoveStateRequest, resp *resource.MoveStateResponse) {
893+
expectedSourceIdentity := testNewRawState(t, map[string]interface{}{
894+
"optionalforimport_attribute": false,
895+
"requiredforimport_attribute": true,
896+
})
897+
898+
if diff := cmp.Diff(req.SourceIdentity, expectedSourceIdentity); diff != "" {
899+
resp.Diagnostics.AddError("Unexpected req.SourceIdentity difference", diff)
900+
}
901+
902+
resp.Diagnostics.Append(resp.TargetIdentity.SetAttribute(ctx, path.Root("optionalforimport_attribute"), "false")...)
903+
resp.Diagnostics.Append(resp.TargetIdentity.SetAttribute(ctx, path.Root("requiredforimport_attribute"), "true")...)
904+
905+
// Prevent missing implementation error, the values do not matter except for response assertion
906+
resp.Diagnostics.Append(resp.TargetState.SetAttribute(ctx, path.Root("id"), "test-id-value")...)
907+
resp.Diagnostics.Append(resp.TargetState.SetAttribute(ctx, path.Root("required_attribute"), "true")...)
908+
},
909+
},
910+
}
911+
},
912+
},
913+
TargetResourceSchema: testSchema,
914+
TargetTypeName: "test_resource",
915+
},
916+
expectedResponse: &fwserver.MoveResourceStateResponse{
917+
TargetPrivate: privatestate.EmptyData(ctx),
918+
TargetState: &tfsdk.State{
919+
Raw: tftypes.NewValue(schemaType, map[string]tftypes.Value{
920+
"id": tftypes.NewValue(tftypes.String, "test-id-value"),
921+
"optional_attribute": tftypes.NewValue(tftypes.String, nil),
922+
"required_attribute": tftypes.NewValue(tftypes.String, "true"),
923+
}),
924+
Schema: testSchema,
925+
},
926+
TargetIdentity: &tfsdk.ResourceIdentity{
927+
Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{
928+
"optionalforimport_attribute": tftypes.NewValue(tftypes.String, "false"),
929+
"requiredforimport_attribute": tftypes.NewValue(tftypes.String, "true"),
930+
}),
931+
Schema: testIdentitySchema,
932+
},
933+
},
934+
},
815935
}
816936

817937
for name, testCase := range testCases {

0 commit comments

Comments
 (0)