Skip to content

Commit

Permalink
Add enableProject test
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrique Santos committed Oct 11, 2023
1 parent d4c2c0c commit dc14043
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit dc14043

Please sign in to comment.