terraform-provider-stackitp.../stackit/internal/services/objectstorage/bucket/resource_test.go
Rüdiger Schmitz 2923621ab0
Feat/stackittpr 20 region adjustments | tfp (migrate first service to new regions concept) (#664)
* feat: completed bucket and credential group

* feat: fix linter warnings

* feat: updated documentation

* feat: updated to current version of the regional api

* feat: implement review findings

* feat: implement further review findings

* fix: make sure region is stored for the data-source in the state
2025-02-10 14:28:33 +01:00

154 lines
3.6 KiB
Go

package objectstorage
import (
"context"
_ "embed"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)
type objectStorageClientMocked struct {
returnError bool
}
func (c *objectStorageClientMocked) EnableServiceExecute(_ context.Context, projectId, _ string) (*objectstorage.ProjectStatus, error) {
if c.returnError {
return nil, fmt.Errorf("create project failed")
}
return &objectstorage.ProjectStatus{
Project: utils.Ptr(projectId),
}, nil
}
func TestMapFields(t *testing.T) {
tests := []struct {
description string
input *objectstorage.GetBucketResponse
expected Model
isValid bool
}{
{
"default_values",
&objectstorage.GetBucketResponse{
Bucket: &objectstorage.Bucket{},
},
Model{
Id: types.StringValue("pid,bname"),
Name: types.StringValue("bname"),
ProjectId: types.StringValue("pid"),
URLPathStyle: types.StringNull(),
URLVirtualHostedStyle: types.StringNull(),
Region: types.StringValue("eu01"),
},
true,
},
{
"simple_values",
&objectstorage.GetBucketResponse{
Bucket: &objectstorage.Bucket{
UrlPathStyle: utils.Ptr("url/path/style"),
UrlVirtualHostedStyle: utils.Ptr("url/virtual/hosted/style"),
},
},
Model{
Id: types.StringValue("pid,bname"),
Name: types.StringValue("bname"),
ProjectId: types.StringValue("pid"),
URLPathStyle: types.StringValue("url/path/style"),
URLVirtualHostedStyle: types.StringValue("url/virtual/hosted/style"),
Region: types.StringValue("eu01"),
},
true,
},
{
"empty_strings",
&objectstorage.GetBucketResponse{
Bucket: &objectstorage.Bucket{
UrlPathStyle: utils.Ptr(""),
UrlVirtualHostedStyle: utils.Ptr(""),
},
},
Model{
Id: types.StringValue("pid,bname"),
Name: types.StringValue("bname"),
ProjectId: types.StringValue("pid"),
URLPathStyle: types.StringValue(""),
URLVirtualHostedStyle: types.StringValue(""),
Region: types.StringValue("eu01"),
},
true,
},
{
"nil_response",
nil,
Model{},
false,
},
{
"no_bucket",
&objectstorage.GetBucketResponse{},
Model{},
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
model := &Model{
ProjectId: tt.expected.ProjectId,
Name: tt.expected.Name,
}
err := mapFields(tt.input, model, "eu01")
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(model, &tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}
func TestEnableProject(t *testing.T) {
tests := []struct {
description string
enableFails bool
isValid bool
}{
{
"default_values",
false,
true,
},
{
"error_response",
true,
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
client := &objectStorageClientMocked{
returnError: tt.enableFails,
}
err := enableProject(context.Background(), &Model{}, "eu01", client)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
})
}
}