Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock out s3 operations for storage tests #19

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions cloud/aws/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,145 @@ func TestS3Upload(t *testing.T) {
})
}
}

func TestS3CreateBucket(t *testing.T) {
assert := assert.New(t)

cases := []tcase{
{
name: "create bucket failure",
args: args{
withAPIOptionsFunc: func(s *middleware.Stack) error {
return s.Finalize.Add(
middleware.FinalizeMiddlewareFunc(
"CreateBucketErrorMock",
func(ctx context.Context, fi middleware.FinalizeInput, fh middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) {
return middleware.FinalizeOutput{
Result: nil,
}, middleware.Metadata{}, fmt.Errorf("CreateBucketError")
},
),
middleware.Before,
)
},
},
want: fmt.Errorf("operation error S3: CreateBucket, CreateBucketError"),
wantErr: true,
},
{
name: "create bucket success",
args: args{
withAPIOptionsFunc: func(s *middleware.Stack) error {
return s.Finalize.Add(
middleware.FinalizeMiddlewareFunc(
"CreateBucketMock",
func(ctx context.Context, fi middleware.FinalizeInput, fh middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) {
return middleware.FinalizeOutput{
Result: &s3.CreateBucketOutput{},
}, middleware.Metadata{}, nil
},
),
middleware.Before,
)
},
},
want: nil,
wantErr: false,
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
awsCfg, err := awsConfig.LoadDefaultConfig(
context.TODO(),
awsConfig.WithRegion("us-west-1"),
awsConfig.WithAPIOptions([]func(*middleware.Stack) error{tt.args.withAPIOptionsFunc}),
)
if err != nil {
t.Fatal(err)
}

cfg := &config.Config{Bucket: "testbucket"}
s3Client := NewS3(cfg, awsCfg)
err = s3Client.CreateBucket(true)
if (err != nil) != tt.wantErr {
assert.Errorf(err, "error = %#v, wantErr %#v", err, tt.wantErr)
return
}
if tt.wantErr && err.Error() != tt.want.Error() {
assert.EqualError(err, tt.want.Error())
}
})
}
}

func TestS3Accessible(t *testing.T) {
assert := assert.New(t)

cases := []tcase{
{
name: "head bucket failure",
args: args{
withAPIOptionsFunc: func(s *middleware.Stack) error {
return s.Finalize.Add(
middleware.FinalizeMiddlewareFunc(
"HeadBucketErrorMock",
func(ctx context.Context, fi middleware.FinalizeInput, fh middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) {
return middleware.FinalizeOutput{
Result: nil,
}, middleware.Metadata{}, fmt.Errorf("HeadBucketError")
},
),
middleware.Before,
)
},
},
want: fmt.Errorf("operation error S3: HeadBucket, HeadBucketError"),
wantErr: true,
},
{
name: "head bucket success",
args: args{
withAPIOptionsFunc: func(s *middleware.Stack) error {
return s.Finalize.Add(
middleware.FinalizeMiddlewareFunc(
"HeadBucketMock",
func(ctx context.Context, fi middleware.FinalizeInput, fh middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) {
return middleware.FinalizeOutput{
Result: &s3.HeadBucketOutput{},
}, middleware.Metadata{}, nil
},
),
middleware.Before,
)
},
},
want: nil,
wantErr: false,
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
awsCfg, err := awsConfig.LoadDefaultConfig(
context.TODO(),
awsConfig.WithRegion("us-west-1"),
awsConfig.WithAPIOptions([]func(*middleware.Stack) error{tt.args.withAPIOptionsFunc}),
)
if err != nil {
t.Fatal(err)
}

cfg := &config.Config{Bucket: "testbucket"}
s3Client := NewS3(cfg, awsCfg)
err = s3Client.Accessible()
if (err != nil) != tt.wantErr {
assert.Errorf(err, "error = %#v, wantErr %#v", err, tt.wantErr)
return
}
if tt.wantErr && err.Error() != tt.want.Error() {
assert.EqualError(err, tt.want.Error())
}
})
}
}
Loading