testcontainers_modules/minio/
mod.rs

1use std::{borrow::Cow, collections::HashMap};
2
3use testcontainers::{core::WaitFor, Image};
4
5const NAME: &str = "minio/minio";
6const TAG: &str = "RELEASE.2025-02-28T09-55-16Z";
7
8const DIR: &str = "/data";
9const CONSOLE_ADDRESS: &str = ":9001";
10
11/// Module to work with [`MinIO`] inside of tests.
12///
13/// Starts an instance of MinIO based on the official [`MinIO docker image`].
14///
15/// MinIO is a high-performance object storage server compatible with Amazon S3 APIs.
16/// The container exposes port `9000` for API access and port `9001` for the web console by default.
17///
18/// # Example
19/// ```
20/// use testcontainers_modules::{minio::MinIO, testcontainers::runners::SyncRunner};
21///
22/// let minio_instance = MinIO::default().start().unwrap();
23/// let host = minio_instance.get_host().unwrap();
24/// let api_port = minio_instance.get_host_port_ipv4(9000).unwrap();
25///
26/// // Use the S3-compatible API at http://{host}:{api_port}
27/// ```
28///
29/// [`MinIO`]: https://min.io/
30/// [`MinIO docker image`]: https://hub.docker.com/r/minio/minio
31#[derive(Debug, Clone)]
32pub struct MinIO {
33    env_vars: HashMap<String, String>,
34    cmd: MinIOServerCmd,
35}
36
37impl Default for MinIO {
38    fn default() -> Self {
39        let mut env_vars = HashMap::new();
40        env_vars.insert(
41            "MINIO_CONSOLE_ADDRESS".to_owned(),
42            CONSOLE_ADDRESS.to_owned(),
43        );
44
45        Self {
46            env_vars,
47            cmd: MinIOServerCmd::default(),
48        }
49    }
50}
51
52/// Configuration for MinIO server command-line arguments.
53///
54/// This struct allows you to customize the MinIO server startup configuration
55/// by setting various options like the data directory, TLS certificates, and logging format.
56#[derive(Debug, Clone)]
57pub struct MinIOServerCmd {
58    /// The directory where MinIO will store data.
59    /// Defaults to "/data" if not specified.
60    pub dir: String,
61    /// Optional directory containing TLS certificates for HTTPS.
62    /// If provided, MinIO will enable TLS/SSL.
63    pub certs_dir: Option<String>,
64    /// Whether to enable JSON formatted logging.
65    /// When true, MinIO outputs logs in JSON format for easier parsing.
66    pub json_log: bool,
67}
68
69impl Default for MinIOServerCmd {
70    fn default() -> Self {
71        Self {
72            dir: DIR.to_owned(),
73            certs_dir: None,
74            json_log: false,
75        }
76    }
77}
78
79impl IntoIterator for &MinIOServerCmd {
80    type Item = String;
81    type IntoIter = <Vec<String> as IntoIterator>::IntoIter;
82
83    fn into_iter(self) -> Self::IntoIter {
84        let mut args = vec!["server".to_owned(), self.dir.to_owned()];
85
86        if let Some(ref certs_dir) = self.certs_dir {
87            args.push("--certs-dir".to_owned());
88            args.push(certs_dir.to_owned())
89        }
90
91        if self.json_log {
92            args.push("--json".to_owned());
93        }
94
95        args.into_iter()
96    }
97}
98
99impl Image for MinIO {
100    fn name(&self) -> &str {
101        NAME
102    }
103
104    fn tag(&self) -> &str {
105        TAG
106    }
107
108    fn ready_conditions(&self) -> Vec<WaitFor> {
109        vec![WaitFor::message_on_stderr("API:")]
110    }
111
112    fn env_vars(
113        &self,
114    ) -> impl IntoIterator<Item = (impl Into<Cow<'_, str>>, impl Into<Cow<'_, str>>)> {
115        &self.env_vars
116    }
117
118    fn cmd(&self) -> impl IntoIterator<Item = impl Into<Cow<'_, str>>> {
119        &self.cmd
120    }
121}
122
123#[cfg(test)]
124mod tests {
125    use aws_config::{meta::region::RegionProviderChain, BehaviorVersion};
126    use aws_sdk_s3::{config::Credentials, Client};
127    use testcontainers::runners::AsyncRunner;
128
129    use crate::minio;
130
131    #[tokio::test]
132    async fn minio_buckets() -> Result<(), Box<dyn std::error::Error + 'static>> {
133        let minio = minio::MinIO::default();
134        let node = minio.start().await?;
135
136        let host_port = node.get_host_port_ipv4(9000).await?;
137        let client = build_s3_client(host_port).await;
138
139        let bucket_name = "test-bucket";
140
141        client
142            .create_bucket()
143            .bucket(bucket_name)
144            .send()
145            .await
146            .expect("Failed to create test bucket");
147
148        let buckets = client
149            .list_buckets()
150            .send()
151            .await
152            .expect("Failed to get list of buckets")
153            .buckets
154            .unwrap();
155        assert_eq!(1, buckets.len());
156        assert_eq!(bucket_name, buckets[0].name.as_ref().unwrap());
157        Ok(())
158    }
159
160    async fn build_s3_client(host_port: u16) -> Client {
161        let endpoint_uri = format!("http://127.0.0.1:{host_port}");
162        let region_provider = RegionProviderChain::default_provider().or_else("us-east-1");
163        let creds = Credentials::new("minioadmin", "minioadmin", None, None, "test");
164
165        // Default MinIO credentials (Can be overridden by ENV container variables)
166        let shared_config = aws_config::defaults(BehaviorVersion::latest())
167            .region(region_provider)
168            .endpoint_url(endpoint_uri)
169            .credentials_provider(creds)
170            .load()
171            .await;
172
173        Client::new(&shared_config)
174    }
175}