testcontainers-cpp 0.2.0
Native C++20 Testcontainers over the Docker Engine API
Loading...
Searching...
No Matches
testcontainers::modules::RabbitMQImage Class Reference

A reusable, copyable description of a RabbitMQ broker: image, credentials, virtual host, preloaded definitions, and plugins to enable. More...

#include <testcontainers/modules/RabbitMQ.hpp>

Public Member Functions

 RabbitMQImage ()
 A config ready to start(): image rabbitmq:3.13-management, ports 5672 + 15672 exposed, credentials guest/guest on vhost "/", ordered readiness (log line, then one diagnostics exec).
RabbitMQImagewith_image (const std::string &reference)
 Override the pinned image with a full reference "name[:tag]" (tag defaults to "latest") — e.g.
RabbitMQImagewith_username (std::string username)
 The broker's provisioned user (RABBITMQ_DEFAULT_USER).
RabbitMQImagewith_password (std::string password)
 The provisioned user's password (RABBITMQ_DEFAULT_PASS).
RabbitMQImagewith_vhost (std::string vhost)
 The default virtual host (RABBITMQ_DEFAULT_VHOST).
RabbitMQImagewith_definitions (std::filesystem::path host_json)
 Import a definitions JSON file (the broker's export format: exchanges, queues, bindings, policies, users, ...) at boot — the standard way to preload topology.
RabbitMQImagewith_definitions_json (std::string json)
 As with_definitions, from in-memory JSON bytes instead of a host file.
RabbitMQImagewith_plugin (std::string plugin)
 Enable a plugin that ships with the image (e.g.
RabbitMQImagewith_env (std::string key, std::string value)
 Set an extra environment variable — image switches beyond the typed setters.
RabbitMQImagewith_label (std::string key, std::string value)
 Attach a metadata label.
RabbitMQImagewith_network (std::string network)
 Join a user-defined network; peers resolve this container by name/alias at <alias>:5672 (kAmqpPort, not the mapped host port).
RabbitMQImagewith_network (const Network &network)
RabbitMQImagewith_network_alias (std::string alias)
RabbitMQImagewith_reuse (bool reuse=true)
 Enable container reuse (effective only when reuse is also enabled globally — see GenericImage::with_reuse).
RabbitMQImagewith_startup_timeout (std::chrono::milliseconds timeout)
 Budget for the whole readiness phase (default: 60s; observed cold boots take ~10s).
RabbitMQImagewith_startup_attempts (int n)
 Retry the whole create→start→wait sequence up to n times.
RabbitMQImagewith_customizer (std::function< void(GenericImage &)> customize)
 Register a callback that customizes the underlying GenericImage — the channel for every option this module does not surface (extra ports, pull policy, ...).
const std::string & username () const noexcept
const std::string & password () const noexcept
const std::string & vhost () const noexcept
GenericImage to_generic () const
 Render the full configuration — env trio, definitions plumbing (seeded account + your files + the load_definitions drop-in), ordered waits, plugin hook, customizers — into a plain GenericImage: the drop-down escape hatch when you need a raw core Container instead of a RabbitMQContainer.
RabbitMQContainer start () const
 Create, start, and wait for the broker (the ordered readiness above).

Static Public Attributes

static constexpr std::string_view kDefaultImage = "rabbitmq:3.13-management"
 The pinned default image.
static constexpr std::uint16_t kAmqpPort = 5672
 The AMQP 0-9-1 port INSIDE the container.
static constexpr std::uint16_t kManagementPort = 15672
 The management HTTP API/UI port INSIDE the container.

Detailed Description

A reusable, copyable description of a RabbitMQ broker: image, credentials, virtual host, preloaded definitions, and plugins to enable.

The with_* builders mutate in place and return *this by reference, so a named config can be configured incrementally and started many times. The default image is the -management variant: the same broker with the management plugin enabled, serving the HTTP API and UI on 15672 — the standard way for a test to inspect queues, connections, and messages without an AMQP client library.

Readiness is deliberately ORDERED: the "Server startup complete" log line first, one rabbitmq-diagnostics check_port_connectivity exec second. An exec-based probe that fires in the first seconds of boot can permanently prevent the broker from starting (a root-owned Erlang cookie the server's own user then cannot read) — keep the module's waits first if you add more through with_customizer.

Constructor & Destructor Documentation

◆ RabbitMQImage()

testcontainers::modules::RabbitMQImage::RabbitMQImage ( )

A config ready to start(): image rabbitmq:3.13-management, ports 5672 + 15672 exposed, credentials guest/guest on vhost "/", ordered readiness (log line, then one diagnostics exec).

Member Function Documentation

◆ with_image()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_image ( const std::string & reference)

Override the pinned image with a full reference "name[:tag]" (tag defaults to "latest") — e.g.

a mirror or a -management tag of a different line.

◆ with_username()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_username ( std::string username)
inline

The broker's provisioned user (RABBITMQ_DEFAULT_USER).

Default "guest". The configured user REPLACES the built-in guest account — after with_username("app") there is no "guest" user on the broker.

◆ with_password()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_password ( std::string password)
inline

The provisioned user's password (RABBITMQ_DEFAULT_PASS).

Default "guest". Must not be empty — the broker's internal auth backend prohibits blank-password logins, so start() throws up front. Test credentials only: the value is visible via inspect.

◆ with_vhost()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_vhost ( std::string vhost)
inline

The default virtual host (RABBITMQ_DEFAULT_VHOST).

Default "/" (the broker's standard default vhost). The configured user gets full permissions on it. Any UTF-8 name is accepted; amqp_url() renders it percent-encoded as one path segment.

◆ with_definitions()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_definitions ( std::filesystem::path host_json)

Import a definitions JSON file (the broker's export format: exchanges, queues, bindings, policies, users, ...) at boot — the standard way to preload topology.

Repeatable; files import in call order, later files winning on conflicting objects. The file name must end in ".json" (anything else throws here).

The configured user/vhost are seeded ALONGSIDE your file, so it only needs the application objects: RabbitMQ skips ALL default-account provisioning when definitions are imported at boot, and without the seeding a definitions file with no "users" entry would leave the broker with no users at all. Declaring the same user/vhost in your file overrides the seeded one (your file imports later). Objects must reference vhosts that exist after import — the seeded vhost is vhost(); declare any other one your objects live in.

◆ with_definitions_json()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_definitions_json ( std::string json)

As with_definitions, from in-memory JSON bytes instead of a host file.

◆ with_plugin()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_plugin ( std::string plugin)
inline

Enable a plugin that ships with the image (e.g.

"rabbitmq_shovel", "rabbitmq_federation") once the broker is ready, via rabbitmq-plugins enable — additive, so the image's own enabled plugins (management, prometheus) are kept. Repeatable; all names are enabled in one command. Plugins that add listeners on new ports (MQTT, STOMP) are enabled but their ports are not published — expose them through with_customizer if you need them mapped. A failed enable (e.g. a typo'd name) fails start().

◆ with_env()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_env ( std::string key,
std::string value )
inline

Set an extra environment variable — image switches beyond the typed setters.

The managed RABBITMQ_DEFAULT_* trio belongs to with_username/with_password/with_vhost: the module applies those keys last, so they win over raw duplicates set here (the image's bash entrypoint applies the last duplicate).

◆ with_label()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_label ( std::string key,
std::string value )
inline

Attach a metadata label.

The module's reuse-visibility label (org.testcontainers.rabbitmq.plugins) is applied after these, so it wins on a duplicate key.

◆ with_network() [1/2]

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_network ( std::string network)
inline

Join a user-defined network; peers resolve this container by name/alias at <alias>:5672 (kAmqpPort, not the mapped host port).

◆ with_network() [2/2]

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_network ( const Network & network)

◆ with_network_alias()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_network_alias ( std::string alias)
inline

◆ with_reuse()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_reuse ( bool reuse = true)
inline

Enable container reuse (effective only when reuse is also enabled globally — see GenericImage::with_reuse).

An adopted broker keeps its definitions and runtime-enabled plugins.

◆ with_startup_timeout()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_startup_timeout ( std::chrono::milliseconds timeout)
inline

Budget for the whole readiness phase (default: 60s; observed cold boots take ~10s).

Image pull time does not count against it.

◆ with_startup_attempts()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_startup_attempts ( int n)
inline

Retry the whole create→start→wait sequence up to n times.

◆ with_customizer()

RabbitMQImage & testcontainers::modules::RabbitMQImage::with_customizer ( std::function< void(GenericImage &)> customize)
inline

Register a callback that customizes the underlying GenericImage — the channel for every option this module does not surface (extra ports, pull policy, ...).

Customizers run when the config is rendered (start() / to_generic()), in registration order, AFTER the module's own rendering — what they set wins over the module. A wait added here runs after the module's ordered waits, which is the SAFE position (see the class note on the Erlang cookie); do not replace the module's waits. Do not set the RABBITMQ_DEFAULT_* env here either: it would desync the credential getters and amqp_url() — use the typed setters instead.

◆ username()

const std::string & testcontainers::modules::RabbitMQImage::username ( ) const
inlinenoexcept

◆ password()

const std::string & testcontainers::modules::RabbitMQImage::password ( ) const
inlinenoexcept

◆ vhost()

const std::string & testcontainers::modules::RabbitMQImage::vhost ( ) const
inlinenoexcept

◆ to_generic()

GenericImage testcontainers::modules::RabbitMQImage::to_generic ( ) const

Render the full configuration — env trio, definitions plumbing (seeded account + your files + the load_definitions drop-in), ordered waits, plugin hook, customizers — into a plain GenericImage: the drop-down escape hatch when you need a raw core Container instead of a RabbitMQContainer.

Throws Error on an invalid config (empty username/vhost) before any daemon contact.

◆ start()

RabbitMQContainer testcontainers::modules::RabbitMQImage::start ( ) const

Create, start, and wait for the broker (the ordered readiness above).

Throws on failure (StartupTimeoutError when the broker never becomes ready, DockerError for daemon failures), like GenericImage::start().

Member Data Documentation

◆ kDefaultImage

std::string_view testcontainers::modules::RabbitMQImage::kDefaultImage = "rabbitmq:3.13-management"
staticconstexpr

The pinned default image.

The management HTTP API (15672) is enabled; AMQP listens on 5672. Getters tied to the management API assume a -management variant. Note on custom/hardened images: the official image ships loopback_users.guest = false — without that line the built-in guest account only works over loopback, so use with_username (custom users are never loopback-restricted).

◆ kAmqpPort

std::uint16_t testcontainers::modules::RabbitMQImage::kAmqpPort = 5672
staticconstexpr

The AMQP 0-9-1 port INSIDE the container.

Peers on a shared docker network connect to <alias-or-name>:kAmqpPort; the test process uses RabbitMQContainer::amqp_port() (the mapped host port) instead.

◆ kManagementPort

std::uint16_t testcontainers::modules::RabbitMQImage::kManagementPort = 15672
staticconstexpr

The management HTTP API/UI port INSIDE the container.