@testcontainers/localstack
Version:
LocalStack module for Testcontainers
64 lines • 2.92 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.StartedLocalStackContainer = exports.LocalstackContainer = exports.LOCALSTACK_PORT = void 0;
const testcontainers_1 = require("testcontainers");
exports.LOCALSTACK_PORT = 4566;
class LocalstackContainer extends testcontainers_1.GenericContainer {
constructor(image = "localstack/localstack:2.2.0") {
super(image);
this.withExposedPorts(exports.LOCALSTACK_PORT).withWaitStrategy(testcontainers_1.Wait.forLogMessage("Ready", 1)).withStartupTimeout(120000);
}
resolveHostname() {
const envVar = "LOCALSTACK_HOST";
let hostnameExternalReason;
if (this.environment[envVar]) {
// do nothing
hostnameExternalReason = "explicitly as environment variable";
}
else if (this.networkAliases && this.networkAliases.length > 0) {
// use the last network alias set
this.withEnvironment({ [envVar]: this.networkAliases.at(this.networkAliases.length - 1) ?? "" });
hostnameExternalReason = "to match last network alias on container with non-default network";
}
else {
this.withEnvironment({ [envVar]: "localhost" });
hostnameExternalReason = "to match host-routable address for container";
}
testcontainers_1.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.
*/
async flagLambdaSessionId() {
const client = await (0, testcontainers_1.getContainerRuntimeClient)();
const reaper = await (0, testcontainers_1.getReaper)(client);
this.withEnvironment({
LAMBDA_DOCKER_FLAGS: `${this.environment["LAMBDA_DOCKER_FLAGS"] ?? ""} -l ${testcontainers_1.LABEL_TESTCONTAINERS_SESSION_ID}=${reaper.sessionId}`,
});
}
async beforeContainerCreated() {
this.resolveHostname();
await this.flagLambdaSessionId();
}
async start() {
return new StartedLocalStackContainer(await super.start());
}
}
exports.LocalstackContainer = LocalstackContainer;
class StartedLocalStackContainer extends testcontainers_1.AbstractStartedContainer {
constructor(startedTestContainer) {
super(startedTestContainer);
}
getPort() {
return this.startedTestContainer.getMappedPort(exports.LOCALSTACK_PORT);
}
/**
* @returns A connection URI in the form of `http://host:port`
*/
getConnectionUri() {
return `http://${this.getHost()}:${this.getPort().toString()}`;
}
}
exports.StartedLocalStackContainer = StartedLocalStackContainer;
//# sourceMappingURL=localstack-container.js.map
;