use docker_wrapper::{DockerCommand, RunCommand};
#[allow(unused_imports)]
use docker_wrapper::{ExecCommand, LogsCommand, RmCommand, StopCommand};
#[allow(unused_imports)]
use std::time::Duration;
#[allow(dead_code)]
fn unique_container_name(prefix: &str) -> String {
format!("{}-{}", prefix, uuid::Uuid::new_v4())
}
#[tokio::test]
async fn test_with_auto_cleanup() {
let container_name = unique_container_name("test-redis");
let output = RunCommand::new("redis:7-alpine")
.name(&container_name)
.detach()
.remove() .execute()
.await
.expect("Failed to start Redis");
println!("Started container: {}", output.0);
tokio::time::sleep(Duration::from_secs(1)).await;
let result = ExecCommand::new(
&container_name,
vec!["redis-cli".to_string(), "PING".to_string()],
)
.execute()
.await
.expect("Failed to execute command");
assert_eq!(result.stdout.trim(), "PONG");
StopCommand::new(&container_name)
.execute()
.await
.expect("Failed to stop container");
}
#[tokio::test]
async fn test_with_manual_cleanup() {
let container_name = unique_container_name("test-nginx");
let output = RunCommand::new("nginx:alpine")
.name(&container_name)
.port(8080, 80)
.detach()
.execute()
.await
.expect("Failed to start Nginx");
println!("Started container: {}", output.0);
tokio::time::sleep(Duration::from_secs(1)).await;
StopCommand::new(&container_name)
.execute()
.await
.expect("Failed to stop container");
RmCommand::new(&container_name)
.force()
.execute()
.await
.expect("Failed to remove container");
}
#[tokio::test]
async fn test_with_error_handling() {
let container_name = unique_container_name("test-postgres");
let result = RunCommand::new("postgres:15")
.name(&container_name)
.env("POSTGRES_PASSWORD", "test")
.detach()
.remove()
.execute()
.await;
match result {
Ok(output) => {
println!("Container started successfully: {}", output.0);
tokio::time::sleep(Duration::from_secs(3)).await;
let _ = StopCommand::new(&container_name).execute().await;
}
Err(e) => {
eprintln!("Failed to start container: {}", e);
if let Ok(logs) = LogsCommand::new(&container_name).tail("50").execute().await {
eprintln!("Container logs:\n{}", logs.stdout);
}
panic!("Test failed due to container startup error");
}
}
}
#[tokio::test]
async fn test_parallel_safe_containers() {
let result1 = start_test_container("test1").await;
let result2 = start_test_container("test2").await;
let result3 = start_test_container("test3").await;
assert!(result1.is_ok());
assert!(result2.is_ok());
assert!(result3.is_ok());
if let Ok(name) = result1 {
let _ = StopCommand::new(&name).execute().await;
}
if let Ok(name) = result2 {
let _ = StopCommand::new(&name).execute().await;
}
if let Ok(name) = result3 {
let _ = StopCommand::new(&name).execute().await;
}
}
#[allow(dead_code)]
async fn start_test_container(test_id: &str) -> Result<String, docker_wrapper::Error> {
let container_name = unique_container_name(&format!("parallel-{}", test_id));
RunCommand::new("alpine")
.name(&container_name)
.cmd(vec!["sleep".to_string(), "10".to_string()])
.detach()
.remove()
.execute()
.await?;
Ok(container_name)
}
#[tokio::test]
async fn test_with_log_inspection() {
let container_name = unique_container_name("test-app");
RunCommand::new("alpine")
.name(&container_name)
.cmd(vec![
"sh".to_string(),
"-c".to_string(),
"echo 'Starting app...'; sleep 1; echo 'App ready!'".to_string(),
])
.detach()
.execute()
.await
.expect("Failed to start container");
tokio::time::sleep(Duration::from_secs(2)).await;
let logs = LogsCommand::new(&container_name)
.execute()
.await
.expect("Failed to fetch logs");
println!("Container logs:\n{}", logs.stdout);
assert!(logs.stdout.contains("Starting app..."));
assert!(logs.stdout.contains("App ready!"));
let _ = RmCommand::new(&container_name).force().execute().await;
}
#[tokio::main]
async fn main() {
println!("Testing Basics Example");
println!("======================");
println!();
println!("This example contains test patterns. Run with:");
println!(" cargo test --example testing_basics");
println!();
println!("Or run individual tests:");
println!(" cargo test --example testing_basics test_with_auto_cleanup");
println!();
println!("See the source code for testing patterns and best practices.");
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn demonstration_test() {
let container_name = unique_container_name("demo");
let output = RunCommand::new("alpine")
.name(&container_name)
.cmd(vec!["echo".to_string(), "Hello from test!".to_string()])
.execute()
.await
.expect("Failed to run container");
println!("Container output: {}", output.0);
assert!(!output.0.is_empty());
}
}