Skip to content

Commit

Permalink
Merge pull request #411 from 88labs/fix/awss3-client-mock
Browse files Browse the repository at this point in the history
fix: awss3 client mock use customResolver
  • Loading branch information
tomtwinkle authored Jan 19, 2024
2 parents 24ee18b + 4f04567 commit d703e41
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 223 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/test-aws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Cache Go modules
id: cache-go
uses: actions/cache@v4
Expand All @@ -52,7 +50,6 @@ jobs:
shell: bash
if: ${{ steps.cache-go.outputs.cache-hit != 'true' }}
run: go mod download

- name: Setup Docker
working-directory: ${{ env.testdir }}
env:
Expand All @@ -63,14 +60,13 @@ jobs:
mkdir -p ./docker/dynamodb/data
sudo chmod 777 ./docker/dynamodb/data
docker compose up -d
- name: Test
working-directory: ${{ env.testdir }}
run: |
go install gotest.tools/gotestsum@latest
# shellcheck disable=SC2046
go test -p 4 -parallel 4 -v ./... -race -coverprofile="coverage.txt" -covermode=atomic -coverpkg=./...
gotestsum --junitfile unit-tests.xml -- -p 4 -parallel 4 -v ./... -race -coverprofile="coverage.txt" -covermode=atomic -coverpkg=./...
- uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ${{ env.testdir }}/coverage.txt
files: ./unit-tests.xml,./coverage.txt
105 changes: 6 additions & 99 deletions aws/awss3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,129 +5,36 @@ import (
"context"
"encoding/gob"
"fmt"
"net"
"net/url"
"sync"

"github.com/88labs/go-utils/aws/awsconfig"

"github.com/88labs/go-utils/aws/ctxawslocal"
"github.com/aws/aws-sdk-go-v2/aws"
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
smithyendpoints "github.com/aws/smithy-go/endpoints"

"github.com/88labs/go-utils/aws/awsconfig"
"github.com/88labs/go-utils/aws/awss3/options/global/s3dialer"
"github.com/88labs/go-utils/aws/ctxawslocal"
)

var (
// GlobalDialer Global http dialer settings for awss3 library
GlobalDialer *s3dialer.ConfGlobalDialer

customMu sync.Mutex
customEndpoint string
customEndpointClient *s3.Client
)

// GetClient
// Get s3 client for aws-sdk-go v2.
// Using ctxawslocal.WithContext, you can make requests for local mocks
func GetClient(ctx context.Context, region awsconfig.Region) (*s3.Client, error) {
if localProfile, ok := getLocalEndpoint(ctx); ok {
customMu.Lock()
defer customMu.Unlock()
var err error
if customEndpointClient != nil {
return customEndpointClient, err
}
customEndpointClient, err = getClientLocal(ctx, *localProfile)
return customEndpointClient, err
}
awsHttpClient := awshttp.NewBuildableClient()
if GlobalDialer != nil {
awsHttpClient.WithDialerOptions(func(dialer *net.Dialer) {
if GlobalDialer.Timeout != 0 {
dialer.Timeout = GlobalDialer.Timeout
}
if GlobalDialer.Deadline != nil {
dialer.Deadline = *GlobalDialer.Deadline
}
if GlobalDialer.KeepAlive != 0 {
dialer.KeepAlive = GlobalDialer.KeepAlive
}
})
return getClientLocal(ctx, *localProfile)
}
// S3 Client
awsCfg, err := awsConfig.LoadDefaultConfig(
ctx,
awsConfig.WithRegion(region.String()),
awsConfig.WithHTTPClient(awsHttpClient),
)
if err != nil {
return nil, fmt.Errorf("unable to load SDK config, %w", err)
}
return s3.NewFromConfig(awsCfg), nil
}

type staticResolver struct{}

