diff --git a/docs/modules/clickhouse.md b/docs/modules/clickhouse.md new file mode 100644 index 000000000..f69752717 --- /dev/null +++ b/docs/modules/clickhouse.md @@ -0,0 +1,55 @@ +# ClickHouse Module + +[ClickHouse](https://clickhouse.com/) is a column-oriented database management system for online analytical processing (OLAP) that allows users to generate analytical reports using SQL queries in real-time. + +## Install + +```bash +npm install @testcontainers/clickhouse --save-dev +``` + +## Examples + + +[Connect and execute query:](../../packages/modules/clickhouse/src/clickhouse-container.test.ts) inside_block:connectWithOptions + + + +[Connect using URL and execute query:](../../packages/modules/clickhouse/src/clickhouse-container.test.ts) inside_block:connectWithUrl + + + +[Connect with username and password and execute query:](../../packages/modules/clickhouse/src/clickhouse-container.test.ts) inside_block:connectWithUsernameAndPassword + + + +[Set database:](../../packages/modules/clickhouse/src/clickhouse-container.test.ts) inside_block:setDatabase + + + +[Set username:](../../packages/modules/clickhouse/src/clickhouse-container.test.ts) inside_block:setUsername + + +### Connection Methods + +The module provides several methods to connect to the ClickHouse container: + +1. `getClientOptions()` - Returns a configuration object suitable for `@clickhouse/client`: + ```typescript + { + url: string; // HTTP URL with host and port + username: string; // Container username + password: string; // Container password + database: string; // Container database + } + ``` +2. `getConnectionUrl()` - Returns a complete HTTP URL including credentials and database: + ``` + http://[username[:password]@][host[:port]]/database + ``` +3. `getHttpUrl()` - Returns the base HTTP URL without credentials: + ``` + http://[host[:port]] + ``` + +These methods can be used with the `@clickhouse/client` package or any other ClickHouse client. diff --git a/mkdocs.yml b/mkdocs.yml index be01dd2ca..60b544f3b 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -51,6 +51,7 @@ nav: - Azurite: modules/azurite.md - Cassandra: modules/cassandra.md - ChromaDB: modules/chromadb.md + - ClickHouse: modules/clickhouse.md - CosmosDB: modules/cosmosdb.md - Couchbase: modules/couchbase.md - CockroachDB: modules/cockroachdb.md diff --git a/package-lock.json b/package-lock.json index bcd5713b4..6cf5bdf92 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1552,6 +1552,26 @@ "tough-cookie": "^4.1.4" } }, + "node_modules/@clickhouse/client": { + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/@clickhouse/client/-/client-1.11.0.tgz", + "integrity": "sha512-VYTQfR0y/BtrIDEjuSce1zv85OvHak5sUhZVyNYJzbAgWHy3jFf8Os7FdUSeqyKav0xGGy+2X+dRanTFjI5Oug==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@clickhouse/client-common": "1.11.0" + }, + "engines": { + "node": ">=16" + } + }, + "node_modules/@clickhouse/client-common": { + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/@clickhouse/client-common/-/client-common-1.11.0.tgz", + "integrity": "sha512-O0xbwv7HiMXayokrf5dYIBpjBnYekcOXWz60T1cXLmiZ8vgrfNRCiOpybJkrMXKnw9D0mWCgPUu/rgMY7U1f4g==", + "dev": true, + "license": "Apache-2.0" + }, "node_modules/@colors/colors": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.5.0.tgz", @@ -5077,6 +5097,10 @@ "resolved": "packages/modules/chromadb", "link": true }, + "node_modules/@testcontainers/clickhouse": { + "resolved": "packages/modules/clickhouse", + "link": true + }, "node_modules/@testcontainers/cockroachdb": { "resolved": "packages/modules/cockroachdb", "link": true @@ -19816,6 +19840,17 @@ } } }, + "packages/modules/clickhouse": { + "name": "@testcontainers/clickhouse", + "version": "10.24.1", + "license": "MIT", + "dependencies": { + "testcontainers": "^10.24.1" + }, + "devDependencies": { + "@clickhouse/client": "^1.11.0" + } + }, "packages/modules/cockroachdb": { "name": "@testcontainers/cockroachdb", "version": "10.24.1", diff --git a/packages/modules/clickhouse/package.json b/packages/modules/clickhouse/package.json new file mode 100644 index 000000000..ed8eb91ba --- /dev/null +++ b/packages/modules/clickhouse/package.json @@ -0,0 +1,37 @@ +{ + "name": "@testcontainers/clickhouse", + "version": "10.24.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": "git+https://github.com/testcontainers/testcontainers-node.git" + }, + "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": "^1.11.0" + }, + "dependencies": { + "testcontainers": "^10.24.1" + } +} \ No newline at end of file diff --git a/packages/modules/clickhouse/src/clickhouse-container.test.ts b/packages/modules/clickhouse/src/clickhouse-container.test.ts new file mode 100644 index 000000000..d96864d01 --- /dev/null +++ b/packages/modules/clickhouse/src/clickhouse-container.test.ts @@ -0,0 +1,129 @@ +import { createClient } from "@clickhouse/client"; +import { ClickHouseContainer } from "./clickhouse-container"; + +interface ClickHouseQueryResponse { + data: T[]; +} + +describe("ClickHouseContainer", { timeout: 180_000 }, () => { + // connectWithOptions { + it("should connect using the client options object", async () => { + const container = await new ClickHouseContainer().start(); + const client = createClient(container.getClientOptions()); + + const result = await client.query({ + query: "SELECT 1 AS value", + format: "JSON", + }); + const data = (await result.json()) as ClickHouseQueryResponse<{ value: number }>; + expect(data?.data?.[0]?.value).toBe(1); + + await client.close(); + await container.stop(); + }); + // } + + // connectWithUrl { + it("should connect using the URL", async () => { + const container = await new ClickHouseContainer().start(); + const client = createClient({ + url: container.getConnectionUrl(), + }); + + const result = await client.query({ + query: "SELECT 1 AS value", + format: "JSON", + }); + + const data = (await result.json()) as ClickHouseQueryResponse<{ value: number }>; + expect(data?.data?.[0]?.value).toBe(1); + + await client.close(); + await container.stop(); + }); + // } + + // connectWithUsernameAndPassword { + it("should connect using the username and password", async () => { + const container = await new ClickHouseContainer() + .withUsername("customUsername") + .withPassword("customPassword") + .start(); + + const client = createClient({ + url: container.getHttpUrl(), + username: container.getUsername(), + password: container.getPassword(), + }); + + const result = await client.query({ + query: "SELECT 1 AS value", + format: "JSON", + }); + + const data = (await result.json()) as ClickHouseQueryResponse<{ value: number }>; + expect(data?.data?.[0]?.value).toBe(1); + + await client.close(); + await container.stop(); + }); + // } + + // setDatabase { + it("should set database", async () => { + const customDatabase = "customDatabase"; + const container = await new ClickHouseContainer().withDatabase(customDatabase).start(); + + const client = createClient(container.getClientOptions()); + + const result = await client.query({ + query: "SELECT currentDatabase() AS current_database", + format: "JSON", + }); + + const data = (await result.json()) as ClickHouseQueryResponse<{ current_database: string }>; + expect(data?.data?.[0]?.current_database).toBe(customDatabase); + + await client.close(); + await container.stop(); + }); + // } + + // setUsername { + it("should set username", async () => { + const customUsername = "customUsername"; + const container = await new ClickHouseContainer().withUsername(customUsername).start(); + + const client = createClient(container.getClientOptions()); + + const result = await client.query({ + query: "SELECT currentUser() AS current_user", + format: "JSON", + }); + + const data = (await result.json()) as ClickHouseQueryResponse<{ current_user: string }>; + expect(data?.data?.[0]?.current_user).toBe(customUsername); + + await client.close(); + await container.stop(); + }); + // } + + it("should work with restarted container", async () => { + const container = await new ClickHouseContainer().start(); + await container.restart(); + + const client = createClient(container.getClientOptions()); + + const result = await client.query({ + query: "SELECT 1 AS value", + format: "JSON", + }); + + const data = (await result.json()) as ClickHouseQueryResponse<{ value: number }>; + expect(data?.data?.[0]?.value).toBe(1); + + await client.close(); + await container.stop(); + }); +}); diff --git a/packages/modules/clickhouse/src/clickhouse-container.ts b/packages/modules/clickhouse/src/clickhouse-container.ts new file mode 100644 index 000000000..8386420be --- /dev/null +++ b/packages/modules/clickhouse/src/clickhouse-container.ts @@ -0,0 +1,130 @@ +import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers"; + +const CLICKHOUSE_PORT = 9000; +const CLICKHOUSE_HTTP_PORT = 8123; + +export class ClickHouseContainer extends GenericContainer { + private username = "test"; + private password = "test"; + private database = "test"; + + constructor(image = "clickhouse/clickhouse-server:25.3-alpine") { + super(image); + this.withExposedPorts(CLICKHOUSE_PORT, CLICKHOUSE_HTTP_PORT); + this.withWaitStrategy( + Wait.forHttp("/", CLICKHOUSE_HTTP_PORT).forResponsePredicate((response) => response === "Ok.\n") + ); + this.withStartupTimeout(120_000); + + // Setting this high ulimits value proactively prevents the "Too many open files" error, + // especially under potentially heavy load during testing. + this.withUlimits({ + nofile: { + hard: 262144, + soft: 262144, + }, + }); + } + + 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 { + this.withEnvironment({ + CLICKHOUSE_USER: this.username, + CLICKHOUSE_PASSWORD: this.password, + CLICKHOUSE_DB: this.database, + }); + + return new StartedClickHouseContainer(await super.start(), this.database, this.username, this.password); + } +} + +export class StartedClickHouseContainer extends AbstractStartedContainer { + constructor( + startedTestContainer: StartedTestContainer, + private readonly database: string, + private readonly username: string, + private readonly password: string + ) { + super(startedTestContainer); + } + + public getPort(): number { + return super.getMappedPort(CLICKHOUSE_PORT); + } + + public getHttpPort(): number { + return super.getMappedPort(CLICKHOUSE_HTTP_PORT); + } + + public getUsername(): string { + return this.username; + } + + public getPassword(): string { + return this.password; + } + + public getDatabase(): string { + return this.database; + } + + /** + * Gets the base HTTP URL (protocol, host and mapped port) for the ClickHouse container's HTTP interface. + * Example: `http://localhost:32768` + */ + public getHttpUrl(): string { + const protocol = "http"; + const host = this.getHost(); + const port = this.getHttpPort(); + return `${protocol}://${host}:${port}`; + } + + /** + * Gets configuration options suitable for passing directly to `createClient({...})` + * from `@clickhouse/client`. Uses the HTTP interface. + */ + public getClientOptions(): { + url?: string; + username: string; + password: string; + database: string; + } { + return { + url: this.getHttpUrl(), + username: this.getUsername(), + password: this.getPassword(), + database: this.getDatabase(), + }; + } + + /** + * Gets a ClickHouse connection URL for the HTTP interface with format: + * http://username:password@hostname:port/database + * @returns The ClickHouse HTTP URL string. + */ + public getConnectionUrl(): string { + const url = new URL(this.getHttpUrl()); + + url.username = this.getUsername(); + url.password = this.getPassword(); + + const dbName = this.getDatabase(); + url.pathname = dbName.startsWith("/") ? dbName : `/${dbName}`; + + return url.toString(); + } +} diff --git a/packages/modules/clickhouse/src/index.ts b/packages/modules/clickhouse/src/index.ts new file mode 100644 index 000000000..1882737bc --- /dev/null +++ b/packages/modules/clickhouse/src/index.ts @@ -0,0 +1 @@ +export * from "./clickhouse-container"; diff --git a/packages/modules/clickhouse/tsconfig.build.json b/packages/modules/clickhouse/tsconfig.build.json new file mode 100644 index 000000000..e5121688c --- /dev/null +++ b/packages/modules/clickhouse/tsconfig.build.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + "exclude": [ + "build", + "src/**/*.test.ts" + ], + "references": [ + { + "path": "../../testcontainers" + } + ] +} \ No newline at end of file diff --git a/packages/modules/clickhouse/tsconfig.json b/packages/modules/clickhouse/tsconfig.json new file mode 100644 index 000000000..1c0258e13 --- /dev/null +++ b/packages/modules/clickhouse/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "outDir": "build", + "rootDir": "src" + }, + "include": ["src/**/*"] +} \ No newline at end of file