Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalstackContainer API enhancement #702

Closed
Closed
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: 14 additions & 0 deletions packages/modules/localstack/src/localstack-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,18 @@ describe("LocalStackContainer", () => {
await container.stop();
await awsCliInDockerNetwork.stop();
});

it("should use access environment variables and default creds", async () => {
const container = await new LocalstackContainer()
.withEnvironment({
DEFAULT_REGION: "eu-west-3",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the DEFAULT_REGION convention come from? I had a quick look at the docs and found https://docs.localstack.cloud/references/configuration/#legacy, which says that it's no longer supported.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you're right. Many things have changed with 3.x and I should have refreshed my knowledge. The point is the default image is still localstack/localstack:2.2.0. As multi-region has been introduced incrementally, this may not affect the module, but it may be nice to upgrade to 3.x to definitely get rid of this question. What do you think?

})
.start();

expect(container.getRegion()).toBe("eu-west-3");
expect(container.getAccessKey()).toBe("test");
expect(container.getSecretKey()).toBe("test");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The container is mapping the environment variables to these getter methods, but this test is not checking that LS is actually using them. From the deprecation notices in the docs I get the impression they're not being used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you're right... I see nothing in current documentation referencing these... Here again, I tried to reproduce the behavior of the test containers-java/local stack module, but this may be inappropriate.


await container.stop();
});
});
30 changes: 30 additions & 0 deletions packages/modules/localstack/src/localstack-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { AbstractStartedContainer, GenericContainer, log, StartedTestContainer,

export const LOCALSTACK_PORT = 4566;

const DEFAULT_REGION = "us-east-1";
const DEFAULT_AWS_ACCESS_KEY_ID = "test";
const DEFAULT_AWS_SECRET_ACCESS_KEY = "test";

export class LocalstackContainer extends GenericContainer {
constructor(image = "localstack/localstack:2.2.0") {
super(image);
Expand Down Expand Up @@ -50,4 +54,30 @@ export class StartedLocalStackContainer extends AbstractStartedContainer {
public getConnectionUri(): string {
return `http://${this.getHost()}:${this.getPort().toString()}`;
}

/**
* Provides a default region that is preconfigured to communicate with a given simulated service.
* @returns A default region
*/
public getRegion(): string {
return this.startedTestContainer.getEnvironment()["DEFAULT_REGION"] || DEFAULT_REGION;
}

/**
* Provides a default access key that is preconfigured to communicate with a given simulated service.
* <a href="https://github.com/localstack/localstack/blob/master/doc/interaction/README.md?plain=1#L32">AWS Access Key</a>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting a 404 for these URLs. I also couldn't find reference to these environment variables in the LS config

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I blindly copy/paste here the comments coming from testcontainers-java/localstack

* @returns A default access key
*/
public getAccessKey(): string {
return this.startedTestContainer.getEnvironment()["AWS_ACCESS_KEY_ID"] || DEFAULT_AWS_ACCESS_KEY_ID;
}

/**
* Provides a default secret key that is preconfigured to communicate with a given simulated service.
* <a href="https://github.com/localstack/localstack/blob/master/doc/interaction/README.md?plain=1#L32">AWS Secret Key</a>
* @returns A default secret key
*/
public getSecretKey(): string {
return this.startedTestContainer.getEnvironment()["AWS_SECRET_ACCESS_KEY"] || DEFAULT_AWS_SECRET_ACCESS_KEY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export class AbstractStartedContainer implements StartedTestContainer {
return this.startedTestContainer.getLabels();
}

public getEnvironment(): Record<string, string> {
return this.startedTestContainer.getEnvironment();
}

public getId(): string {
return this.startedTestContainer.getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ export class StartedGenericContainer implements StartedTestContainer {
return this.inspectResult.Config.Labels;
}

public getEnvironment(): Record<string, string> {
const environment: Record<string, string> = {};
this.inspectResult.Config.Env.forEach((env) => {
const kv: string[] = env.split("=");
environment[kv[0]] = kv[1];
});
return environment;
}

public getNetworkNames(): string[] {
return Object.keys(this.getNetworkSettings());
}
Expand Down
1 change: 1 addition & 0 deletions packages/testcontainers/src/test-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface StartedTestContainer {
getMappedPort(port: number): number;
getName(): string;
getLabels(): Labels;
getEnvironment(): Record<string, string>;
getId(): string;
getNetworkNames(): string[];
getNetworkId(networkName: string): string;
Expand Down
Loading