|
| 1 | +import { Client } from "@opensearch-project/opensearch"; |
| 2 | +import { getImage } from "../../../testcontainers/src/utils/test-helper"; |
| 3 | +import { OpenSearchContainer } from "./opensearch-container"; |
| 4 | + |
| 5 | +const IMAGE = getImage(__dirname); |
| 6 | +const images = ["opensearchproject/opensearch:2.19.2", IMAGE]; |
| 7 | + |
| 8 | +describe("OpenSearchContainer", { timeout: 180_000 }, () => { |
| 9 | + // createIndex { |
| 10 | + it.each(images)("should create an index with %s", async (image) => { |
| 11 | + const container = await new OpenSearchContainer(image).start(); |
| 12 | + const client = new Client({ |
| 13 | + node: container.getHttpUrl(), |
| 14 | + auth: { |
| 15 | + username: container.getUsername(), |
| 16 | + password: container.getPassword(), |
| 17 | + }, |
| 18 | + ssl: { |
| 19 | + // trust the self-signed cert |
| 20 | + rejectUnauthorized: false, |
| 21 | + }, |
| 22 | + }); |
| 23 | + |
| 24 | + await client.indices.create({ index: "people" }); |
| 25 | + const existsResponse = await client.indices.exists({ index: "people" }); |
| 26 | + expect(existsResponse.body).toBe(true); |
| 27 | + await container.stop(); |
| 28 | + }); |
| 29 | + // } |
| 30 | + |
| 31 | + // indexDocument { |
| 32 | + it("should index a document", async () => { |
| 33 | + const container = await new OpenSearchContainer(IMAGE).start(); |
| 34 | + const client = new Client({ |
| 35 | + node: container.getHttpUrl(), |
| 36 | + auth: { |
| 37 | + username: container.getUsername(), |
| 38 | + password: container.getPassword(), |
| 39 | + }, |
| 40 | + ssl: { |
| 41 | + rejectUnauthorized: false, |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + const document = { id: "1", name: "John Doe" }; |
| 46 | + |
| 47 | + await client.index({ |
| 48 | + index: "people", |
| 49 | + id: document.id, |
| 50 | + body: document, |
| 51 | + }); |
| 52 | + |
| 53 | + const getResponse = await client.get({ index: "people", id: document.id }); |
| 54 | + expect(getResponse.body._source).toStrictEqual(document); |
| 55 | + await container.stop(); |
| 56 | + }); |
| 57 | + // } |
| 58 | + |
| 59 | + it("should work with restarted container", async () => { |
| 60 | + const container = await new OpenSearchContainer(IMAGE).start(); |
| 61 | + await container.restart(); |
| 62 | + |
| 63 | + const client = new Client({ |
| 64 | + node: container.getHttpUrl(), |
| 65 | + auth: { |
| 66 | + username: container.getUsername(), |
| 67 | + password: container.getPassword(), |
| 68 | + }, |
| 69 | + ssl: { |
| 70 | + rejectUnauthorized: false, |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + await client.indices.create({ index: "people" }); |
| 75 | + const existsResponse = await client.indices.exists({ index: "people" }); |
| 76 | + expect(existsResponse.body).toBe(true); |
| 77 | + await container.stop(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should throw when given an invalid password", () => { |
| 81 | + expect(() => new OpenSearchContainer(IMAGE).withPassword("weakpwd")).toThrowError(/Password "weakpwd" is too weak/); |
| 82 | + }); |
| 83 | + |
| 84 | + // customPassword { |
| 85 | + it("should set custom password", async () => { |
| 86 | + const container = await new OpenSearchContainer(IMAGE).withPassword("Str0ng!Passw0rd2025").start(); |
| 87 | + |
| 88 | + const client = new Client({ |
| 89 | + node: container.getHttpUrl(), |
| 90 | + auth: { |
| 91 | + username: container.getUsername(), |
| 92 | + password: container.getPassword(), |
| 93 | + }, |
| 94 | + ssl: { |
| 95 | + rejectUnauthorized: false, |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + await client.indices.create({ index: "people" }); |
| 100 | + const existsResponse = await client.indices.exists({ index: "people" }); |
| 101 | + expect(existsResponse.body).toBe(true); |
| 102 | + await container.stop(); |
| 103 | + }); |
| 104 | + // } |
| 105 | +}); |
0 commit comments