|
| 1 | +package fromproto6_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/internal/fromproto6" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
| 13 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 14 | +) |
| 15 | + |
| 16 | +func TestConfig(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + testProto6Type := tftypes.Object{ |
| 20 | + AttributeTypes: map[string]tftypes.Type{ |
| 21 | + "test_attribute": tftypes.String, |
| 22 | + }, |
| 23 | + } |
| 24 | + |
| 25 | + testProto6Value := tftypes.NewValue(testProto6Type, map[string]tftypes.Value{ |
| 26 | + "test_attribute": tftypes.NewValue(tftypes.String, "test-value"), |
| 27 | + }) |
| 28 | + |
| 29 | + testProto6DynamicValue, err := tfprotov6.NewDynamicValue(testProto6Type, testProto6Value) |
| 30 | + |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("unexpected error calling tfprotov6.NewDynamicValue(): %s", err) |
| 33 | + } |
| 34 | + |
| 35 | + testFwSchema := &tfsdk.Schema{ |
| 36 | + Attributes: map[string]tfsdk.Attribute{ |
| 37 | + "test_attribute": { |
| 38 | + Required: true, |
| 39 | + Type: types.StringType, |
| 40 | + }, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + testFwSchemaInvalid := &tfsdk.Schema{ |
| 45 | + Attributes: map[string]tfsdk.Attribute{ |
| 46 | + "test_attribute": { |
| 47 | + Required: true, |
| 48 | + Type: types.BoolType, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + testCases := map[string]struct { |
| 54 | + input *tfprotov6.DynamicValue |
| 55 | + schema *tfsdk.Schema |
| 56 | + expected *tfsdk.Config |
| 57 | + expectedDiagnostics diag.Diagnostics |
| 58 | + }{ |
| 59 | + "nil": { |
| 60 | + input: nil, |
| 61 | + expected: nil, |
| 62 | + }, |
| 63 | + "missing-schema": { |
| 64 | + input: &testProto6DynamicValue, |
| 65 | + expected: nil, |
| 66 | + expectedDiagnostics: diag.Diagnostics{ |
| 67 | + diag.NewErrorDiagnostic( |
| 68 | + "Unable to Convert Configuration", |
| 69 | + "An unexpected error was encountered when converting the configuration from the protocol type. "+ |
| 70 | + "This is always an issue in the Terraform Provider SDK used to implement the provider and should be reported to the provider developers.\n\n"+ |
| 71 | + "Please report this to the provider developer:\n\n"+ |
| 72 | + "Missing schema.", |
| 73 | + ), |
| 74 | + }, |
| 75 | + }, |
| 76 | + "invalid-schema": { |
| 77 | + input: &testProto6DynamicValue, |
| 78 | + schema: testFwSchemaInvalid, |
| 79 | + expected: nil, |
| 80 | + expectedDiagnostics: diag.Diagnostics{ |
| 81 | + diag.NewErrorDiagnostic( |
| 82 | + "Unable to Convert Configuration", |
| 83 | + "An unexpected error was encountered when converting the configuration from the protocol type. "+ |
| 84 | + "This is always an issue in the Terraform Provider SDK used to implement the provider and should be reported to the provider developers.\n\n"+ |
| 85 | + "Please report this to the provider developer:\n\n"+ |
| 86 | + "AttributeName(\"test_attribute\"): couldn't decode bool: msgpack: invalid code=aa decoding bool", |
| 87 | + ), |
| 88 | + }, |
| 89 | + }, |
| 90 | + "valid": { |
| 91 | + input: &testProto6DynamicValue, |
| 92 | + schema: testFwSchema, |
| 93 | + expected: &tfsdk.Config{ |
| 94 | + Raw: testProto6Value, |
| 95 | + Schema: *testFwSchema, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for name, testCase := range testCases { |
| 101 | + name, testCase := name, testCase |
| 102 | + |
| 103 | + t.Run(name, func(t *testing.T) { |
| 104 | + t.Parallel() |
| 105 | + |
| 106 | + got, diags := fromproto6.Config(context.Background(), testCase.input, testCase.schema) |
| 107 | + |
| 108 | + if diff := cmp.Diff(got, testCase.expected); diff != "" { |
| 109 | + t.Errorf("unexpected difference: %s", diff) |
| 110 | + } |
| 111 | + |
| 112 | + if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" { |
| 113 | + t.Errorf("unexpected diagnostics difference: %s", diff) |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
0 commit comments