Skip to content
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
14 changes: 12 additions & 2 deletions packages/modules/localstack/src/localstack-container.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LOCALSTACK_PORT, LocalstackContainer } from "./localstack-container";
import { HeadBucketCommand, S3Client, CreateBucketCommand } from "@aws-sdk/client-s3";
import { CreateBucketCommand, HeadBucketCommand, S3Client } from "@aws-sdk/client-s3";
import { GenericContainer, log, Network, StartedTestContainer } from "testcontainers";
import { LABEL_TESTCONTAINERS_SESSION_ID } from "testcontainers/src/utils/labels";
import { LocalstackContainer, LOCALSTACK_PORT } from "./localstack-container";

const runAwsCliAgainstDockerNetworkContainer = async (
command: string,
Expand Down Expand Up @@ -103,4 +104,13 @@ describe("LocalStackContainer", () => {

await container.stop();
});

it("should add LAMBDA_DOCKER_FLAGS with sessionId label", async () => {
const container = await new LocalstackContainer().start();
const sessionId = container.getLabels()[LABEL_TESTCONTAINERS_SESSION_ID];

const { output, exitCode } = await container.exec(["printenv", "LAMBDA_DOCKER_FLAGS"]);
expect(exitCode).toBe(0);
expect(output).toContain(`${LABEL_TESTCONTAINERS_SESSION_ID}=${sessionId}`);
});
});
23 changes: 22 additions & 1 deletion packages/modules/localstack/src/localstack-container.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { AbstractStartedContainer, GenericContainer, log, StartedTestContainer, Wait } from "testcontainers";
import {
AbstractStartedContainer,
GenericContainer,
getContainerRuntimeClient,
log,
StartedTestContainer,
Wait,
} from "testcontainers";
import { getReaper } from "testcontainers/src/reaper/reaper";
import { LABEL_TESTCONTAINERS_SESSION_ID } from "testcontainers/src/utils/labels";

export const LOCALSTACK_PORT = 4566;

export class LocalstackContainer extends GenericContainer {
constructor(image = "localstack/localstack:2.2.0") {
super(image);

this.withExposedPorts(LOCALSTACK_PORT).withWaitStrategy(Wait.forLogMessage("Ready", 1)).withStartupTimeout(120_000);
}

Expand All @@ -25,8 +35,19 @@ export class LocalstackContainer extends GenericContainer {
log.info(`${envVar} environment variable set to "${this.environment[envVar]}" (${hostnameExternalReason})`);
}

/**
* Configure the LocalStack container to add sessionId when starting lambda container.
* This allows the lambda container to be identified by the {@link Reaper} and cleaned up on exit.
*/
private async flagLambdaSessionId(): Promise<void> {
const client = await getContainerRuntimeClient();
const reaper = await getReaper(client);
this.withEnvironment({ LAMBDA_DOCKER_FLAGS: `-l ${LABEL_TESTCONTAINERS_SESSION_ID}=${reaper.sessionId}` });
}

protected override async beforeContainerCreated(): Promise<void> {
this.resolveHostname();
await this.flagLambdaSessionId();
}

public override async start(): Promise<StartedLocalStackContainer> {
Expand Down