This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
forked from kubernetes-sigs/cosi-driver-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4885e9e
commit b368d9e
Showing
2 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package s3client_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/scality/cosi/pkg/util/s3client" // Adjust the import path according to your module name | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
// MockS3Client implements the S3API interface for testing | ||
type MockS3Client struct { | ||
CreateBucketFunc func(ctx context.Context, input *s3.CreateBucketInput, opts ...func(*s3.Options)) (*s3.CreateBucketOutput, error) | ||
} | ||
|
||
func (m *MockS3Client) CreateBucket(ctx context.Context, input *s3.CreateBucketInput, opts ...func(*s3.Options)) (*s3.CreateBucketOutput, error) { | ||
if m.CreateBucketFunc != nil { | ||
return m.CreateBucketFunc(ctx, input, opts...) | ||
} | ||
return &s3.CreateBucketOutput{}, nil | ||
} | ||
|
||
func TestS3Client(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "S3Client Suite") | ||
} | ||
|
||
var _ = Describe("S3Client", func() { | ||
|
||
var params s3client.S3Params | ||
|
||
BeforeEach(func() { | ||
// Define default test S3Params | ||
params = s3client.S3Params{ | ||
AccessKey: "test-access-key", | ||
SecretKey: "test-secret-key", | ||
Endpoint: "https://s3.mock.endpoint", | ||
Region: "us-west-2", | ||
TLSCert: nil, | ||
Debug: false, | ||
} | ||
}) | ||
|
||
Describe("InitS3Client", func() { | ||
It("should initialize the S3 client without error", func() { | ||
client, err := s3client.InitS3Client(params) | ||
Expect(err).To(BeNil()) | ||
Expect(client).NotTo(BeNil()) | ||
}) | ||
|
||
It("should use the default region when none is provided", func() { | ||
params.Region = "" | ||
client, err := s3client.InitS3Client(params) | ||
Expect(err).To(BeNil()) | ||
Expect(client).NotTo(BeNil()) | ||
}) | ||
}) | ||
|
||
Describe("CreateBucket", func() { | ||
It("should return no error if the bucket already exists", func() { | ||
client, err := s3client.InitS3Client(params) | ||
Expect(err).To(BeNil()) | ||
|
||
mockS3 := &MockS3Client{ | ||
CreateBucketFunc: func(ctx context.Context, input *s3.CreateBucketInput, opts ...func(*s3.Options)) (*s3.CreateBucketOutput, error) { | ||
return nil, fmt.Errorf("BucketAlreadyExists: The requested bucket name is not available") | ||
}, | ||
} | ||
|
||
client.S3Service = mockS3 | ||
|
||
err = client.CreateBucket("existing-bucket", params) | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
}) |