Skip to content

Commit da96ae0

Browse files
Fix Elasticsearch tests
1 parent 5fe7f94 commit da96ae0

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

packages/modules/elasticsearch/src/elasticsearch-container.test.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import { getImage } from "../../../testcontainers/src/utils/test-helper";
33
import { ElasticsearchContainer } from "./elasticsearch-container";
44

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

78
describe("ElasticsearchContainer", { timeout: 180_000 }, () => {
89
// createIndex {
9-
it("should create an index", async () => {
10-
const container = await new ElasticsearchContainer(IMAGE).start();
11-
const client = new Client({ node: container.getHttpUrl() });
10+
it.each(images)("should create an index with %s", async (image) => {
11+
const container = await new ElasticsearchContainer(image).start();
12+
const client = new Client({
13+
node: container.getHttpUrl(),
14+
auth: { username: container.getUsername(), password: container.getPassword() },
15+
});
1216

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

@@ -20,7 +24,10 @@ describe("ElasticsearchContainer", { timeout: 180_000 }, () => {
2024
// indexDocument {
2125
it("should index a document", async () => {
2226
const container = await new ElasticsearchContainer(IMAGE).start();
23-
const client = new Client({ node: container.getHttpUrl() });
27+
const client = new Client({
28+
node: container.getHttpUrl(),
29+
auth: { username: container.getUsername(), password: container.getPassword() },
30+
});
2431

2532
const document = {
2633
id: "1",
@@ -41,7 +48,24 @@ describe("ElasticsearchContainer", { timeout: 180_000 }, () => {
4148
const container = await new ElasticsearchContainer(IMAGE).start();
4249
await container.restart();
4350

44-
const client = new Client({ node: container.getHttpUrl() });
51+
const client = new Client({
52+
node: container.getHttpUrl(),
53+
auth: { username: container.getUsername(), password: container.getPassword() },
54+
});
55+
56+
await client.indices.create({ index: "people" });
57+
58+
expect(await client.indices.exists({ index: "people" })).toBe(true);
59+
await container.stop();
60+
}); // }
61+
62+
it("should set custom password", async () => {
63+
const container = await new ElasticsearchContainer(IMAGE).withPassword("testPassword").start();
64+
65+
const client = new Client({
66+
node: container.getHttpUrl(),
67+
auth: { username: container.getUsername(), password: container.getPassword() },
68+
});
4569

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
1-
import { AbstractStartedContainer, GenericContainer, StartedTestContainer } from "testcontainers";
1+
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers";
22

33
const ELASTIC_SEARCH_HTTP_PORT = 9200;
44

55
export class ElasticsearchContainer extends GenericContainer {
6+
private password = "changeme";
7+
private readonly username = "elastic";
8+
private readonly defaultWaitStrategy = Wait.forHttp("/", ELASTIC_SEARCH_HTTP_PORT).withBasicCredentials(
9+
this.username,
10+
this.password
11+
);
12+
613
constructor(image: string) {
714
super(image);
815
this.withExposedPorts(ELASTIC_SEARCH_HTTP_PORT)
9-
.withEnvironment({ "discovery.type": "single-node" })
16+
.withEnvironment({
17+
"discovery.type": "single-node",
18+
"xpack.security.http.ssl.enabled": "false",
19+
})
1020
.withCopyContentToContainer([
1121
{
1222
content: "-Xmx2G\n",
1323
target: "/usr/share/elasticsearch/config/jvm.options.d/elasticsearch-default-memory-vm.options",
1424
},
1525
])
26+
.withWaitStrategy(this.defaultWaitStrategy)
1627
.withStartupTimeout(120_000);
1728
}
1829

30+
public withPassword(password: string): this {
31+
this.password = password;
32+
this.defaultWaitStrategy.withBasicCredentials(this.username, this.password);
33+
return this;
34+
}
35+
1936
public override async start(): Promise<StartedElasticsearchContainer> {
20-
return new StartedElasticsearchContainer(await super.start());
37+
this.withEnvironment({
38+
ELASTIC_PASSWORD: this.password,
39+
});
40+
41+
return new StartedElasticsearchContainer(await super.start(), this.username, this.password);
2142
}
2243
}
2344

2445
export class StartedElasticsearchContainer extends AbstractStartedContainer {
25-
constructor(override readonly startedTestContainer: StartedTestContainer) {
46+
constructor(
47+
override readonly startedTestContainer: StartedTestContainer,
48+
private readonly username: string,
49+
private readonly password: string
50+
) {
2651
super(startedTestContainer);
2752
}
2853

@@ -33,4 +58,12 @@ export class StartedElasticsearchContainer extends AbstractStartedContainer {
3358
public getHttpUrl(): string {
3459
return `http://${this.getHost()}:${this.getPort()}`;
3560
}
61+
62+
public getUsername(): string {
63+
return this.username;
64+
}
65+
66+
public getPassword(): string {
67+
return this.password;
68+
}
3669
}

0 commit comments

Comments
 (0)