Skip to content

Commit

Permalink
feat: testcontainers#701 Align Localstack module API on Java version …
Browse files Browse the repository at this point in the history
…- Add environment variables retrieval after container startup

Signed-off-by: Laurent Broudoux <[email protected]>
  • Loading branch information
lbroudoux committed Jan 5, 2024
1 parent 916b02f commit 6ef49e0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
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'
})
.start();

expect(container.getRegion()).toBe('eu-west-3');
expect(container.getAccessKey()).toBe('test');
expect(container.getSecretKey()).toBe('test');

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>
* @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

0 comments on commit 6ef49e0

Please sign in to comment.