Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an Elastic Search extension #9

Merged
merged 1 commit into from
Aug 15, 2023
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
57 changes: 57 additions & 0 deletions __tests__/elasticsearch/elastic-search-extension.t.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {afterEach, describe, expect, it} from "@jest/globals";
import {ElasticSearchExtension} from "../../src";

describe("ElasticSearchExtension", () => {
afterEach(async () => {
if (global.ELASTIC_SEARCH_CONTAINER) {
await global.ELASTIC_SEARCH_CONTAINER.stop();
global.ELASTIC_SEARCH_CONTAINER = undefined;
}
});

describe("startElasticSearchContainer", () => {
it("should start the elastic search container", async () => {
await ElasticSearchExtension.startElasticSearchContainer();

expect(global.ELASTIC_SEARCH_CONTAINER).toBeDefined();

await global.ELASTIC_SEARCH_CONTAINER.stop();
}, 60_000);
});

describe("stopElasticSearchContainer", () => {
it("should stop the elastic search container", async () => {
await ElasticSearchExtension.startElasticSearchContainer();
await ElasticSearchExtension.stopElasticSearchContainer();

expect(global.ELASTIC_SEARCH_CONTAINER).toBeUndefined();
}, 60_000);

it("should throw error when container is not previously started", () => {
expect(
ElasticSearchExtension.stopElasticSearchContainer(),
).rejects.toEqual(
Error(
"IllegalStateException: Elastic Search container has not been previously started",
),
);

expect(global.ELASTIC_SEARCH_CONTAINER).toBeUndefined();
});
});

describe("getElasticSearchUrl", () => {
it("should return the base url for the started container", async () => {
await ElasticSearchExtension.startElasticSearchContainer();

const url = ElasticSearchExtension.getElasticSearchUrl();
expect(url).toContain("http://localhost:");
}, 60_000);

it("should throw error when container is not previously started", () => {
expect(() => ElasticSearchExtension.getElasticSearchUrl()).toThrow(
"IllegalStateException: Elastic Search container has not been previously started",
);
});
});
});
93 changes: 92 additions & 1 deletion package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@jest/globals": "29.6.2",
"@kiwiproject/kiwi-js": "0.8.0",
"@testcontainers/elasticsearch": "10.2.1",
"@testcontainers/postgresql": "10.2.1",
"jest": "29.6.2",
"mongodb": "5.7.0",
Expand Down
34 changes: 34 additions & 0 deletions src/elasticsearch/elastic-search-extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {KiwiPreconditions} from "@kiwiproject/kiwi-js";
import {ElasticsearchContainer} from "@testcontainers/elasticsearch";

async function startElasticSearchContainer() {
global.ELASTIC_SEARCH_CONTAINER = await new ElasticsearchContainer(
"elasticsearch:8.6.1",
)
.withEnvironment({ "xpack.security.enabled": "false" })
.start();
}

async function stopElasticSearchContainer() {
KiwiPreconditions.checkState(
global.ELASTIC_SEARCH_CONTAINER !== undefined,
"Elastic Search container has not been previously started",
);
await global.ELASTIC_SEARCH_CONTAINER.stop();
global.ELASTIC_SEARCH_CONTAINER = undefined;
}

function getElasticSearchUrl(): string {
KiwiPreconditions.checkState(
global.ELASTIC_SEARCH_CONTAINER !== undefined,
"Elastic Search container has not been previously started",
);

return global.ELASTIC_SEARCH_CONTAINER.getHttpUrl();
}

export const ElasticSearchExtension = {
startElasticSearchContainer,
stopElasticSearchContainer,
getElasticSearchUrl,
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { MongoExtension } from "./mongo/mongo-extension";
export { PostgresExtension } from "./postgres/postgres-extension";
export { ElasticSearchExtension } from "./elasticsearch/elastic-search-extension";