use log::info;
use serial_test::serial;
mod test_utils;
use test_utils::{setup_logging, DockerComposeEnv};
#[cfg(test)]
mod tests {
use super::*;
use std::process::{Child, Command};
use std::thread;
use std::time::Duration;
use tokio::runtime::Runtime;
fn start_process(command: &str, args: &[&str]) -> Child {
Command::new(command)
.args(args)
.spawn()
.expect("Failed to start process")
}
#[test]
#[serial]
fn test_example_web_server() {
setup_logging();
info!("Starting example web server test");
let mut docker = DockerComposeEnv::new();
match docker.start() {
Ok(_) => info!("Docker environment started successfully"),
Err(e) => {
info!("Docker environment start failed: {}. Test skipped.", e);
return;
}
};
let status = Command::new("cargo")
.args([
"build",
"--example",
"web_server",
"--example",
"test_client",
])
.status()
.expect("Failed to build examples");
assert!(status.success(), "Failed to build examples");
let mut web_server = start_process("cargo", &["run", "--example", "web_server"]);
thread::sleep(Duration::from_secs(5));
let rt = Runtime::new().unwrap();
let result = rt.block_on(async {
let mut test_client = start_process(
"cargo",
&[
"run",
"--example",
"test_client",
"--",
"--restart-sub-vault",
],
);
thread::sleep(Duration::from_secs(35));
let client_status = test_client.try_wait().expect("Failed to get client status");
if client_status.is_none() {
test_client.kill().expect("Failed to kill test client");
test_client
.wait()
.expect("Failed to wait for test client to exit");
}
client_status.map_or(true, |s| s.success())
});
web_server.kill().expect("Failed to kill web server");
web_server
.wait()
.expect("Failed to wait for web server to exit");
if let Err(e) = docker.stop() {
info!("Failed to stop Docker Compose: {}", e);
} else {
info!("Docker Compose environment stopped successfully");
}
assert!(result, "Example test failed");
}
}