use std::time::Duration;
use rightsize::{Container, ContainerGuard, Result, Wait};
const PORT: u16 = 3306;
pub struct MariaDbContainer {
container: Container,
username: String,
password: String,
database: String,
}
impl MariaDbContainer {
pub fn new() -> Self {
Self::with_image("mariadb:11.4")
}
pub fn with_image(image: &str) -> Self {
let username = "test".to_string();
let password = "test".to_string();
let database = "test".to_string();
let container = Container::new(image)
.with_exposed_ports(&[PORT])
.with_env("MARIADB_USER", &username)
.with_env("MARIADB_PASSWORD", &password)
.with_env("MARIADB_DATABASE", &database)
.with_env("MARIADB_ROOT_PASSWORD", "test")
.waiting_for(
Wait::for_log_message(r".*port: 3306.*mariadb\.org binary distribution.*", 1)
.with_startup_timeout(Duration::from_secs(60)),
);
Self {
container,
username,
password,
database,
}
}
pub fn with_username(mut self, username: &str) -> Self {
self.username = username.to_string();
self.container = self.container.with_env("MARIADB_USER", username);
self
}
pub fn with_password(mut self, password: &str) -> Self {
self.password = password.to_string();
self.container = self.container.with_env("MARIADB_PASSWORD", password);
self
}
pub fn with_database(mut self, database: &str) -> Self {
self.database = database.to_string();
self.container = self.container.with_env("MARIADB_DATABASE", database);
self
}
pub async fn start(self) -> Result<MariaDbGuard> {
crate::register_default_backends();
let guard = self.container.start().await?;
Ok(MariaDbGuard {
guard,
username: self.username,
password: self.password,
database: self.database,
})
}
}
impl Default for MariaDbContainer {
fn default() -> Self {
Self::new()
}
}
pub struct MariaDbGuard {
guard: ContainerGuard,
username: String,
password: String,
database: String,
}
impl MariaDbGuard {
pub fn username(&self) -> &str {
&self.username
}
pub fn password(&self) -> &str {
&self.password
}
pub fn database_name(&self) -> &str {
&self.database
}
pub fn connection_string(&self) -> String {
format!(
"mysql://{}:{}@{}:{}/{}",
self.username,
self.password,
self.guard.host(),
self.guard.get_mapped_port(PORT).unwrap(),
self.database,
)
}
pub async fn stop(self) -> Result<()> {
self.guard.stop().await
}
}
impl std::ops::Deref for MariaDbGuard {
type Target = ContainerGuard;
fn deref(&self) -> &ContainerGuard {
&self.guard
}
}
#[cfg(test)]
mod tests {
use super::*;
use rightsize::wait::{WaitStrategy, WaitTarget};
#[test]
fn defaults_are_the_test_trio() {
let c = MariaDbContainer::new();
assert_eq!(c.username, "test");
assert_eq!(c.password, "test");
assert_eq!(c.database, "test");
}
#[test]
fn builders_override_the_defaults() {
let c = MariaDbContainer::new()
.with_username("alice")
.with_password("s3cret")
.with_database("app");
assert_eq!(c.username, "alice");
assert_eq!(c.password, "s3cret");
assert_eq!(c.database, "app");
}
const CAPTURED_LOG: &str = "\
2026-07-04 8:47:29 0 [Note] mariadbd: ready for connections.
Version: '11.4.12-MariaDB-ubu2404' socket: '/run/mysqld/mysqld.sock' port: 0 mariadb.org binary distribution
2026-07-04 8:47:30 0 [Note] Server socket created on IP: '0.0.0.0', port: '3306'.
2026-07-04 8:47:30 0 [Note] Server socket created on IP: '::', port: '3306'.
2026-07-04 8:47:30 0 [Note] mariadbd: ready for connections.
Version: '11.4.12-MariaDB-ubu2404' socket: '/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution";
struct FakeTarget(std::sync::Mutex<String>);
#[async_trait::async_trait]
impl WaitTarget for FakeTarget {
fn host(&self) -> &str {
"127.0.0.1"
}
fn mapped_port(&self, guest_port: u16) -> u16 {
guest_port
}
fn exposed_guest_ports(&self) -> Vec<u16> {
vec![PORT]
}
async fn current_logs(&self) -> String {
self.0.lock().unwrap().clone()
}
fn describe(&self) -> String {
"fake-mariadb".to_string()
}
}
#[tokio::test]
async fn temp_server_port_zero_line_does_not_signal_ready() {
let partial: String = CAPTURED_LOG.lines().take(2).collect::<Vec<_>>().join("\n");
let target = FakeTarget(std::sync::Mutex::new(partial));
let err = Wait::for_log_message(r".*port: 3306.*mariadb\.org binary distribution.*", 1)
.with_startup_timeout(Duration::from_millis(300))
.wait_until_ready(&target)
.await
.expect_err("the temp server's port: 0 line must not signal ready");
let _ = err;
}
#[tokio::test]
async fn only_the_real_servers_port_3306_line_signals_ready() {
let target = FakeTarget(std::sync::Mutex::new(CAPTURED_LOG.to_string()));
Wait::for_log_message(r".*port: 3306.*mariadb\.org binary distribution.*", 1)
.with_startup_timeout(Duration::from_secs(5))
.wait_until_ready(&target)
.await
.expect("the real server's port: 3306 line must signal ready");
}
}