rightsize_modules/rabbitmq.rs
1//! A single-node RabbitMQ container with the management plugin enabled. Defaults to a
2//! `guest`/`guest` credential pair (the image's own default) so
3//! [`RabbitMqGuard::amqp_url`] is usable with zero configuration; call
4//! [`RabbitMqContainer::with_username`]/[`RabbitMqContainer::with_password`] before
5//! `start()` to override either.
6//!
7//! ### Readiness — verified against a real 4.x boot
8//!
9//! `rabbitmq:4-management-alpine` still prints the same `"Server startup complete"`
10//! line the 3.x series used (captured verbatim from a real boot with this module's
11//! env):
12//!
13//! ```text
14//! ...
15//! 2026-07-04 08:47:17.936423+00:00 [info] <0.1036.0> started TCP listener on [::]:5672
16//! completed with 4 plugins.
17//! 2026-07-04 08:47:18.001311+00:00 [info] <0.900.0> Server startup complete; 4 plugins started.
18//! 2026-07-04 08:47:18.001311+00:00 [info] <0.900.0> * rabbitmq_prometheus
19//! 2026-07-04 08:47:18.001311+00:00 [info] <0.900.0> * rabbitmq_management
20//! 2026-07-04 08:47:18.001311+00:00 [info] <0.900.0> * rabbitmq_management_agent
21//! 2026-07-04 08:47:18.001311+00:00 [info] <0.900.0> * rabbitmq_web_dispatch
22//! ```
23//!
24//! The line appears exactly once, so `for_log_message` at `times=1` is unambiguous —
25//! unlike Postgres/MySQL/MariaDB, there is no double-boot restart to race here. The
26//! management API's own `/api/health/checks/...` endpoints require authenticated
27//! requests, so the log line is the simpler and equally reliable readiness signal.
28//!
29//! No control characters were found in the image's baked env (checked
30//! via `docker image inspect`), so no env override is needed here — unlike
31//! [`crate::postgres::PostgresContainer`].
32//!
33//! No `with_memory_limit` override: booted clean on msb's default ~450M microVM RAM
34//! (observed ~5.5s IT round-trip on both backends — an Erlang VM, not a JVM, so no
35//! Paketo/QuickStart-style heap demand; no memory-ladder escalation was needed).
36//!
37//! ### A 4.x behavior change worth knowing (not this module's concern, but bites naive clients)
38//!
39//! RabbitMQ 4.x deprecates `transient_nonexcl_queues` and, per the broker's own startup
40//! warning, "this feature can still be used for now" but a client that declares a
41//! **non-durable, non-exclusive** queue (`durable=false, exclusive=false`) may be
42//! rejected with `reply-code=541 INTERNAL_ERROR` depending on the deployed policy —
43//! reproduced directly against this module's pinned image. Declare durable,
44//! non-exclusive queues (or exclusive transient ones) from client code exercising this
45//! container; this module itself declares no queues.
46
47use rightsize::{Container, ContainerGuard, Result, Wait};
48
49const AMQP_PORT: u16 = 5672;
50const MANAGEMENT_PORT: u16 = 15672;
51
52/// A single-node RabbitMQ container with the management plugin enabled.
53pub struct RabbitMqContainer {
54 container: Container,
55 username: String,
56 password: String,
57}
58
59impl RabbitMqContainer {
60 /// Builds a container from the pinned default image
61 /// (`rabbitmq:4-management-alpine`).
62 pub fn new() -> Self {
63 Self::with_image("rabbitmq:4-management-alpine")
64 }
65
66 /// Builds a container from a caller-chosen image.
67 pub fn with_image(image: &str) -> Self {
68 let username = "guest".to_string();
69 let password = "guest".to_string();
70 let container = Container::new(image)
71 .with_exposed_ports(&[AMQP_PORT, MANAGEMENT_PORT])
72 .with_env("RABBITMQ_DEFAULT_USER", &username)
73 .with_env("RABBITMQ_DEFAULT_PASS", &password)
74 // Exactly-once log line (see the module doc for the captured excerpt) —
75 // no restart race.
76 .waiting_for(Wait::for_log_message(".*Server startup complete.*", 1));
77 Self {
78 container,
79 username,
80 password,
81 }
82 }
83
84 /// Overrides `RABBITMQ_DEFAULT_USER` (default `guest`).
85 pub fn with_username(mut self, username: &str) -> Self {
86 self.username = username.to_string();
87 self.container = self.container.with_env("RABBITMQ_DEFAULT_USER", username);
88 self
89 }
90
91 /// Overrides `RABBITMQ_DEFAULT_PASS` (default `guest`).
92 pub fn with_password(mut self, password: &str) -> Self {
93 self.password = password.to_string();
94 self.container = self.container.with_env("RABBITMQ_DEFAULT_PASS", password);
95 self
96 }
97
98 /// Boots the container.
99 pub async fn start(self) -> Result<RabbitMqGuard> {
100 let guard = self.container.start().await?;
101 Ok(RabbitMqGuard {
102 guard,
103 username: self.username,
104 password: self.password,
105 })
106 }
107}
108
109impl Default for RabbitMqContainer {
110 fn default() -> Self {
111 Self::new()
112 }
113}
114
115/// The running guard for a [`RabbitMqContainer`].
116pub struct RabbitMqGuard {
117 guard: ContainerGuard,
118 username: String,
119 password: String,
120}
121
122impl RabbitMqGuard {
123 /// The configured management/AMQP user (default `guest`).
124 pub fn username(&self) -> &str {
125 &self.username
126 }
127
128 /// The configured management/AMQP password (default `guest`).
129 pub fn password(&self) -> &str {
130 &self.password
131 }
132
133 /// An `amqp://` URL (with credentials) for the running container's AMQP listener.
134 pub fn amqp_url(&self) -> String {
135 format!(
136 "amqp://{}:{}@{}:{}",
137 self.username,
138 self.password,
139 self.guard.host(),
140 self.guard.get_mapped_port(AMQP_PORT).unwrap()
141 )
142 }
143
144 /// The management UI/API base URI for the running container.
145 pub fn management_url(&self) -> String {
146 format!(
147 "http://{}:{}",
148 self.guard.host(),
149 self.guard.get_mapped_port(MANAGEMENT_PORT).unwrap()
150 )
151 }
152
153 /// Stops and removes the container, releasing its host ports.
154 pub async fn stop(self) -> Result<()> {
155 self.guard.stop().await
156 }
157}
158
159impl std::ops::Deref for RabbitMqGuard {
160 type Target = ContainerGuard;
161 fn deref(&self) -> &ContainerGuard {
162 &self.guard
163 }
164}
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169
170 #[test]
171 fn defaults_are_the_guest_pair() {
172 let c = RabbitMqContainer::new();
173 assert_eq!(c.username, "guest");
174 assert_eq!(c.password, "guest");
175 }
176
177 #[test]
178 fn builders_override_the_defaults() {
179 let c = RabbitMqContainer::new()
180 .with_username("alice")
181 .with_password("s3cret");
182 assert_eq!(c.username, "alice");
183 assert_eq!(c.password, "s3cret");
184 }
185}