Skip to content

Commit

Permalink
Create a Minio extension (#11)
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
chrisrohr committed Aug 16, 2023
1 parent 807a046 commit c4761ed
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 14 deletions.
4 changes: 2 additions & 2 deletions __tests__/elasticsearch/elastic-search-extension.t.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {afterEach, describe, expect, it} from "@jest/globals";
import {ElasticSearchExtension} from "../../src";
import { afterEach, describe, expect, it } from "@jest/globals";
import { ElasticSearchExtension } from "../../src";

describe("ElasticSearchExtension", () => {
afterEach(async () => {
Expand Down
64 changes: 64 additions & 0 deletions __tests__/minio/minio-extension.t.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { afterEach, describe, expect, it } from "@jest/globals";
import { MinioExtension } from "../../src";

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

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

expect(global.MINIO_CONTAINER).toBeDefined();

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

describe("stopMinioContainer", () => {
it("should stop the minio container", async () => {
await MinioExtension.startMinioSearchContainer(
"minioadmin",
"keyboard cat",
);
await MinioExtension.stopMinioSearchContainer();

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

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

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

describe("getMinioPort", () => {
it("should return the mapped port for the started container", async () => {
await MinioExtension.startMinioSearchContainer(
"minioadmin",
"keyboard cat",
);

const port = MinioExtension.getMinioPort();
expect(port).toBeGreaterThan(0);
}, 60_000);

it("should throw error when container is not previously started", () => {
expect(() => MinioExtension.getMinioPort()).toThrow(
"IllegalStateException: Minio container has not been previously started",
);
});
});
});
4 changes: 2 additions & 2 deletions __tests__/mongo/mongo-extension.t.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {afterEach, describe, expect, it} from "@jest/globals";
import {MongoExtension} from "../../src";
import { afterEach, describe, expect, it } from "@jest/globals";
import { MongoExtension } from "../../src";

describe("MongoExtension", () => {
afterEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/postgres/postgres-extension.t.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {afterEach, describe, expect, it} from "@jest/globals";
import {PostgresExtension} from "../../src";
import {Client} from "pg";
import { afterEach, describe, expect, it } from "@jest/globals";
import { PostgresExtension } from "../../src";
import { Client } from "pg";

describe("PostgresExtension", () => {
afterEach(async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/elasticsearch/elastic-search-extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {KiwiPreconditions} from "@kiwiproject/kiwi-js";
import {ElasticsearchContainer} from "@testcontainers/elasticsearch";
import { KiwiPreconditions } from "@kiwiproject/kiwi-js";
import { ElasticsearchContainer } from "@testcontainers/elasticsearch";

async function startElasticSearchContainer() {
global.ELASTIC_SEARCH_CONTAINER = await new ElasticsearchContainer(
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { MongoExtension } from "./mongo/mongo-extension";
export { PostgresExtension } from "./postgres/postgres-extension";
export { ElasticSearchExtension } from "./elasticsearch/elastic-search-extension";
export { MinioExtension } from "./minio/minio-extension";
44 changes: 44 additions & 0 deletions src/minio/minio-extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { KiwiPreconditions } from "@kiwiproject/kiwi-js";
import { GenericContainer, Wait } from "testcontainers";

async function startMinioSearchContainer(accessKey: string, secretKey: string) {
global.MINIO_CONTAINER = await new GenericContainer(
"quay.io/minio/minio:latest",
)
.withEnvironment({
MINIO_BROWSER: "off",
MINIO_ROOT_USER: accessKey,
MINIO_ROOT_PASSWORD: secretKey,
})
.withExposedPorts(9000)
.withWaitStrategy(
Wait.forAll([Wait.forListeningPorts(), Wait.forLogMessage(/1 Online/)]),
)
.withTmpFs({ "/data": "rw,noexec,nosuid" })
.withCommand(["server", "/data"])
.start();
}

async function stopMinioSearchContainer() {
KiwiPreconditions.checkState(
global.MINIO_CONTAINER !== undefined,
"Minio container has not been previously started",
);
await global.MINIO_CONTAINER.stop();
global.MINIO_CONTAINER = undefined;
}

function getMinioPort(): number {
KiwiPreconditions.checkState(
global.MINIO_CONTAINER !== undefined,
"Minio container has not been previously started",
);

return global.MINIO_CONTAINER.getMappedPort(9000);
}

export const MinioExtension = {
startMinioSearchContainer,
stopMinioSearchContainer,
getMinioPort,
};
4 changes: 2 additions & 2 deletions src/mongo/mongo-extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {KiwiPreconditions} from "@kiwiproject/kiwi-js";
import {GenericContainer} from "testcontainers";
import { KiwiPreconditions } from "@kiwiproject/kiwi-js";
import { GenericContainer } from "testcontainers";

async function startMongoContainer() {
global.MONGO_CONTAINER = await new GenericContainer("mongo")
Expand Down
6 changes: 3 additions & 3 deletions src/postgres/postgres-extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {KiwiPreconditions} from "@kiwiproject/kiwi-js";
import {PostgreSqlContainer} from "@testcontainers/postgresql";
import {Client} from "pg";
import { KiwiPreconditions } from "@kiwiproject/kiwi-js";
import { PostgreSqlContainer } from "@testcontainers/postgresql";
import { Client } from "pg";

async function startPostgresContainer() {
global.POSTGRES_CONTAINER = await new PostgreSqlContainer().start();
Expand Down

0 comments on commit c4761ed

Please sign in to comment.