diff --git a/tests/contest/contest/src/main.rs b/tests/contest/contest/src/main.rs index d4d2dbac2..10d60f85a 100644 --- a/tests/contest/contest/src/main.rs +++ b/tests/contest/contest/src/main.rs @@ -21,6 +21,7 @@ use crate::tests::linux_ns_itype::get_ns_itype_tests; use crate::tests::mounts_recursive::get_mounts_recursive_test; use crate::tests::no_pivot::get_no_pivot_test; use crate::tests::pidfile::get_pidfile_test; +use crate::tests::process::get_process_test; use crate::tests::process_oom_score_adj::get_process_oom_score_adj_test; use crate::tests::process_rlimits::get_process_rlimits_test; use crate::tests::process_user::get_process_user_test; @@ -117,6 +118,7 @@ fn main() -> Result<()> { let scheduler = get_scheduler_test(); let io_priority_test = get_io_priority_test(); let devices = get_devices_test(); + let process = get_process_test(); let process_user = get_process_user_test(); let process_rlimtis = get_process_rlimits_test(); let no_pivot = get_no_pivot_test(); @@ -144,6 +146,7 @@ fn main() -> Result<()> { tm.add_test_group(Box::new(sysctl)); tm.add_test_group(Box::new(scheduler)); tm.add_test_group(Box::new(devices)); + tm.add_test_group(Box::new(process)); tm.add_test_group(Box::new(process_user)); tm.add_test_group(Box::new(process_rlimtis)); tm.add_test_group(Box::new(no_pivot)); diff --git a/tests/contest/contest/src/tests/mod.rs b/tests/contest/contest/src/tests/mod.rs index 9cb049e04..b3edce99a 100644 --- a/tests/contest/contest/src/tests/mod.rs +++ b/tests/contest/contest/src/tests/mod.rs @@ -11,6 +11,7 @@ pub mod linux_ns_itype; pub mod mounts_recursive; pub mod no_pivot; pub mod pidfile; +pub mod process; pub mod process_oom_score_adj; pub mod process_rlimits; pub mod process_user; diff --git a/tests/contest/contest/src/tests/process/mod.rs b/tests/contest/contest/src/tests/process/mod.rs new file mode 100644 index 000000000..8237bcd5f --- /dev/null +++ b/tests/contest/contest/src/tests/process/mod.rs @@ -0,0 +1,2 @@ +mod process_test; +pub use process_test::get_process_test; diff --git a/tests/contest/contest/src/tests/process/process_test.rs b/tests/contest/contest/src/tests/process/process_test.rs new file mode 100644 index 000000000..14cea7580 --- /dev/null +++ b/tests/contest/contest/src/tests/process/process_test.rs @@ -0,0 +1,50 @@ +use std::fs; + +use anyhow::{bail, Context, Ok, Result}; +use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; +use test_framework::{test_result, Test, TestGroup, TestResult}; + +use crate::utils::test_inside_container; + +fn create_spec() -> Result { + let mut process = ProcessBuilder::default() + .args(vec!["runtimetest".to_string(), "process".to_string()]) + .cwd("/test") + .build() + .expect("error in creating process config"); + let mut env = process.env().clone().unwrap(); + env.push("testa=valuea".to_string()); + env.push("testb=123".to_string()); + process.set_env(Some(env)); + + let spec = SpecBuilder::default() + .process(process) + .build() + .context("failed to build spec")?; + + Ok(spec) +} + +fn process_test() -> TestResult { + let spec = test_result!(create_spec()); + + test_inside_container(spec, &|bundle| { + match fs::create_dir(bundle.join("test")) { + Result::Ok(_) => { /*This is expected*/ } + Err(e) => { + bail!(e) + } + } + + Ok(()) + }) +} + +pub fn get_process_test() -> TestGroup { + let mut process_test_group = TestGroup::new("process"); + + let test = Test::new("process_test", Box::new(process_test)); + process_test_group.add(vec![Box::new(test)]); + + process_test_group +} diff --git a/tests/contest/runtimetest/src/main.rs b/tests/contest/runtimetest/src/main.rs index 2de6bb7e0..07bdd0572 100644 --- a/tests/contest/runtimetest/src/main.rs +++ b/tests/contest/runtimetest/src/main.rs @@ -44,6 +44,7 @@ fn main() { "io_priority_class_be" => tests::test_io_priority_class(&spec, IoprioClassBe), "io_priority_class_idle" => tests::test_io_priority_class(&spec, IoprioClassIdle), "devices" => tests::validate_devices(&spec), + "process" => tests::validate_process(&spec), "process_user" => tests::validate_process_user(&spec), "process_rlimits" => tests::validate_process_rlimits(&spec), "no_pivot" => tests::validate_rootfs(), diff --git a/tests/contest/runtimetest/src/tests.rs b/tests/contest/runtimetest/src/tests.rs index ae2e6772f..2a42d5bd6 100644 --- a/tests/contest/runtimetest/src/tests.rs +++ b/tests/contest/runtimetest/src/tests.rs @@ -1,3 +1,4 @@ +use std::env; use std::fs::{self, read_dir}; use std::os::linux::fs::MetadataExt; use std::os::unix::fs::{FileTypeExt, PermissionsExt}; @@ -550,6 +551,38 @@ pub fn test_io_priority_class(spec: &Spec, io_priority_class: IOPriorityClass) { } } +pub fn validate_process(spec: &Spec) { + let process = spec.process().as_ref().unwrap(); + let expected_cwd = process.cwd(); + let cwd = &getcwd().unwrap(); + + if expected_cwd != cwd { + eprintln!( + "error due to spec cwd want {:?}, got {:?}", + expected_cwd, cwd + ) + } + + for env_str in process.env().as_ref().unwrap().iter() { + match env_str.split_once("=") { + Some((env_key, expected_val)) => { + let actual_val = env::var(env_key).unwrap(); + if actual_val != expected_val { + eprintln!( + "error due to spec environment value of {:?} want {:?}, got {:?}", + env_key, expected_val, actual_val + ) + } + } + None => { + eprintln!( + "spec env value is not correct : expected key=value format, got {env_str}" + ) + } + } + } +} + pub fn validate_process_user(spec: &Spec) { let process = spec.process().as_ref().unwrap(); let expected_uid = Uid::from(process.user().uid());