Repository: pingcap/tidb
File: pkg/objstore/s3store/client_test.go (Go)

Code before:
				s := CreateS3Suite(t)
				cli := &s3Client{
					svc:          s.MockS3,
					BucketPrefix: storeapi.NewBucketPrefix("bucket", "prefix/"),
					options:      &backuppb.S3{Bucket: "bucket"},
					isKS3:        tc.s3Compatible,
				}

				s.MockS3.EXPECT().PutObject(gomock.Any(), gomock.Any(), gomock.Any()).
					DoAndReturn(func(_ context.Context, input *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
						require.Equal(t, "bucket", aws.ToString(input.Bucket))
						require.Equal(t, "prefix/object", aws.ToString(input.Key))
						assertContentMD5Option(t, optFns, tc.s3Compatible)
						return &s3.PutObjectOutput{}, nil
					})

				require.NoError(t, cli.PutObject(ctx, "object", []byte("data")))
				require.True(t, s.Controller.Satisfied())
			})
		}
	})

	t.Run("CheckPutAndDeleteObject", func(t *testing.T) {
		for _, tc := range []struct {
			name         string
			s3Compatible bool
		}{
			{name: "official_s3", s3Compatible: false},
			{name: "ks3", s3Compatible: true},
		} {
			t.Run(tc.name, func(t *testing.T) {
				s := CreateS3Suite(t)
				cli := &s3Client{
					svc:          s.MockS3,
					BucketPrefix: storeapi.NewBucketPrefix("bucket", "prefix/"),
					isKS3:        tc.s3Compatible,
				}

				s.MockS3.EXPECT().PutObject(gomock.Any(), gomock.Any(), gomock.Any()).
					DoAndReturn(func(_ context.Context, input *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
						require.Equal(t, "bucket", aws.ToString(input.Bucket))
						require.True(t, strings.HasPrefix(aws.ToString(input.Key), "prefix/perm-check/"))
						assertContentMD5Option(t, optFns, tc.s3Compatible)
						return &s3.PutObjectOutput{}, nil
					})
				s.MockS3.EXPECT().DeleteObject(gomock.Any(), gomock.Any(), gomock.Any()).
					DoAndReturn(func(_ context.Context, input *s3.DeleteObjectInput, _ ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) {
						require.Equal(t, "bucket", aws.ToString(input.Bucket))
						require.True(t, strings.HasPrefix(aws.ToString(input.Key), "prefix/perm-check/"))
						return &s3.DeleteObjectOutput{}, nil
					})

Review comment (suggestion):
The test case variable name `s3Compatible` is misleading. It should be renamed to something like `isKS3` to match the semantic change of this PR and the field it's assigned to.

Code after:
				s := CreateS3Suite(t)
				cli := &s3Client{
					svc:          s.MockS3,
					BucketPrefix: storeapi.NewBucketPrefix("bucket", "prefix/"),
					options:      &backuppb.S3{Bucket: "bucket"},
					s3Compatible: tc.s3Compatible,
				}

				s.MockS3.EXPECT().PutObject(gomock.Any(), gomock.Any(), gomock.Any()).
					DoAndReturn(func(_ context.Context, input *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
						require.Equal(t, "bucket", aws.ToString(input.Bucket))
						require.Equal(t, "prefix/object", aws.ToString(input.Key))
						assertContentMD5Option(t, optFns, tc.s3Compatible)
						return &s3.PutObjectOutput{}, nil
					})

				require.NoError(t, cli.PutObject(ctx, "object", []byte("data")))
				require.True(t, s.Controller.Satisfied())
			})
		}
	})

	t.Run("CheckPutAndDeleteObject", func(t *testing.T) {
		for _, tc := range []struct {
			name         string
			s3Compatible bool
		}{
			{name: "official_s3", s3Compatible: false},
			{name: "s3_compatible", s3Compatible: true},
		} {
			t.Run(tc.name, func(t *testing.T) {
				s := CreateS3Suite(t)
				cli := &s3Client{
					svc:          s.MockS3,
					BucketPrefix: storeapi.NewBucketPrefix("bucket", "prefix/"),
					s3Compatible: tc.s3Compatible,
				}

				s.MockS3.EXPECT().PutObject(gomock.Any(), gomock.Any(), gomock.Any()).
					DoAndReturn(func(_ context.Context, input *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
						require.Equal(t, "bucket", aws.ToString(input.Bucket))
						require.True(t, strings.HasPrefix(aws.ToString(input.Key), "prefix/perm-check/"))
						assertContentMD5Option(t, optFns, tc.s3Compatible)
						return &s3.PutObjectOutput{}, nil
					})
				s.MockS3.EXPECT().DeleteObject(gomock.Any(), gomock.Any(), gomock.Any()).
					DoAndReturn(func(_ context.Context, input *s3.DeleteObjectInput, _ ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) {
						require.Equal(t, "bucket", aws.ToString(input.Bucket))
						require.True(t, strings.HasPrefix(aws.ToString(input.Key), "prefix/perm-check/"))
						return &s3.DeleteObjectOutput{}, nil
					})