Skip to content

Commit

Permalink
Tweaks and additions to the extensions to better support running test…
Browse files Browse the repository at this point in the history
…s in parallel
  • Loading branch information
chrisrohr committed Aug 24, 2023
1 parent 518d5a5 commit 74195c6
Show file tree
Hide file tree
Showing 11 changed files with 480 additions and 30 deletions.
140 changes: 137 additions & 3 deletions __tests__/elasticsearch/elastic-search-extension.t.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { afterEach, describe, expect, it } from "@jest/globals";
import { ElasticSearchExtension } from "../../src";
import { Client } from "@elastic/elasticsearch";

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

delete process.env.ELASTIC_SEARCH_EXTENSION_BASE_URI;
});

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();
expect(process.env.ELASTIC_SEARCH_EXTENSION_BASE_URI).toEqual(
global.ELASTIC_SEARCH_CONTAINER.getHttpUrl(),
);
}, 60_000);
});

Expand All @@ -25,21 +29,32 @@ describe("ElasticSearchExtension", () => {
await ElasticSearchExtension.stopElasticSearchContainer();

expect(global.ELASTIC_SEARCH_CONTAINER).toBeUndefined();
expect(process.env.ELASTIC_SEARCH_EXTENSION_BASE_URI).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",
"IllegalStateException: Elastic Search container has not been previously started or is not running in band",
),
);

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

describe("setElasticSearchUrl", () => {
it("should set the uri env variable for the elastic search url", () => {
ElasticSearchExtension.setElasticSearchUrl("localhost", 12345);

expect(process.env.ELASTIC_SEARCH_EXTENSION_BASE_URI).toEqual(
"http://localhost:12345",
);
});
});

describe("getElasticSearchUrl", () => {
it("should return the base url for the started container", async () => {
await ElasticSearchExtension.startElasticSearchContainer();
Expand All @@ -54,4 +69,123 @@ describe("ElasticSearchExtension", () => {
);
});
});

describe("createIndex", () => {
it("should successfully create the requested index", async () => {
await ElasticSearchExtension.startElasticSearchContainer();

const mappings = {
properties: {
eventType: {
type: "keyword",
index: true,
},
},
};

const pipeline = {
id: "kiwi-attachment-pipeline",
body: {
processors: [
{
attachment: {
field: "pdfVersionRaw",
target_field: "pdfVersion",
remove_binary: true,
},
},
],
},
};

await ElasticSearchExtension.createIndex("kiwi", mappings, [pipeline]);

const client = new Client({
node: ElasticSearchExtension.getElasticSearchUrl(),
});

const indices = await client.cat.indices({ format: "json" });
const indexNames = indices.map((i) => i.index);
expect(indexNames).toContain("kiwi");

await client.close();
}, 60_000);
});

describe("clearIndex", () => {
it("should clear all data in the given index", async () => {
await ElasticSearchExtension.startElasticSearchContainer();

const client = new Client({
node: ElasticSearchExtension.getElasticSearchUrl(),
});

await client.indices.create({
index: "kiwi_clear",
mappings: {
properties: {
eventType: {
type: "keyword",
index: true,
},
},
},
});

await client.index({
index: "kiwi_clear",
document: {
eventType: "foo",
},
id: "12345",
refresh: true,
});

const countBeforeDelete = await client.count({
index: "kiwi_clear",
});

expect(countBeforeDelete.count).toEqual(1);

await ElasticSearchExtension.clearIndex("kiwi_clear");

const countAfterDelete = await client.count({
index: "kiwi_clear",
});

expect(countAfterDelete.count).toEqual(0);
}, 60_000);
});

describe("deleteIndex", () => {
it("should delete a given index", async () => {
await ElasticSearchExtension.startElasticSearchContainer();

const client = new Client({
node: ElasticSearchExtension.getElasticSearchUrl(),
});

await client.indices.create({
index: "kiwi_delete",
mappings: {
properties: {
eventType: {
type: "keyword",
index: true,
},
},
},
});

const indicesBeforeDelete = await client.cat.indices({ format: "json" });
const indexNamesBeforeDelete = indicesBeforeDelete.map((i) => i.index);
expect(indexNamesBeforeDelete).toContain("kiwi_delete");

await ElasticSearchExtension.deleteIndex("kiwi_delete");

const indicesAfterDelete = await client.cat.indices({ format: "json" });
const indexNamesAfterDelete = indicesAfterDelete.map((i) => i.index);
expect(indexNamesAfterDelete).not.toContain("kiwi_delete");
}, 60_000);
});
});
16 changes: 13 additions & 3 deletions __tests__/minio/minio-extension.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ describe("MinioExtension", () => {
await global.MINIO_CONTAINER.stop();
global.MINIO_CONTAINER = undefined;
}

delete process.env.MINIO_EXTENSION_PORT;
});

