testcontainers_modules/minio/
mod.rs1use 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#[allow(missing_docs)]
12#[derive(Debug, Clone)]
14pub struct MinIO {
15 env_vars: HashMap<String, String>,
16 cmd: MinIOServerCmd,
17}
18
19impl Default for MinIO {
20 fn default() -> Self {
21 let mut env_vars = HashMap::new();
22 env_vars.insert(
23 "MINIO_CONSOLE_ADDRESS".to_owned(),
24 CONSOLE_ADDRESS.to_owned(),
25 );
26
27 Self {
28 env_vars,
29 cmd: MinIOServerCmd::default(),
30 }
31 }
32}
33
34#[allow(missing_docs)]
35#[derive(Debug, Clone)]
37pub struct MinIOServerCmd {
38 #[allow(missing_docs)]
39 pub dir: String,
41 #[allow(missing_docs)]
42 pub certs_dir: Option<String>,
44 #[allow(missing_docs)]
45 pub json_log: bool,
47}
48
49impl Default for MinIOServerCmd {
50 fn default() -> Self {
51 Self {
52 dir: DIR.to_owned(),
53 certs_dir: None,
54 json_log: false,
55 }
56 }
57}
58
59impl IntoIterator for &MinIOServerCmd {
60 type Item = String;
61 type IntoIter = <Vec<String> as IntoIterator>::IntoIter;
62
63 fn into_iter(self) -> Self::IntoIter {
64 let mut args = vec!["server".to_owned(), self.dir.to_owned()];
65
66 if let Some(ref certs_dir) = self.certs_dir {
67 args.push("--certs-dir".to_owned());
68 args.push(certs_dir.to_owned())
69 }
70
71 if self.json_log {
72 args.push("--json".to_owned());
73 }
74
75 args.into_iter()
76 }
77}
78
79impl Image for MinIO {
80 fn name(&self) -> &str {
81 NAME
82 }
83
84 fn tag(&self) -> &str {
85 TAG
86 }
87
88 fn ready_conditions(&self) -> Vec<WaitFor> {
89 vec![WaitFor::message_on_stderr("API:")]
90 }
91
92 fn env_vars(
93 &self,
94 ) -> impl IntoIterator<Item = (impl Into<Cow<'_, str>>, impl Into<Cow<'_, str>>)> {
95 &self.env_vars
96 }
97
98 fn cmd(&self) -> impl IntoIterator<Item = impl Into<Cow<'_, str>>> {
99 &self.cmd
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use aws_config::{meta::region::RegionProviderChain, BehaviorVersion};
106 use aws_sdk_s3::{config::Credentials, Client};
107 use testcontainers::runners::AsyncRunner;
108
109 use crate::minio;
110
111 #[tokio::test]
112 async fn minio_buckets() -> Result<(), Box<dyn std::error::Error + 'static>> {
113 let minio = minio::MinIO::default();
114 let node = minio.start().await?;
115
116 let host_port = node.get_host_port_ipv4(9000).await?;
117 let client = build_s3_client(host_port).await;
118
119 let bucket_name = "test-bucket";
120
121 client
122 .create_bucket()
123 .bucket(bucket_name)
124 .send()
125 .await
126 .expect("Failed to create test bucket");
127
128 let buckets = client
129 .list_buckets()
130 .send()
131 .await
132 .expect("Failed to get list of buckets")
133 .buckets
134 .unwrap();
135 assert_eq!(1, buckets.len());
136 assert_eq!(bucket_name, buckets[0].name.as_ref().unwrap());
137 Ok(())
138 }
139
140 async fn build_s3_client(host_port: u16) -> Client {
141 let endpoint_uri = format!("http://127.0.0.1:{host_port}");
142 let region_provider = RegionProviderChain::default_provider().or_else("us-east-1");
143 let creds = Credentials::new("minioadmin", "minioadmin", None, None, "test");
144
145 let shared_config = aws_config::defaults(BehaviorVersion::latest())
147 .region(region_provider)
148 .endpoint_url(endpoint_uri)
149 .credentials_provider(creds)
150 .load()
151 .await;
152
153 Client::new(&shared_config)
154 }
155}