Skip to content
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

feat: add mariadb module #127

Merged
merged 1 commit into from
May 2, 2024
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ elasticmq = []
google_cloud_sdk_emulators = []
kafka = []
localstack = []
mariadb = []
minio = []
mongo = []
mosquitto = []
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub mod kafka;
#[cfg(feature = "localstack")]
#[cfg_attr(docsrs, doc(cfg(feature = "localstack")))]
pub mod localstack;
#[cfg(feature = "mariadb")]
#[cfg_attr(docsrs, doc(cfg(feature = "mariadb")))]
pub mod mariadb;
#[cfg(feature = "minio")]
#[cfg_attr(docsrs, doc(cfg(feature = "minio")))]
pub mod minio;
Expand Down
110 changes: 110 additions & 0 deletions src/mariadb/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use std::collections::HashMap;

use testcontainers::{core::WaitFor, Image};

const NAME: &str = "mariadb";
const TAG: &str = "11.3";

/// Module to work with [`MariaDB`] inside of tests.
///
/// Starts an instance of MariaDB with no password set for the root user and a default database named `test` created.
///
/// This module is based on the official [`MariaDB docker image`].
///
/// # Example
/// ```
/// use testcontainers_modules::{testcontainers::runners::SyncRunner, mariadb};
///
/// let mariadb_instance = mariadb::Mariadb::default().start();
/// let mariadb_url = format!("mariadb://{}:{}/test", mariadb_instance.get_host(), mariadb_instance.get_host_port_ipv4(3306));
/// ```
///
/// [`MariaDB`]: https://www.mariadb.com/
/// [`MariaDB docker image`]: https://hub.docker.com/_/mariadb
#[derive(Debug)]
pub struct Mariadb {
env_vars: HashMap<String, String>,
}

impl Default for Mariadb {
fn default() -> Self {
let mut env_vars = HashMap::new();
env_vars.insert("MARIADB_DATABASE".to_owned(), "test".to_owned());
env_vars.insert("MARIADB_ALLOW_EMPTY_ROOT_PASSWORD".into(), "1".into());

Self { env_vars }
}
}

impl Image for Mariadb {
type Args = ();

fn name(&self) -> String {
NAME.to_owned()
}

fn tag(&self) -> String {
TAG.to_owned()
}

fn ready_conditions(&self) -> Vec<WaitFor> {
vec![
WaitFor::message_on_stderr("mariadbd: ready for connections."),
WaitFor::message_on_stderr("port: 3306"),
]
}

fn env_vars(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
Box::new(self.env_vars.iter())
}
}

#[cfg(test)]
mod tests {
use mysql::prelude::Queryable;

use crate::{
mariadb::Mariadb as MariadbImage,
testcontainers::{runners::SyncRunner, RunnableImage},
};

#[test]
fn mariadb_one_plus_one() {
let mariadb_image = MariadbImage::default();
let node = mariadb_image.start();

let connection_string = &format!(
"mysql://root@{}:{}/test",
node.get_host(),
node.get_host_port_ipv4(3306)
);
let mut conn = mysql::Conn::new(mysql::Opts::from_url(connection_string).unwrap()).unwrap();

let first_row = conn.query_first("SELECT 1 + 1;").unwrap();
assert_eq!(first_row, Some(2));

let first_column: i32 = first_row.unwrap();
assert_eq!(first_column, 2);
}

#[test]
fn mariadb_custom_version() {
let image = RunnableImage::from(MariadbImage::default()).with_tag("11.2.3");
let node = image.start();

let connection_string = &format!(
"mysql://root@{}:{}/test",
node.get_host(),
node.get_host_port_ipv4(3306)
);

let mut conn = mysql::Conn::new(mysql::Opts::from_url(connection_string).unwrap()).unwrap();
let first_row: Option<String> = conn.query_first("SELECT version()").unwrap();
let first_column: String = first_row.unwrap();
assert!(
first_column.starts_with("11.2.3"),
"Expected version to start with 11.2.3, got: {}",
first_column
);
}
}
Loading