2021-10-12 12:21:23 +02:00
|
|
|
use crate::utils::{
|
2023-05-08 20:40:14 +02:00
|
|
|
delete_container, generate_uuid, get_runtime_path, get_state, kill_container, prepare_bundle,
|
|
|
|
State,
|
2021-10-12 12:21:23 +02:00
|
|
|
};
|
|
|
|
use anyhow::anyhow;
|
2022-03-05 07:49:13 +01:00
|
|
|
use std::{
|
|
|
|
fs::File,
|
|
|
|
process::{Command, Stdio},
|
|
|
|
};
|
2021-10-12 12:21:23 +02:00
|
|
|
use test_framework::{Test, TestGroup, TestResult};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
#[inline]
|
2023-05-08 20:40:14 +02:00
|
|
|
fn cleanup(id: &Uuid, bundle: &tempfile::TempDir) {
|
2022-01-28 14:40:12 +01:00
|
|
|
let str_id = id.to_string();
|
|
|
|
kill_container(&str_id, bundle).unwrap().wait().unwrap();
|
|
|
|
delete_container(&str_id, bundle).unwrap().wait().unwrap();
|
2021-10-12 12:21:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// here we have to manually create and manage the container
|
|
|
|
// as the test_inside container does not provide a way to set the pid file argument
|
|
|
|
fn test_pidfile() -> TestResult {
|
|
|
|
// create id for the container and pidfile
|
|
|
|
let container_id = generate_uuid();
|
|
|
|
|
|
|
|
// create temp dir for bundle and for storing the pid
|
2023-05-08 20:40:14 +02:00
|
|
|
let bundle = prepare_bundle().unwrap();
|
|
|
|
let pidfile_dir = tempfile::tempdir().unwrap();
|
2022-03-05 07:49:13 +01:00
|
|
|
let pidfile_path = pidfile_dir.as_ref().join("pidfile");
|
|
|
|
let _ = File::create(&pidfile_path).unwrap();
|
2021-10-12 12:21:23 +02:00
|
|
|
|
|
|
|
// start the container
|
|
|
|
Command::new(get_runtime_path())
|
|
|
|
.stdin(Stdio::null())
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.arg("--root")
|
|
|
|
.arg(bundle.as_ref().join("runtime"))
|
|
|
|
.arg("create")
|
|
|
|
.arg(container_id.to_string())
|
|
|
|
.arg("--bundle")
|
|
|
|
.arg(bundle.as_ref().join("bundle"))
|
|
|
|
.arg("--pid-file")
|
2022-03-05 07:49:13 +01:00
|
|
|
.arg(pidfile_path)
|
2021-10-12 12:21:23 +02:00
|
|
|
.spawn()
|
|
|
|
.unwrap()
|
|
|
|
.wait()
|
|
|
|
.unwrap();
|
|
|
|
|
2022-01-30 12:41:37 +01:00
|
|
|
let (out, err) = get_state(&container_id.to_string(), &bundle).unwrap();
|
2021-10-12 12:21:23 +02:00
|
|
|
|
|
|
|
if !err.is_empty() {
|
|
|
|
cleanup(&container_id, &bundle);
|
2021-10-21 10:21:03 +02:00
|
|
|
return TestResult::Failed(anyhow!("error in state : {}", err));
|
2021-10-12 12:21:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let state: State = serde_json::from_str(&out).unwrap();
|
|
|
|
|
|
|
|
if state.id != container_id.to_string() {
|
|
|
|
cleanup(&container_id, &bundle);
|
2021-10-16 16:13:11 +02:00
|
|
|
return TestResult::Failed(anyhow!(
|
2021-10-21 10:21:03 +02:00
|
|
|
"error in state : id not matched ,expected {} got {}",
|
2021-10-12 12:21:23 +02:00
|
|
|
container_id,
|
|
|
|
state.id
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if state.status != "created" {
|
|
|
|
cleanup(&container_id, &bundle);
|
2021-10-16 16:13:11 +02:00
|
|
|
return TestResult::Failed(anyhow!(
|
2021-10-21 10:21:03 +02:00
|
|
|
"error in state : status not matched ,expected 'created' got {}",
|
2021-10-12 12:21:23 +02:00
|
|
|
state.status
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// get pid from the pidfile
|
|
|
|
let pidfile: i32 = std::fs::read_to_string(pidfile_dir.as_ref().join("pidfile"))
|
|
|
|
.unwrap()
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// get pid from the state
|
|
|
|
if state.pid.unwrap() != pidfile {
|
|
|
|
cleanup(&container_id, &bundle);
|
2021-10-16 16:13:11 +02:00
|
|
|
return TestResult::Failed(anyhow!(
|
2021-10-21 10:21:03 +02:00
|
|
|
"error : pid not matched ,expected {} as per state, but got {} from pidfile instead",
|
2021-10-12 12:21:23 +02:00
|
|
|
state.pid.unwrap(),
|
|
|
|
pidfile
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup(&container_id, &bundle);
|
2021-10-16 16:13:11 +02:00
|
|
|
TestResult::Passed
|
2021-10-12 12:21:23 +02:00
|
|
|
}
|
|
|
|
|
2022-09-24 06:24:20 +02:00
|
|
|
pub fn get_pidfile_test() -> TestGroup {
|
2021-10-12 12:21:23 +02:00
|
|
|
let pidfile = Test::new("pidfile", Box::new(test_pidfile));
|
|
|
|
let mut tg = TestGroup::new("pidfile");
|
|
|
|
tg.add(vec![Box::new(pidfile)]);
|
|
|
|
tg
|
|
|
|
}
|