|
3 | 3 |
|
4 | 4 | package fwserver_test |
5 | 5 |
|
6 | | -// TODO:Actions: Add unit tests once InvokeAction is implemented |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/action" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/action/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/provider" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 20 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 21 | +) |
| 22 | + |
| 23 | +func TestServerInvokeAction(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + testType := tftypes.Object{ |
| 27 | + AttributeTypes: map[string]tftypes.Type{ |
| 28 | + "test_required": tftypes.String, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + testConfigValue := tftypes.NewValue(testType, map[string]tftypes.Value{ |
| 33 | + "test_required": tftypes.NewValue(tftypes.String, "test-config-value"), |
| 34 | + }) |
| 35 | + |
| 36 | + testUnlinkedSchema := schema.UnlinkedSchema{ |
| 37 | + Attributes: map[string]schema.Attribute{ |
| 38 | + "test_required": schema.StringAttribute{ |
| 39 | + Required: true, |
| 40 | + }, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + testUnlinkedConfig := &tfsdk.Config{ |
| 45 | + Raw: testConfigValue, |
| 46 | + Schema: testUnlinkedSchema, |
| 47 | + } |
| 48 | + |
| 49 | + testCases := map[string]struct { |
| 50 | + server *fwserver.Server |
| 51 | + request *fwserver.InvokeActionRequest |
| 52 | + expectedResponse *fwserver.InvokeActionResponse |
| 53 | + configureProviderReq *provider.ConfigureRequest |
| 54 | + }{ |
| 55 | + "nil": { |
| 56 | + server: &fwserver.Server{ |
| 57 | + Provider: &testprovider.Provider{}, |
| 58 | + }, |
| 59 | + expectedResponse: &fwserver.InvokeActionResponse{}, |
| 60 | + }, |
| 61 | + "unlinked-nil-config": { |
| 62 | + server: &fwserver.Server{ |
| 63 | + Provider: &testprovider.Provider{}, |
| 64 | + }, |
| 65 | + request: &fwserver.InvokeActionRequest{ |
| 66 | + ActionSchema: testUnlinkedSchema, |
| 67 | + Action: &testprovider.Action{ |
| 68 | + InvokeMethod: func(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { |
| 69 | + if !req.Config.Raw.IsNull() { |
| 70 | + resp.Diagnostics.AddError("Unexpected Config in action Invoke", "Expected Config to be null") |
| 71 | + } |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + expectedResponse: &fwserver.InvokeActionResponse{}, |
| 76 | + }, |
| 77 | + "request-config": { |
| 78 | + server: &fwserver.Server{ |
| 79 | + Provider: &testprovider.Provider{}, |
| 80 | + }, |
| 81 | + request: &fwserver.InvokeActionRequest{ |
| 82 | + Config: testUnlinkedConfig, |
| 83 | + ActionSchema: testUnlinkedSchema, |
| 84 | + Action: &testprovider.Action{ |
| 85 | + InvokeMethod: func(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { |
| 86 | + var config struct { |
| 87 | + TestRequired types.String `tfsdk:"test_required"` |
| 88 | + } |
| 89 | + |
| 90 | + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) |
| 91 | + |
| 92 | + if config.TestRequired.ValueString() != "test-config-value" { |
| 93 | + resp.Diagnostics.AddError("unexpected req.Config value: %s", config.TestRequired.ValueString()) |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + expectedResponse: &fwserver.InvokeActionResponse{}, |
| 99 | + }, |
| 100 | + "action-configure-data": { |
| 101 | + server: &fwserver.Server{ |
| 102 | + ActionConfigureData: "test-provider-configure-value", |
| 103 | + Provider: &testprovider.Provider{}, |
| 104 | + }, |
| 105 | + request: &fwserver.InvokeActionRequest{ |
| 106 | + Config: testUnlinkedConfig, |
| 107 | + ActionSchema: testUnlinkedSchema, |
| 108 | + Action: &testprovider.ActionWithConfigure{ |
| 109 | + ConfigureMethod: func(ctx context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) { |
| 110 | + providerData, ok := req.ProviderData.(string) |
| 111 | + |
| 112 | + if !ok { |
| 113 | + resp.Diagnostics.AddError( |
| 114 | + "Unexpected ConfigureRequest.ProviderData", |
| 115 | + fmt.Sprintf("Expected string, got: %T", req.ProviderData), |
| 116 | + ) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + if providerData != "test-provider-configure-value" { |
| 121 | + resp.Diagnostics.AddError( |
| 122 | + "Unexpected ConfigureRequest.ProviderData", |
| 123 | + fmt.Sprintf("Expected test-provider-configure-value, got: %q", providerData), |
| 124 | + ) |
| 125 | + } |
| 126 | + }, |
| 127 | + Action: &testprovider.Action{ |
| 128 | + InvokeMethod: func(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { |
| 129 | + // In practice, the Configure method would save the |
| 130 | + // provider data to the Action implementation and |
| 131 | + // use it here. The fact that Configure is able to |
| 132 | + // read the data proves this can work. |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + expectedResponse: &fwserver.InvokeActionResponse{}, |
| 138 | + }, |
| 139 | + "response-diagnostics": { |
| 140 | + server: &fwserver.Server{ |
| 141 | + Provider: &testprovider.Provider{}, |
| 142 | + }, |
| 143 | + request: &fwserver.InvokeActionRequest{ |
| 144 | + Config: testUnlinkedConfig, |
| 145 | + ActionSchema: testUnlinkedSchema, |
| 146 | + Action: &testprovider.Action{ |
| 147 | + InvokeMethod: func(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { |
| 148 | + resp.Diagnostics.AddWarning("warning summary", "warning detail") |
| 149 | + resp.Diagnostics.AddError("error summary", "error detail") |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + expectedResponse: &fwserver.InvokeActionResponse{ |
| 154 | + Diagnostics: diag.Diagnostics{ |
| 155 | + diag.NewWarningDiagnostic( |
| 156 | + "warning summary", |
| 157 | + "warning detail", |
| 158 | + ), |
| 159 | + diag.NewErrorDiagnostic( |
| 160 | + "error summary", |
| 161 | + "error detail", |
| 162 | + ), |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + for name, testCase := range testCases { |
| 169 | + t.Run(name, func(t *testing.T) { |
| 170 | + t.Parallel() |
| 171 | + |
| 172 | + if testCase.configureProviderReq != nil { |
| 173 | + configureProviderResp := &provider.ConfigureResponse{} |
| 174 | + testCase.server.ConfigureProvider(context.Background(), testCase.configureProviderReq, configureProviderResp) |
| 175 | + } |
| 176 | + |
| 177 | + response := &fwserver.InvokeActionResponse{} |
| 178 | + testCase.server.InvokeAction(context.Background(), testCase.request, response) |
| 179 | + |
| 180 | + if diff := cmp.Diff(response, testCase.expectedResponse); diff != "" { |
| 181 | + t.Errorf("unexpected difference: %s", diff) |
| 182 | + } |
| 183 | + }) |
| 184 | + } |
| 185 | +} |
0 commit comments