Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
13 changes: 13 additions & 0 deletions docs/modules/clickhouse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Clickhouse Module

[Clickhouse](https://clickhouse.com/) is an open source OLAP columnar database. You can checkout the official [Clickhouse JavaScript](https://clickhouse.com/docs/en/integrations/language-clients/javascript) driver here.

## Install

```bash
npm install @testcontainers/clickhouse --save-dev
```

## Example

[Test suite](../../packages/modules/clickhouse/src/clickhouse-container.test.ts)
32 changes: 32 additions & 0 deletions package-lock.json

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

11 changes: 11 additions & 0 deletions packages/modules/clickhouse/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Config } from "jest";
import * as path from "path";

const config: Config = {
preset: "ts-jest",
moduleNameMapper: {
"^testcontainers$": path.resolve(__dirname, "../../testcontainers/src"),
},
};

export default config;
37 changes: 37 additions & 0 deletions packages/modules/clickhouse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@testcontainers/clickhouse",
"version": "10.2.1",
"license": "MIT",
"keywords": [
"clickhouse",
"testing",
"docker",
"testcontainers"
],
"description": "Clickhouse module for Testcontainers",
"homepage": "https://github.com/testcontainers/testcontainers-node#readme",
"repository": {
"type": "git",
"url": "https://github.com/testcontainers/testcontainers-node"
},
"bugs": {
"url": "https://github.com/testcontainers/testcontainers-node/issues"
},
"main": "build/index.js",
"files": [
"build"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .",
"build": "tsc --project tsconfig.build.json"
},
"devDependencies": {
"@clickhouse/client": "^0.2.2"
},
"dependencies": {
"testcontainers": "^10.2.1"
}
}
89 changes: 89 additions & 0 deletions packages/modules/clickhouse/src/clickhouse-container.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { ClickhouseContainer, StartedClickhouseContainer } from "./clickhouse-container";
import { ClickHouseClient, createClient } from "@clickhouse/client";
import path from "path";

const CONFIG_FILE_MODE = parseInt("0644", 8)

