Skip to content

Commit

Permalink
Fix LocalStack container hostname resolution (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
joebowbeer authored Sep 11, 2024
1 parent f8f82a5 commit 0e4a798
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
34 changes: 34 additions & 0 deletions packages/modules/localstack/src/localstack-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,39 @@ describe("LocalStackContainer", () => {
expect(response).toContain(`http://localstack:${LOCALSTACK_PORT}`);
await container.stop();
await awsCliInDockerNetwork.stop();
await network.stop();
});

it("should not override LOCALSTACK_HOST assignment", async () => {
const container = await new LocalstackContainer()
.withEnvironment({ LOCALSTACK_HOST: "myhost" })
.withNetworkAliases("myalias")
.start();

const { output, exitCode } = await container.exec(["printenv", "LOCALSTACK_HOST"]);
expect(exitCode).toBe(0);
expect(output).toContain("myhost");

await container.stop();
});

it("should override LOCALSTACK_HOST with last network alias", async () => {
const container = await new LocalstackContainer().withNetworkAliases("other", "myalias").start();

const { output, exitCode } = await container.exec(["printenv", "LOCALSTACK_HOST"]);
expect(exitCode).toBe(0);
expect(output).toContain("myalias");

await container.stop();
});

it("should assign LOCALSTACK_HOST to localhost", async () => {
const container = await new LocalstackContainer().start();

const { output, exitCode } = await container.exec(["printenv", "LOCALSTACK_HOST"]);
expect(exitCode).toBe(0);
expect(output).toContain("localhost");

await container.stop();
});
});
12 changes: 8 additions & 4 deletions packages/modules/localstack/src/localstack-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export const LOCALSTACK_PORT = 4566;
export class LocalstackContainer extends GenericContainer {
constructor(image = "localstack/localstack:2.2.0") {
super(image);
this.resolveHostname();
this.withExposedPorts(LOCALSTACK_PORT).withWaitStrategy(Wait.forLogMessage("Ready", 1)).withStartupTimeout(120_000);
}

Expand All @@ -16,13 +15,18 @@ export class LocalstackContainer extends GenericContainer {
// do nothing
hostnameExternalReason = "explicitly as environment variable";
} else if (this.networkAliases && this.networkAliases.length > 0) {
this.environment[envVar] = this.networkAliases.at(this.networkAliases.length - 1) || ""; // use the last network alias set
// 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({ LOCALSTACK_HOST: "localhost" });
this.withEnvironment({ [envVar]: "localhost" });
hostnameExternalReason = "to match host-routable address for container";
}
log.info(`${envVar} environment variable set to ${this.environment[envVar]} (${hostnameExternalReason})"`);
log.info(`${envVar} environment variable set to "${this.environment[envVar]}" (${hostnameExternalReason})`);
}

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

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

0 comments on commit 0e4a798

Please sign in to comment.