Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor packages #680

Merged
merged 2 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions client/sns/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package sns

import (
"context"
"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/credentials"
"github.com/aws/aws-sdk-go-v2/service/sns"
testaws "github.com/beatlabs/patron/test/aws"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/mocktracer"
Expand All @@ -29,9 +31,9 @@ func Test_SNS_Publish_Message_v2(t *testing.T) {
t.Cleanup(func() { mtr.Reset() })

const topic = "test_publish_message_v2"
api, err := testaws.CreateSNSAPI(region, endpoint)
api, err := createSNSAPI(region, endpoint)
require.NoError(t, err)
arn, err := testaws.CreateSNSTopic(api, topic)
arn, err := createSNSTopic(api, topic)
require.NoError(t, err)
pub, err := New(api)
require.NoError(t, err)
Expand All @@ -53,3 +55,53 @@ func Test_SNS_Publish_Message_v2(t *testing.T) {
// Metrics
assert.Equal(t, 1, testutil.CollectAndCount(publishDurationMetrics, "client_sns_publish_duration_seconds"))
}

func createSNSAPI(region, endpoint string) (*sns.Client, error) {
cfg, err := createConfig(sns.ServiceID, region, endpoint)
if err != nil {
return nil, err
}

api := sns.NewFromConfig(cfg)

return api, nil
}

func createSNSTopic(api SNSAPI, topic string) (string, error) {
out, err := api.CreateTopic(context.Background(), &sns.CreateTopicInput{
Name: aws.String(topic),
})
if err != nil {
return "", fmt.Errorf("failed to create topic %s: %w", topic, err)
}

return *out.TopicArn, nil
}

type SNSAPI interface {
CreateTopic(ctx context.Context, params *sns.CreateTopicInput, optFns ...func(*sns.Options)) (*sns.CreateTopicOutput, error)
}

func createConfig(awsServiceID, awsRegion, awsEndpoint string) (aws.Config, error) {
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == awsServiceID && region == awsRegion {
return aws.Endpoint{
URL: awsEndpoint,
SigningRegion: awsRegion,
}, nil
}
// returning EndpointNotFoundError will allow the service to fallback to it's default resolution
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})

cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(awsRegion),
config.WithEndpointResolverWithOptions(customResolver),
config.WithCredentialsProvider(aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider("test", "test", ""))),
)
if err != nil {
return aws.Config{}, fmt.Errorf("failed to create AWS config: %w", err)
}

return cfg, nil
}
59 changes: 56 additions & 3 deletions client/sqs/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ package sqs
import (
"context"
"encoding/json"
"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/credentials"
"github.com/aws/aws-sdk-go-v2/service/sqs"
testaws "github.com/beatlabs/patron/test/aws"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/mocktracer"
Expand All @@ -36,9 +38,9 @@ func Test_SQS_Publish_Message(t *testing.T) {

const queueName = "test-sqs-publish-v2"

api, err := testaws.CreateSQSAPI(region, endpoint)
api, err := createSQSAPI(region, endpoint)
require.NoError(t, err)
queue, err := testaws.CreateSQSQueue(api, queueName)
queue, err := createSQSQueue(api, queueName)
require.NoError(t, err)

pub, err := New(api)
Expand Down Expand Up @@ -77,3 +79,54 @@ func Test_SQS_Publish_Message(t *testing.T) {
assert.Equal(t, expected, mtr.FinishedSpans()[0].Tags())
assert.Equal(t, 1, testutil.CollectAndCount(publishDurationMetrics, "client_sqs_publish_duration_seconds"))
}

func createSQSQueue(api SQSAPI, queueName string) (string, error) {
out, err := api.CreateQueue(context.Background(), &sqs.CreateQueueInput{
QueueName: aws.String(queueName),
})
if err != nil {
return "", err
}

return *out.QueueUrl, nil
}

type SQSAPI interface {
CreateQueue(ctx context.Context, params *sqs.CreateQueueInput, optFns ...func(*sqs.Options)) (*sqs.CreateQueueOutput, error)
ReceiveMessage(ctx context.Context, params *sqs.ReceiveMessageInput, optFns ...func(*sqs.Options)) (*sqs.ReceiveMessageOutput, error)
}

func createSQSAPI(region, endpoint string) (*sqs.Client, error) {
cfg, err := createConfig(sqs.ServiceID, region, endpoint)
if err != nil {
return nil, err
}

api := sqs.NewFromConfig(cfg)

return api, nil
}

func createConfig(awsServiceID, awsRegion, awsEndpoint string) (aws.Config, error) {
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == awsServiceID && region == awsRegion {
return aws.Endpoint{
URL: awsEndpoint,
SigningRegion: awsRegion,
}, nil
}
// returning EndpointNotFoundError will allow the service to fallback to it's default resolution
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})

cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(awsRegion),
config.WithEndpointResolverWithOptions(customResolver),
config.WithCredentialsProvider(aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider("test", "test", ""))),
)
if err != nil {
return aws.Config{}, fmt.Errorf("failed to create AWS config: %w", err)
}

return cfg, nil
}
Loading
Loading