Skip to content

Commit 086a5c9

Browse files
committed
Add unit tests
1 parent b4c8974 commit 086a5c9

File tree

4 files changed

+122
-24
lines changed

4 files changed

+122
-24
lines changed

castai/reservations/mapping_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package reservations
22

33
import (
4+
"testing"
5+
"time"
6+
47
"github.com/castai/terraform-provider-castai/castai/sdk"
58
"github.com/samber/lo"
69
"github.com/stretchr/testify/require"
7-
"testing"
8-
"time"
910
)
1011

1112
func TestMapCsvRecordsToReservationResources(t *testing.T) {

castai/resource_role_bindings.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ func resourceRoleBindingsCreate(ctx context.Context, data *schema.ResourceData,
225225
}
226226
scope := convertScopeToSDK(data)
227227
scopes := convertScopesToSDK(data)
228-
scopes = append(scopes, scope)
229228

230229
resp, err := client.RbacServiceAPICreateRoleBindingsWithResponse(ctx, organizationID, sdk.RbacServiceAPICreateRoleBindingsJSONRequestBody{
231230
{
@@ -276,7 +275,6 @@ func resourceRoleBindingsUpdate(ctx context.Context, data *schema.ResourceData,
276275
}
277276
scope := convertScopeToSDK(data)
278277
scopes := convertScopesToSDK(data)
279-
scopes = append(scopes, scope)
280278

281279
resp, err := client.RbacServiceAPIUpdateRoleBindingWithResponse(ctx, organizationID, roleBindingID, sdk.RbacServiceAPIUpdateRoleBindingJSONRequestBody{
282280
Definition: sdk.CastaiRbacV1beta1RoleBindingDefinition{
@@ -352,25 +350,27 @@ func assignRoleBindingData(roleBinding *sdk.CastaiRbacV1beta1RoleBinding, data *
352350
return fmt.Errorf("setting role binding role id: %w", err)
353351
}
354352

355-
if roleBinding.Definition.Scope.Organization != nil {
356-
err := data.Set(FieldRoleBindingsScope, []any{
357-
map[string]any{
358-
FieldRoleBindingsScopeKind: RoleBindingScopeKindOrganization,
359-
FieldRoleBindingsScopeResourceID: roleBinding.Definition.Scope.Organization.Id,
360-
},
361-
})
362-
if err != nil {
363-
return fmt.Errorf("parsing scope organization: %w", err)
364-
}
365-
} else if roleBinding.Definition.Scope.Cluster != nil {
366-
err := data.Set(FieldRoleBindingsScope, []any{
367-
map[string]any{
368-
FieldRoleBindingsScopeKind: RoleBindingScopeKindCluster,
369-
FieldRoleBindingsScopeResourceID: roleBinding.Definition.Scope.Cluster.Id,
370-
},
371-
})
372-
if err != nil {
373-
return fmt.Errorf("parsing scope cluster: %w", err)
353+
if roleBinding.Definition.Scope != nil {
354+
if roleBinding.Definition.Scope.Organization != nil {
355+
err := data.Set(FieldRoleBindingsScope, []any{
356+
map[string]any{
357+
FieldRoleBindingsScopeKind: RoleBindingScopeKindOrganization,
358+
FieldRoleBindingsScopeResourceID: roleBinding.Definition.Scope.Organization.Id,
359+
},
360+
})
361+
if err != nil {
362+
return fmt.Errorf("parsing scope organization: %w", err)
363+
}
364+
} else if roleBinding.Definition.Scope.Cluster != nil {
365+
err := data.Set(FieldRoleBindingsScope, []any{
366+
map[string]any{
367+
FieldRoleBindingsScopeKind: RoleBindingScopeKindCluster,
368+
FieldRoleBindingsScopeResourceID: roleBinding.Definition.Scope.Cluster.Id,
369+
},
370+
})
371+
if err != nil {
372+
return fmt.Errorf("parsing scope cluster: %w", err)
373+
}
374374
}
375375
}
376376

castai/resource_role_bindings_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,103 @@ subjects.0.subject.2.kind = group
246246
subjects.0.subject.2.service_account_id =
247247
subjects.0.subject.2.user_id =
248248
Tainted = false
249+
`, data.State().String())
250+
})
251+
252+
t.Run("when RbacServiceAPI respond with 200 then populate the state with scopes", func(t *testing.T) {
253+
t.Parallel()
254+
255+
r := require.New(t)
256+
mockClient := mock_sdk.NewMockClientInterface(gomock.NewController(t))
257+
258+
ctx := context.Background()
259+
provider := &ProviderConfig{
260+
api: &sdk.ClientWithResponses{
261+
ClientInterface: mockClient,
262+
},
263+
}
264+
265+
organizationID := "4e4cd9eb-82eb-407e-a926-e5fef81cab50"
266+
roleBindingID := "a83b7bf2-5a99-45d9-bcac-b969386e751f"
267+
roleID := "4df39779-dfb2-48d3-91d8-7ee5bd2bca4b"
268+
userID := "671b2ebb-f361-42f0-aa2f-3049de93f8c1"
269+
serviceAccountID := "b11f5945-22ca-4101-a86e-d6e37f44a415"
270+
groupID := "844d2bf2-870d-42da-a81c-4e19befc78fc"
271+
272+
body := io.NopCloser(bytes.NewReader([]byte(`{
273+
"id": "` + roleBindingID + `",
274+
"organizationId": "` + organizationID + `",
275+
"name": "role-binding-name",
276+
"description": "role-binding-description",
277+
"definition": {
278+
"roleId": "` + roleID + `",
279+
"scopes": [
280+
{
281+
"organization": {
282+
"id": "` + organizationID + `"
283+
}
284+
}
285+
],
286+
"subjects": [
287+
{
288+
"user": {
289+
"id": "` + userID + `"
290+
}
291+
},
292+
{
293+
"serviceAccount": {
294+
"id": "` + serviceAccountID + `"
295+
}
296+
},
297+
{
298+
"group": {
299+
"id": "` + groupID + `"
300+
}
301+
}
302+
]
303+
}
304+
}`)))
305+
306+
mockClient.EXPECT().
307+
RbacServiceAPIGetRoleBinding(gomock.Any(), organizationID, roleBindingID).
308+
Return(&http.Response{StatusCode: 200, Body: body, Header: map[string][]string{"Content-Type": {"json"}}}, nil)
309+
310+
stateValue := cty.ObjectVal(map[string]cty.Value{
311+
"organization_id": cty.StringVal(organizationID),
312+
})
313+
state := terraform.NewInstanceStateShimmedFromValue(stateValue, 0)
314+
state.ID = roleBindingID
315+
316+
resource := resourceRoleBindings()
317+
data := resource.Data(state)
318+
319+
result := resource.ReadContext(ctx, data, provider)
320+
321+
r.Nil(result)
322+
r.False(result.HasError())
323+
r.Equal(`ID = `+roleBindingID+`
324+
description = role-binding-description
325+
name = role-binding-name
326+
organization_id = `+organizationID+`
327+
role_id = `+roleID+`
328+
scopes.# = 1
329+
scopes.0.kind = organization
330+
scopes.0.resource_id = `+organizationID+`
331+
subjects.# = 1
332+
subjects.0.subject.# = 3
333+
subjects.0.subject.0.group_id =
334+
subjects.0.subject.0.kind = user
335+
subjects.0.subject.0.service_account_id =
336+
subjects.0.subject.0.user_id = `+userID+`
337+
subjects.0.subject.1.group_id =
338+
subjects.0.subject.1.kind = service_account
339+
subjects.0.subject.1.service_account_id = `+serviceAccountID+`
340+
subjects.0.subject.1.user_id =
341+
subjects.0.subject.2.group_id = `+groupID+`
342+
subjects.0.subject.2.kind = group
343+
subjects.0.subject.2.service_account_id =
344+
subjects.0.subject.2.user_id =
345+
Tainted = false
249346
`, data.State().String())
250347
})
251348
}

docs/resources/role_bindings.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)