describe("ClickhouseContainer", () => {
jest.setTimeout(240_000);

it("should work with defaults", async () => {
const container = await new ClickhouseContainer().start();
const client = createClickhouseContainerHttpClient(container);
await _test(client, container.getDatabase());
await client.close();
await container.stop();
});

it("should work with custom credentials", async () => {
const container = await new ClickhouseContainer()
.withUsername(`un${(Math.random()*1000000) | 0}`)
.withPassword(`pass${(Math.random()*1000000) | 0}`)
.start();
const client = createClickhouseContainerHttpClient(container);
await _test(client, container.getDatabase());
await client.close();
await container.stop();
});

it("should work with custom database and custom yaml config", async () => {
const CONFIG_PATH_YAML = "/etc/clickhouse-server/config.d/config.yaml";
const container = await new ClickhouseContainer()
.withDatabase(`db${(Math.random()*1000000) | 0}`)
.withPassword("")
.withCopyFilesToContainer([{source: path.join("testdata", "config.yaml"), target: CONFIG_PATH_YAML, mode: CONFIG_FILE_MODE}])
.start();
const client = createClickhouseContainerHttpClient(container);
await _test(client, container.getDatabase());
await client.close();
await container.stop();
});

it("should work with custom image and custom xml config example", async () => {
const CONFIG_PATH_XML = "/etc/clickhouse-server/config.d/config.xml";
const container = await new ClickhouseContainer("23.8-alpine")
.withPassword("")
.withCopyFilesToContainer([{source: path.join("testdata", "config.xml"), target: CONFIG_PATH_XML, mode: CONFIG_FILE_MODE}])
.start();
const client = createClickhouseContainerHttpClient(container);
await _test(client, container.getDatabase());
await client.close();
await container.stop();
});

function createClickhouseContainerHttpClient(container: StartedClickhouseContainer) {
return createClient({
host: container.getHttpUrl("http"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the current support the getHttpUrl() can return the http protocol. If, at some point in the future, https is supported another method/fn can be added or revisited

username: container.getUsername(),
password: container.getPassword(),
});
}

async function _test(client: ClickHouseClient, db: string) {
const tableName = 'array_json_each_row';
await client.command({
query: `DROP TABLE IF EXISTS ${db}.${tableName}`,
});
await client.command({
query: `
CREATE TABLE ${db}.${tableName}
(id UInt64, name String)
ENGINE MergeTree()
ORDER BY (id)
`,
});
await client.insert({
table: `${db}.${tableName}`,
values: [
{ id: 42, name: 'foo' },
{ id: 42, name: 'bar' },
],
format: 'JSONEachRow',
});
const rows = await client.query({
query: `SELECT * FROM ${db}.${tableName}`,
format: 'JSONEachRow',
});
expect(await rows.json()).toEqual([{id:"42",name:"foo"},{id:"42",name:"bar"}]);
}
});
95 changes: 95 additions & 0 deletions packages/modules/clickhouse/src/clickhouse-container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers";

const IMAGE_NAME = "clickhouse/clickhouse-server";
const DEFAULT_IMAGE_VER = "23.3.8.21-alpine"

const HTTP_PORT = 8123;
const NATIVE_PORT = 9000;

export class ClickhouseContainer extends GenericContainer {
private database = "test";
private username = "test";
private password = "test";

constructor(imageVer = DEFAULT_IMAGE_VER) {
super(`${IMAGE_NAME}:${imageVer}`);
}

public withDatabase(database: string): this {
this.database = database;
return this;
}

public withUsername(username: string): this {
this.username = username;
return this;
}

public withPassword(password: string): this {
this.password = password;
return this;
}

public override async start(): Promise<StartedClickhouseContainer> {
this.withExposedPorts(...(this.hasExposedPorts ? this.exposedPorts : [HTTP_PORT, NATIVE_PORT]))
.withEnvironment({
CLICKHOUSE_DB: this.database,
CLICKHOUSE_USER: this.username,
CLICKHOUSE_PASSWORD: this.password,
})
.withWaitStrategy(Wait.forHttp(`/ping`, HTTP_PORT)
.forStatusCode(200))
.withStartupTimeout(120_000);

return new StartedClickhouseContainer(
await super.start(),
HTTP_PORT,
NATIVE_PORT,
this.database,
this.username,
this.password,
);
}
}

export class StartedClickhouseContainer extends AbstractStartedContainer {
private readonly hostHttpPort: number;
private readonly hostNativePort: number;

constructor(
startedTestContainer: StartedTestContainer,
httpPort: number,
nativePort: number,
private readonly database: string,
private readonly username: string,
private readonly password: string,
) {
super(startedTestContainer);
this.hostHttpPort = startedTestContainer.getMappedPort(httpPort);
this.hostNativePort = startedTestContainer.getMappedPort(nativePort);
}

public getHttpUrl(schema: string): string {
return this.toUrl(schema, this.hostHttpPort)
}

public getNativeUrl(schema: string): string {
return this.toUrl(schema, this.hostNativePort)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you provide an example of this please? or better add a test :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as clickhouse-js doesnt support native protocol I removed the js methods. Added https one


private toUrl(schema: string, port: number): string {
return `${schema}://${this.startedTestContainer.getHost()}:${port}`
}

public getDatabase(): string {
return this.database;
}

public getUsername(): string {
return this.username;
}

public getPassword(): string {
return this.password;
}
}
1 change: 1 addition & 0 deletions packages/modules/clickhouse/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ClickhouseContainer, StartedClickhouseContainer } from "./clickhouse-container";
3 changes: 3 additions & 0 deletions packages/modules/clickhouse/testdata/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<clickhouse>
<allow_no_password>1</allow_no_password>
</clickhouse>
1 change: 1 addition & 0 deletions packages/modules/clickhouse/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allow_no_password: true
13 changes: 13 additions & 0 deletions packages/modules/clickhouse/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "./tsconfig.json",
"exclude": [
"build",
"jest.config.ts",
"src/**/*.test.ts"
],
"references": [
{
"path": "../../testcontainers"
}
]
}
21 changes: 21 additions & 0 deletions packages/modules/clickhouse/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build",
"paths": {
"testcontainers": [
"../../testcontainers/src"
]
}
},
"exclude": [
"build",
"jest.config.ts"
],
"references": [
{
"path": "../../testcontainers"
}
]
}