-
-
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
Closed
tomershafir
wants to merge
14
commits into
testcontainers:main
from
tomershafir:feat/clickhouse-module
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8db9a91
add basic clickhouse module
tomershafir 09895ed
clean
tomershafir c00a525
rm container config file interface and add examples to jests
tomershafir 9149913
add doc file
tomershafir e8471d3
ret url instead port
tomershafir b3a510f
pin http schema
tomershafir 36b6a45
Merge branch 'main' into feat/clickhouse-module
cristianrgreco ec04099
Update mkdocs.yml
tomershafir a3f2628
Merge branch 'main' into feat/clickhouse-module
tomershafir a973b3c
use uuid instead math.random for linter and rename test method
tomershafir 39fe137
fix lint issues
tomershafir b9c351d
add https and pin native to tcp
tomershafir 1e24e78
expose native tcp/tcps port but rm js method for them
tomershafir 47a9adf
rm native ports
tomershafir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
packages/modules/clickhouse/src/clickhouse-container.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { ClickhouseContainer, StartedClickhouseContainer } from "./clickhouse-container"; | ||
| import { ClickHouseClient, createClient } from "@clickhouse/client"; | ||
| import path from "path"; | ||
| import { RandomUuid } from "testcontainers"; | ||
|
|
||
| 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 testExample(client, container.getDatabase()); | ||
| await client.close(); | ||
| await container.stop(); | ||
| }); | ||
|
|
||
| it("should work with custom credentials", async () => { | ||
| const uuid = new RandomUuid(); | ||
| const container = await new ClickhouseContainer() | ||
| .withUsername(`un-${uuid.nextUuid()}`) | ||
| .withPassword(`pass-${uuid.nextUuid()}`) | ||
| .start(); | ||
| const client = createClickhouseContainerHttpClient(container); | ||
| await testExample(client, container.getDatabase()); | ||
| await client.close(); | ||
| await container.stop(); | ||
| }); | ||
|
|
||
| it("should work with custom database and custom yaml config", async () => { | ||
| const uuid = new RandomUuid(); | ||
| const CONFIG_PATH_YAML = "/etc/clickhouse-server/config.d/config.yaml"; | ||
| const container = await new ClickhouseContainer() | ||
| .withDatabase(`db-${uuid.nextUuid()}`) | ||
| .withPassword("") | ||
| .withCopyFilesToContainer([ | ||
| { source: path.join("testdata", "config.yaml"), target: CONFIG_PATH_YAML, mode: CONFIG_FILE_MODE }, | ||
| ]) | ||
| .start(); | ||
| const client = createClickhouseContainerHttpClient(container); | ||
| await testExample(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 testExample(client, container.getDatabase()); | ||
| await client.close(); | ||
| await container.stop(); | ||
| }); | ||
|
|
||
| function createClickhouseContainerHttpClient(container: StartedClickhouseContainer) { | ||
| return createClient({ | ||
| host: container.getHttpUrl(), | ||
| username: container.getUsername(), | ||
| password: container.getPassword(), | ||
| }); | ||
| } | ||
|
|
||
| async function testExample(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" }, | ||
| ]); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers"; | ||
|
|
||
| const IMAGE_NAME = "clickhouse/clickhouse-server"; | ||
| const DEFAULT_IMAGE_VER = "23.3.8.21-alpine"; | ||
|
|
||
| // The official clickhouse-js client doesn't support the native protocol for now. See https://github.com/ClickHouse/clickhouse-js | ||
| const HTTP_PORT = 8123; | ||
| const HTTPS_PORT = 8443; | ||
|
|
||
| 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, HTTPS_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, | ||
| HTTPS_PORT, | ||
| this.database, | ||
| this.username, | ||
| this.password | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export class StartedClickhouseContainer extends AbstractStartedContainer { | ||
| private readonly hostHttpPort: number; | ||
| private readonly hostHttpsPort: number; | ||
|
|
||
| constructor( | ||
| startedTestContainer: StartedTestContainer, | ||
| httpPort: number, | ||
| httpsPort: number, | ||
| private readonly database: string, | ||
| private readonly username: string, | ||
| private readonly password: string | ||
| ) { | ||
| super(startedTestContainer); | ||
| this.hostHttpPort = startedTestContainer.getMappedPort(httpPort); | ||
| this.hostHttpsPort = startedTestContainer.getMappedPort(httpsPort); | ||
| } | ||
|
|
||
| public getHttpUrl(): string { | ||
| return this.toUrl("http", this.hostHttpPort); | ||
| } | ||
|
|
||
| public getHttpsUrl(): string { | ||
| return this.toUrl("https", this.hostHttpsPort); | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { ClickhouseContainer, StartedClickhouseContainer } from "./clickhouse-container"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <clickhouse> | ||
| <allow_no_password>1</allow_no_password> | ||
| </clickhouse> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| allow_no_password: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| ] | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.