Skip to content

Commit 0067938

Browse files
committed
feat: Adds Cockroachdb container
1 parent 3b48cc0 commit 0067938

File tree

10 files changed

+288
-0
lines changed

10 files changed

+288
-0
lines changed

docs/modules/cockroachdb.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# CockroachDB Module
2+
3+
[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.
4+
5+
6+
## Install
7+
8+
```bash
9+
npm install @testcontainers/cockroachdb --save-dev
10+
```
11+
12+
## Examples
13+
14+
<!--codeinclude-->
15+
[Connect and execute query:](../../packages/modules/cockroachdb/src/cockroachdb-container.test.ts) inside_block:connect
16+
<!--/codeinclude-->
17+
18+
<!--codeinclude-->
19+
[Connect and execute query using URI:](../../packages/modules/cockroachdb/src/cockroachdb-container.test.ts) inside_block:uriConnect
20+
<!--/codeinclude-->
21+
22+
<!--codeinclude-->
23+
[Set database:](../../packages/modules/cockroachdb/src/cockroachdb-container.test.ts) inside_block:setDatabase
24+
<!--/codeinclude-->
25+
26+
<!--codeinclude-->
27+
[Set username:](../../packages/modules/cockroachdb/src/cockroachdb-container.test.ts) inside_block:setUsername
28+
<!--/codeinclude-->

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ nav:
4747
- Cassandra: modules/cassandra.md
4848
- ChromaDB: modules/chromadb.md
4949
- Couchbase: modules/couchbase.md
50+
- CockroachDB: modules/cockroachdb.md
5051
- Elasticsearch: modules/elasticsearch.md
5152
- EventStoreDB: modules/eventstoredb.md
5253
- GCloud: modules/gcloud.md

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Config } from "jest";
2+
import * as path from "path";
3+
4+
const config: Config = {
5+
preset: "ts-jest",
6+
moduleNameMapper: {
7+
"^testcontainers$": path.resolve(__dirname, "../../testcontainers/src"),
8+
},
9+
};
10+
11+
export default config;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "@testcontainers/cockroachdb",
3+
"version": "10.18.0",
4+
"license": "MIT",
5+
"keywords": [
6+
"cockroachdb",
7+
"crdb",
8+
"testing",
9+
"docker",
10+
"testcontainers"
11+
],
12+
"description": "CockroachDB module for Testcontainers",
13+
"homepage": "https://github.com/testcontainers/testcontainers-node#readme",
14+
"repository": {
15+
"type": "git",
16+
"url": "https://github.com/testcontainers/testcontainers-node"
17+
},
18+
"bugs": {
19+
"url": "https://github.com/testcontainers/testcontainers-node/issues"
20+
},
21+
"main": "build/index.js",
22+
"files": [
23+
"build"
24+
],
25+
"publishConfig": {
26+
"access": "public"
27+
},
28+
"scripts": {
29+
"prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .",
30+
"build": "tsc --project tsconfig.build.json"
31+
},
32+
"devDependencies": {
33+
"@types/pg": "^8.11.6",
34+
"pg": "^8.12.0"
35+
},
36+
"dependencies": {
37+
"testcontainers": "^10.18.0"
38+
}
39+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { Client } from "pg";
2+
import { CockroachDbContainer } from "./cockroachdb-container";
3+
4+
describe("CockroachDbContainer", () => {
5+
jest.setTimeout(180_000);
6+
7+
// connect {
8+
it("should connect and return a query result", async () => {
9+
const container = await new CockroachDbContainer().start();
10+
11+
console.log(container.getDatabase(), container.getHost(), container.getPort());
12+
13+
const client = new Client({
14+
host: container.getHost(),
15+
port: container.getPort(),
16+
database: container.getDatabase(),
17+
user: container.getUsername(),
18+
ssl: false,
19+
});
20+
21+
await client.connect();
22+
23+
const result = await client.query("SELECT 1");
24+
expect(result.rows[0]).toEqual({ "?column?": "1" });
25+
26+
await client.end();
27+
await container.stop();
28+
});
29+
// }
30+
31+
// uriConnect {
32+
it("should work with database URI", async () => {
33+
const container = await new CockroachDbContainer().start();
34+
35+
const client = new Client({
36+
connectionString: container.getConnectionUri(),
37+
});
38+
await client.connect();
39+
40+
const result = await client.query("SELECT 1");
41+
expect(result.rows[0]).toEqual({ "?column?": "1" });
42+
43+
await client.end();
44+
await container.stop();
45+
});
46+
// }
47+
48+
// setDatabase {
49+
it("should set database", async () => {
50+
const container = await new CockroachDbContainer().withDatabase("custom_database").start();
51+
52+
const client = new Client({
53+
host: container.getHost(),
54+
port: container.getPort(),
55+
database: container.getDatabase(),
56+
user: container.getUsername(),
57+
});
58+
await client.connect();
59+
60+
const result = await client.query("SELECT current_database()");
61+
expect(result.rows[0]).toEqual({ current_database: "custom_database" });
62+
63+
await client.end();
64+
await container.stop();
65+
});
66+
// }
67+
68+
// setUsername {
69+
it("should set username", async () => {
70+
const container = await new CockroachDbContainer().withUsername("custom_username").start();
71+
72+
const client = new Client({
73+
host: container.getHost(),
74+
port: container.getPort(),
75+
database: container.getDatabase(),
76+
user: container.getUsername(),
77+
});
78+
await client.connect();
79+
80+
const result = await client.query("SELECT current_user");
81+
expect(result.rows[0]).toEqual({ current_user: "custom_username" });
82+
83+
await client.end();
84+
await container.stop();
85+
});
86+
// }
87+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers";
2+
3+
const COCKROACH_PORT = 26257;
4+
5+
export class CockroachDbContainer extends GenericContainer {
6+
private database = "test";
7+
private username = "test";
8+
9+
constructor(image = "cockroachdb/cockroach:v24.3.5") {
10+
super(image);
11+
this.withExposedPorts(COCKROACH_PORT)
12+
.withCommand(["start-single-node", "--insecure"])
13+
.withWaitStrategy(Wait.forLogMessage(/.*end running init files.*/, 1))
14+
.withStartupTimeout(120_000);
15+
}
16+
17+
public withDatabase(database: string): this {
18+
this.database = database;
19+
return this;
20+
}
21+
22+
public withUsername(username: string): this {
23+
this.username = username;
24+
return this;
25+
}
26+
27+
public override async start(): Promise<StartedCockroachDbContainer> {
28+
this.withEnvironment({
29+
COCKROACH_DATABASE: this.database,
30+
COCKROACH_USER: this.username,
31+
});
32+
return new StartedCockroachDbContainer(await super.start(), this.database, this.username);
33+
}
34+
}
35+
36+
export class StartedCockroachDbContainer extends AbstractStartedContainer {
37+
private readonly port: number;
38+
39+
constructor(
40+
startedTestContainer: StartedTestContainer,
41+
private readonly database: string,
42+
private readonly username: string
43+
) {
44+
super(startedTestContainer);
45+
this.port = startedTestContainer.getMappedPort(COCKROACH_PORT);
46+
}
47+
48+
public getPort(): number {
49+
return this.port;
50+
}
51+
52+
public getDatabase(): string {
53+
return this.database;
54+
}
55+
56+
public getUsername(): string {
57+
return this.username;
58+
}
59+
60+
/**
61+
* @returns A connection URI in the form of `postgres[ql]://[username[:password]@][host[:port],]/database`
62+
*/
63+
public getConnectionUri(): string {
64+
const url = new URL("", "postgres://");
65+
url.hostname = this.getHost();
66+
url.port = this.getPort().toString();
67+
url.pathname = this.getDatabase();
68+
url.username = this.getUsername();
69+
return url.toString();
70+
}
71+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { CockroachDbContainer, StartedCockroachDbContainer } from "./cockroachdb-container";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": [
4+
"build",
5+
"jest.config.ts",
6+
"src/**/*.test.ts"
7+
],
8+
"references": [
9+
{
10+
"path": "../../testcontainers"
11+
}
12+
]
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"extends": "../../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "build",
6+
"paths": {
7+
"testcontainers": [
8+
"../../testcontainers/src"
9+
]
10+
}
11+
},
12+
"exclude": [
13+
"build",
14+
"jest.config.ts"
15+
],
16+
"references": [
17+
{
18+
"path": "../../testcontainers"
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)