Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/modules/mosquitto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Mosquitto

## Install

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

## Examples

These examples use the following libraries:

- [mqtt](https://www.npmjs.com/package/mqtt)

npm install mqtt

Choose an image from the [container registry](https://hub.docker.com/r/eclipse-mosquitto) and substitute `IMAGE`.

### Produce/consume a message (anonymous)

<!--codeinclude-->
[](../../packages/modules/mosquitto/src/mosquitto-container.test.ts) inside_block:mosquittoConnectAnonymous
<!--/codeinclude-->

### Produce/consume a message (with credentials)

<!--codeinclude-->
[](../../packages/modules/mosquitto/src/mosquitto-container.test.ts) inside_block:mosquittoConnectWithCredentials
<!--/codeinclude-->
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ nav:
- MinIO: modules/minio.md
- Mockserver: modules/mockserver.md
- MongoDB: modules/mongodb.md
- Mosquitto: modules/mosquitto.md
- MSSQLServer: modules/mssqlserver.md
- MySQL: modules/mysql.md
- Nats: modules/nats.md
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/modules/mosquitto/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM eclipse-mosquitto:2.0.22
38 changes: 38 additions & 0 deletions packages/modules/mosquitto/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@testcontainers/mosquitto",
"version": "12.0.3",
"license": "MIT",
"keywords": [
"mosquitto",
"mqtt",
"testing",
"docker",
"testcontainers"
],
"description": "Mosquitto 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": {
"mqtt": "^5.15.1"
},
"dependencies": {
"testcontainers": "^12.0.3"
}
}
1 change: 1 addition & 0 deletions packages/modules/mosquitto/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MosquittoContainer, StartedMosquittoContainer } from "./mosquitto-container";
59 changes: 59 additions & 0 deletions packages/modules/mosquitto/src/mosquitto-container.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import mqtt from "mqtt";
import { expect } from "vitest";
import { getImage } from "../../../testcontainers/src/utils/test-helper";
import { MosquittoContainer } from "./mosquitto-container";

const IMAGE = getImage(__dirname);

describe("MosquittoContainer", { timeout: 240_000 }, () => {
it("should connect to Mosquitto via MQTT.js (anonymous)", async () => {
// mosquittoConnectAnonymous {
await using container = await new MosquittoContainer(IMAGE).start();

expect(container.getConnectionString()).toBe(`mqtt://${container.getHost()}:${container.getPort()}`);

const mqttClient = await mqtt.connectAsync(container.getConnectionString());

const firstMessagePromise = new Promise<{ topic: string; message: Buffer }>((resolve, reject) => {
mqttClient.once("message", (topic, message) => resolve({ topic, message }));
mqttClient.once("error", (err) => reject(err));
});

await mqttClient.subscribeAsync("test");
await mqttClient.publishAsync("test", "Test Message");

const { message } = await firstMessagePromise;
expect(message.toString()).toEqual("Test Message");

mqttClient.end();
// }
});

it("should connect to Mosquitto via MQTT.js (with credentials)", async () => {
// mosquittoConnectWithCredentials {
await using container = await new MosquittoContainer(IMAGE)
.withUsername("testuser")
.withPassword("testpass")
.start();

expect(container.getConnectionString()).toBe(
`mqtt://testuser:testpass@${container.getHost()}:${container.getPort()}`
);

const mqttClient = await mqtt.connectAsync(container.getConnectionString());

const firstMessagePromise = new Promise<{ topic: string; message: Buffer }>((resolve, reject) => {
mqttClient.once("message", (topic, message) => resolve({ topic, message }));
mqttClient.once("error", (err) => reject(err));
});

await mqttClient.subscribeAsync("secure");
await mqttClient.publishAsync("secure", "Secure Message");

const { message } = await firstMessagePromise;
expect(message.toString()).toEqual("Secure Message");

mqttClient.end();
// }
});
});
70 changes: 70 additions & 0 deletions packages/modules/mosquitto/src/mosquitto-container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers";

const MQTT_PORT = 1883;
const CONFIG_PATH = "/mosquitto/config/mosquitto.conf";
const PASSWORD_FILE_PATH = "/mosquitto/config/passwords";

export class MosquittoContainer extends GenericContainer {
private username?: string;
private password?: string;

constructor(image: string) {
super(image);
this.withExposedPorts(MQTT_PORT).withWaitStrategy(Wait.forListeningPorts()).withStartupTimeout(120_000);
}

public withUsername(username: string): this {
if (!username) throw new Error("Username should not be empty.");
this.username = username;
return this;
}

public withPassword(password: string): this {
if (!password) throw new Error("Password should not be empty.");
this.password = password;
return this;
}
Comment thread
cristianrgreco marked this conversation as resolved.

public override async start(): Promise<StartedMosquittoContainer> {
if ((this.username === undefined) !== (this.password === undefined)) {
throw new Error("Both username and password must be set together.");
}

if (this.username !== undefined && this.password !== undefined) {
Comment thread
cristianrgreco marked this conversation as resolved.
const config = `listener ${MQTT_PORT}\npassword_file ${PASSWORD_FILE_PATH}\n`;
this.withCopyContentToContainer([{ content: config, target: CONFIG_PATH }])
.withEnvironment({ MQTT_USER: this.username, MQTT_PASS: this.password })
.withEntrypoint(["/bin/sh"])
.withCommand([
"-c",
`mosquitto_passwd -b -c "${PASSWORD_FILE_PATH}" "$MQTT_USER" "$MQTT_PASS" && chown mosquitto:mosquitto "${PASSWORD_FILE_PATH}" && exec mosquitto -c "${CONFIG_PATH}"`,
]);
} else {
const config = `listener ${MQTT_PORT}\nallow_anonymous true\n`;
this.withCopyContentToContainer([{ content: config, target: CONFIG_PATH }]);
}

return new StartedMosquittoContainer(await super.start(), this.username, this.password);
}
}

export class StartedMosquittoContainer extends AbstractStartedContainer {
constructor(
startedTestContainer: StartedTestContainer,
private readonly username?: string,
private readonly password?: string
) {
super(startedTestContainer);
}

public getPort(): number {
return this.getMappedPort(MQTT_PORT);
}

public getConnectionString(): string {
if (this.username && this.password) {
return `mqtt://${encodeURIComponent(this.username)}:${encodeURIComponent(this.password)}@${this.getHost()}:${this.getPort()}`;
}
return `mqtt://${this.getHost()}:${this.getPort()}`;
}
}
12 changes: 12 additions & 0 deletions packages/modules/mosquitto/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"exclude": [
"build",
"src/**/*.test.ts"
],
"references": [
{
"path": "../../testcontainers"
}
]
}
20 changes: 20 additions & 0 deletions packages/modules/mosquitto/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build",
"paths": {
"testcontainers": [
"../../testcontainers/src"
]
}
},
"exclude": [
"build"
],
"references": [
{
"path": "../../testcontainers"
}
]
}
Loading