Skip to content

Commit

Permalink
move GlobalOption test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtwinkle committed Aug 31, 2023
1 parent bb7f1d6 commit 6f7718a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 39 deletions.
39 changes: 0 additions & 39 deletions aws/awss3/awss3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/88labs/go-utils/aws/awsconfig"
"github.com/88labs/go-utils/aws/awss3"
"github.com/88labs/go-utils/aws/awss3/options/global/s3dialer"
"github.com/88labs/go-utils/aws/awss3/options/s3download"
"github.com/88labs/go-utils/aws/awss3/options/s3head"
"github.com/88labs/go-utils/aws/awss3/options/s3list"
Expand Down Expand Up @@ -893,41 +892,3 @@ func TestSelectCSVHeaders(t *testing.T) {
assert.Error(t, err)
})
}

func TestGlobalOptionWithHeadObject(t *testing.T) {
ctx := ctxawslocal.WithContext(
context.Background(),
ctxawslocal.WithS3Endpoint("http://127.0.0.1:29000"), // use Minio
ctxawslocal.WithAccessKey("DUMMYACCESSKEYEXAMPLE"),
ctxawslocal.WithSecretAccessKey("DUMMYSECRETKEYEXAMPLE"),
)
s3Client, err := awss3.GetClient(ctx, TestRegion)
assert.NoError(t, err)

createFixture := func(fileSize int) awss3.Key {
key := fmt.Sprintf("awstest/%s.txt", ulid.MustNew())
uploader := manager.NewUploader(s3Client)
input := s3.PutObjectInput{
Body: bytes.NewReader(bytes.Repeat([]byte{1}, fileSize)),
Bucket: aws.String(TestBucket),
Key: aws.String(key),
Expires: aws.Time(time.Now().Add(10 * time.Minute)),
}
if _, err := uploader.Upload(ctx, &input); err != nil {
assert.NoError(t, err)
}
return awss3.Key(key)
}

t.Run("If the option is specified", func(t *testing.T) {
key := createFixture(100)
dialer := s3dialer.NewConfGlobalDialer()
dialer.WithTimeout(time.Second)
dialer.WithKeepAlive(2 * time.Second)
dialer.WithDeadline(time.Now().Add(time.Second))
awss3.GlobalDialer = dialer
res, err := awss3.HeadObject(ctx, TestRegion, TestBucket, key)
assert.NoError(t, err)
assert.Equal(t, int64(100), res.ContentLength)
})
}
62 changes: 62 additions & 0 deletions aws/awss3/options/global/global_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package global_test

import (
"bytes"
"context"
"fmt"
"testing"
"time"

"github.com/88labs/go-utils/aws/awsconfig"
"github.com/88labs/go-utils/aws/awss3"
"github.com/88labs/go-utils/aws/awss3/options/global/s3dialer"
"github.com/88labs/go-utils/aws/ctxawslocal"
"github.com/88labs/go-utils/ulid"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/stretchr/testify/assert"
)

const (
TestBucket = "test"
TestRegion = awsconfig.RegionTokyo
)

func TestGlobalOptionWithHeadObject(t *testing.T) {
ctx := ctxawslocal.WithContext(
context.Background(),
ctxawslocal.WithS3Endpoint("http://127.0.0.1:29000"), // use Minio
ctxawslocal.WithAccessKey("DUMMYACCESSKEYEXAMPLE"),
ctxawslocal.WithSecretAccessKey("DUMMYSECRETKEYEXAMPLE"),
)
s3Client, err := awss3.GetClient(ctx, TestRegion)
assert.NoError(t, err)

createFixture := func(fileSize int) awss3.Key {
key := fmt.Sprintf("awstest/%s.txt", ulid.MustNew())
uploader := manager.NewUploader(s3Client)
input := s3.PutObjectInput{
Body: bytes.NewReader(bytes.Repeat([]byte{1}, fileSize)),
Bucket: aws.String(TestBucket),
Key: aws.String(key),
Expires: aws.Time(time.Now().Add(10 * time.Minute)),
}
if _, err := uploader.Upload(ctx, &input); err != nil {
assert.NoError(t, err)
}
return awss3.Key(key)
}

t.Run("If the option is specified", func(t *testing.T) {
key := createFixture(100)
dialer := s3dialer.NewConfGlobalDialer()
dialer.WithTimeout(time.Second)
dialer.WithKeepAlive(2 * time.Second)
dialer.WithDeadline(time.Now().Add(time.Second))
awss3.GlobalDialer = dialer
res, err := awss3.HeadObject(ctx, TestRegion, TestBucket, key)
assert.NoError(t, err)
assert.Equal(t, int64(100), res.ContentLength)
})
}

0 comments on commit 6f7718a

Please sign in to comment.