describe("startMinioContainer", () => {
it("should start the minio container", async () => {
await MinioExtension.startMinioContainer();

expect(global.MINIO_CONTAINER).toBeDefined();

await global.MINIO_CONTAINER.stop();
expect(process.env.MINIO_EXTENSION_PORT).toBeDefined();
}, 60_000);
});

Expand All @@ -25,19 +26,28 @@ describe("MinioExtension", () => {
await MinioExtension.stopMinioContainer();

expect(global.MINIO_CONTAINER).toBeUndefined();
expect(process.env.MINIO_EXTENSION_PORT).toBeUndefined();
}, 60_000);

it("should throw error when container is not previously started", () => {
expect(MinioExtension.stopMinioContainer()).rejects.toEqual(
Error(
"IllegalStateException: Minio container has not been previously started",
"IllegalStateException: Minio container has not been previously started or is not running in band",
),
);

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

describe("setMinioPort", () => {
it("should set the uri env variable for the minio port", () => {
MinioExtension.setMinioPort(12345);

expect(process.env.MINIO_EXTENSION_PORT).toEqual("12345");
});
});

describe("getMinioPort", () => {
it("should return the mapped port for the started container", async () => {
await MinioExtension.startMinioContainer();
Expand Down
61 changes: 59 additions & 2 deletions __tests__/mongo/mongo-extension.t.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { afterEach, describe, expect, it } from "@jest/globals";
import { MongoClient } from "mongodb";
import { MongoExtension } from "../../src";

describe("MongoExtension", () => {
Expand All @@ -7,6 +8,8 @@ describe("MongoExtension", () => {
await global.MONGO_CONTAINER.stop();
global.MONGO_CONTAINER = undefined;
}

delete process.env.MONGO_EXTENSION_BASE_URI;
});

describe("startMongoContainer", () => {
Expand All @@ -15,7 +18,9 @@ describe("MongoExtension", () => {

expect(global.MONGO_CONTAINER).toBeDefined();

await global.MONGO_CONTAINER.stop();
expect(process.env.MONGO_EXTENSION_BASE_URI).toEqual(
`mongodb://${global.MONGO_CONTAINER.getHost()}:${global.MONGO_CONTAINER.getFirstMappedPort()}/`,
);
}, 60_000);
});

Expand All @@ -25,19 +30,30 @@ describe("MongoExtension", () => {
await MongoExtension.stopMongoContainer();

expect(global.MONGO_CONTAINER).toBeUndefined();
expect(process.env.MONGO_EXTENSION_BASE_URI).toBeUndefined();
}, 60_000);

it("should throw error when container is not previously started", () => {
expect(MongoExtension.stopMongoContainer()).rejects.toEqual(
Error(
"IllegalStateException: Mongo container has not been previously started",
"IllegalStateException: Mongo container has not been previously started or is not running in band",
),
);

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

describe("setMongoBaseUrl", () => {
it("should set the uri env variable for the mongo url", () => {
MongoExtension.setMongoBaseUrl("localhost", 12345);

expect(process.env.MONGO_EXTENSION_BASE_URI).toEqual(
"mongodb://localhost:12345/",
);
});
});

describe("getMongoBaseUrl", () => {
it("should return the base url for the started container", async () => {
await MongoExtension.startMongoContainer();
Expand Down Expand Up @@ -68,4 +84,45 @@ describe("MongoExtension", () => {
);
});
});

describe("dropDatabase", () => {
it("should drop the given database from mongo", async () => {
await MongoExtension.startMongoContainer();

const dbClient = new MongoClient(
MongoExtension.getMongoUriWithDb("kiwi_drop"),
);
await dbClient.connect();
const kiwiDb = dbClient.db("kiwi_drop");
await kiwiDb.createCollection("foo");
await dbClient.close();

const client = new MongoClient(MongoExtension.getMongoBaseUrl());
const adminDb = client.db("admin");

const dbListingBeforeDrop = await adminDb.command({
listDatabases: 1,
nameOnly: true,
});

const dbNamesBeforeDrop = dbListingBeforeDrop.databases.map(
(db: { name: any }) => db.name,
);
expect(dbNamesBeforeDrop).toContain("kiwi_drop");

await MongoExtension.dropDatabase("kiwi_drop");

const dbListingAfterDrop = await adminDb.command({
listDatabases: 1,
nameOnly: true,
});

const dbNamesAfterDrop = dbListingAfterDrop.databases.map(
(db: { name: any }) => db.name,
);
expect(dbNamesAfterDrop).not.toContain("kiwi_drop");

await client.close();
}, 60_000);
});
});
Loading

0 comments on commit 74195c6

Please sign in to comment.