-
-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathlocalstack-container.test.ts
More file actions
128 lines (106 loc) · 4.6 KB
/
localstack-container.test.ts
File metadata and controls
128 lines (106 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { CreateBucketCommand, HeadBucketCommand, S3Client } from "@aws-sdk/client-s3";
import { GenericContainer, LABEL_TESTCONTAINERS_SESSION_ID, log, Network, StartedTestContainer } from "testcontainers";
import { LocalstackContainer, LOCALSTACK_PORT } from "./localstack-container";
const runAwsCliAgainstDockerNetworkContainer = async (
command: string,
awsCliInDockerNetwork: StartedTestContainer
): Promise<string> => {
const commandParts = `/usr/local/bin/aws --region eu-west-1 ${command} --endpoint-url http://localstack:${LOCALSTACK_PORT} --no-verify-ssl`;
const execResult = await awsCliInDockerNetwork.exec(commandParts);
expect(execResult.exitCode).toEqual(0);
log.info(execResult.output);
return execResult.output;
};
describe("LocalStackContainer", { timeout: 180_000 }, () => {
// createS3Bucket {
it("should create a S3 bucket", async () => {
const container = await new LocalstackContainer().start();
const client = new S3Client({
endpoint: container.getConnectionUri(),
forcePathStyle: true,
region: "us-east-1",
credentials: {
secretAccessKey: "test",
accessKeyId: "test",
},
});
const input = {
Bucket: "testcontainers",
};
const command = new CreateBucketCommand(input);
const createBucketResponse = await client.send(command);
expect(createBucketResponse.$metadata.httpStatusCode).toEqual(200);
const headBucketResponse = await client.send(new HeadBucketCommand(input));
expect(headBucketResponse.$metadata.httpStatusCode).toEqual(200);
await container.stop();
});
// }
it("should use custom network", async () => {
const network = await new Network().start();
const container = await new LocalstackContainer()
.withNetwork(network)
.withNetworkAliases("notthis", "localstack") // the last alias is used for HOSTNAME_EXTERNAL
.start();
const awsCliInDockerNetwork = await new GenericContainer("amazon/aws-cli:2.7.27")
.withNetwork(network)
.withEntrypoint(["bash"])
.withCommand(["-c", "sleep infinity"])
.withEnvironment({
AWS_ACCESS_KEY_ID: "test",
AWS_SECRET_ACCESS_KEY: "test",
AWS_REGION: "us-east-1",
})
.start();
const response = await runAwsCliAgainstDockerNetworkContainer(
"sqs create-queue --queue-name baz",
awsCliInDockerNetwork
);
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();
});
it("should add LAMBDA_DOCKER_FLAGS with sessionId label", async () => {
const container = await new LocalstackContainer().start();
const sessionId = container.getLabels()[LABEL_TESTCONTAINERS_SESSION_ID];
const { output, exitCode } = await container.exec(["printenv", "LAMBDA_DOCKER_FLAGS"]);
expect(exitCode).toBe(0);
expect(output).toContain(`${LABEL_TESTCONTAINERS_SESSION_ID}=${sessionId}`);
});
it("should concatenate sessionId label to LAMBDA_DOCKER_FLAGS", async () => {
const container = await new LocalstackContainer()
.withEnvironment({
LAMBDA_DOCKER_FLAGS: `-l mylabel=myvalue`,
})
.start();
const sessionId = container.getLabels()[LABEL_TESTCONTAINERS_SESSION_ID];
const { output, exitCode } = await container.exec(["printenv", "LAMBDA_DOCKER_FLAGS"]);
expect(exitCode).toBe(0);
expect(output).toContain(`${LABEL_TESTCONTAINERS_SESSION_ID}=${sessionId}`);
expect(output).toContain(`mylabel=myvalue`);
});
});