|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package fromproto6_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
| 12 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 13 | + |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/internal/fromproto6" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/statestore" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/statestore/schema" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 20 | +) |
| 21 | + |
| 22 | +func TestConfigureStateStoreRequest(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + testProto6Type := tftypes.Object{ |
| 26 | + AttributeTypes: map[string]tftypes.Type{ |
| 27 | + "test_attribute": tftypes.String, |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + testProto6Value := tftypes.NewValue(testProto6Type, map[string]tftypes.Value{ |
| 32 | + "test_attribute": tftypes.NewValue(tftypes.String, "test-value"), |
| 33 | + }) |
| 34 | + |
| 35 | + testProto6DynamicValue, err := tfprotov6.NewDynamicValue(testProto6Type, testProto6Value) |
| 36 | + |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("unexpected error calling tfprotov6.NewDynamicValue(): %s", err) |
| 39 | + } |
| 40 | + |
| 41 | + testFwSchema := schema.Schema{ |
| 42 | + Attributes: map[string]schema.Attribute{ |
| 43 | + "test_attribute": schema.StringAttribute{ |
| 44 | + Required: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + testCases := map[string]struct { |
| 50 | + input *tfprotov6.ConfigureStateStoreRequest |
| 51 | + statestoreSchema fwschema.Schema |
| 52 | + expected *statestore.ConfigureStateStoreRequest |
| 53 | + expectedDiagnostics diag.Diagnostics |
| 54 | + }{ |
| 55 | + "nil": { |
| 56 | + input: nil, |
| 57 | + expected: nil, |
| 58 | + }, |
| 59 | + "empty": { |
| 60 | + input: &tfprotov6.ConfigureStateStoreRequest{}, |
| 61 | + expected: &statestore.ConfigureStateStoreRequest{}, |
| 62 | + }, |
| 63 | + "config-missing-schema": { |
| 64 | + input: &tfprotov6.ConfigureStateStoreRequest{ |
| 65 | + Config: &testProto6DynamicValue, |
| 66 | + }, |
| 67 | + expected: &statestore.ConfigureStateStoreRequest{}, |
| 68 | + expectedDiagnostics: diag.Diagnostics{ |
| 69 | + diag.NewErrorDiagnostic( |
| 70 | + "Unable to Convert Configuration", |
| 71 | + "An unexpected error was encountered when converting the configuration from the protocol type. "+ |
| 72 | + "This is always an issue in terraform-plugin-framework used to implement the statestore and should be reported to the statestore developers.\n\n"+ |
| 73 | + "Please report this to the statestore developer:\n\n"+ |
| 74 | + "Missing schema.", |
| 75 | + ), |
| 76 | + }, |
| 77 | + }, |
| 78 | + "config": { |
| 79 | + input: &tfprotov6.ConfigureStateStoreRequest{ |
| 80 | + Config: &testProto6DynamicValue, |
| 81 | + }, |
| 82 | + statestoreSchema: testFwSchema, |
| 83 | + expected: &statestore.ConfigureStateStoreRequest{ |
| 84 | + Config: tfsdk.Config{ |
| 85 | + Raw: testProto6Value, |
| 86 | + Schema: testFwSchema, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + "typename": { |
| 91 | + input: &tfprotov6.ConfigureStateStoreRequest{ |
| 92 | + TypeName: "foo", |
| 93 | + }, |
| 94 | + expected: &statestore.ConfigureStateStoreRequest{ |
| 95 | + TypeName: "foo", |
| 96 | + }, |
| 97 | + }, |
| 98 | + "client-capabilities": { |
| 99 | + input: &tfprotov6.ConfigureStateStoreRequest{ |
| 100 | + Capabilities: &tfprotov6.ConfigureStateStoreClientCapabilities{ |
| 101 | + DeferralAllowed: true, |
| 102 | + }, |
| 103 | + }, |
| 104 | + expected: &statestore.ConfigureStateStoreRequest{ |
| 105 | + Capabilities: statestore.ConfigureStateStoreClientCapabilities{ |
| 106 | + DeferralAllowed: true, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + "client-capabilities-unset": { |
| 111 | + input: &tfprotov6.ConfigureStateStoreRequest{}, |
| 112 | + expected: &statestore.ConfigureStateStoreRequest{ |
| 113 | + Capabilities: statestore.ConfigureStateStoreClientCapabilities{ |
| 114 | + DeferralAllowed: false, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + for name, testCase := range testCases { |
| 121 | + t.Run(name, func(t *testing.T) { |
| 122 | + t.Parallel() |
| 123 | + |
| 124 | + got, diags := fromproto6.ConfigureStateStoreRequest(context.Background(), testCase.input, testCase.statestoreSchema) |
| 125 | + |
| 126 | + if diff := cmp.Diff(got, testCase.expected); diff != "" { |
| 127 | + t.Errorf("unexpected difference: %s", diff) |
| 128 | + } |
| 129 | + |
| 130 | + if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" { |
| 131 | + t.Errorf("unexpected diagnostics difference: %s", diff) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments