|
| 1 | +import { LOCALSTACK_PORT, LocalstackContainer } from "./localstack-container"; |
| 2 | +import { HeadBucketCommand, S3Client, CreateBucketCommand } from "@aws-sdk/client-s3"; |
| 3 | +import { GenericContainer, log, Network, StartedTestContainer } from "testcontainers"; |
| 4 | + |
| 5 | +const runAwsCliAgainstDockerNetworkContainer = async ( |
| 6 | + command: string, |
| 7 | + awsCliInDockerNetwork: StartedTestContainer |
| 8 | +): Promise<string> => { |
| 9 | + const commandParts = `/usr/local/bin/aws --region eu-west-1 ${command} --endpoint-url http://localstack:${LOCALSTACK_PORT} --no-verify-ssl`; |
| 10 | + const execResult = await awsCliInDockerNetwork.exec(commandParts); |
| 11 | + expect(execResult.exitCode).toEqual(0); |
| 12 | + log.info(execResult.output); |
| 13 | + return execResult.output; |
| 14 | +}; |
| 15 | + |
| 16 | +describe("LocalStackContainer", () => { |
| 17 | + jest.setTimeout(180_000); |
| 18 | + |
| 19 | + // createS3Bucket { |
| 20 | + it("should create a S3 bucket", async () => { |
| 21 | + const container = await new LocalstackContainer().start(); |
| 22 | + |
| 23 | + const client = new S3Client({ |
| 24 | + endpoint: container.getConnectionUri(), |
| 25 | + forcePathStyle: true, |
| 26 | + region: "us-east-1", |
| 27 | + credentials: { |
| 28 | + secretAccessKey: "test", |
| 29 | + accessKeyId: "test", |
| 30 | + }, |
| 31 | + }); |
| 32 | + const input = { |
| 33 | + Bucket: "testcontainers", |
| 34 | + }; |
| 35 | + const command = new CreateBucketCommand(input); |
| 36 | + |
| 37 | + const createBucketResponse = await client.send(command); |
| 38 | + expect(createBucketResponse.$metadata.httpStatusCode).toEqual(200); |
| 39 | + const headBucketResponse = await client.send(new HeadBucketCommand(input)); |
| 40 | + expect(headBucketResponse.$metadata.httpStatusCode).toEqual(200); |
| 41 | + |
| 42 | + await container.stop(); |
| 43 | + }); |
| 44 | + // } |
| 45 | + |
| 46 | + it("should use custom network", async () => { |
| 47 | + const network = await new Network().start(); |
| 48 | + const container = await new LocalstackContainer() |
| 49 | + .withNetwork(network) |
| 50 | + .withNetworkAliases("notthis", "localstack") // the last alias is used for HOSTNAME_EXTERNAL |
| 51 | + .start(); |
| 52 | + |
| 53 | + const awsCliInDockerNetwork = await new GenericContainer("amazon/aws-cli:2.7.27") |
| 54 | + .withNetwork(network) |
| 55 | + .withEntrypoint(["bash"]) |
| 56 | + .withCommand(["-c", "echo 'START'; sleep infinity"]) |
| 57 | + .withEnvironment({ |
| 58 | + AWS_ACCESS_KEY_ID: "test", |
| 59 | + AWS_SECRET_ACCESS_KEY: "test", |
| 60 | + AWS_REGION: "us-east-1", |
| 61 | + }) |
| 62 | + .start(); |
| 63 | + |
| 64 | + const response = await runAwsCliAgainstDockerNetworkContainer( |
| 65 | + "sqs create-queue --queue-name baz", |
| 66 | + awsCliInDockerNetwork |
| 67 | + ); |
| 68 | + expect(response).toContain(`http://localstack:${LOCALSTACK_PORT}`); |
| 69 | + await container.stop(); |
| 70 | + await awsCliInDockerNetwork.stop(); |
| 71 | + }); |
| 72 | +}); |
0 commit comments