Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
init tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag4DSB committed Oct 21, 2024
1 parent 4885e9e commit b368d9e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/util/s3client/s3client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"k8s.io/klog/v2"
)

type S3API interface {
CreateBucket(ctx context.Context, input *s3.CreateBucketInput, opts ...func(*s3.Options)) (*s3.CreateBucketOutput, error)
}

const (
defaultRegion = "us-east-1"
requestTimeout = 15 * time.Second
Expand All @@ -35,7 +39,7 @@ type S3Params struct {
}

type S3Client struct {
S3Service *s3.Client
S3Service S3API
}

func InitS3Client(params S3Params) (*S3Client, error) {
Expand Down
80 changes: 80 additions & 0 deletions pkg/util/s3client/s3client_test.go
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())
})
})
})

0 comments on commit b368d9e

Please sign in to comment.