Search code examples
testcontainerslocalstackjunit-jupiter

localstack testcontainer startup script


I'm trying to use localstack testContainer to create an S3 and SQS localy.

Here is my code to create the test container :

    @Container
static LocalStackContainer localStack =
        new LocalStackContainer(DockerImageName.parse("localstack/localstack"))
                .withClasspathResourceMapping("./localstack_bootstrap/sqs_bootstrap.sh",
                        "/etc/localstack/init/ready.d/sqs_bootstrap.sh", BindMode.READ_ONLY)
                .withServices(SQS)
                .withServices(S3)
                .waitingFor(Wait.forLogMessage("Initialized.", 1));

i tryied also with this configuration :

 .withClasspathResourceMapping("./localstack_bootstrap",
                        "/docker-entrypoint-initaws.d/", BindMode.READ_ONLY)

And here is the script i use to create localstack stuff :

c/test/resources/localstack/init.sh
#!/bin/sh
echo "start creating queue"
awslocal sqs create-queue --queue-name test-order-queue
echo "Initialized."

But it seems the script is not executed so i think i'm doing something wrong but i'm not able to figure out it.

ANy help is welcome


Solution

  • please try using the .withFileSystemBind(hostPath, containerPath) method. The key difference is that the classpath is a runtime concept that helps the JVM find classes and resources, while the file system is where your actual files are stored. Here's a working example. Make sure you keep the /etc/localstack/init/ready.d container path. That's where init hooks go in the ready phase: https://docs.localstack.cloud/references/init-hooks/. Hope this helps.