testcontainerslocalstackjunit-jupiter

LocalStack TestContainer unable to match logs using WaitForLog


I'm trying to start a localstack container, this container launches a script at startup to initialize an S3 bucket, a SQS queue and a DynamoDB Table with a GSI.

Here I'm using a waitStrategy Wait.forLogMessage

But the regex never catches the message so the startup fails.

Here my container:

@Container
static LocalStackContainer localStack =
        new LocalStackContainer(DockerImageName.parse("localstack/localstack"))
                .withFileSystemBind("src/test/resources/localstack/sqs_bootstrap.sh",
                        "/etc/localstack/init/ready.d/sqs_bootstrap.sh")
                .withServices(SQS, S3, DYNAMODB)
                .withEnv("DEBUG", "1")
                //.withLogConsumer((log) -> System.out.println(log.getUtf8String()))
                .withStartupTimeout(Duration.ofMinutes(1))
                .waitingFor(
                        Wait.forLogMessage("localStack Ready to accept connections", 3)
                );

Here is my script:

#!/usr/bin/env bash
echo "start localstack configuration."

set -euo pipefail

# enable debug
# set -x


echo "==================="
LOCALSTACK_HOST=localhost
AWS_REGION=eu-west-1

create_queue() {
    local QUEUE_NAME_TO_CREATE=$1
    awslocal sqs create-queue --queue-name ${QUEUE_NAME_TO_CREATE}
}

create_s3(){
    local BUCKET_NAME_TO_CREATE=$1
    awslocal s3api create-bucket --bucket ${BUCKET_NAME_TO_CREATE}
}

create_table(){
  local TABLE_NAME=$1
  local PARTITION_KEY=$2
  local PARTITION_KEY_TYPE=$3

  awslocal dynamodb create-table --table-name ${TABLE_NAME} --key-schema AttributeName=${PARTITION_KEY},KeyType=HASH \
    --attribute-definitions AttributeName=${PARTITION_KEY},AttributeType=${PARTITION_KEY_TYPE} \
    --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1

    # Add the global secondary index to the table
    awslocal dynamodb update-table --table-name ${TABLE_NAME} \
    --attribute-definitions AttributeName=searchKey,AttributeType=S \
    --global-secondary-index-updates '[{ "Create": { "IndexName": "searchIndex", "KeySchema": [{ "AttributeName": "searchKey", "KeyType": "HASH" }], "Projection": { "ProjectionType": "ALL" }, "ProvisionedThroughput": { "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 } } }]'
}

echo "configuring sqs"
create_queue "oagQueue"

echo "configuring s3"
create_s3 "pss"

echo "configuring flightSchedule Table"
create_table "ps_oag" "pk" "S"

echo "localStack Ready to accept connections"

I've tried different patterns, but nothing works. I also tried a healthCheck Strategy, my container startups correctly, but then my test fails cause the test under code tries to connect to the localstack container during the script execution.

Any help appreciated.

Have a nice day


Solution

  • Can you please try using .*localStack Ready to accept connections.*\\n? I assume that method is looking for a specific pattern when it parses that String. Shortly breaking it down, we have:

    Here are the docs where they have an example: https://java.testcontainers.org/features/startup_and_waits/