-
-
Notifications
You must be signed in to change notification settings - Fork 251
Add Clickhouse module #648
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
Changes from 2 commits
8db9a91
09895ed
c00a525
9149913
e8471d3
b3a510f
36b6a45
ec04099
a3f2628
a973b3c
39fe137
b9c351d
1e24e78
47a9adf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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; |
| 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" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { ClickhouseContainer, StartedClickhouseContainer } from "./clickhouse-container"; | ||
| import { ClickHouseClient, createClient } from "@clickhouse/client"; | ||
| import path from "path"; | ||
|
|
||
| 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", async () => { | ||
| const container = await new ClickhouseContainer() | ||
| .withDatabase(`db${(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 image", async () => { | ||
| const container = await new ClickhouseContainer("23.8-alpine").start(); | ||
| const client = createClickhouseContainerHttpClient(container); | ||
| await _test(client, container.getDatabase()); | ||
| await client.close(); | ||
| await container.stop(); | ||
| }); | ||
|
|
||
| it("should work with custom xml config", async () => { | ||
| const container = await new ClickhouseContainer() | ||
| .withPassword("") | ||
| .withXmlConfigFile(path.join("testdata", "config.xml")) | ||
| .start(); | ||
| const client = createClickhouseContainerHttpClient(container); | ||
| await _test(client, container.getDatabase()); | ||
| await client.close(); | ||
| await container.stop(); | ||
| }); | ||
|
|
||
| it("should work with custom yaml config", async () => { | ||
| const container = await new ClickhouseContainer() | ||
| .withPassword("") | ||
| .withXmlConfigFile(path.join("testdata", "config.yaml")) | ||
| .start(); | ||
| const client = createClickhouseContainerHttpClient(container); | ||
| await _test(client, container.getDatabase()); | ||
| await client.close(); | ||
| await container.stop(); | ||
| }); | ||
|
|
||
| function createClickhouseContainerHttpClient(container: StartedClickhouseContainer) { | ||
| return createClient({ | ||
| host: `http://${container.getHost()}:${container.getHostPorts().http}`, | ||
| 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"}]); | ||
| } | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| 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; | ||
|
|
||
| const CONFIG_PATH_XML = "/etc/clickhouse-server/config.d/config.xml"; | ||
| const CONFIG_PATH_YAML = "/etc/clickhouse-server/config.d/config.yaml"; | ||
| const CONFIG_FILE_MODE = parseInt("0644", 8) | ||
|
|
||
| export class ClickhouseContainer extends GenericContainer { | ||
| private database = "test"; | ||
| private username = "test"; | ||
| private password = "test"; | ||
| private hostConfigPathXml: string = ""; | ||
| private hostConfigPathYaml: string = ""; | ||
|
|
||
| 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 withXmlConfigFile(path: string): this { | ||
| this.hostConfigPathXml = path; | ||
| return this; | ||
| } | ||
|
|
||
| public withYamlConfigFile(path: string): this { | ||
| this.hostConfigPathYaml = path; | ||
| 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); | ||
|
|
||
| if (this.hostConfigPathXml) | ||
| this.withCopyFilesToContainer([{source: this.hostConfigPathXml, target: CONFIG_PATH_XML, mode: CONFIG_FILE_MODE}]); | ||
| if (this.hostConfigPathYaml) | ||
| this.withCopyFilesToContainer([{source: this.hostConfigPathYaml, target: CONFIG_PATH_YAML, mode: CONFIG_FILE_MODE}]); | ||
|
||
|
|
||
| return new StartedClickhouseContainer( | ||
| await super.start(), | ||
| HTTP_PORT, | ||
| NATIVE_PORT, | ||
| this.database, | ||
| this.username, | ||
| this.password, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export class StartedClickhouseContainer extends AbstractStartedContainer { | ||
| private readonly httpHostPort: number; | ||
| private readonly nativeHostPort: number; | ||
|
|
||
| constructor( | ||
| startedTestContainer: StartedTestContainer, | ||
| httpPort: number, | ||
| nativePort: number, | ||
| private readonly database: string, | ||
| private readonly username: string, | ||
| private readonly password: string, | ||
| ) { | ||
| super(startedTestContainer); | ||
| this.httpHostPort = startedTestContainer.getMappedPort(httpPort); | ||
| this.nativeHostPort = startedTestContainer.getMappedPort(nativePort); | ||
| } | ||
|
|
||
| public getHostPorts(): {http: number; native: number} { | ||
| return {http: this.httpHostPort, native: this.nativeHostPort} | ||
| } | ||
|
||
|
|
||
| public getDatabase(): string { | ||
| return this.database; | ||
| } | ||
|
|
||
| public getUsername(): string { | ||
| return this.username; | ||
| } | ||
|
|
||
| public getPassword(): string { | ||
| return this.password; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { ClickhouseContainer, StartedClickhouseContainer } from "./clickhouse-container"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <clickhouse> | ||
| <allow_no_password>1</allow_no_password> | ||
| </clickhouse> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| allow_no_password: true |
| 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" | ||
| } | ||
| ] | ||
| } |
| 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" | ||
| } | ||
| ] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this should be returned by the container implementation for easy access?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the client should be a different module rather than part the container abstraction
it also couples the dev to a dep he may not want (even though it may make sense to couple because its an official client), for example qryn project doesnt use this client atm