Skip to content

Commit

Permalink
COSI-73: update errors with aws errors in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag4DSB committed Jan 2, 2025
1 parent beeac49 commit 9384c12
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
32 changes: 20 additions & 12 deletions pkg/clients/iam/iam_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func TestIAMClient(t *testing.T) {

var _ = Describe("IAMClient", func() {
var params util.StorageClientParameters
accessDeniedError := &smithy.GenericAPIError{
Code: "AccessDenied",
Message: "Access Denied",
}

BeforeEach(func() {
params = util.StorageClientParameters{
Expand Down Expand Up @@ -61,15 +65,15 @@ var _ = Describe("IAMClient", func() {

It("should return an error when CreateUser fails", func(ctx SpecContext) {
mockIAM.CreateUserFunc = func(ctx context.Context, input *iam.CreateUserInput, opts ...func(*iam.Options)) (*iam.CreateUserOutput, error) {
return nil, fmt.Errorf("simulated CreateUser failure")
return nil, accessDeniedError
}

client, _ := iamclient.InitIAMClient(params)
client.IAMService = mockIAM

err := client.CreateUser(ctx, "test-user")
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("simulated CreateUser failure"))
Expect(errors.As(err, &accessDeniedError)).To(BeTrue())
})

It("should attach an inline policy with the correct name and content", func(ctx SpecContext) {
Expand All @@ -92,15 +96,15 @@ var _ = Describe("IAMClient", func() {

It("should return an error when PutUserPolicy fails", func(ctx SpecContext) {
mockIAM.PutUserPolicyFunc = func(ctx context.Context, input *iam.PutUserPolicyInput, opts ...func(*iam.Options)) (*iam.PutUserPolicyOutput, error) {
return nil, fmt.Errorf("simulated PutUserPolicy failure")
return nil, accessDeniedError
}

client, _ := iamclient.InitIAMClient(params)
client.IAMService = mockIAM

err := client.AttachS3WildcardInlinePolicy(ctx, "test-user", "test-bucket")
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("simulated PutUserPolicy failure"))
Expect(errors.As(err, &accessDeniedError)).To(BeTrue())
})

It("should create an access key successfully", func(ctx SpecContext) {
Expand All @@ -125,7 +129,7 @@ var _ = Describe("IAMClient", func() {

It("should return an error when CreateAccessKey fails", func(ctx SpecContext) {
mockIAM.CreateAccessKeyFunc = func(ctx context.Context, input *iam.CreateAccessKeyInput, opts ...func(*iam.Options)) (*iam.CreateAccessKeyOutput, error) {
return nil, fmt.Errorf("simulated CreateAccessKey failure")
return nil, accessDeniedError
}

client, _ := iamclient.InitIAMClient(params)
Expand All @@ -134,12 +138,16 @@ var _ = Describe("IAMClient", func() {
output, err := client.CreateAccessKey(ctx, "test-user")
Expect(err).NotTo(BeNil())
Expect(output).To(BeNil())
Expect(err.Error()).To(ContainSubstring("simulated CreateAccessKey failure"))
Expect(errors.As(err, &accessDeniedError)).To(BeTrue())
})
})

Describe("CreateBucketAccess", func() {
var mockIAM *mock.MockIAMClient
accessDeniedError := &smithy.GenericAPIError{
Code: "AccessDenied",
Message: "Access Denied",
}

BeforeEach(func() {
mockIAM = &mock.MockIAMClient{}
Expand Down Expand Up @@ -179,7 +187,7 @@ var _ = Describe("IAMClient", func() {

It("should return an error if CreateUser fails", func(ctx SpecContext) {
mockIAM.CreateUserFunc = func(ctx context.Context, input *iam.CreateUserInput, opts ...func(*iam.Options)) (*iam.CreateUserOutput, error) {
return nil, fmt.Errorf("simulated CreateUser failure")
return nil, accessDeniedError
}

client, _ := iamclient.InitIAMClient(params)
Expand All @@ -188,7 +196,7 @@ var _ = Describe("IAMClient", func() {
output, err := client.CreateBucketAccess(ctx, "test-user", "test-bucket")
Expect(err).NotTo(BeNil())
Expect(output).To(BeNil())
Expect(err.Error()).To(ContainSubstring("simulated CreateUser failure"))
Expect(errors.As(err, &accessDeniedError)).To(BeTrue())
})

It("should return an error if AttachS3WildcardInlinePolicy fails", func(ctx SpecContext) {
Expand All @@ -197,7 +205,7 @@ var _ = Describe("IAMClient", func() {
}

mockIAM.PutUserPolicyFunc = func(ctx context.Context, input *iam.PutUserPolicyInput, opts ...func(*iam.Options)) (*iam.PutUserPolicyOutput, error) {
return nil, fmt.Errorf("simulated AttachS3WildcardInlinePolicy failure")
return nil, accessDeniedError
}

client, _ := iamclient.InitIAMClient(params)
Expand All @@ -206,7 +214,7 @@ var _ = Describe("IAMClient", func() {
output, err := client.CreateBucketAccess(ctx, "test-user", "test-bucket")
Expect(err).NotTo(BeNil())
Expect(output).To(BeNil())
Expect(err.Error()).To(ContainSubstring("simulated AttachS3WildcardInlinePolicy failure"))
Expect(errors.As(err, &accessDeniedError)).To(BeTrue())
})

It("should return an error if CreateAccessKey fails", func(ctx SpecContext) {
Expand All @@ -219,7 +227,7 @@ var _ = Describe("IAMClient", func() {
}

mockIAM.CreateAccessKeyFunc = func(ctx context.Context, input *iam.CreateAccessKeyInput, opts ...func(*iam.Options)) (*iam.CreateAccessKeyOutput, error) {
return nil, fmt.Errorf("simulated CreateAccessKey failure")
return nil, accessDeniedError
}

client, _ := iamclient.InitIAMClient(params)
Expand All @@ -228,7 +236,7 @@ var _ = Describe("IAMClient", func() {
output, err := client.CreateBucketAccess(ctx, "test-user", "test-bucket")
Expect(err).NotTo(BeNil())
Expect(output).To(BeNil())
Expect(err.Error()).To(ContainSubstring("simulated CreateAccessKey failure"))
Expect(errors.As(err, &accessDeniedError)).To(BeTrue())
})
})

Expand Down
10 changes: 8 additions & 2 deletions pkg/clients/s3/s3_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package s3client_test

import (
"context"
"errors"
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/smithy-go"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
s3client "github.com/scality/cosi-driver/pkg/clients/s3"
Expand Down Expand Up @@ -131,6 +133,10 @@ var _ = Describe("S3Client", func() {
Describe("DeleteBucket", func() {
var mockS3 *mock.MockS3Client
var client *s3client.S3Client
accessDeniedError := &smithy.GenericAPIError{
Code: "AccessDenied",
Message: "Access Denied",
}

BeforeEach(func() {
mockS3 = &mock.MockS3Client{}
Expand All @@ -146,12 +152,12 @@ var _ = Describe("S3Client", func() {

It("should handle errors when deleting a bucket", func(ctx SpecContext) {
mockS3.DeleteBucketFunc = func(ctx context.Context, input *s3.DeleteBucketInput, opts ...func(*s3.Options)) (*s3.DeleteBucketOutput, error) {
return nil, fmt.Errorf("mock delete bucket error")
return nil, accessDeniedError
}

err := client.DeleteBucket(ctx, "test-bucket")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("mock delete bucket error"))
Expect(errors.As(err, &accessDeniedError)).To(BeTrue())
})
})
})

0 comments on commit 9384c12

Please sign in to comment.