Skip to content

Commit db61b6a

Browse files
Merge branch 'main' into npm-audit-11-03
2 parents 01013e4 + b31edb8 commit db61b6a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/modules/localstack/src/localstack-container.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CreateBucketCommand, HeadBucketCommand, S3Client } from "@aws-sdk/client-s3";
22
import { GenericContainer, log, Network, StartedTestContainer } from "testcontainers";
3+
import { LABEL_TESTCONTAINERS_SESSION_ID } from "testcontainers/src/utils/labels";
34
import { LocalstackContainer, LOCALSTACK_PORT } from "./localstack-container";
45

56
const runAwsCliAgainstDockerNetworkContainer = async (
@@ -101,4 +102,13 @@ describe("LocalStackContainer", { timeout: 180_000 }, () => {
101102

102103
await container.stop();
103104
});
105+
106+
it("should add LAMBDA_DOCKER_FLAGS with sessionId label", async () => {
107+
const container = await new LocalstackContainer().start();
108+
const sessionId = container.getLabels()[LABEL_TESTCONTAINERS_SESSION_ID];
109+
110+
const { output, exitCode } = await container.exec(["printenv", "LAMBDA_DOCKER_FLAGS"]);
111+
expect(exitCode).toBe(0);
112+
expect(output).toContain(`${LABEL_TESTCONTAINERS_SESSION_ID}=${sessionId}`);
113+
});
104114
});

packages/modules/localstack/src/localstack-container.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
import { AbstractStartedContainer, GenericContainer, log, StartedTestContainer, Wait } from "testcontainers";
1+
import {
2+
AbstractStartedContainer,
3+
GenericContainer,
4+
getContainerRuntimeClient,
5+
log,
6+
StartedTestContainer,
7+
Wait,
8+
} from "testcontainers";
9+
import { getReaper } from "testcontainers/src/reaper/reaper";
10+
import { LABEL_TESTCONTAINERS_SESSION_ID } from "testcontainers/src/utils/labels";
211

312
export const LOCALSTACK_PORT = 4566;
413

514
export class LocalstackContainer extends GenericContainer {
615
constructor(image = "localstack/localstack:2.2.0") {
716
super(image);
17+
818
this.withExposedPorts(LOCALSTACK_PORT).withWaitStrategy(Wait.forLogMessage("Ready", 1)).withStartupTimeout(120_000);
919
}
1020

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

38+
/**
39+
* Configure the LocalStack container to add sessionId when starting lambda container.
40+
* This allows the lambda container to be identified by the {@link Reaper} and cleaned up on exit.
41+
*/
42+
private async flagLambdaSessionId(): Promise<void> {
43+
const client = await getContainerRuntimeClient();
44+
const reaper = await getReaper(client);
45+
this.withEnvironment({ LAMBDA_DOCKER_FLAGS: `-l ${LABEL_TESTCONTAINERS_SESSION_ID}=${reaper.sessionId}` });
46+
}
47+
2848
protected override async beforeContainerCreated(): Promise<void> {
2949
this.resolveHostname();
50+
await this.flagLambdaSessionId();
3051
}
3152

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

0 commit comments

Comments
 (0)