|
3 | 3 |
|
4 | 4 | package fromproto5_test |
5 | 5 |
|
6 | | -// TODO:Actions: Add unit tests once this mapping logic is complete |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5" |
| 12 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 13 | + |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/action" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/action/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/internal/fromproto5" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" |
| 20 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 21 | +) |
| 22 | + |
| 23 | +func TestInvokeActionRequest(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + testProto5Type := tftypes.Object{ |
| 27 | + AttributeTypes: map[string]tftypes.Type{ |
| 28 | + "test_attribute": tftypes.String, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + testProto5Value := tftypes.NewValue(testProto5Type, map[string]tftypes.Value{ |
| 33 | + "test_attribute": tftypes.NewValue(tftypes.String, "test-value"), |
| 34 | + }) |
| 35 | + |
| 36 | + testProto5DynamicValue, err := tfprotov5.NewDynamicValue(testProto5Type, testProto5Value) |
| 37 | + |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("unexpected error calling tfprotov5.NewDynamicValue(): %s", err) |
| 40 | + } |
| 41 | + |
| 42 | + testUnlinkedSchema := schema.UnlinkedSchema{ |
| 43 | + Attributes: map[string]schema.Attribute{ |
| 44 | + "test_attribute": schema.StringAttribute{ |
| 45 | + Required: true, |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + testCases := map[string]struct { |
| 51 | + input *tfprotov5.InvokeActionRequest |
| 52 | + actionSchema fwschema.Schema |
| 53 | + actionImpl action.Action |
| 54 | + providerMetaSchema fwschema.Schema |
| 55 | + expected *fwserver.InvokeActionRequest |
| 56 | + expectedDiagnostics diag.Diagnostics |
| 57 | + }{ |
| 58 | + "nil": { |
| 59 | + input: nil, |
| 60 | + expected: nil, |
| 61 | + }, |
| 62 | + "empty": { |
| 63 | + input: &tfprotov5.InvokeActionRequest{}, |
| 64 | + expected: nil, |
| 65 | + expectedDiagnostics: diag.Diagnostics{ |
| 66 | + diag.NewErrorDiagnostic( |
| 67 | + "Missing Action Schema", |
| 68 | + "An unexpected error was encountered when handling the request. "+ |
| 69 | + "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+ |
| 70 | + "Please report this to the provider developer:\n\n"+ |
| 71 | + "Missing schema.", |
| 72 | + ), |
| 73 | + }, |
| 74 | + }, |
| 75 | + "config-missing-schema": { |
| 76 | + input: &tfprotov5.InvokeActionRequest{ |
| 77 | + Config: &testProto5DynamicValue, |
| 78 | + }, |
| 79 | + expected: nil, |
| 80 | + expectedDiagnostics: diag.Diagnostics{ |
| 81 | + diag.NewErrorDiagnostic( |
| 82 | + "Missing Action Schema", |
| 83 | + "An unexpected error was encountered when handling the request. "+ |
| 84 | + "This is always an issue in terraform-plugin-framework 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 | + "Missing schema.", |
| 87 | + ), |
| 88 | + }, |
| 89 | + }, |
| 90 | + "config": { |
| 91 | + input: &tfprotov5.InvokeActionRequest{ |
| 92 | + Config: &testProto5DynamicValue, |
| 93 | + }, |
| 94 | + actionSchema: testUnlinkedSchema, |
| 95 | + expected: &fwserver.InvokeActionRequest{ |
| 96 | + Config: &tfsdk.Config{ |
| 97 | + Raw: testProto5Value, |
| 98 | + Schema: testUnlinkedSchema, |
| 99 | + }, |
| 100 | + ActionSchema: testUnlinkedSchema, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for name, testCase := range testCases { |
| 106 | + t.Run(name, func(t *testing.T) { |
| 107 | + t.Parallel() |
| 108 | + |
| 109 | + got, diags := fromproto5.InvokeActionRequest(context.Background(), testCase.input, testCase.actionImpl, testCase.actionSchema) |
| 110 | + |
| 111 | + if diff := cmp.Diff(got, testCase.expected); diff != "" { |
| 112 | + t.Errorf("unexpected difference: %s", diff) |
| 113 | + } |
| 114 | + |
| 115 | + if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" { |
| 116 | + t.Errorf("unexpected diagnostics difference: %s", diff) |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments