Skip to content

Commit

Permalink
Add RabbitMQ module (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
gundersong committed Jun 10, 2024
1 parent 441743c commit e5e772b
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/modules/rabbitmq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# RabbitMQ Module

[RabbitMQ](https://www.rabbitmq.com/) is a reliable and mature messaging and streaming broker, which is easy to deploy on cloud environments, on-premises, and on your local machine. It is currently used by millions worldwide.

## Install

```bash
npm install @testcontainers/rabbitmq --save-dev
```

## Examples

<!--codeinclude-->
[Connect:](../../packages/modules/rabbitmq/src/rabbitmq-container.test.ts) inside_block:start
<!--/codeinclude-->

<!--codeinclude-->
[Set credentials:](../../packages/modules/rabbitmq/src/rabbitmq-container.test.ts) inside_block:credentials
<!--/codeinclude-->

<!--codeinclude-->
[Publish and subscribe:](../../packages/modules/rabbitmq/src/rabbitmq-container.test.ts) inside_block:pubsub
<!--/codeinclude-->
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ nav:
- Neo4J: modules/neo4j.md
- PostgreSQL: modules/postgresql.md
- Qdrant: modules/qdrant.md
- RabbitMQ: modules/rabbitmq.md
- Redis: modules/redis.md
- Redpanda: modules/redpanda.md
- Selenium: modules/selenium.md
Expand Down
112 changes: 112 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/modules/rabbitmq/jest.config.ts
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;
38 changes: 38 additions & 0 deletions packages/modules/rabbitmq/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@testcontainers/rabbitmq",
"version": "10.9.0",
"license": "MIT",
"keywords": [
"rabbitmq",
"testing",
"docker",
"testcontainers"
],
"description": "RabbitMQ 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.9.0"
},
"devDependencies": {
"@types/amqplib": "^0.10.4",
"amqplib": "^0.10.4"
}
}
1 change: 1 addition & 0 deletions packages/modules/rabbitmq/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { RabbitMQContainer, StartedRabbitMQContainer } from "./rabbitmq-container";
68 changes: 68 additions & 0 deletions packages/modules/rabbitmq/src/rabbitmq-container.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import amqp from "amqplib";
import { RabbitMQContainer } from "./rabbitmq-container";

describe("RabbitMQContainer", () => {
jest.setTimeout(240_000);

// start {
it("should start, connect and close", async () => {
const rabbitMQContainer = await new RabbitMQContainer().start();

const connection = await amqp.connect(rabbitMQContainer.getAmqpUrl());
await connection.close();

await rabbitMQContainer.stop();
});
// }

// credentials {
it("different username and password", async () => {
const USER = "user";
const PASSWORD = "password";

const rabbitMQContainer = await new RabbitMQContainer()
.withEnvironment({
RABBITMQ_DEFAULT_USER: USER,
RABBITMQ_DEFAULT_PASS: PASSWORD,
})
.start();

const connection = await amqp.connect({
username: USER,
password: PASSWORD,
port: rabbitMQContainer.getMappedPort(5672),
});

await connection.close();

await rabbitMQContainer.stop();
});
// }

// pubsub {
it("test publish and subscribe", async () => {
const QUEUE = "test";
const PAYLOAD = "Hello World";

const rabbitMQContainer = await new RabbitMQContainer().start();
const connection = await amqp.connect(rabbitMQContainer.getAmqpUrl());

const channel = await connection.createChannel();
await channel.assertQueue(QUEUE);

channel.sendToQueue(QUEUE, Buffer.from(PAYLOAD));

await new Promise((resolve) => {
channel.consume(QUEUE, (message) => {
expect(message?.content.toString()).toEqual(PAYLOAD);
resolve(true);
});
});

await channel.close();
await connection.close();

await rabbitMQContainer.stop();
}, 10_000);
// }
});
39 changes: 39 additions & 0 deletions packages/modules/rabbitmq/src/rabbitmq-container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers";

const AMQP_PORT = 5672;
const AMQPS_PORT = 5671;
const HTTPS_PORT = 15671;
const HTTP_PORT = 15672;
const RABBITMQ_DEFAULT_USER = "guest";
const RABBITMQ_DEFAULT_PASS = "guest";

export class RabbitMQContainer extends GenericContainer {
constructor(image = "rabbitmq:3.12.11-management-alpine") {
super(image);
this.withExposedPorts(AMQP_PORT, AMQPS_PORT, HTTPS_PORT, HTTP_PORT)
.withEnvironment({
RABBITMQ_DEFAULT_USER,
RABBITMQ_DEFAULT_PASS,
})
.withWaitStrategy(Wait.forLogMessage("Server startup complete"))
.withStartupTimeout(30_000);
}

public override async start(): Promise<StartedRabbitMQContainer> {
return new StartedRabbitMQContainer(await super.start());
}
}

export class StartedRabbitMQContainer extends AbstractStartedContainer {
constructor(startedTestContainer: StartedTestContainer) {
super(startedTestContainer);
}

public getAmqpUrl(): string {
return `amqp://${this.getHost()}:${this.getMappedPort(AMQP_PORT)}`;
}

public getAmqpsUrl(): string {
return `amqps://${this.getHost()}:${this.getMappedPort(AMQPS_PORT)}`;
}
}
13 changes: 13 additions & 0 deletions packages/modules/rabbitmq/tsconfig.build.json
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"
}
]
}
Loading

0 comments on commit e5e772b

Please sign in to comment.