diff --git a/docs/modules/dynamodb.md b/docs/modules/dynamodb.md new file mode 100644 index 000000000..8ae64ea2b --- /dev/null +++ b/docs/modules/dynamodb.md @@ -0,0 +1,29 @@ +# DynamoDB + +## Install + +```bash +npm install @testcontainers/dynamodb --save-dev +``` + +## Examples + +These examples use the following libraries: + +- [@aws-sdk/client-dynamodb](https://www.npmjs.com/package/@aws-sdk/client-dynamodb) + + npm install @aws-sdk/client-dynamodb + +Choose an image from the [container registry](https://hub.docker.com/r/amazon/dynamodb-local) and substitute `IMAGE`. + +### Connect to local endpoint + + +[](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:connectEndpoint + + +### Use a shared database + + +[](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:sharedDB + diff --git a/mkdocs.yml b/mkdocs.yml index 7e70bc96e..4a2dd3027 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -64,6 +64,7 @@ nav: - CosmosDB: modules/cosmosdb.md - Couchbase: modules/couchbase.md - CouchDB: modules/couchdb.md + - DynamoDB: modules/dynamodb.md - Elasticsearch: modules/elasticsearch.md - Etcd: modules/etcd.md - GCloud: modules/gcloud.md diff --git a/package-lock.json b/package-lock.json index 6a5c74e9a..e2d7d9f84 100644 --- a/package-lock.json +++ b/package-lock.json @@ -61,6 +61,28 @@ "node": ">=20.0.0" } }, + "node_modules/@aws-sdk/client-dynamodb": { + "version": "3.1168.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-dynamodb/-/client-dynamodb-3.1168.0.tgz", + "integrity": "sha512-pIuS8zdk0RimAlCQn8xVyO78Ohyl2aeH/2ADAsqO25qFyGqq1Vfa+DuLnzpLQCUXgJr0Zaza1aCaRNXWdyfZ7g==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.0", + "@aws-sdk/credential-provider-node": "^3.972.97", + "@aws-sdk/dynamodb-codec": "^3.973.58", + "@aws-sdk/middleware-endpoint-discovery": "^3.972.39", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.40.0", + "@smithy/fetch-http-handler": "^5.8.1", + "@smithy/node-http-handler": "^4.14.2", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, "node_modules/@aws-sdk/client-s3": { "version": "3.1167.0", "resolved": "https://registry.npmjs.org/@aws-sdk/client-s3/-/client-s3-3.1167.0.tgz", @@ -260,6 +282,53 @@ "node": ">=20.0.0" } }, + "node_modules/@aws-sdk/dynamodb-codec": { + "version": "3.973.58", + "resolved": "https://registry.npmjs.org/@aws-sdk/dynamodb-codec/-/dynamodb-codec-3.973.58.tgz", + "integrity": "sha512-OrqMNzvjk37ssH/qoD6Cd25TLk2cT1lCMwCNM+frwbOQAKJuAbFSgH6OOtaI71l5fA8F7GPuMr9Ck57SAtY98g==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.0", + "@smithy/core": "^3.40.0", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/endpoint-cache": { + "version": "3.972.11", + "resolved": "https://registry.npmjs.org/@aws-sdk/endpoint-cache/-/endpoint-cache-3.972.11.tgz", + "integrity": "sha512-8q1ICxcDjHId3bBryuu/j+1L9y5/3uQnwzLDt5j2ElcjZSoWmFtymdJy7OjLrluSMe0Z4mq5bcH4fxBXvlEHfw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "mnemonist": "0.38.3", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/middleware-endpoint-discovery": { + "version": "3.972.39", + "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-endpoint-discovery/-/middleware-endpoint-discovery-3.972.39.tgz", + "integrity": "sha512-AtZS+UK7CciPbdiszPoG08A23kUojFxFnWIv155uPieSPcHR9D9Sovtp19U0j5qmD2DzHpG9OI7mb0Kiyvf3mA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/endpoint-cache": "^3.972.11", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.40.0", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, "node_modules/@aws-sdk/middleware-sdk-s3": { "version": "3.972.89", "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-sdk-s3/-/middleware-sdk-s3-3.972.89.tgz", @@ -5007,6 +5076,10 @@ "resolved": "packages/modules/couchdb", "link": true }, + "node_modules/@testcontainers/dynamodb": { + "resolved": "packages/modules/dynamodb", + "link": true + }, "node_modules/@testcontainers/elasticsearch": { "resolved": "packages/modules/elasticsearch", "link": true @@ -10574,6 +10647,16 @@ "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==", "license": "MIT" }, + "node_modules/mnemonist": { + "version": "0.38.3", + "resolved": "https://registry.npmjs.org/mnemonist/-/mnemonist-0.38.3.tgz", + "integrity": "sha512-2K9QYubXx/NAjv4VLq1d1Ly8pWNC5L3BrixtdkyTegXWJIqY+zLNDhhX/A+ZwWt70tB1S8H4BE8FLYEFyNoOBw==", + "dev": true, + "license": "MIT", + "dependencies": { + "obliterator": "^1.6.1" + } + }, "node_modules/mockserver-client": { "version": "9.2.0", "resolved": "https://registry.npmjs.org/mockserver-client/-/mockserver-client-9.2.0.tgz", @@ -11447,6 +11530,13 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/obliterator": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/obliterator/-/obliterator-1.6.1.tgz", + "integrity": "sha512-9WXswnqINnnhOG/5SLimUlzuU1hFJUc8zkwyD59Sd+dPOMf05PmnYG/d6Q7HZ+KmgkZJa1PxRso6QdM3sTNHig==", + "dev": true, + "license": "MIT" + }, "node_modules/obug": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/obug/-/obug-3.0.0.tgz", @@ -14736,6 +14826,17 @@ "nano": "^11.0.8" } }, + "packages/modules/dynamodb": { + "name": "@testcontainers/dynamodb", + "version": "12.2.1", + "license": "MIT", + "dependencies": { + "testcontainers": "^12.2.1" + }, + "devDependencies": { + "@aws-sdk/client-dynamodb": "^3.1168.0" + } + }, "packages/modules/elasticsearch": { "name": "@testcontainers/elasticsearch", "version": "12.2.2", diff --git a/packages/modules/dynamodb/Dockerfile b/packages/modules/dynamodb/Dockerfile new file mode 100644 index 000000000..4fc11a57d --- /dev/null +++ b/packages/modules/dynamodb/Dockerfile @@ -0,0 +1 @@ +FROM amazon/dynamodb-local:3.3.2 diff --git a/packages/modules/dynamodb/package.json b/packages/modules/dynamodb/package.json new file mode 100644 index 000000000..d0e5ece41 --- /dev/null +++ b/packages/modules/dynamodb/package.json @@ -0,0 +1,37 @@ +{ + "name": "@testcontainers/dynamodb", + "version": "12.2.1", + "license": "MIT", + "keywords": [ + "dynamodb", + "testing", + "docker", + "testcontainers" + ], + "description": "DynamoDB module for Testcontainers", + "homepage": "https://github.com/testcontainers/testcontainers-node#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/testcontainers/testcontainers-node.git" + }, + "bugs": { + "url": "https://github.com/testcontainers/testcontainers-node/issues" + }, + "main": "build/index.js", + "files": [ + "build" + ], + "publishConfig": { + "access": "public" + }, + "scripts": { + "prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .", + "build": "tsc --project tsconfig.build.json" + }, + "dependencies": { + "testcontainers": "^12.2.1" + }, + "devDependencies": { + "@aws-sdk/client-dynamodb": "^3.1168.0" + } +} diff --git a/packages/modules/dynamodb/src/dynamodb-container.test.ts b/packages/modules/dynamodb/src/dynamodb-container.test.ts new file mode 100644 index 000000000..55008cc38 --- /dev/null +++ b/packages/modules/dynamodb/src/dynamodb-container.test.ts @@ -0,0 +1,157 @@ +import { + CreateTableCommand, + DynamoDBClient, + GetItemCommand, + ListTablesCommand, + PutItemCommand, + waitUntilTableExists, +} from "@aws-sdk/client-dynamodb"; +import { getImage } from "../../../testcontainers/src/utils/test-helper"; +import { DynamoDBContainer } from "./dynamodb-container"; + +const IMAGE = getImage(__dirname); + +async function createTestTable(client: DynamoDBClient): Promise<{ + addUser: (userId: number, name: string) => Promise; + getUser: (userId: number) => Promise; +}> { + const command = new CreateTableCommand({ + TableName: "users", + BillingMode: "PAY_PER_REQUEST", + AttributeDefinitions: [ + { + AttributeName: "userId", + AttributeType: "N", + }, + ], + KeySchema: [ + { + AttributeName: "userId", + KeyType: "HASH", + }, + ], + }); + + await client.send(command); + await waitUntilTableExists({ client, maxWaitTime: 30 }, { TableName: "users" }); + + return { + addUser: async (userId, name) => { + const putCommand = new PutItemCommand({ + TableName: "users", + Item: { + userId: { N: userId.toString() }, + name: { S: name }, + }, + }); + await client.send(putCommand); + }, + getUser: async (userId) => { + const getCommand = new GetItemCommand({ + TableName: "users", + Key: { + userId: { N: userId.toString() }, + }, + ConsistentRead: true, + }); + const result = await client.send(getCommand); + return result.Item?.name?.S ?? null; + }, + }; +} + +function createClient(endpoint: string, accessKeyId: string = "AKIAIOSFODNN7EXAMPLE"): DynamoDBClient { + return new DynamoDBClient({ + region: "us-east-1", + credentials: { + accessKeyId, + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + endpoint, + }); +} + +describe("DynamoDBContainer", { timeout: 180_000 }, () => { + it("connects with endpoint", async () => { + // connectEndpoint { + await using container = await new DynamoDBContainer(IMAGE).start(); + const client = new DynamoDBClient({ + region: "us-east-1", + credentials: { + // use any credentials + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + endpoint: container.getEndpoint(), + }); + + const command = new ListTablesCommand({}); + const result = await client.send(command); + + expect(result.TableNames).toStrictEqual([]); + // } + }); + + it("stores data", async () => { + await using container = await new DynamoDBContainer(IMAGE).start(); + const client = createClient(container.getEndpoint()); + + const { addUser, getUser } = await createTestTable(client); + await addUser(1, "Bob"); + await addUser(2, "Alice"); + + const userA = await getUser(1); + const userB = await getUser(2); + expect(userA).toBe("Bob"); + expect(userB).toBe("Alice"); + }); + + it("separates data by client by default", async () => { + await using container = await new DynamoDBContainer(IMAGE).start(); + + const clientA = createClient(container.getEndpoint(), "AKIAIOSFODNN7EXAMPL1"); + const clientB = createClient(container.getEndpoint(), "AKIAIOSFODNN7EXAMPL2"); + + await createTestTable(clientA); + + const resultA = await clientA.send(new ListTablesCommand()); + const resultB = await clientB.send(new ListTablesCommand()); + + expect(resultA.TableNames).toStrictEqual(["users"]); + expect(resultB.TableNames).toStrictEqual([]); + }); + + it("uses a shared table when requested", async () => { + // sharedDB { + await using container = await new DynamoDBContainer(IMAGE).withSharedDB().start(); + + // Both clients will see the same data even though the + // ACCESS_KEY_ID differ + const clientA = new DynamoDBClient({ + region: "us-east-1", + endpoint: container.getEndpoint(), + credentials: { + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + }); + + const clientB = new DynamoDBClient({ + region: "us-east-1", + endpoint: container.getEndpoint(), + credentials: { + accessKeyId: "AKIAIOSFODNN7EXAMPL2", + secretAccessKey: "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", + }, + }); + // } + + await createTestTable(clientA); + + const resultA = await clientA.send(new ListTablesCommand()); + const resultB = await clientB.send(new ListTablesCommand()); + + expect(resultA.TableNames).toStrictEqual(["users"]); + expect(resultB.TableNames).toStrictEqual(["users"]); + }); +}); diff --git a/packages/modules/dynamodb/src/dynamodb-container.ts b/packages/modules/dynamodb/src/dynamodb-container.ts new file mode 100644 index 000000000..4a57458e9 --- /dev/null +++ b/packages/modules/dynamodb/src/dynamodb-container.ts @@ -0,0 +1,40 @@ +import { AbstractStartedContainer, GenericContainer, Wait } from "testcontainers"; + +// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.Endpoint +const DYNAMODB_PORT = 8000; + +export class DynamoDBContainer extends GenericContainer { + private sharedDB: boolean = false; + + constructor(image: string) { + super(image); + this.withExposedPorts(DYNAMODB_PORT) + .withStartupTimeout(120_000) + .withWaitStrategy(Wait.forHttp("/", DYNAMODB_PORT).forStatusCode(400)) + .withEnvironment({ + DDB_LOCAL_TELEMETRY: "0", + }); + } + + public withSharedDB(): this { + this.sharedDB = true; + return this; + } + + public override async start(): Promise { + this.withCommand(["-jar", "DynamoDBLocal.jar", "-inMemory", ...(this.sharedDB ? ["-sharedDb"] : [])]); + + const started = await super.start(); + return new StartedDynamoDBContainer(started); + } +} + +export class StartedDynamoDBContainer extends AbstractStartedContainer { + public getPort(): number { + return this.startedTestContainer.getMappedPort(DYNAMODB_PORT); + } + + public getEndpoint(): string { + return `http://${this.getHost()}:${this.getPort()}`; + } +} diff --git a/packages/modules/dynamodb/src/index.ts b/packages/modules/dynamodb/src/index.ts new file mode 100644 index 000000000..9c6beccd5 --- /dev/null +++ b/packages/modules/dynamodb/src/index.ts @@ -0,0 +1 @@ +export { DynamoDBContainer, StartedDynamoDBContainer } from "./dynamodb-container"; diff --git a/packages/modules/dynamodb/tsconfig.build.json b/packages/modules/dynamodb/tsconfig.build.json new file mode 100644 index 000000000..91dc68846 --- /dev/null +++ b/packages/modules/dynamodb/tsconfig.build.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "exclude": ["build", "src/**/*.test.ts"], + "references": [ + { + "path": "../../testcontainers" + } + ] +} diff --git a/packages/modules/dynamodb/tsconfig.json b/packages/modules/dynamodb/tsconfig.json new file mode 100644 index 000000000..a06097b42 --- /dev/null +++ b/packages/modules/dynamodb/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build", + "paths": { + "testcontainers": ["../../testcontainers/src"] + } + }, + "exclude": ["build"], + "references": [ + { + "path": "../../testcontainers" + } + ] +}