Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add process test #2968

Merged
merged 23 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/contest/contest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions tests/contest/contest/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/contest/contest/src/tests/process/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod process_test;
pub use process_test::get_process_test;
50 changes: 50 additions & 0 deletions tests/contest/contest/src/tests/process/process_test.rs
Original file line number Diff line number Diff line change
@@ -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<Spec> {
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
}
1 change: 1 addition & 0 deletions tests/contest/runtimetest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
33 changes: 33 additions & 0 deletions tests/contest/runtimetest/src/tests.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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());
Expand Down