-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
798c057
commit 1b274c1
Showing
10 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,29 @@ | ||
# Cassandra Module | ||
|
||
[Cassandra](https://cassandra.apache.org/_/index.html) is a free and open source, distributed NoSQL database management system. It is designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure. | ||
|
||
|
||
|
||
## Install | ||
|
||
```bash | ||
npm install @testcontainers/cassandra --save-dev | ||
``` | ||
|
||
## Examples | ||
|
||
<!--codeinclude--> | ||
[Connect:](../../packages/modules/cassandra/src/cassandra-container.test.ts) inside_block:connectWithDefaultCredentials | ||
<!--/codeinclude--> | ||
|
||
<!--codeinclude--> | ||
[Connect with custom credentials:](../../packages/modules/cassandra/src/cassandra-container.test.ts) inside_block:connectWithCustomCredentials | ||
<!--/codeinclude--> | ||
|
||
<!--codeinclude--> | ||
[With custom datacenter / rack](../../packages/modules/cassandra/src/cassandra-container.test.ts) inside_block:customDataSenterAndRack | ||
<!--/codeinclude--> | ||
|
||
<!--codeinclude--> | ||
[Insert & fetch data:](../../packages/modules/cassandra/src/cassandra-container.test.ts) inside_block:createAndFetchData | ||
<!--/codeinclude--> |
This file contains 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 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 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/cassandra", | ||
"version": "10.13.2", | ||
"license": "MIT", | ||
"keywords": [ | ||
"mariadb", | ||
"testing", | ||
"docker", | ||
"testcontainers" | ||
], | ||
"description": "Cassandra 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" | ||
}, | ||
"dependencies": { | ||
"testcontainers": "^10.13.2" | ||
}, | ||
"devDependencies": { | ||
"cassandra-driver": "^4.7.2" | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
packages/modules/cassandra/src/cassandra-container.test.ts
This file contains 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,112 @@ | ||
import { Client } from "cassandra-driver"; | ||
import { CassandraContainer } from "./cassandra-container"; | ||
|
||
describe("Cassandra", () => { | ||
jest.setTimeout(240_000); | ||
|
||
// connectWithDefaultCredentials { | ||
it("should connect and execute a query with default credentials", async () => { | ||
const container = await new CassandraContainer("cassandra:5.0.2").start(); | ||
|
||
const client = new Client({ | ||
contactPoints: [container.getContactPoint()], | ||
localDataCenter: container.getDatacenter(), | ||
keyspace: "system", | ||
}); | ||
|
||
await client.connect(); | ||
|
||
const result = await client.execute("SELECT release_version FROM system.local"); | ||
expect(result.rows[0].release_version).toBe("5.0.2"); | ||
|
||
await client.shutdown(); | ||
await container.stop(); | ||
}); | ||
// } | ||
|
||
// connectWithCustomCredentials { | ||
it("should connect with custom username and password", async () => { | ||
const username = "testUser"; | ||
const password = "testPassword"; | ||
|
||
const container = await new CassandraContainer().withUsername(username).withPassword(password).start(); | ||
|
||
const client = new Client({ | ||
contactPoints: [container.getContactPoint()], | ||
localDataCenter: container.getDatacenter(), | ||
credentials: { username, password }, | ||
keyspace: "system", | ||
}); | ||
|
||
await client.connect(); | ||
|
||
const result = await client.execute("SELECT release_version FROM system.local"); | ||
expect(result.rows.length).toBeGreaterThan(0); | ||
|
||
await client.shutdown(); | ||
await container.stop(); | ||
}); | ||
// } | ||
|
||
// customDataSenterAndRack { | ||
it("should set datacenter and rack", async () => { | ||
const customDataCenter = "customDC"; | ||
const customRack = "customRack"; | ||
const container = await new CassandraContainer().withDatacenter(customDataCenter).withRack(customRack).start(); | ||
|
||
const client = new Client({ | ||
contactPoints: [container.getContactPoint()], | ||
localDataCenter: container.getDatacenter(), | ||
}); | ||
|
||
await client.connect(); | ||
const result = await client.execute("SELECT data_center, rack FROM system.local"); | ||
expect(result.rows[0].data_center).toBe(customDataCenter); | ||
expect(result.rows[0].rack).toBe(customRack); | ||
|
||
await client.shutdown(); | ||
await container.stop(); | ||
}); | ||
// } | ||
|
||
// createAndFetchData { | ||
it("should create keyspace, a table, insert data, and retrieve it", async () => { | ||
const container = await new CassandraContainer().start(); | ||
|
||
const client = new Client({ | ||
contactPoints: [container.getContactPoint()], | ||
localDataCenter: container.getDatacenter(), | ||
}); | ||
|
||
await client.connect(); | ||
|
||
// Create the keyspace | ||
await client.execute(` | ||
CREATE KEYSPACE IF NOT EXISTS test_keyspace | ||
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} | ||
`); | ||
|
||
await client.execute("USE test_keyspace"); | ||
|
||
// Create the table. | ||
await client.execute(` | ||
CREATE TABLE IF NOT EXISTS test_keyspace.users ( | ||
id UUID PRIMARY KEY, | ||
name text | ||
) | ||
`); | ||
|
||
// Insert a record | ||
const id = "d002cd08-401a-47d6-92d7-bb4204d092f8"; // Fixed UUID for testing | ||
const username = "Testy McTesterson"; | ||
client.execute("INSERT INTO test_keyspace.users (id, name) VALUES (?, ?)", [id, username]); | ||
|
||
// Fetch and verify the record | ||
const result = await client.execute("SELECT * FROM test_keyspace.users WHERE id = ?", [id], { prepare: true }); | ||
expect(result.rows[0].name).toBe(username); | ||
|
||
await client.shutdown(); | ||
await container.stop(); | ||
}); | ||
// } | ||
}); |
This file contains 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,89 @@ | ||
import { AbstractStartedContainer, GenericContainer, type StartedTestContainer } from "testcontainers"; | ||
|
||
const CASSANDRA_PORT = 9042; | ||
|
||
export class CassandraContainer extends GenericContainer { | ||
private dc = "dc1"; | ||
private rack = "rack1"; | ||
private username = "cassandra"; | ||
private password = "cassandra"; | ||
|
||
constructor(image = "cassandra:5.0.2") { | ||
super(image); | ||
this.withExposedPorts(CASSANDRA_PORT).withStartupTimeout(120_000); | ||
} | ||
|
||
public withDatacenter(dc: string): this { | ||
this.dc = dc; | ||
return this; | ||
} | ||
|
||
public withRack(rack: string): this { | ||
this.rack = rack; | ||
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<StartedCassandraContainer> { | ||
this.withEnvironment({ | ||
CASSANDRA_DC: this.dc, | ||
CASSANDRA_RACK: this.rack, | ||
CASSANDRA_LISTEN_ADDRESS: "auto", | ||
CASSANDRA_BROADCAST_ADDRESS: "auto", | ||
CASSANDRA_RPC_ADDRESS: "0.0.0.0", | ||
CASSANDRA_USERNAME: this.username, | ||
CASSANDRA_PASSWORD: this.password, | ||
CASSANDRA_SNITCH: "GossipingPropertyFileSnitch", | ||
CASSANDRA_ENDPOINT_SNITCH: "GossipingPropertyFileSnitch", | ||
}); | ||
return new StartedCassandraContainer(await super.start(), this.dc, this.rack, this.username, this.password); | ||
} | ||
} | ||
|
||
export class StartedCassandraContainer extends AbstractStartedContainer { | ||
private readonly port: number; | ||
|
||
constructor( | ||
startedTestContainer: StartedTestContainer, | ||
private readonly dc: string, | ||
private readonly rack: string, | ||
private readonly username: string, | ||
private readonly password: string | ||
) { | ||
super(startedTestContainer); | ||
this.port = startedTestContainer.getMappedPort(CASSANDRA_PORT); | ||
} | ||
|
||
public getPort(): number { | ||
return this.port; | ||
} | ||
|
||
public getDatacenter(): string { | ||
return this.dc; | ||
} | ||
|
||
public getRack(): string { | ||
return this.rack; | ||
} | ||
|
||
public getUsername(): string { | ||
return this.username; | ||
} | ||
|
||
public getPassword(): string { | ||
return this.password; | ||
} | ||
|
||
public getContactPoint(): string { | ||
return `${this.getHost()}:${this.getPort()}`; | ||
} | ||
} |
This file contains 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 { CassandraContainer, StartedCassandraContainer } from "./cassandra-container"; |
This file contains 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" | ||
} | ||
] | ||
} |
Oops, something went wrong.