Skip to content
Merged
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
10 changes: 7 additions & 3 deletions docs/modules/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ npm install @testcontainers/elasticsearch --save-dev

## Examples

These examples use the following libraries:
These examples use the following libraries. Use the client major that matches your Elasticsearch major:

- [@elastic/elasticsearch](https://www.npmjs.com/package/@elastic/elasticsearch)
- [@elastic/elasticsearch 9](https://www.npmjs.com/package/@elastic/elasticsearch) for Elasticsearch 9

npm install @elastic/elasticsearch
npm install @elastic/elasticsearch@9

- [@elastic/elasticsearch 8](https://www.npmjs.com/package/@elastic/elasticsearch/v/8.19.2) for Elasticsearch 8

npm install @elastic/elasticsearch@8

Choose an image from the [container registry](https://hub.docker.com/_/elasticsearch) and substitute `IMAGE`.

Expand Down
66 changes: 65 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/modules/elasticsearch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"build": "tsc --project tsconfig.build.json"
},
"devDependencies": {
"@elastic/elasticsearch": "^9.5.1"
"@elastic/elasticsearch": "^9.5.1",
"@elastic/elasticsearch-8": "npm:@elastic/elasticsearch@^8.19.2"
},
"dependencies": {
"testcontainers": "^12.2.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Client } from "@elastic/elasticsearch-8";
import { ElasticsearchContainer } from "./elasticsearch-container";

const IMAGE = "elasticsearch:8.19.23";

describe("ElasticsearchContainer 8", { concurrent: false, timeout: 180_000 }, () => {
it("should create an index", async () => {
await using container = await new ElasticsearchContainer(IMAGE).start();

const client = new Client({
node: container.getHttpUrl(),
auth: { username: container.getUsername(), password: container.getPassword() },
});

await client.indices.create({ index: "people" });

expect(await client.indices.exists({ index: "people" })).toBe(true);
});

it("should index a document", async () => {
await using container = await new ElasticsearchContainer(IMAGE).start();

const client = new Client({
node: container.getHttpUrl(),
auth: { username: container.getUsername(), password: container.getPassword() },
});

const document = {
id: "1",
name: "John Doe",
};
await client.index({
index: "people",
id: document.id,
document,
});

expect((await client.get({ index: "people", id: document.id }))._source).toStrictEqual(document);
});

it("should set custom password", async () => {
await using container = await new ElasticsearchContainer(IMAGE).withPassword("testPassword").start();

const client = new Client({
node: container.getHttpUrl(),
auth: { username: container.getUsername(), password: container.getPassword() },
});

await client.indices.create({ index: "people" });

expect(await client.indices.exists({ index: "people" })).toBe(true);
});

it("should work with restarted container", async () => {
await using container = await new ElasticsearchContainer(IMAGE).start();
await container.restart();

const client = new Client({
node: container.getHttpUrl(),
auth: { username: container.getUsername(), password: container.getPassword() },
});

await client.indices.create({ index: "people" });

expect(await client.indices.exists({ index: "people" })).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { getImage } from "../../../testcontainers/src/utils/test-helper";
import { ElasticsearchContainer } from "./elasticsearch-container";

const IMAGE = getImage(__dirname);
const images = ["elasticsearch:7.17.28", "elasticsearch:8.18.1", IMAGE];

describe("ElasticsearchContainer", { timeout: 180_000 }, () => {
it.each(images)("should create an index with %s", async (image) => {
describe("ElasticsearchContainer", { concurrent: false, timeout: 180_000 }, () => {
it("should create an index", async () => {
// createIndex {
await using container = await new ElasticsearchContainer(image).start();
await using container = await new ElasticsearchContainer(IMAGE).start();

const client = new Client({
node: container.getHttpUrl(),
Expand Down
28 changes: 14 additions & 14 deletions packages/modules/mongodb/src/mongodb-atlas-local-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IntervalRetry } from "testcontainers";
import { getImage } from "../../../testcontainers/src/utils/test-helper";
import { MongoDBAtlasLocalContainer } from "./mongodb-atlas-local-container";

const IMAGE = getImage(__dirname, 1);
const IMAGES = ["mongodb/mongodb-atlas-local:7.0.43", getImage(__dirname, 1)];
const ATLAS_SEARCH_INDEX = {
mappings: {
dynamic: false,
Expand All @@ -23,28 +23,28 @@ const ATLAS_SEARCH_INDEX = {
},
};

describe("MongoDBAtlasLocalContainer", { timeout: 240_000 }, () => {
it("should provide a connection string", async () => {
describe.each(IMAGES)("MongoDBAtlasLocalContainer (%s)", { concurrent: false, timeout: 240_000 }, (image) => {
it.concurrent("should provide a connection string", async () => {
// connectAtlasLocal {
await using container = await new MongoDBAtlasLocalContainer(IMAGE).start();
await using container = await new MongoDBAtlasLocalContainer(image).start();
// }

expect(container.getConnectionString()).toBe(`mongodb://${container.getHost()}:${container.getMappedPort(27017)}/`);
});

it("should provide a database connection string", async () => {
it.concurrent("should provide a database connection string", async () => {
// connectAtlasLocalDatabase {
await using container = await new MongoDBAtlasLocalContainer(IMAGE).start();
await using container = await new MongoDBAtlasLocalContainer(image).start();
// }

expect(container.getDatabaseConnectionString()).toBe(
`mongodb://${container.getHost()}:${container.getMappedPort(27017)}/test`
);
});

it("should provide connection strings with credentials", async () => {
it.concurrent("should provide connection strings with credentials", async () => {
// connectAtlasLocalWithCredentials {
await using container = await new MongoDBAtlasLocalContainer(IMAGE)
await using container = await new MongoDBAtlasLocalContainer(image)
.withUsername("customUsername")
.withPassword("customPassword")
.start();
Expand All @@ -58,8 +58,8 @@ describe("MongoDBAtlasLocalContainer", { timeout: 240_000 }, () => {
);
});

it("should connect to mongodb atlas local", async () => {
await using container = await new MongoDBAtlasLocalContainer(IMAGE).start();
it.concurrent("should connect to mongodb atlas local", async () => {
await using container = await new MongoDBAtlasLocalContainer(image).start();

const db = await mongoose.createConnection(container.getConnectionString(), { directConnection: true }).asPromise();

Expand All @@ -73,8 +73,8 @@ describe("MongoDBAtlasLocalContainer", { timeout: 240_000 }, () => {
await db.close();
});

it("should connect to mongodb atlas local with credentials", async () => {
await using container = await new MongoDBAtlasLocalContainer(IMAGE)
it.concurrent("should connect to mongodb atlas local with credentials", async () => {
await using container = await new MongoDBAtlasLocalContainer(image)
.withUsername("customUsername")
.withPassword("customPassword")
.start();
Expand All @@ -93,9 +93,9 @@ describe("MongoDBAtlasLocalContainer", { timeout: 240_000 }, () => {
await db.close();
});

it("should create an atlas search index and search it", async () => {
it.concurrent("should create an atlas search index and search it", async () => {
// createAtlasIndexAndSearchIt {
await using atlasLocalContainer = await new MongoDBAtlasLocalContainer(IMAGE).start();
await using atlasLocalContainer = await new MongoDBAtlasLocalContainer(image).start();

const db = await mongoose
.createConnection(atlasLocalContainer.getConnectionString(), {
Expand Down
10 changes: 5 additions & 5 deletions packages/modules/mongodb/src/mongodb-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import mongoose from "mongoose";
import { getImage } from "../../../testcontainers/src/utils/test-helper";
import { MongoDBContainer } from "./mongodb-container";

const IMAGE = getImage(__dirname);
const IMAGES = ["mongo:7.0.43", "mongo:8.3.11", getImage(__dirname)];

describe("MongoDBContainer", { timeout: 240_000 }, () => {
it.each([IMAGE, "mongo:6.0.25", "mongo:4.4.29"])("should work with %s", async (image) => {
describe.each(IMAGES)("MongoDBContainer (%s)", { concurrent: false, timeout: 240_000 }, (image) => {
it.concurrent("should connect", async () => {
// connectMongo {
await using container = await new MongoDBContainer(image).start();

Expand All @@ -22,9 +22,9 @@ describe("MongoDBContainer", { timeout: 240_000 }, () => {
// }
});

it("should connect with credentials", async () => {
it.concurrent("should connect with credentials", async () => {
// connectWithCredentials {
await using container = await new MongoDBContainer(IMAGE)
await using container = await new MongoDBContainer(image)
.withUsername("customUsername")
.withPassword("customPassword")
.start();
Expand Down
27 changes: 13 additions & 14 deletions packages/modules/opensearch/src/opensearch-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { Client } from "@opensearch-project/opensearch";
import { getImage } from "../../../testcontainers/src/utils/test-helper";
import { OpenSearchContainer } from "./opensearch-container";

const IMAGE = getImage(__dirname);
const images = ["opensearchproject/opensearch:2.19.2", IMAGE];
const IMAGES = ["opensearchproject/opensearch:2.19.6", getImage(__dirname)];

describe("OpenSearchContainer", { timeout: 180_000 }, () => {
it.each(images)("should create an index with %s", async (image) => {
describe.each(IMAGES)("OpenSearchContainer (%s)", { concurrent: false, timeout: 180_000 }, (image) => {
it.concurrent("should create an index", async () => {
// opensearchCreateIndex {
await using container = await new OpenSearchContainer(image).start();

Expand All @@ -28,9 +27,9 @@ describe("OpenSearchContainer", { timeout: 180_000 }, () => {
// }
});

it("should index a document", async () => {
it.concurrent("should index a document", async () => {
// opensearchIndexDocument {
await using container = await new OpenSearchContainer(IMAGE).start();
await using container = await new OpenSearchContainer(image).start();

const client = new Client({
node: container.getHttpUrl(),
Expand All @@ -56,8 +55,8 @@ describe("OpenSearchContainer", { timeout: 180_000 }, () => {
// }
});

it("should work with restarted container", async () => {
await using container = await new OpenSearchContainer(IMAGE).start();
it.concurrent("should work with restarted container", async () => {
await using container = await new OpenSearchContainer(image).start();
await container.restart();

const client = new Client({
Expand All @@ -77,13 +76,13 @@ describe("OpenSearchContainer", { timeout: 180_000 }, () => {
expect(existsResponse.body).toBe(true);
});

it("should throw when given an invalid password", () => {
expect(() => new OpenSearchContainer(IMAGE).withPassword("weakpwd")).toThrowError(/Password "weakpwd" is too weak/);
it.concurrent("should throw when given an invalid password", () => {
expect(() => new OpenSearchContainer(image).withPassword("weakpwd")).toThrowError(/Password "weakpwd" is too weak/);
});

it("should set custom password", async () => {
it.concurrent("should set custom password", async () => {
// opensearchCustomPassword {
await using container = await new OpenSearchContainer(IMAGE).withPassword("Str0ng!Passw0rd2025").start();
await using container = await new OpenSearchContainer(image).withPassword("Str0ng!Passw0rd2025").start();
// }

const client = new Client({
Expand All @@ -103,9 +102,9 @@ describe("OpenSearchContainer", { timeout: 180_000 }, () => {
expect(body).toBe(true);
});

it("should be reachable with security disabled", async () => {
it.concurrent("should be reachable with security disabled", async () => {
// opensearchDisableSecurity {
await using container = await new OpenSearchContainer(IMAGE).withSecurityEnabled(false).start();
await using container = await new OpenSearchContainer(image).withSecurityEnabled(false).start();

const client = new Client({
node: container.getHttpUrl(),
Expand Down
Loading