Merged in feature/docinitialisation (pull request #41)
Queuing Changes and Cfg Testing * staarting * staarting * startedpush * note * save * mocking * removederrs * fixtests * cleanuperrs * newenvsetup * preppingtests * queue * mmovetocfgpassunittests * sortoutconfig * passinginteg * deps * fixtests
This commit is contained in:
+50
-44
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"queryorchestration/internal/server/queue"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -18,29 +19,20 @@ import (
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
)
|
||||
|
||||
type QueueConfig struct {
|
||||
Container testcontainers.Container
|
||||
Client *sqs.Client
|
||||
URL string
|
||||
External *ExternalQueue
|
||||
}
|
||||
|
||||
type ExternalQueue struct {
|
||||
Endpoint string
|
||||
Credentials aws.CredentialsProvider
|
||||
Region string
|
||||
URL string
|
||||
type queueContainerConfig struct {
|
||||
Container testcontainers.Container
|
||||
ExternalEndpoint string
|
||||
NetworkEndpoint string
|
||||
}
|
||||
|
||||
type CreateQueueConfig struct {
|
||||
Network *testcontainers.DockerNetwork
|
||||
Cfg *serviceconfig.BaseConfig
|
||||
}
|
||||
|
||||
func CreateQueue(t *testing.T, ctx context.Context, cfg *CreateQueueConfig) (*QueueConfig, func()) {
|
||||
queueName := "test-queue"
|
||||
region := "us-east-1"
|
||||
func createQueueContainer(t *testing.T, ctx context.Context, cfg *CreateQueueConfig) (*queueContainerConfig, func()) {
|
||||
alias := "localstack"
|
||||
provider := credentials.NewStaticCredentialsProvider("test", "test", "")
|
||||
provider := credentials.NewStaticCredentialsProvider(cfg.Cfg.AWSKeyID, cfg.Cfg.AWSSecretKey, cfg.Cfg.AWSSessionToken)
|
||||
|
||||
port, err := nat.NewPort("tcp", "4566")
|
||||
if err != nil {
|
||||
@@ -53,8 +45,7 @@ func CreateQueue(t *testing.T, ctx context.Context, cfg *CreateQueueConfig) (*Qu
|
||||
"AWS_ACCESS_KEY_ID": provider.Value.AccessKeyID,
|
||||
"AWS_SECRET_ACCESS_KEY": provider.Value.SecretAccessKey,
|
||||
"AWS_SESSION_TOKEN": provider.Value.SessionToken,
|
||||
"AWS_DEFAULT_REGION": region,
|
||||
"AWS_REGION": region,
|
||||
"AWS_REGION": cfg.Cfg.AWSRegion,
|
||||
"SERVICES": "sqs",
|
||||
"SKIP_SSL_CERT_DOWNLOAD": "1",
|
||||
"LOCALSTACK_HOST": alias,
|
||||
@@ -92,32 +83,20 @@ func CreateQueue(t *testing.T, ctx context.Context, cfg *CreateQueueConfig) (*Qu
|
||||
t.Fatalf("Failed to extract port: %v", err)
|
||||
}
|
||||
|
||||
endpoint := fmt.Sprintf("http://%s:%s", host, mappedPort.Port())
|
||||
sqsCfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region), config.WithCredentialsProvider(provider), config.WithBaseEndpoint(endpoint))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
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
|
||||
}
|
||||
|
||||
client := sqs.NewFromConfig(sqsCfg)
|
||||
|
||||
queueM, err := client.CreateQueue(ctx, &sqs.CreateQueueInput{
|
||||
QueueName: aws.String(queueName),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
endpoint = fmt.Sprintf("http://%s:%s", alias, port.Port())
|
||||
|
||||
return &QueueConfig{
|
||||
Container: container,
|
||||
Client: client,
|
||||
URL: *queueM.QueueUrl,
|
||||
External: &ExternalQueue{
|
||||
Region: region,
|
||||
Credentials: provider,
|
||||
Endpoint: endpoint,
|
||||
},
|
||||
return &queueContainerConfig{
|
||||
Container: container,
|
||||
ExternalEndpoint: extEndpoint,
|
||||
NetworkEndpoint: endpoint,
|
||||
}, func() {
|
||||
err := container.Terminate(ctx)
|
||||
if err != nil {
|
||||
@@ -126,11 +105,38 @@ func CreateQueue(t *testing.T, ctx context.Context, cfg *CreateQueueConfig) (*Qu
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg.Cfg.QueueClient = sqs.NewFromConfig(sqsCfg)
|
||||
|
||||
return clean
|
||||
}
|
||||
|
||||
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),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return *queueM.QueueUrl
|
||||
}
|
||||
|
||||
func AssertMessageWait(t *testing.T, ctx context.Context, cfg *queue.Config, attrs []string) types.Message {
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
result, err := queue.Receive(ctx, cfg, attrs)
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, result.Messages)
|
||||
assert.Len(t, result.Messages, 1)
|
||||
assert.NotNil(t, result.Messages[0])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user