-
-
Notifications
You must be signed in to change notification settings - Fork 264
Add Mosquitto module #1390
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
cristianrgreco
merged 12 commits into
testcontainers:main
from
ChxGuillaume:feat/mosquitto-module
Jun 29, 2026
Merged
Add Mosquitto module #1390
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bccad75
Add Mosquitto module for Testcontainers, including container setup, t…
ChxGuillaume 6fc7ba0
Update Mosquitto module: refine connection string tests, adjust start…
ChxGuillaume fcc2a86
Split Mosquitto tests: extracted credential-based connection test int…
ChxGuillaume 09e4677
Pin Mosquitto base image to version 2.0.22 in Dockerfile
ChxGuillaume 6574f9f
Reintegrate Mosquitto credential-based test into main test file and u…
ChxGuillaume e5b5965
Validate non-empty username and password in Mosquitto container confi…
ChxGuillaume 1723a5a
Validate that username and password are set together in Mosquitto con…
ChxGuillaume ffd1018
Reorder Mosquitto module entry in mkdocs navigation configuration.
ChxGuillaume 2d65607
Update Mosquitto container wait strategy to use listening ports inste…
ChxGuillaume 8f4c37c
Add libc requirements for various bindings in package-lock.json
ChxGuillaume f25806a
Simplify `StartedMosquittoContainer` port handling by removing redund…
ChxGuillaume 676fffb
Fix formatting in `getPort` method and adjust libc definitions in `pa…
ChxGuillaume 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,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--> |
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 @@ | ||
| FROM eclipse-mosquitto:2.0.22 |
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,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" | ||
| } | ||
| } |
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 { MosquittoContainer, StartedMosquittoContainer } from "./mosquitto-container"; |
59 changes: 59 additions & 0 deletions
59
packages/modules/mosquitto/src/mosquitto-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,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(); | ||
| // } | ||
| }); | ||
| }); |
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,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; | ||
| } | ||
|
|
||
| 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) { | ||
|
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()}`; | ||
| } | ||
| } | ||
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,12 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "exclude": [ | ||
| "build", | ||
| "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,20 @@ | ||
| { | ||
| "extends": "../../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "rootDir": "src", | ||
| "outDir": "build", | ||
| "paths": { | ||
| "testcontainers": [ | ||
| "../../testcontainers/src" | ||
| ] | ||
| } | ||
| }, | ||
| "exclude": [ | ||
| "build" | ||
| ], | ||
| "references": [ | ||
| { | ||
| "path": "../../testcontainers" | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
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.