|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package fromproto5_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/internal/fromproto5" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/internal/privatestate" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 18 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5" |
| 19 | +) |
| 20 | + |
| 21 | +func TestMoveResourceStateRequest(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + testFwSchema := schema.Schema{ |
| 25 | + Attributes: map[string]schema.Attribute{ |
| 26 | + "test_attribute": schema.StringAttribute{ |
| 27 | + Required: true, |
| 28 | + }, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + testCases := map[string]struct { |
| 33 | + input *tfprotov5.MoveResourceStateRequest |
| 34 | + resourceSchema fwschema.Schema |
| 35 | + resource resource.Resource |
| 36 | + expected *fwserver.MoveResourceStateRequest |
| 37 | + expectedDiagnostics diag.Diagnostics |
| 38 | + }{ |
| 39 | + "nil": { |
| 40 | + input: nil, |
| 41 | + expected: nil, |
| 42 | + }, |
| 43 | + "SourcePrivate": { |
| 44 | + input: &tfprotov5.MoveResourceStateRequest{ |
| 45 | + SourcePrivate: privatestate.MustMarshalToJson(map[string][]byte{ |
| 46 | + ".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`), |
| 47 | + "providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`), |
| 48 | + }), |
| 49 | + }, |
| 50 | + resourceSchema: testFwSchema, |
| 51 | + expected: &fwserver.MoveResourceStateRequest{ |
| 52 | + SourcePrivate: &privatestate.Data{ |
| 53 | + Framework: map[string][]byte{ |
| 54 | + ".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`), |
| 55 | + }, |
| 56 | + Provider: privatestate.MustProviderData(context.Background(), privatestate.MustMarshalToJson(map[string][]byte{ |
| 57 | + "providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`), |
| 58 | + })), |
| 59 | + }, |
| 60 | + TargetResourceSchema: testFwSchema, |
| 61 | + }, |
| 62 | + }, |
| 63 | + "SourcePrivate-malformed-json": { |
| 64 | + input: &tfprotov5.MoveResourceStateRequest{ |
| 65 | + SourcePrivate: []byte(`{`), |
| 66 | + }, |
| 67 | + resourceSchema: testFwSchema, |
| 68 | + expected: &fwserver.MoveResourceStateRequest{ |
| 69 | + TargetResourceSchema: testFwSchema, |
| 70 | + }, |
| 71 | + expectedDiagnostics: diag.Diagnostics{ |
| 72 | + diag.NewErrorDiagnostic( |
| 73 | + "Error Decoding Private State", |
| 74 | + "An error was encountered when decoding private state: unexpected end of JSON input.\n\n"+ |
| 75 | + "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", |
| 76 | + ), |
| 77 | + }, |
| 78 | + }, |
| 79 | + "SourcePrivate-empty-json": { |
| 80 | + input: &tfprotov5.MoveResourceStateRequest{ |
| 81 | + SourcePrivate: []byte("{}"), |
| 82 | + }, |
| 83 | + resourceSchema: testFwSchema, |
| 84 | + expected: &fwserver.MoveResourceStateRequest{ |
| 85 | + SourcePrivate: &privatestate.Data{ |
| 86 | + Framework: map[string][]byte{}, |
| 87 | + Provider: privatestate.EmptyProviderData(context.Background()), |
| 88 | + }, |
| 89 | + TargetResourceSchema: testFwSchema, |
| 90 | + }, |
| 91 | + }, |
| 92 | + "SourceProviderAddress": { |
| 93 | + input: &tfprotov5.MoveResourceStateRequest{ |
| 94 | + SourceProviderAddress: "example.com/namespace/type", |
| 95 | + }, |
| 96 | + resourceSchema: testFwSchema, |
| 97 | + expected: &fwserver.MoveResourceStateRequest{ |
| 98 | + SourceProviderAddress: "example.com/namespace/type", |
| 99 | + TargetResourceSchema: testFwSchema, |
| 100 | + }, |
| 101 | + }, |
| 102 | + "SourceRawState": { |
| 103 | + input: &tfprotov5.MoveResourceStateRequest{ |
| 104 | + SourceState: testNewTfprotov5RawState(t, map[string]interface{}{ |
| 105 | + "test_attribute": "test-value", |
| 106 | + }), |
| 107 | + }, |
| 108 | + resourceSchema: testFwSchema, |
| 109 | + expected: &fwserver.MoveResourceStateRequest{ |
| 110 | + SourceRawState: testNewTfprotov6RawState(t, map[string]interface{}{ |
| 111 | + "test_attribute": "test-value", |
| 112 | + }), |
| 113 | + TargetResourceSchema: testFwSchema, |
| 114 | + }, |
| 115 | + }, |
| 116 | + "SourceSchemaVersion": { |
| 117 | + input: &tfprotov5.MoveResourceStateRequest{ |
| 118 | + SourceSchemaVersion: 123, |
| 119 | + }, |
| 120 | + resourceSchema: testFwSchema, |
| 121 | + expected: &fwserver.MoveResourceStateRequest{ |
| 122 | + SourceSchemaVersion: 123, |
| 123 | + TargetResourceSchema: testFwSchema, |
| 124 | + }, |
| 125 | + }, |
| 126 | + "SourceTypeName": { |
| 127 | + input: &tfprotov5.MoveResourceStateRequest{ |
| 128 | + SourceTypeName: "examplecloud_thing", |
| 129 | + }, |
| 130 | + resourceSchema: testFwSchema, |
| 131 | + expected: &fwserver.MoveResourceStateRequest{ |
| 132 | + SourceTypeName: "examplecloud_thing", |
| 133 | + TargetResourceSchema: testFwSchema, |
| 134 | + }, |
| 135 | + }, |
| 136 | + "TargetResourceSchema": { |
| 137 | + input: &tfprotov5.MoveResourceStateRequest{}, |
| 138 | + resourceSchema: testFwSchema, |
| 139 | + expected: &fwserver.MoveResourceStateRequest{ |
| 140 | + TargetResourceSchema: testFwSchema, |
| 141 | + }, |
| 142 | + }, |
| 143 | + "TargetResourceSchema-missing": { |
| 144 | + input: &tfprotov5.MoveResourceStateRequest{}, |
| 145 | + expected: nil, |
| 146 | + expectedDiagnostics: diag.Diagnostics{ |
| 147 | + diag.NewErrorDiagnostic( |
| 148 | + "Framework Implementation Error", |
| 149 | + "An unexpected issue was encountered when converting the MoveResourceState RPC request information from the protocol type to the framework type. "+ |
| 150 | + "The resource schema was missing. "+ |
| 151 | + "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.", |
| 152 | + ), |
| 153 | + }, |
| 154 | + }, |
| 155 | + "TargetTypeName": { |
| 156 | + input: &tfprotov5.MoveResourceStateRequest{ |
| 157 | + TargetTypeName: "examplecloud_thing", |
| 158 | + }, |
| 159 | + resourceSchema: testFwSchema, |
| 160 | + expected: &fwserver.MoveResourceStateRequest{ |
| 161 | + TargetResourceSchema: testFwSchema, |
| 162 | + TargetTypeName: "examplecloud_thing", |
| 163 | + }, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + for name, testCase := range testCases { |
| 168 | + name, testCase := name, testCase |
| 169 | + |
| 170 | + t.Run(name, func(t *testing.T) { |
| 171 | + t.Parallel() |
| 172 | + |
| 173 | + got, diags := fromproto5.MoveResourceStateRequest(context.Background(), testCase.input, testCase.resource, testCase.resourceSchema) |
| 174 | + |
| 175 | + if diff := cmp.Diff(got, testCase.expected); diff != "" { |
| 176 | + t.Errorf("unexpected difference: %s", diff) |
| 177 | + } |
| 178 | + |
| 179 | + if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" { |
| 180 | + t.Errorf("unexpected diagnostics difference: %s", diff) |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
0 commit comments