-
-
Notifications
You must be signed in to change notification settings - Fork 251
Add CockroachDB module #910
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
af977a9
feat: Adds Cockroachdb container
leourbina 8ca9e3d
Update healthcheck command
cristianrgreco 0742fd1
Port is not guaranteed to be different on restart
cristianrgreco 55bb9b9
Merge branch 'main' into pr/leourbina/910
cristianrgreco 075173a
Add support for custom health checks in CockroachDbContainer
cristianrgreco 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,28 @@ | ||
| # CockroachDB Module | ||
|
|
||
| [CockroachDB](https://github.com/cockroachdb/cockroach) is a cloud-native, postgresql compatible, distributed SQL database designed to build, scale, and manage modern, data-intensive applications. | ||
|
|
||
|
|
||
| ## Install | ||
|
|
||
| ```bash | ||
| npm install @testcontainers/cockroachdb --save-dev | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| <!--codeinclude--> | ||
| [Connect and execute query:](../../packages/modules/cockroachdb/src/cockroachdb-container.test.ts) inside_block:connect | ||
| <!--/codeinclude--> | ||
|
|
||
| <!--codeinclude--> | ||
| [Connect and execute query using URI:](../../packages/modules/cockroachdb/src/cockroachdb-container.test.ts) inside_block:uriConnect | ||
| <!--/codeinclude--> | ||
|
|
||
| <!--codeinclude--> | ||
| [Set database:](../../packages/modules/cockroachdb/src/cockroachdb-container.test.ts) inside_block:setDatabase | ||
| <!--/codeinclude--> | ||
|
|
||
| <!--codeinclude--> | ||
| [Set username:](../../packages/modules/cockroachdb/src/cockroachdb-container.test.ts) inside_block:setUsername | ||
| <!--/codeinclude--> |
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,39 @@ | ||
| { | ||
| "name": "@testcontainers/cockroachdb", | ||
| "version": "10.18.0", | ||
| "license": "MIT", | ||
| "keywords": [ | ||
| "cockroachdb", | ||
| "crdb", | ||
| "testing", | ||
| "docker", | ||
| "testcontainers" | ||
| ], | ||
| "description": "CockroachDB 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": { | ||
| "@types/pg": "^8.11.6", | ||
| "pg": "^8.12.0" | ||
| }, | ||
| "dependencies": { | ||
| "testcontainers": "^10.18.0" | ||
| } | ||
| } |
115 changes: 115 additions & 0 deletions
115
packages/modules/cockroachdb/src/cockroachdb-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,115 @@ | ||
| import { Client } from "pg"; | ||
| import { CockroachDbContainer } from "./cockroachdb-container"; | ||
|
|
||
| describe("CockroachDbContainer", () => { | ||
| jest.setTimeout(180_000); | ||
|
|
||
| // connect { | ||
| it("should connect and return a query result", async () => { | ||
| const container = await new CockroachDbContainer().start(); | ||
|
|
||
| const client = new Client({ | ||
| host: container.getHost(), | ||
| port: container.getPort(), | ||
| database: container.getDatabase(), | ||
| user: container.getUsername(), | ||
| ssl: false, | ||
| }); | ||
|
|
||
| await client.connect(); | ||
|
|
||
| const result = await client.query("SELECT 1"); | ||
| expect(result.rows[0]).toEqual({ "?column?": "1" }); | ||
|
|
||
| await client.end(); | ||
| await container.stop(); | ||
| }); | ||
| // } | ||
|
|
||
| // uriConnect { | ||
| it("should work with database URI", async () => { | ||
| const container = await new CockroachDbContainer().start(); | ||
|
|
||
| const client = new Client({ | ||
| connectionString: container.getConnectionUri(), | ||
| }); | ||
| await client.connect(); | ||
|
|
||
| const result = await client.query("SELECT 1"); | ||
| expect(result.rows[0]).toEqual({ "?column?": "1" }); | ||
|
|
||
| await client.end(); | ||
| await container.stop(); | ||
| }); | ||
| // } | ||
|
|
||
| // setDatabase { | ||
| it("should set database", async () => { | ||
| const container = await new CockroachDbContainer().withDatabase("custom_database").start(); | ||
|
|
||
| const client = new Client({ | ||
| host: container.getHost(), | ||
| port: container.getPort(), | ||
| database: container.getDatabase(), | ||
| user: container.getUsername(), | ||
| }); | ||
| await client.connect(); | ||
|
|
||
| const result = await client.query("SELECT current_database()"); | ||
| expect(result.rows[0]).toEqual({ current_database: "custom_database" }); | ||
|
|
||
| await client.end(); | ||
| await container.stop(); | ||
| }); | ||
| // } | ||
|
|
||
| // setUsername { | ||
| it("should set username", async () => { | ||
| const container = await new CockroachDbContainer().withUsername("custom_username").start(); | ||
|
|
||
| const client = new Client({ | ||
| host: container.getHost(), | ||
| port: container.getPort(), | ||
| database: container.getDatabase(), | ||
| user: container.getUsername(), | ||
| }); | ||
| await client.connect(); | ||
|
|
||
| const result = await client.query("SELECT current_user"); | ||
| expect(result.rows[0]).toEqual({ current_user: "custom_username" }); | ||
|
|
||
| await client.end(); | ||
| await container.stop(); | ||
| }); | ||
| // } | ||
|
|
||
| it("should work with restarted container", async () => { | ||
| const container = await new CockroachDbContainer().start(); | ||
| await container.restart(); | ||
|
|
||
| const client = new Client({ | ||
| host: container.getHost(), | ||
| port: container.getPort(), | ||
| database: container.getDatabase(), | ||
| user: container.getUsername(), | ||
| }); | ||
| await client.connect(); | ||
|
|
||
| const result = await client.query("SELECT 1"); | ||
| expect(result.rows[0]).toEqual({ "?column?": "1" }); | ||
|
|
||
| await client.end(); | ||
| await container.stop(); | ||
| }); | ||
|
|
||
| it("should allow custom healthcheck", async () => { | ||
| const container = new CockroachDbContainer().withHealthCheck({ | ||
| test: ["CMD-SHELL", "exit 1"], | ||
| interval: 100, | ||
| retries: 0, | ||
| timeout: 0, | ||
| }); | ||
|
|
||
| await expect(() => container.start()).rejects.toThrow(); | ||
| }); | ||
| }); | ||
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,76 @@ | ||
| import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers"; | ||
|
|
||
| const COCKROACH_PORT = 26257; | ||
| const COCKROACH_HTTP_PORT = 26258; | ||
|
|
||
| export class CockroachDbContainer extends GenericContainer { | ||
| private database = "test"; | ||
| private username = "test"; | ||
cristianrgreco marked this conversation as resolved.
Dismissed
Show dismissed
Hide dismissed
|
||
|
|
||
| constructor(image = "cockroachdb/cockroach:v24.3.5") { | ||
| super(image); | ||
| this.withExposedPorts(COCKROACH_PORT, COCKROACH_HTTP_PORT) | ||
| .withCommand(["start-single-node", "--insecure", `--http-addr=0.0.0.0:${COCKROACH_HTTP_PORT}`]) | ||
| .withWaitStrategy(Wait.forHealthCheck()); | ||
| } | ||
|
|
||
| public withDatabase(database: string): this { | ||
| this.database = database; | ||
| return this; | ||
| } | ||
|
|
||
| public withUsername(username: string): this { | ||
| this.username = username; | ||
| return this; | ||
| } | ||
|
|
||
| public override async start(): Promise<StartedCockroachDbContainer> { | ||
| this.withEnvironment({ | ||
| COCKROACH_DATABASE: this.database, | ||
| COCKROACH_USER: this.username, | ||
| }); | ||
| if (!this.healthCheck) { | ||
| this.withHealthCheck({ | ||
| test: ["CMD-SHELL", `cockroach sql --insecure -u ${this.username} -d ${this.database} -e "SELECT 1;"`], | ||
| interval: 250, | ||
| timeout: 1000, | ||
| retries: 1000, | ||
| }); | ||
| } | ||
| return new StartedCockroachDbContainer(await super.start(), this.database, this.username); | ||
| } | ||
| } | ||
|
|
||
| export class StartedCockroachDbContainer extends AbstractStartedContainer { | ||
| constructor( | ||
| startedTestContainer: StartedTestContainer, | ||
| private readonly database: string, | ||
| private readonly username: string | ||
| ) { | ||
| super(startedTestContainer); | ||
| } | ||
|
|
||
| public getPort(): number { | ||
| return this.startedTestContainer.getMappedPort(COCKROACH_PORT); | ||
| } | ||
|
|
||
| public getDatabase(): string { | ||
| return this.database; | ||
| } | ||
|
|
||
| public getUsername(): string { | ||
| return this.username; | ||
| } | ||
|
|
||
| /** | ||
| * @returns A connection URI in the form of `postgres[ql]://[username[:password]@][host[:port],]/database` | ||
| */ | ||
| public getConnectionUri(): string { | ||
| const url = new URL("", "postgres://"); | ||
| url.hostname = this.getHost(); | ||
| url.port = this.getPort().toString(); | ||
| url.pathname = this.getDatabase(); | ||
| url.username = this.getUsername(); | ||
| return url.toString(); | ||
| } | ||
| } | ||
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 { CockroachDbContainer, StartedCockroachDbContainer } from "./cockroachdb-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,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.