From 28abce5900e0c47d15bc8f27f7a0c43b5b9c30e1 Mon Sep 17 00:00:00 2001 From: Anurag Mittal Date: Thu, 2 Jan 2025 18:48:12 +0100 Subject: [PATCH] COSI-73: update errors with aws errors in unit tests --- pkg/clients/iam/iam_client_test.go | 32 +++++++++++++++++++----------- pkg/clients/s3/s3_client_test.go | 10 ++++++++-- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/pkg/clients/iam/iam_client_test.go b/pkg/clients/iam/iam_client_test.go index a12786ab..279518dc 100644 --- a/pkg/clients/iam/iam_client_test.go +++ b/pkg/clients/iam/iam_client_test.go @@ -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{ @@ -61,7 +65,7 @@ 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) @@ -69,7 +73,7 @@ var _ = Describe("IAMClient", func() { 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) { @@ -92,7 +96,7 @@ 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) @@ -100,7 +104,7 @@ var _ = Describe("IAMClient", func() { 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) { @@ -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) @@ -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{} @@ -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) @@ -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) { @@ -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) @@ -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) { @@ -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) @@ -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()) }) }) diff --git a/pkg/clients/s3/s3_client_test.go b/pkg/clients/s3/s3_client_test.go index 01c5fe04..da5b8a04 100644 --- a/pkg/clients/s3/s3_client_test.go +++ b/pkg/clients/s3/s3_client_test.go @@ -2,6 +2,7 @@ package s3client_test import ( "context" + "errors" "fmt" "testing" @@ -9,6 +10,7 @@ import ( "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" @@ -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{} @@ -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()) }) }) })