From dc14043b1ca220c052a68f3b30d0c2e29b25c2f3 Mon Sep 17 00:00:00 2001 From: Henrique Santos Date: Wed, 11 Oct 2023 17:25:49 +0100 Subject: [PATCH] Add enableProject test --- .../credentialsgroup/resource.go | 22 ++--- .../credentialsgroup/resource_test.go | 92 +++++++++++++++++-- 2 files changed, 96 insertions(+), 18 deletions(-) diff --git a/stackit/internal/services/objectstorage/credentialsgroup/resource.go b/stackit/internal/services/objectstorage/credentialsgroup/resource.go index 6425447e..7fdb21f3 100644 --- a/stackit/internal/services/objectstorage/credentialsgroup/resource.go +++ b/stackit/internal/services/objectstorage/credentialsgroup/resource.go @@ -28,11 +28,6 @@ var ( _ resource.ResourceWithImportState = &credentialsGroupResource{} ) -// needed for testing -type objectStorageClient interface { - GetCredentialsGroupsExecute(ctx context.Context, projectId string) (*objectstorage.GetCredentialsGroupsResponse, error) -} - type Model struct { Id types.String `tfsdk:"id"` // needed by TF CredentialsGroupId types.String `tfsdk:"credentials_group_id"` @@ -162,8 +157,8 @@ func (r *credentialsGroupResource) Create(ctx context.Context, req resource.Crea DisplayName: utils.Ptr(credentialsGroupName), } - // handle project init - err := r.enableProject(ctx, &model) + // Handle project init + err := enableProject(ctx, &model, r.client) if resp.Diagnostics.HasError() { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credentials group", fmt.Sprintf("Enabling object storage project before creation: %v", err)) return @@ -292,12 +287,17 @@ func mapCredentialsGroup(credentialsGroup objectstorage.CredentialsGroup, model ) } -// enableProject enables object storage for the specified project. If the project already exists, nothing happens -func (r *credentialsGroupResource) enableProject(ctx context.Context, model *Model) error { +type objectStorageClient interface { + CreateProjectExecute(ctx context.Context, projectId string) (*objectstorage.GetProjectResponse, error) + GetCredentialsGroupsExecute(ctx context.Context, projectId string) (*objectstorage.GetCredentialsGroupsResponse, error) +} + +// enableProject enables object storage for the specified project. If the project is already enabled, nothing happens +func enableProject(ctx context.Context, model *Model, client objectStorageClient) error { projectId := model.ProjectId.ValueString() - // From the object storage OAS: Creation will also be successful if the project already exists, but will not create a duplicate - _, err := r.client.CreateProject(ctx, projectId).Execute() + // From the object storage OAS: Creation will also be successful if the project is already enabled, but will not create a duplicate + _, err := client.CreateProjectExecute(ctx, projectId) if err != nil { return fmt.Errorf("failed to create object storage project: %w", err) } diff --git a/stackit/internal/services/objectstorage/credentialsgroup/resource_test.go b/stackit/internal/services/objectstorage/credentialsgroup/resource_test.go index a653e564..ac4b48db 100644 --- a/stackit/internal/services/objectstorage/credentialsgroup/resource_test.go +++ b/stackit/internal/services/objectstorage/credentialsgroup/resource_test.go @@ -12,16 +12,25 @@ import ( ) type objectStorageClientMocked struct { - getCredentialsGroupsFails bool - getCredentialsGroupsResponse *objectstorage.GetCredentialsGroupsResponse + returnError bool + createProjectExecuteResp *objectstorage.GetProjectResponse + getCredentialsGroupsResp *objectstorage.GetCredentialsGroupsResponse +} + +func (c *objectStorageClientMocked) CreateProjectExecute(_ context.Context, _ string) (*objectstorage.GetProjectResponse, error) { + if c.returnError { + return nil, fmt.Errorf("get credentials groups failed") + } + + return c.createProjectExecuteResp, nil } func (c *objectStorageClientMocked) GetCredentialsGroupsExecute(_ context.Context, _ string) (*objectstorage.GetCredentialsGroupsResponse, error) { - if c.getCredentialsGroupsFails { - return c.getCredentialsGroupsResponse, fmt.Errorf("get credentials groups failed") + if c.returnError { + return nil, fmt.Errorf("get credentials groups failed") } - return c.getCredentialsGroupsResponse, nil + return c.getCredentialsGroupsResp, nil } func TestMapFields(t *testing.T) { @@ -115,6 +124,75 @@ func TestMapFields(t *testing.T) { } } +func TestEnableProject(t *testing.T) { + tests := []struct { + description string + mockedResp *objectstorage.GetProjectResponse + expected Model + enableFails bool + isValid bool + }{ + { + "default_values", + &objectstorage.GetProjectResponse{}, + Model{ + Id: types.StringValue("pid,cid"), + Name: types.StringNull(), + ProjectId: types.StringValue("pid"), + CredentialsGroupId: types.StringValue("cid"), + URN: types.StringNull(), + }, + false, + true, + }, + { + "nil_response", + nil, + Model{ + Id: types.StringValue("pid,cid"), + Name: types.StringNull(), + ProjectId: types.StringValue("pid"), + CredentialsGroupId: types.StringValue("cid"), + URN: types.StringNull(), + }, + false, + true, + }, + { + "error_response", + &objectstorage.GetProjectResponse{}, + Model{ + Id: types.StringValue("pid,cid"), + Name: types.StringNull(), + ProjectId: types.StringValue("pid"), + CredentialsGroupId: types.StringValue("cid"), + URN: types.StringNull(), + }, + true, + false, + }, + } + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + client := &objectStorageClientMocked{ + returnError: tt.enableFails, + createProjectExecuteResp: tt.mockedResp, + } + model := &Model{ + ProjectId: tt.expected.ProjectId, + CredentialsGroupId: tt.expected.CredentialsGroupId, + } + err := enableProject(context.Background(), model, client) + if !tt.isValid && err == nil { + t.Fatalf("Should have failed") + } + if tt.isValid && err != nil { + t.Fatalf("Should not have failed: %v", err) + } + }) + } +} + func TestReadCredentialsGroups(t *testing.T) { tests := []struct { description string @@ -222,8 +300,8 @@ func TestReadCredentialsGroups(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { client := &objectStorageClientMocked{ - getCredentialsGroupsFails: tt.getCredentialsGroupsFails, - getCredentialsGroupsResponse: tt.mockedResp, + returnError: tt.getCredentialsGroupsFails, + getCredentialsGroupsResp: tt.mockedResp, } model := &Model{ ProjectId: tt.expected.ProjectId,