// ResolveEndpoint
// Local test mocks endpoints to connect to minio
//
// FIXME: EndpointResolverWithOptionsFunc substitutes staticResolver for endpoint mock
func (*staticResolver) ResolveEndpoint(_ context.Context, p s3.EndpointParameters) (
smithyendpoints.Endpoint, error,
) {
if customEndpoint != "" {
endpoint, err := url.Parse(customEndpoint)
if err != nil {
return smithyendpoints.Endpoint{}, fmt.Errorf("unable to parse endpoint, %w", err)
}
if p.Bucket != nil {
endpoint = endpoint.JoinPath(*p.Bucket)
}
// This value will be used as-is when making the request.
return smithyendpoints.Endpoint{
URI: *endpoint,
}, nil
}
return smithyendpoints.Endpoint{}, &aws.EndpointNotFoundError{}
}

func getClientLocal(ctx context.Context, localProfile LocalProfile) (*s3.Client, error) {
// FIXME: EndpointResolverWithOptionsFunc substitutes staticResolver for endpoint mock
// because HostnameImmutable is not enabled. (github.com/aws/aws-sdk-go-v2/config v1.25.4)
// https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/endpoints/
//customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
// if service == s3.ServiceID {
// return aws.Endpoint{
// PartitionID: "aws",
// URL: localProfile.Endpoint,
// SigningRegion: region,
// HostnameImmutable: true,
// }, nil
// }
// // returning EndpointNotFoundError will allow the service to fallback to it's default resolution
// return aws.Endpoint{}, &aws.EndpointNotFoundError{}
//})
awsHttpClient := awshttp.NewBuildableClient()
if GlobalDialer != nil {
awsHttpClient.WithDialerOptions(func(dialer *net.Dialer) {
if GlobalDialer.Timeout != 0 {
dialer.Timeout = GlobalDialer.Timeout
}
if GlobalDialer.Deadline != nil {
dialer.Deadline = *GlobalDialer.Deadline
}
if GlobalDialer.KeepAlive != 0 {
dialer.KeepAlive = GlobalDialer.KeepAlive
}
})
}
awsCfg, err := awsConfig.LoadDefaultConfig(ctx,
awsConfig.WithHTTPClient(awsHttpClient),
//awsConfig.WithEndpointResolverWithOptions(customResolver),
awsConfig.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: localProfile.AccessKey,
Expand All @@ -139,9 +46,9 @@ func getClientLocal(ctx context.Context, localProfile LocalProfile) (*s3.Client,
if err != nil {
return nil, fmt.Errorf("unable to load SDK config, %w", err)
}
customEndpoint = localProfile.Endpoint
return s3.NewFromConfig(awsCfg, func(o *s3.Options) {
o.EndpointResolverV2 = &staticResolver{}
o.BaseEndpoint = aws.String(localProfile.Endpoint)
o.UsePathStyle = true
}), nil
}

Expand Down
63 changes: 0 additions & 63 deletions aws/awss3/options/global/global_test.go

This file was deleted.

50 changes: 0 additions & 50 deletions aws/awss3/options/global/s3dialer/s3dialer.go

This file was deleted.

8 changes: 4 additions & 4 deletions aws/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ require (
github.com/88labs/go-utils/ulid v0.3.0
github.com/88labs/go-utils/utf8bom v0.4.0
github.com/aws/aws-sdk-go-v2 v1.24.1
github.com/aws/aws-sdk-go-v2/config v1.26.4
github.com/aws/aws-sdk-go-v2/credentials v1.16.15
github.com/aws/aws-sdk-go-v2/config v1.26.5
github.com/aws/aws-sdk-go-v2/credentials v1.16.16
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.12.15
github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v1.6.15
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.15.12
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.15.13
github.com/aws/aws-sdk-go-v2/service/cognitoidentity v1.21.7
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.26.9
github.com/aws/aws-sdk-go-v2/service/s3 v1.48.0
Expand All @@ -37,7 +37,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.8.11 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.10 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.16.10 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.18.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.18.7 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.7 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions aws/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.4 h1:OCs21ST2LrepDfD3
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.4/go.mod h1:usURWEKSNNAcAZuzRn/9ZYPT8aZQkR7xcCtunK/LkJo=
github.com/aws/aws-sdk-go-v2/config v1.26.4 h1:Juj7LhtxNudNUlfX22K5AnLafO+v4eq9PA3VWSCIQs4=
github.com/aws/aws-sdk-go-v2/config v1.26.4/go.mod h1:tioqQ7wvxMYnTDpoTTLHhV3Zh+z261i/f2oz+ds8eNI=
github.com/aws/aws-sdk-go-v2/config v1.26.5 h1:lodGSevz7d+kkFJodfauThRxK9mdJbyutUxGq1NNhvw=
github.com/aws/aws-sdk-go-v2/config v1.26.5/go.mod h1:DxHrz6diQJOc9EwDslVRh84VjjrE17g+pVZXUeSxaDU=
github.com/aws/aws-sdk-go-v2/credentials v1.16.15 h1:P0/m1LU08MF2kRzx4P//+7lNjiJod1z4xI2WpWhdpTQ=
github.com/aws/aws-sdk-go-v2/credentials v1.16.15/go.mod h1:pgtMCf7Dx4GWw5EpHOTc2Sy17LIP0A0N2C9nQ83pQ/0=
github.com/aws/aws-sdk-go-v2/credentials v1.16.16 h1:8q6Rliyv0aUFAVtzaldUEcS+T5gbadPbWdV1WcAddK8=
github.com/aws/aws-sdk-go-v2/credentials v1.16.16/go.mod h1:UHVZrdUsv63hPXFo1H7c5fEneoVo9UXiz36QG1GEPi0=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.12.15 h1:34/YcQBavs4hTUqy9wq3k7f8b+eQqvKDHRithYOD4Gw=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.12.15/go.mod h1:DbKcs3L/AKDeVNZzKOttxLu/x9bm4VKercDGy42/AGo=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v1.6.15 h1:nNOuo7q+hjvf97f5dmnv84IwsySeIyzFdGb5Jyrspjw=
Expand All @@ -18,6 +22,8 @@ github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.11 h1:c5I5iH+DZcH3xOIMlz3/tC
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.11/go.mod h1:cRrYDYAMUohBJUtUnOhydaMHtiK/1NZ0Otc9lIb6O0Y=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.15.12 h1:0FMZy36RSYvcvVzEf1xbNdebLHZewW40QWP+P8jCMVk=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.15.12/go.mod h1:+chyahvarkb3HibkNei9IQEM9P5cWD5w2kgXCa3Hh0I=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.15.13 h1:8Nt4LBUEKV0FxLBO2BmRzDKax3hp2LRMKySMBwL4vMc=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.15.13/go.mod h1:t5QEDu/FBJJM4kslbQlTSpYtnhoWDNmHSsgQojIxE0o=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.10 h1:vF+Zgd9s+H4vOXd5BMaPWykta2a6Ih0AKLq/X6NYKn4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.10/go.mod h1:6BkRjejp/GR4411UGqkX8+wFMbFbqsUIimfK4XjOKR4=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.10 h1:nYPe006ktcqUji8S2mqXf9c/7NdiKriOwMvWQHgYztw=
Expand Down Expand Up @@ -48,6 +54,8 @@ github.com/aws/aws-sdk-go-v2/service/sqs v1.29.7 h1:tRNrFDGRm81e6nTX5Q4CFblea99e
github.com/aws/aws-sdk-go-v2/service/sqs v1.29.7/go.mod h1:8GWUDux5Z2h6z2efAtr54RdHXtLm8sq7Rg85ZNY/CZM=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.6 h1:dGrs+Q/WzhsiUKh82SfTVN66QzyulXuMDTV/G8ZxOac=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.6/go.mod h1:+mJNDdF+qiUlNKNC3fxn74WWNN+sOiGOEImje+3ScPM=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.7 h1:eajuO3nykDPdYicLlP3AGgOyVN3MOlFmZv7WGTuJPow=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.7/go.mod h1:+mJNDdF+qiUlNKNC3fxn74WWNN+sOiGOEImje+3ScPM=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.7 h1:QPMJf+Jw8E1l7zqhZmMlFw6w1NmfkfiSK8mS4zOx3BA=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.7/go.mod h1:ykf3COxYI0UJmxcfcxcVuz7b6uADi1FkiUz6Eb7AgM8=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.7 h1:NzO4Vrau795RkUdSHKEwiR01FaGzGOH1EETJ+5QHnm0=
Expand Down

0 comments on commit d703e41

Please sign in to comment.