2025-01-10 19:17:20 +00:00
|
|
|
package test
|
2025-01-08 18:14:15 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2025-02-03 17:30:50 +00:00
|
|
|
"queryorchestration/internal/serviceconfig"
|
2025-02-03 18:51:44 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/queue"
|
2025-01-08 18:14:15 +00:00
|
|
|
"testing"
|
2025-01-10 19:17:20 +00:00
|
|
|
"time"
|
2025-01-08 18:14:15 +00:00
|
|
|
|
|
|
|
|
"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"
|
2025-01-10 19:17:20 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
|
2025-01-08 18:14:15 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2025-01-10 19:17:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2025-01-08 18:14:15 +00:00
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
type queueContainerConfig struct {
|
|
|
|
|
Container testcontainers.Container
|
|
|
|
|
ExternalEndpoint string
|
|
|
|
|
NetworkEndpoint string
|
2025-01-10 19:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CreateQueueConfig struct {
|
|
|
|
|
Network *testcontainers.DockerNetwork
|
2025-02-03 17:30:50 +00:00
|
|
|
Cfg *serviceconfig.BaseConfig
|
2025-01-10 19:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
func createQueueContainer(t *testing.T, ctx context.Context, cfg *CreateQueueConfig) (*queueContainerConfig, func()) {
|
2025-01-08 18:14:15 +00:00
|
|
|
alias := "localstack"
|
2025-02-03 17:30:50 +00:00
|
|
|
provider := credentials.NewStaticCredentialsProvider(cfg.Cfg.AWSKeyID, cfg.Cfg.AWSSecretKey, cfg.Cfg.AWSSessionToken)
|
2025-01-08 18:14:15 +00:00
|
|
|
|
|
|
|
|
port, err := nat.NewPort("tcp", "4566")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to create port: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := testcontainers.ContainerRequest{
|
2025-01-10 11:12:03 +00:00
|
|
|
Image: "localstack/localstack:4.0.3",
|
2025-01-08 18:14:15 +00:00
|
|
|
Env: map[string]string{
|
|
|
|
|
"AWS_ACCESS_KEY_ID": provider.Value.AccessKeyID,
|
|
|
|
|
"AWS_SECRET_ACCESS_KEY": provider.Value.SecretAccessKey,
|
|
|
|
|
"AWS_SESSION_TOKEN": provider.Value.SessionToken,
|
2025-02-03 17:30:50 +00:00
|
|
|
"AWS_REGION": cfg.Cfg.AWSRegion,
|
2025-01-08 18:14:15 +00:00
|
|
|
"SERVICES": "sqs",
|
|
|
|
|
"SKIP_SSL_CERT_DOWNLOAD": "1",
|
|
|
|
|
"LOCALSTACK_HOST": alias,
|
|
|
|
|
"SQS_ENDPOINT_STRATEGY": "path",
|
|
|
|
|
},
|
|
|
|
|
ExposedPorts: []string{port.Port()},
|
2025-01-15 12:19:49 +00:00
|
|
|
WaitingFor: wait.ForAll(
|
|
|
|
|
// wait.ForExposedPort(),
|
|
|
|
|
wait.ForListeningPort(port),
|
|
|
|
|
wait.ForLog("Ready."),
|
|
|
|
|
),
|
2025-01-08 18:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-10 19:17:20 +00:00
|
|
|
if cfg.Network != nil {
|
|
|
|
|
req.Networks = []string{cfg.Network.Name}
|
|
|
|
|
req.NetworkAliases = map[string][]string{
|
|
|
|
|
cfg.Network.Name: {alias},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 18:14:15 +00:00
|
|
|
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
|
|
|
|
ContainerRequest: req,
|
|
|
|
|
Started: true,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to start container: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
host, err := container.Host(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to extract host: %v", err)
|
|
|
|
|
}
|
|
|
|
|
mappedPort, err := container.MappedPort(ctx, port)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to extract port: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
extEndpoint := fmt.Sprintf("http://%s:%s", host, mappedPort.Port())
|
|
|
|
|
t.Setenv("AWS_ENDPOINT_URL_SQS", extEndpoint)
|
|
|
|
|
cfg.Cfg.AWSEndpointUrlSQS = extEndpoint
|
|
|
|
|
|
|
|
|
|
var endpoint string
|
|
|
|
|
if cfg.Network != nil {
|
|
|
|
|
endpoint = fmt.Sprintf("http://%s:%s", alias, port.Port())
|
|
|
|
|
cfg.Cfg.AWSEndpointUrlSQS = endpoint
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &queueContainerConfig{
|
|
|
|
|
Container: container,
|
|
|
|
|
ExternalEndpoint: extEndpoint,
|
|
|
|
|
NetworkEndpoint: endpoint,
|
|
|
|
|
}, func() {
|
|
|
|
|
err := container.Terminate(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CreateQueueClient(t *testing.T, ctx context.Context, cfg *CreateQueueConfig) func() {
|
|
|
|
|
qcfg, clean := createQueueContainer(t, ctx, cfg)
|
|
|
|
|
|
|
|
|
|
provider := credentials.NewStaticCredentialsProvider(cfg.Cfg.AWSKeyID, cfg.Cfg.AWSSecretKey, cfg.Cfg.AWSSessionToken)
|
|
|
|
|
|
|
|
|
|
sqsCfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(cfg.Cfg.AWSRegion), config.WithCredentialsProvider(provider), config.WithBaseEndpoint(qcfg.ExternalEndpoint))
|
2025-01-08 18:14:15 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
cfg.Cfg.QueueClient = sqs.NewFromConfig(sqsCfg)
|
|
|
|
|
|
|
|
|
|
return clean
|
|
|
|
|
}
|
2025-01-08 18:14:15 +00:00
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
func CreateQueue(t *testing.T, ctx context.Context, cfg *serviceconfig.BaseConfig, name string) string {
|
|
|
|
|
queueM, err := cfg.QueueClient.CreateQueue(ctx, &sqs.CreateQueueInput{
|
|
|
|
|
QueueName: aws.String(name),
|
2025-01-08 18:14:15 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
return *queueM.QueueUrl
|
2025-01-08 18:14:15 +00:00
|
|
|
}
|
2025-01-10 19:17:20 +00:00
|
|
|
|
2025-02-03 18:51:44 +00:00
|
|
|
func AssertMessageWait(t *testing.T, ctx context.Context, cfg serviceconfig.ConfigProvider, params *queue.ReceiveParams) types.Message {
|
2025-02-03 17:30:50 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2025-01-10 19:17:20 +00:00
|
|
|
|
2025-02-03 18:51:44 +00:00
|
|
|
result, err := cfg.ReceiveFromQueue(ctx, params)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.NotNil(t, result.Messages)
|
2025-01-10 19:17:20 +00:00
|
|
|
assert.Len(t, result.Messages, 1)
|
|
|
|
|
assert.NotNil(t, result.Messages[0])
|
|
|
|
|
|
|
|
|
|
return result.Messages[0]
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 18:51:44 +00:00
|
|
|
func AssertMessageBodyWait(t *testing.T, ctx context.Context, cfg serviceconfig.ConfigProvider, url string, body string) {
|
|
|
|
|
message := AssertMessageWait(t, ctx, cfg, &queue.ReceiveParams{
|
|
|
|
|
QueueURL: url,
|
|
|
|
|
})
|
2025-01-10 19:17:20 +00:00
|
|
|
|
|
|
|
|
assert.Equal(t, body, *message.Body)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 18:51:44 +00:00
|
|
|
func AssertMessageAttrWait(t *testing.T, ctx context.Context, cfg serviceconfig.ConfigProvider, url string, name string, value string) {
|
|
|
|
|
message := AssertMessageWait(t, ctx, cfg, &queue.ReceiveParams{
|
|
|
|
|
QueueURL: url,
|
|
|
|
|
Attributes: []string{name},
|
|
|
|
|
})
|
2025-01-10 19:17:20 +00:00
|
|
|
|
|
|
|
|
assert.NotNil(t, message.MessageAttributes[name])
|
|
|
|
|
assert.Equal(t, value, *(message.MessageAttributes[name]).StringValue)
|
|
|
|
|
}
|