From bebee51f1ea758e893153224036a289cf6393fcc Mon Sep 17 00:00:00 2001 From: AngrySean Date: Mon, 25 Nov 2024 19:53:54 +0800 Subject: [PATCH] refactor(test): refine function create_container (#2973) * refactor(test): refine function create_container add CreateOptions as para to function create_container for create container with different options Signed-off-by: xujihui1985 * fix: fix failed unittest Signed-off-by: xujihui1985 --------- Signed-off-by: xujihui1985 Signed-off-by: Akiyama --- .../contest/src/tests/devices/devices_test.rs | 3 +- .../contest/src/tests/domainname/mod.rs | 3 +- .../contest/src/tests/example/hello_world.rs | 3 +- .../contest/contest/src/tests/hooks/invoke.rs | 7 +- .../contest/contest/src/tests/hostname/mod.rs | 5 +- .../src/tests/io_priority/io_priority_test.rs | 7 +- .../contest/src/tests/mounts_recursive/mod.rs | 33 ++--- .../contest/contest/src/tests/no_pivot/mod.rs | 8 +- .../contest/src/tests/process/process_test.rs | 3 +- .../process_oom_score_adj_test.rs | 3 +- .../process_rlimits/process_rlimits_test.rs | 3 +- .../tests/process_user/process_user_test.rs | 3 +- .../readonly_paths/readonly_paths_tests.rs | 9 +- .../src/tests/scheduler/scheduler_policy.rs | 5 +- .../contest/contest/src/tests/seccomp/mod.rs | 3 +- tests/contest/contest/src/tests/sysctl/mod.rs | 3 +- tests/contest/contest/src/utils/test_utils.rs | 131 ++++-------------- 17 files changed, 85 insertions(+), 147 deletions(-) diff --git a/tests/contest/contest/src/tests/devices/devices_test.rs b/tests/contest/contest/src/tests/devices/devices_test.rs index e83d2a273..0a187510d 100644 --- a/tests/contest/contest/src/tests/devices/devices_test.rs +++ b/tests/contest/contest/src/tests/devices/devices_test.rs @@ -5,6 +5,7 @@ use oci_spec::runtime::{ use test_framework::{test_result, Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; fn create_spec() -> Result { let device1 = LinuxDeviceBuilder::default() @@ -59,7 +60,7 @@ fn create_spec() -> Result { fn devices_test() -> TestResult { let spec = test_result!(create_spec()); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } pub fn get_devices_test() -> TestGroup { diff --git a/tests/contest/contest/src/tests/domainname/mod.rs b/tests/contest/contest/src/tests/domainname/mod.rs index 188029372..2e2dacef0 100644 --- a/tests/contest/contest/src/tests/domainname/mod.rs +++ b/tests/contest/contest/src/tests/domainname/mod.rs @@ -1,6 +1,7 @@ use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; use test_framework::{ConditionalTest, TestGroup, TestResult}; +use crate::utils::test_utils::CreateOptions; use crate::utils::{is_runtime_runc, test_inside_container}; fn get_spec(domainname: &str) -> Spec { @@ -21,7 +22,7 @@ fn get_spec(domainname: &str) -> Spec { fn set_domainname_test() -> TestResult { let spec = get_spec("domainname"); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } pub fn get_domainname_tests() -> TestGroup { diff --git a/tests/contest/contest/src/tests/example/hello_world.rs b/tests/contest/contest/src/tests/example/hello_world.rs index 316fd6982..231b2c284 100644 --- a/tests/contest/contest/src/tests/example/hello_world.rs +++ b/tests/contest/contest/src/tests/example/hello_world.rs @@ -3,6 +3,7 @@ use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; use test_framework::{test_result, Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; ////////// ANCHOR: get_example_spec fn create_spec() -> Result { @@ -25,7 +26,7 @@ fn create_spec() -> Result { ////////// ANCHOR: example_test fn example_test() -> TestResult { let spec = test_result!(create_spec()); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } ////////// ANCHOR_END: example_test diff --git a/tests/contest/contest/src/tests/hooks/invoke.rs b/tests/contest/contest/src/tests/hooks/invoke.rs index 7cd22dac2..9076f4484 100644 --- a/tests/contest/contest/src/tests/hooks/invoke.rs +++ b/tests/contest/contest/src/tests/hooks/invoke.rs @@ -5,7 +5,7 @@ use anyhow::anyhow; use oci_spec::runtime::{Hook, HookBuilder, HooksBuilder, ProcessBuilder, Spec, SpecBuilder}; use test_framework::{Test, TestGroup, TestResult}; -use crate::utils::test_utils::start_container; +use crate::utils::test_utils::{start_container, CreateOptions}; use crate::utils::{create_container, delete_container, generate_uuid, prepare_bundle, set_config}; const HOOK_OUTPUT_FILE: &str = "output"; @@ -71,7 +71,10 @@ fn get_test(test_name: &'static str) -> Test { let id_str = id.to_string(); let bundle = prepare_bundle().unwrap(); set_config(&bundle, &spec).unwrap(); - create_container(&id_str, &bundle).unwrap().wait().unwrap(); + create_container(&id_str, &bundle, &CreateOptions::default()) + .unwrap() + .wait() + .unwrap(); start_container(&id_str, &bundle).unwrap().wait().unwrap(); delete_container(&id_str, &bundle).unwrap().wait().unwrap(); let log = { diff --git a/tests/contest/contest/src/tests/hostname/mod.rs b/tests/contest/contest/src/tests/hostname/mod.rs index 1a740d8c5..17e0ce49b 100644 --- a/tests/contest/contest/src/tests/hostname/mod.rs +++ b/tests/contest/contest/src/tests/hostname/mod.rs @@ -2,6 +2,7 @@ use oci_spec::runtime::{LinuxBuilder, ProcessBuilder, Spec, SpecBuilder}; use test_framework::{Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; fn create_spec(hostname: &str) -> Spec { SpecBuilder::default() @@ -25,7 +26,7 @@ fn create_spec(hostname: &str) -> Spec { fn hostname_test() -> TestResult { let spec = create_spec("hostname-specific"); - test_inside_container(spec, &|_| { + test_inside_container(spec, &CreateOptions::default(), &|_| { // As long as the container is created, we expect the hostname to be determined // by the spec, so nothing to prepare prior. Ok(()) @@ -34,7 +35,7 @@ fn hostname_test() -> TestResult { fn empty_hostname() -> TestResult { let spec = create_spec(""); - test_inside_container(spec, &|_| { + test_inside_container(spec, &CreateOptions::default(), &|_| { // As long as the container is created, we expect the hostname to be determined // by the spec, so nothing to prepare prior. Ok(()) diff --git a/tests/contest/contest/src/tests/io_priority/io_priority_test.rs b/tests/contest/contest/src/tests/io_priority/io_priority_test.rs index b1a978a42..85476c087 100644 --- a/tests/contest/contest/src/tests/io_priority/io_priority_test.rs +++ b/tests/contest/contest/src/tests/io_priority/io_priority_test.rs @@ -4,6 +4,7 @@ use oci_spec::runtime::{ }; use test_framework::{test_result, ConditionalTest, TestGroup, TestResult}; +use crate::utils::test_utils::CreateOptions; use crate::utils::{is_runtime_runc, test_inside_container}; fn create_spec( @@ -38,7 +39,7 @@ fn io_priority_class_rt_test() -> TestResult { "io_priority_class_rt", 1, )); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } fn io_priority_class_be_test() -> TestResult { @@ -47,7 +48,7 @@ fn io_priority_class_be_test() -> TestResult { "io_priority_class_be", 2, )); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } fn io_priority_class_idle_test() -> TestResult { @@ -56,7 +57,7 @@ fn io_priority_class_idle_test() -> TestResult { "io_priority_class_idle", 3, )); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } pub fn get_io_priority_test() -> TestGroup { diff --git a/tests/contest/contest/src/tests/mounts_recursive/mod.rs b/tests/contest/contest/src/tests/mounts_recursive/mod.rs index 976d5008b..c775ff89d 100644 --- a/tests/contest/contest/src/tests/mounts_recursive/mod.rs +++ b/tests/contest/contest/src/tests/mounts_recursive/mod.rs @@ -18,6 +18,7 @@ use oci_spec::runtime::{ use test_framework::{Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; fn get_spec(added_mounts: Vec, process_args: Vec) -> Spec { let mut mounts = get_default_mounts(); @@ -112,7 +113,7 @@ fn check_recursive_readonly() -> TestResult { vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|_| { + let result = test_inside_container(spec, &CreateOptions::default(), &|_| { setup_mount(&rro_dir_path, &rro_subdir_path); Ok(()) }); @@ -152,7 +153,7 @@ fn check_recursive_nosuid() -> TestResult { ], ); - let result = test_inside_container(spec, &|bundle_path| { + let result = test_inside_container(spec, &CreateOptions::default(), &|bundle_path| { setup_mount(&rnosuid_dir_path, &rnosuid_subdir_path); let executable_file_path = bundle_path.join("bin").join(executable_file_name); @@ -225,7 +226,7 @@ fn check_recursive_rsuid() -> TestResult { vec![mount_spec], vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - test_inside_container(spec, &|_| { + test_inside_container(spec, &CreateOptions::default(), &|_| { let original_file_path = rsuid_dir_path.join("file"); let file = File::create(original_file_path)?; let mut permission = file.metadata()?.permissions(); @@ -256,7 +257,7 @@ fn check_recursive_noexec() -> TestResult { vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|bundle_path| { + let result = test_inside_container(spec, &CreateOptions::default(), &|bundle_path| { setup_mount(&rnoexec_dir_path, &rnoexec_subdir_path); let executable_file_name = "echo"; @@ -297,7 +298,7 @@ fn check_recursive_rexec() -> TestResult { vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|bundle_path| { + let result = test_inside_container(spec, &CreateOptions::default(), &|bundle_path| { setup_mount(&rnoexec_dir_path, &rnoexec_subdir_path); let executable_file_name = "echo"; @@ -338,7 +339,7 @@ fn check_recursive_rdiratime() -> TestResult { vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|_| Ok(())); + let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())); fs::remove_dir(rdiratime_base_dir).unwrap(); result @@ -362,7 +363,7 @@ fn check_recursive_rnodiratime() -> TestResult { vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|_| Ok(())); + let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())); fs::remove_dir(rnodiratime_base_dir).unwrap(); result } @@ -383,7 +384,7 @@ fn check_recursive_rdev() -> TestResult { vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } fn check_recursive_rnodev() -> TestResult { @@ -402,7 +403,7 @@ fn check_recursive_rnodev() -> TestResult { vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } fn check_recursive_readwrite() -> TestResult { @@ -423,7 +424,7 @@ fn check_recursive_readwrite() -> TestResult { vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|_| { + let result = test_inside_container(spec, &CreateOptions::default(), &|_| { setup_mount(&rrw_dir_path, &rrw_subdir_path); Ok(()) }); @@ -451,7 +452,7 @@ fn check_recursive_rrelatime() -> TestResult { vec![mount_spec], vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|_| Ok(())); + let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())); fs::remove_dir_all(rrelatime_dir_path).unwrap(); result @@ -475,7 +476,7 @@ fn check_recursive_rnorelatime() -> TestResult { vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|_| Ok(())); + let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())); fs::remove_dir_all(rnorelatime_dir_path).unwrap(); result @@ -499,7 +500,7 @@ fn check_recursive_rnoatime() -> TestResult { vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|_| Ok(())); + let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())); fs::remove_dir_all(rnoatime_dir_path).unwrap(); result @@ -522,7 +523,7 @@ fn check_recursive_rstrictatime() -> TestResult { vec![mount_spec], vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|_| Ok(())); + let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())); fs::remove_dir_all(rstrictatime_dir_path).unwrap(); result @@ -548,7 +549,7 @@ fn check_recursive_rnosymfollow() -> TestResult { vec![mount_spec], vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|_| { + let result = test_inside_container(spec, &CreateOptions::default(), &|_| { let original_file_path = format!("{}/{}", rnosymfollow_dir_path.to_str().unwrap(), "file"); let file = File::create(&original_file_path)?; let link_file_path = format!("{}/{}", rnosymfollow_dir_path.to_str().unwrap(), "link"); @@ -587,7 +588,7 @@ fn check_recursive_rsymfollow() -> TestResult { vec![mount_spec], vec!["runtimetest".to_string(), "mounts_recursive".to_string()], ); - let result = test_inside_container(spec, &|_| { + let result = test_inside_container(spec, &CreateOptions::default(), &|_| { let original_file_path = format!("{}/{}", rsymfollow_dir_path.to_str().unwrap(), "file"); let file = File::create(&original_file_path)?; let link_file_path = format!("{}/{}", rsymfollow_dir_path.to_str().unwrap(), "link"); diff --git a/tests/contest/contest/src/tests/no_pivot/mod.rs b/tests/contest/contest/src/tests/no_pivot/mod.rs index 8540a058a..644b4365b 100644 --- a/tests/contest/contest/src/tests/no_pivot/mod.rs +++ b/tests/contest/contest/src/tests/no_pivot/mod.rs @@ -2,7 +2,7 @@ use anyhow::{Context, Result}; use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; use test_framework::{test_result, Test, TestGroup, TestResult}; -use crate::utils::test_utils::test_inside_container_with_no_pivot; +use crate::utils::test_utils::{test_inside_container, CreateOptions}; fn create_spec() -> Result { SpecBuilder::default() @@ -17,7 +17,11 @@ fn create_spec() -> Result { fn no_pivot_test() -> TestResult { let spec = test_result!(create_spec()); - test_inside_container_with_no_pivot(spec, &|_| Ok(())) + test_inside_container( + spec, + &CreateOptions::default().with_no_pivot_root(), + &|_| Ok(()), + ) } pub fn get_no_pivot_test() -> TestGroup { diff --git a/tests/contest/contest/src/tests/process/process_test.rs b/tests/contest/contest/src/tests/process/process_test.rs index 14cea7580..81c6b501f 100644 --- a/tests/contest/contest/src/tests/process/process_test.rs +++ b/tests/contest/contest/src/tests/process/process_test.rs @@ -5,6 +5,7 @@ use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; use test_framework::{test_result, Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; fn create_spec() -> Result { let mut process = ProcessBuilder::default() @@ -28,7 +29,7 @@ fn create_spec() -> Result { fn process_test() -> TestResult { let spec = test_result!(create_spec()); - test_inside_container(spec, &|bundle| { + test_inside_container(spec, &CreateOptions::default(), &|bundle| { match fs::create_dir(bundle.join("test")) { Result::Ok(_) => { /*This is expected*/ } Err(e) => { diff --git a/tests/contest/contest/src/tests/process_oom_score_adj/process_oom_score_adj_test.rs b/tests/contest/contest/src/tests/process_oom_score_adj/process_oom_score_adj_test.rs index ac8167807..03cea34c6 100644 --- a/tests/contest/contest/src/tests/process_oom_score_adj/process_oom_score_adj_test.rs +++ b/tests/contest/contest/src/tests/process_oom_score_adj/process_oom_score_adj_test.rs @@ -4,6 +4,7 @@ use rand::Rng; use test_framework::{test_result, Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; fn generate_random_number() -> i32 { let mut rng = rand::thread_rng(); @@ -30,7 +31,7 @@ fn create_spec() -> Result { fn process_oom_score_adj_test() -> TestResult { let spec = test_result!(create_spec()); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } pub fn get_process_oom_score_adj_test() -> TestGroup { diff --git a/tests/contest/contest/src/tests/process_rlimits/process_rlimits_test.rs b/tests/contest/contest/src/tests/process_rlimits/process_rlimits_test.rs index 7d5456d50..18f503351 100644 --- a/tests/contest/contest/src/tests/process_rlimits/process_rlimits_test.rs +++ b/tests/contest/contest/src/tests/process_rlimits/process_rlimits_test.rs @@ -5,6 +5,7 @@ use oci_spec::runtime::{ use test_framework::{test_result, Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; const GIGABYTES: u64 = 1024 * 1024 * 1024; @@ -54,7 +55,7 @@ fn create_spec() -> Result { fn process_rlimits_test() -> TestResult { let spec = test_result!(create_spec()); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } pub fn get_process_rlimits_test() -> TestGroup { diff --git a/tests/contest/contest/src/tests/process_user/process_user_test.rs b/tests/contest/contest/src/tests/process_user/process_user_test.rs index 825cb3254..5329ccbbf 100644 --- a/tests/contest/contest/src/tests/process_user/process_user_test.rs +++ b/tests/contest/contest/src/tests/process_user/process_user_test.rs @@ -4,6 +4,7 @@ use rand::Rng; use test_framework::{test_result, Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; // Generates a Vec with a random number of elements (between 5 and 15), // where each element is a random u32 value between 0 and 65535. @@ -43,7 +44,7 @@ fn create_spec() -> Result { } fn process_user_test() -> TestResult { let spec = test_result!(create_spec()); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } pub fn get_process_user_test() -> TestGroup { diff --git a/tests/contest/contest/src/tests/readonly_paths/readonly_paths_tests.rs b/tests/contest/contest/src/tests/readonly_paths/readonly_paths_tests.rs index 36b77ccb6..c90784ccf 100644 --- a/tests/contest/contest/src/tests/readonly_paths/readonly_paths_tests.rs +++ b/tests/contest/contest/src/tests/readonly_paths/readonly_paths_tests.rs @@ -6,6 +6,7 @@ use oci_spec::runtime::{LinuxBuilder, ProcessBuilder, Spec, SpecBuilder}; use test_framework::{Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; fn get_spec(readonly_paths: Vec) -> Spec { SpecBuilder::default() @@ -60,7 +61,7 @@ fn check_readonly_paths() -> TestResult { ]; let spec = get_spec(ro_paths); - test_inside_container(spec, &|bundle_path| { + test_inside_container(spec, &CreateOptions::default(), &|bundle_path| { use std::{fs, io}; let test_dir = bundle_path.join(&ro_dir_sub); @@ -111,7 +112,7 @@ fn check_readonly_rel_path() -> TestResult { let ro_paths = vec![ro_rel_path.to_string()]; let spec = get_spec(ro_paths); - test_inside_container(spec, &|bundle_path| { + test_inside_container(spec, &CreateOptions::default(), &|bundle_path| { use std::{fs, io}; let test_file = bundle_path.join(ro_rel_path); @@ -142,7 +143,7 @@ fn check_readonly_symlinks() -> TestResult { let spec = get_spec(ro_paths); - let res = test_inside_container(spec, &|bundle_path| { + let res = test_inside_container(spec, &CreateOptions::default(), &|bundle_path| { use std::{fs, io}; let test_file = bundle_path.join(ro_symlink); @@ -193,7 +194,7 @@ fn test_node(mode: u32) -> TestResult { let spec = get_spec(ro_paths); - test_inside_container(spec, &|bundle_path| { + test_inside_container(spec, &CreateOptions::default(), &|bundle_path| { use std::os::unix::fs::OpenOptionsExt; use std::{fs, io}; let test_file = bundle_path.join(ro_device); diff --git a/tests/contest/contest/src/tests/scheduler/scheduler_policy.rs b/tests/contest/contest/src/tests/scheduler/scheduler_policy.rs index bbf68712d..4bbc4c6f1 100644 --- a/tests/contest/contest/src/tests/scheduler/scheduler_policy.rs +++ b/tests/contest/contest/src/tests/scheduler/scheduler_policy.rs @@ -4,6 +4,7 @@ use oci_spec::runtime::{ }; use test_framework::{test_result, ConditionalTest, TestGroup, TestResult}; +use crate::utils::test_utils::CreateOptions; use crate::utils::{is_runtime_runc, test_inside_container}; fn create_spec(policy: LinuxSchedulerPolicy, execute_test: &str) -> Result { @@ -33,7 +34,7 @@ fn scheduler_policy_other_test() -> TestResult { LinuxSchedulerPolicy::SchedOther, "scheduler_policy_other" )); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } fn scheduler_policy_batch_test() -> TestResult { @@ -41,7 +42,7 @@ fn scheduler_policy_batch_test() -> TestResult { LinuxSchedulerPolicy::SchedBatch, "scheduler_policy_batch" )); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } pub fn get_scheduler_test() -> TestGroup { diff --git a/tests/contest/contest/src/tests/seccomp/mod.rs b/tests/contest/contest/src/tests/seccomp/mod.rs index ba583da28..1f9448abc 100644 --- a/tests/contest/contest/src/tests/seccomp/mod.rs +++ b/tests/contest/contest/src/tests/seccomp/mod.rs @@ -5,6 +5,7 @@ use oci_spec::runtime::{ use test_framework::{Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; fn create_spec(seccomp: LinuxSeccomp) -> Spec { SpecBuilder::default() @@ -36,7 +37,7 @@ fn seccomp_test() -> TestResult { .build() .unwrap(), ); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) } pub fn get_seccomp_test() -> TestGroup { diff --git a/tests/contest/contest/src/tests/sysctl/mod.rs b/tests/contest/contest/src/tests/sysctl/mod.rs index d80703e44..81c35120c 100644 --- a/tests/contest/contest/src/tests/sysctl/mod.rs +++ b/tests/contest/contest/src/tests/sysctl/mod.rs @@ -4,6 +4,7 @@ use oci_spec::runtime::{LinuxBuilder, ProcessBuilder, Spec, SpecBuilder}; use test_framework::{Test, TestGroup, TestResult}; use crate::utils::test_inside_container; +use crate::utils::test_utils::CreateOptions; fn create_spec(sysctl: HashMap) -> Spec { SpecBuilder::default() @@ -28,7 +29,7 @@ fn sysctl_test() -> TestResult { "net.ipv4.ip_forward".to_string(), "1".to_string(), )])); - test_inside_container(spec, &|_| { + test_inside_container(spec, &CreateOptions::default(), &|_| { // As long as the container is created, we expect the kernel parameters to be determined by // the spec, so nothing to prepare prior. Ok(()) diff --git a/tests/contest/contest/src/utils/test_utils.rs b/tests/contest/contest/src/utils/test_utils.rs index ceb31262e..0964a9a04 100644 --- a/tests/contest/contest/src/utils/test_utils.rs +++ b/tests/contest/contest/src/utils/test_utils.rs @@ -42,7 +42,19 @@ pub struct ContainerData { pub create_result: std::io::Result, } -fn create_container_command>(id: &str, dir: P, with_pivot_root: bool) -> Command { +#[derive(Debug, Default)] +pub struct CreateOptions { + no_pivot: bool, +} + +impl CreateOptions { + pub fn with_no_pivot_root(mut self) -> Self { + self.no_pivot = true; + self + } +} + +fn create_container_command>(id: &str, dir: P, options: &CreateOptions) -> Command { let mut command = Command::new(get_runtime_path()); command .stdout(Stdio::piped()) @@ -53,22 +65,19 @@ fn create_container_command>(id: &str, dir: P, with_pivot_root: b .arg(id) .arg("--bundle") .arg(dir.as_ref().join("bundle")); - if with_pivot_root { + if options.no_pivot { command.arg("--no-pivot"); } command } /// Starts the runtime with given directory as root directory -pub fn create_container>(id: &str, dir: P) -> Result { - let res = create_container_command(id, dir, false) - .spawn() - .context("could not create container")?; - Ok(res) -} - -pub fn create_container_no_pivot>(id: &str, dir: P) -> Result { - let res = create_container_command(id, dir, true) +pub fn create_container>( + id: &str, + dir: P, + options: &CreateOptions, +) -> Result { + let res = create_container_command(id, dir, options) .spawn() .context("could not create container")?; Ok(res) @@ -135,7 +144,8 @@ pub fn test_outside_container( let id_str = id.to_string(); let bundle = prepare_bundle().unwrap(); set_config(&bundle, &spec).unwrap(); - let create_result = create_container(&id_str, &bundle).unwrap().wait(); + let options = CreateOptions::default(); + let create_result = create_container(&id_str, &bundle, &options).unwrap().wait(); let (out, err) = get_state(&id_str, &bundle).unwrap(); let state: Option = match serde_json::from_str(&out) { Ok(v) => Some(v), @@ -156,6 +166,7 @@ pub fn test_outside_container( // mostly needs a name that better expresses what this actually does pub fn test_inside_container( spec: Spec, + options: &CreateOptions, setup_for_test: &dyn Fn(&Path) -> Result<()>, ) -> TestResult { let id = generate_uuid(); @@ -190,101 +201,7 @@ pub fn test_inside_container( .join("runtimetest"), ) .unwrap(); - let create_process = create_container(&id_str, &bundle).unwrap(); - // here we do not wait for the process by calling wait() as in the test_outside_container - // function because we need the output of the runtimetest. If we call wait, it will return - // and we won't have an easy way of getting the stdio of the runtimetest. - // Thus to make sure the container is created, we just wait for sometime, and - // assume that the create command was successful. If it wasn't we can catch that error - // in the start_container, as we can not start a non-created container anyways - std::thread::sleep(std::time::Duration::from_millis(1000)); - match start_container(&id_str, &bundle) - .unwrap() - .wait_with_output() - { - Ok(c) => c, - Err(e) => return TestResult::Failed(anyhow!("container start failed : {:?}", e)), - }; - - let create_output = create_process - .wait_with_output() - .context("getting output after starting the container failed") - .unwrap(); - - let stdout = String::from_utf8_lossy(&create_output.stdout); - if !stdout.is_empty() { - println!( - "{:?}", - anyhow!("container stdout was not empty, found : {}", stdout) - ) - } - let stderr = String::from_utf8_lossy(&create_output.stderr); - if !stderr.is_empty() { - return TestResult::Failed(anyhow!( - "container stderr was not empty, found : {}", - stderr - )); - } - - let (out, err) = get_state(&id_str, &bundle).unwrap(); - if !err.is_empty() { - return TestResult::Failed(anyhow!( - "error in getting state after starting the container : {}", - err - )); - } - - let state: State = match serde_json::from_str(&out) { - Ok(v) => v, - Err(e) => return TestResult::Failed(anyhow!("error in parsing state of container after start in test_inside_container : stdout : {}, parse error : {}",out,e)), - }; - if state.status != "stopped" { - return TestResult::Failed(anyhow!("error : unexpected container status in test_inside_runtime : expected stopped, got {}, container state : {:?}",state.status,state)); - } - kill_container(&id_str, &bundle).unwrap().wait().unwrap(); - delete_container(&id_str, &bundle).unwrap().wait().unwrap(); - TestResult::Passed -} - -// just copy-pasted from test_inside_container for now, but with no pivot root -// need to refactor this to avoid duplication -pub fn test_inside_container_with_no_pivot( - spec: Spec, - setup_for_test: &dyn Fn(&Path) -> Result<()>, -) -> TestResult { - let id = generate_uuid(); - let id_str = id.to_string(); - let bundle = prepare_bundle().unwrap(); - - // This will do the required setup for the test - test_result!(setup_for_test( - &bundle.as_ref().join("bundle").join("rootfs") - )); - - set_config(&bundle, &spec).unwrap(); - // as we have to run runtimetest inside the container, and is expects - // the config.json to be at path /config.json we save it there - let path = bundle - .as_ref() - .join("bundle") - .join("rootfs") - .join("config.json"); - spec.save(path).unwrap(); - - let runtimetest_path = get_runtimetest_path(); - // The config will directly use runtime as the command to be run, so we have to - // save the runtimetest binary at its /bin - std::fs::copy( - runtimetest_path, - bundle - .as_ref() - .join("bundle") - .join("rootfs") - .join("bin") - .join("runtimetest"), - ) - .unwrap(); - let create_process = create_container_no_pivot(&id_str, &bundle).unwrap(); + let create_process = create_container(&id_str, &bundle, options).unwrap(); // here we do not wait for the process by calling wait() as in the test_outside_container // function because we need the output of the runtimetest. If we call wait, it will return // and we won't have an easy way of getting the stdio of the runtimetest.