|
| 1 | +package castai |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/golang/mock/gomock" |
| 11 | + "github.com/hashicorp/go-cty/cty" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/castai/terraform-provider-castai/castai/sdk" |
| 16 | + mock_sdk "github.com/castai/terraform-provider-castai/castai/sdk/mock" |
| 17 | +) |
| 18 | + |
| 19 | +func TestImpersonationServiceAccountDataSourceRead(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + responseBody string |
| 25 | + statusCode int |
| 26 | + expectedID string |
| 27 | + expectedError bool |
| 28 | + errorContains string |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "successful response with id and email", |
| 32 | + responseBody: `{ |
| 33 | + "id": "test-service-account-id", |
| 34 | + "email": "test@example.com" |
| 35 | +}`, |
| 36 | + statusCode: 200, |
| 37 | + expectedID: "test-service-account-id", |
| 38 | + expectedError: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "successful response with id only", |
| 42 | + responseBody: `{ |
| 43 | + "id": "another-service-account-id" |
| 44 | +}`, |
| 45 | + statusCode: 200, |
| 46 | + expectedID: "another-service-account-id", |
| 47 | + expectedError: false, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "api error response", |
| 51 | + responseBody: `{"message": "Internal server error"}`, |
| 52 | + statusCode: 500, |
| 53 | + expectedError: true, |
| 54 | + errorContains: "retrieving impersonation service account", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "unauthorized response", |
| 58 | + responseBody: `{"message": "Unauthorized"}`, |
| 59 | + statusCode: 401, |
| 60 | + expectedError: true, |
| 61 | + errorContains: "retrieving impersonation service account", |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + r := require.New(t) |
| 68 | + mockClient := mock_sdk.NewMockClientInterface(gomock.NewController(t)) |
| 69 | + |
| 70 | + ctx := context.Background() |
| 71 | + provider := &ProviderConfig{ |
| 72 | + api: &sdk.ClientWithResponses{ |
| 73 | + ClientInterface: mockClient, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + body := io.NopCloser(bytes.NewReader([]byte(tt.responseBody))) |
| 78 | + |
| 79 | + mockClient.EXPECT(). |
| 80 | + ExternalClusterAPIImpersonationServiceAccount(gomock.Any(), gomock.Any(), gomock.Any()). |
| 81 | + Return(&http.Response{ |
| 82 | + StatusCode: tt.statusCode, |
| 83 | + Body: body, |
| 84 | + Header: map[string][]string{"Content-Type": {"json"}}, |
| 85 | + }, nil) |
| 86 | + |
| 87 | + state := terraform.NewInstanceStateShimmedFromValue(cty.ObjectVal(map[string]cty.Value{}), 0) |
| 88 | + |
| 89 | + resource := dataSourceImpersonationServiceAccount() |
| 90 | + data := resource.Data(state) |
| 91 | + |
| 92 | + result := resource.ReadContext(ctx, data, provider) |
| 93 | + |
| 94 | + if tt.expectedError { |
| 95 | + r.True(result.HasError()) |
| 96 | + if tt.errorContains != "" { |
| 97 | + r.Contains(result[0].Summary, tt.errorContains) |
| 98 | + } |
| 99 | + } else { |
| 100 | + r.Nil(result) |
| 101 | + r.False(result.HasError()) |
| 102 | + r.Equal(tt.expectedID, data.Id()) |
| 103 | + r.Equal(tt.expectedID, data.Get(FieldImpersonationServiceAccountId)) |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
0 commit comments