|
| 1 | +package fwserver_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "slices" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/list" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 16 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 17 | +) |
| 18 | + |
| 19 | +func TestServerListResource(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + testSchema := schema.Schema{ |
| 23 | + Attributes: map[string]schema.Attribute{ |
| 24 | + "test_computed": schema.StringAttribute{ |
| 25 | + Computed: true, |
| 26 | + }, |
| 27 | + "test_required": schema.StringAttribute{ |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + testType := tftypes.Object{ |
| 34 | + AttributeTypes: map[string]tftypes.Type{ |
| 35 | + "test_attribute": tftypes.String, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + testResourceObjectValue1 := tftypes.NewValue(testType, map[string]tftypes.Value{ |
| 40 | + "test_attribute": tftypes.NewValue(tftypes.String, "test-value-1"), |
| 41 | + }) |
| 42 | + |
| 43 | + testResourceObjectValue2 := tftypes.NewValue(testType, map[string]tftypes.Value{ |
| 44 | + "test_attribute": tftypes.NewValue(tftypes.String, "test-value-2"), |
| 45 | + }) |
| 46 | + |
| 47 | + testIdentitySchema := identityschema.Schema{ |
| 48 | + Attributes: map[string]identityschema.Attribute{ |
| 49 | + "test_id": identityschema.StringAttribute{ |
| 50 | + RequiredForImport: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + testIdentityType := tftypes.Object{ |
| 56 | + AttributeTypes: map[string]tftypes.Type{ |
| 57 | + "test_id": tftypes.String, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + testIdentityValue1 := tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ |
| 62 | + "test_id": tftypes.NewValue(tftypes.String, "new-id-123"), |
| 63 | + }) |
| 64 | + |
| 65 | + testIdentityValue2 := tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ |
| 66 | + "test_id": tftypes.NewValue(tftypes.String, "new-id-456"), |
| 67 | + }) |
| 68 | + |
| 69 | + // nilIdentityValue := tftypes.NewValue(testIdentityType, nil) |
| 70 | + |
| 71 | + testCases := map[string]struct { |
| 72 | + server *fwserver.Server |
| 73 | + request *fwserver.ListResourceRequest |
| 74 | + expectedStreamEvents []fwserver.ListResourceEvent |
| 75 | + }{ |
| 76 | + "success-with-zero-results": { |
| 77 | + server: &fwserver.Server{ |
| 78 | + Provider: &testprovider.Provider{}, |
| 79 | + }, |
| 80 | + request: &fwserver.ListResourceRequest{ |
| 81 | + ListResource: &testprovider.ListResource{ |
| 82 | + ListResourceMethod: func(ctx context.Context, req list.ListResourceRequest, resp *list.ListResourceResponse) { // TODO |
| 83 | + resp.Results = slices.Values([]list.ListResourceEvent{}) |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + expectedStreamEvents: []fwserver.ListResourceEvent{}, |
| 88 | + }, |
| 89 | + "success-with-nil-results": { |
| 90 | + server: &fwserver.Server{ |
| 91 | + Provider: &testprovider.Provider{}, |
| 92 | + }, |
| 93 | + request: &fwserver.ListResourceRequest{ |
| 94 | + ListResource: &testprovider.ListResource{ |
| 95 | + ListResourceMethod: func(ctx context.Context, req list.ListResourceRequest, resp *list.ListResourceResponse) { // TODO |
| 96 | + // Do nothing, so that resp.Results is nil |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + expectedStreamEvents: []fwserver.ListResourceEvent{}, |
| 101 | + }, |
| 102 | + |
| 103 | + "success-with-multiple-results": { |
| 104 | + server: &fwserver.Server{ |
| 105 | + Provider: &testprovider.Provider{}, |
| 106 | + }, |
| 107 | + request: &fwserver.ListResourceRequest{ |
| 108 | + ListResource: &testprovider.ListResource{ |
| 109 | + ListResourceMethod: func(ctx context.Context, req list.ListResourceRequest, resp *list.ListResourceResponse) { // TODO |
| 110 | + resp.Results = slices.Values([]list.ListResourceEvent{ |
| 111 | + { |
| 112 | + Identity: &tfsdk.ResourceIdentity{ |
| 113 | + Schema: testIdentitySchema, |
| 114 | + Raw: testIdentityValue1, |
| 115 | + }, |
| 116 | + ResourceObject: &tfsdk.ResourceObject{ |
| 117 | + Schema: testSchema, |
| 118 | + Raw: testResourceObjectValue1, |
| 119 | + }, |
| 120 | + DisplayName: "Test Resource 1", |
| 121 | + Diagnostics: diag.Diagnostics{}, |
| 122 | + }, |
| 123 | + { |
| 124 | + Identity: &tfsdk.ResourceIdentity{ |
| 125 | + Schema: testIdentitySchema, |
| 126 | + Raw: testIdentityValue2, |
| 127 | + }, |
| 128 | + ResourceObject: &tfsdk.ResourceObject{ |
| 129 | + Schema: testSchema, |
| 130 | + Raw: testResourceObjectValue2, |
| 131 | + }, |
| 132 | + DisplayName: "Test Resource 2", |
| 133 | + Diagnostics: diag.Diagnostics{}, |
| 134 | + }, |
| 135 | + }) |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + expectedStreamEvents: []fwserver.ListResourceEvent{ |
| 140 | + { |
| 141 | + Identity: &tfsdk.ResourceIdentity{ |
| 142 | + Schema: testIdentitySchema, |
| 143 | + Raw: testIdentityValue1, |
| 144 | + }, |
| 145 | + ResourceObject: &tfsdk.ResourceObject{ |
| 146 | + Schema: testSchema, |
| 147 | + Raw: testResourceObjectValue1, |
| 148 | + }, |
| 149 | + DisplayName: "Test Resource 1", |
| 150 | + Diagnostics: diag.Diagnostics{}, |
| 151 | + }, |
| 152 | + { |
| 153 | + Identity: &tfsdk.ResourceIdentity{ |
| 154 | + Schema: testIdentitySchema, |
| 155 | + Raw: testIdentityValue2, |
| 156 | + }, |
| 157 | + ResourceObject: &tfsdk.ResourceObject{ |
| 158 | + Schema: testSchema, |
| 159 | + Raw: testResourceObjectValue2, |
| 160 | + }, |
| 161 | + DisplayName: "Test Resource 2", |
| 162 | + Diagnostics: diag.Diagnostics{}, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + for name, testCase := range testCases { |
| 169 | + t.Run(name, func(t *testing.T) { |
| 170 | + t.Parallel() |
| 171 | + |
| 172 | + response := &fwserver.ListResourceStream{} |
| 173 | + testCase.server.ListResource(context.Background(), testCase.request, response) |
| 174 | + |
| 175 | + events := slices.AppendSeq([]fwserver.ListResourceEvent{}, response.Results) |
| 176 | + if diff := cmp.Diff(events, testCase.expectedStreamEvents); diff != "" { |
| 177 | + t.Errorf("unexpected difference: %s", diff) |
| 178 | + } |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
0 commit comments