#![cfg(target_os = "linux")]
use lazy_static::lazy_static;
use std::env;
use std::path::PathBuf;
use std::process::{Command, Output};
use std::sync::Mutex;
lazy_static! {
static ref DOCKER_TEST_MUTEX: Mutex<()> = Mutex::new(());
}
struct DockerComposeGuard {
compose_file_path: PathBuf,
}
impl DockerComposeGuard {
fn new(compose_file_path: PathBuf) -> Self {
Self { compose_file_path }
}
fn run_compose_command(&self, args: &[&str]) -> std::io::Result<Output> {
let mut cmd = Command::new("docker-compose");
cmd.arg("-f").arg(&self.compose_file_path).args(args);
println!(
"--- Running Docker Command: docker-compose -f {:?} {} ---",
self.compose_file_path,
args.join(" ")
);
cmd.output()
}
}
impl Drop for DockerComposeGuard {
fn drop(&mut self) {
println!("--- Tearing down Docker environment ---");
match self.run_compose_command(&["down", "--volumes", "--remove-orphans"]) {
Ok(output) => {
if !output.status.success() {
eprintln!(
"--- docker-compose down failed ---\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
} else {
println!("--- Docker environment teardown successful ---");
}
}
Err(e) => {
eprintln!("--- Failed to execute docker-compose down: {} ---", e);
}
}
}
}
fn get_compose_file_path() -> PathBuf {
let crate_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let compose_file_path = crate_root.join("tests/loopback_test_resources/docker-compose.yml");
if !compose_file_path.exists() {
panic!(
"docker-compose.yml not found at expected path: {:?}",
compose_file_path
);
}
compose_file_path
}
fn run_docker_test(test_name: &str) {
let compose_file_path = get_compose_file_path();
unsafe {
env::set_var("POWERLINK_TEST_TO_RUN", test_name);
}
let guard = DockerComposeGuard::new(compose_file_path);
let output = guard
.run_compose_command(&[
"up",
"--build",
"--abort-on-container-exit",
"--exit-code-from",
"mn", ])
.expect("Failed to execute docker-compose up command");
println!(
"--- docker-compose stdout ---\n{}",
String::from_utf8_lossy(&output.stdout)
);
eprintln!(
"--- docker-compose stderr ---\n{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(
output.status.success(),
"The Docker integration test '{}' failed. Check the logs above.",
test_name
);
}
#[test]
fn test_ident_request_sequence() {
let _lock = DOCKER_TEST_MUTEX.lock().unwrap();
println!("--- [HOST] Running Test: test_cn_responds_to_ident_request ---");
run_docker_test("test_cn_responds_to_ident_request");
}
#[test]
fn test_sdo_read_sequence() {
let _lock = DOCKER_TEST_MUTEX.lock().unwrap();
println!("--- [HOST] Running Test: test_sdo_read_by_index_over_asnd ---");
run_docker_test("test_sdo_read_by_index_over_asnd");
}