mod common;
use common::{TempDir, run_in_userns, start_fake_container};
use std::{env, path::Path};
fn check_test_prerequisites() -> Option<String> {
let static_shell = match env::var("CNTR_TEST_SHELL").ok() {
Some(path) => path,
None => {
eprintln!("Skipping test: CNTR_TEST_SHELL environment variable not set");
return None;
}
};
if !Path::new(&static_shell).exists() {
eprintln!(
"Skipping test: CNTR_TEST_SHELL path does not exist: {}",
static_shell
);
return None;
}
if !cntr::syscalls::capability::has_mount_api() {
eprintln!("Skipping test: mount API not available on this kernel");
return None;
}
Some(static_shell)
}
#[test]
fn test_attach_integration() {
let static_shell = match check_test_prerequisites() {
Some(shell) => shell,
None => return,
};
run_in_userns(|| {
let container = start_fake_container();
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let base_dir = temp_dir.path().to_str().expect("Invalid temp path");
let cntr_bin = env!("CARGO_BIN_EXE_cntr");
let pid_str = container.pid.to_string();
let status = std::process::Command::new(cntr_bin)
.env("CNTR_BASE_DIR", base_dir)
.args(&["attach", "-t", "process_id", &pid_str, "--"])
.args(&[
&static_shell,
"-c",
&format!(
"set -x && \
test -d {} && \
test -f {}/tmp/container-marker && \
test -x {}/bin/sh && \
grep -q fake-container {}/tmp/container-marker && \
echo 'All checks passed'",
base_dir, base_dir, base_dir, base_dir
),
])
.status()
.expect("Failed to execute cntr attach");
assert!(
status.success(),
"Attach test failed with status: {:?}",
status
);
});
}
#[test]
fn test_exec_direct() {
let _static_shell = match check_test_prerequisites() {
Some(shell) => shell,
None => return,
};
run_in_userns(|| {
let container = start_fake_container();
let cntr_bin = env!("CARGO_BIN_EXE_cntr");
let pid_str = container.pid.to_string();
let status = std::process::Command::new(cntr_bin)
.args(&["exec", "-t", "process_id", &pid_str, "--"])
.args(&[
"/bin/sh",
"-c",
"test -f /tmp/container-marker && echo 'Exec direct test passed'",
])
.status()
.expect("Failed to execute cntr exec");
assert!(
status.success(),
"Exec direct test failed with status: {:?}",
status
);
});
}