From b156f6bb4f6cf4cb56458f3f8d02e0705be1e100 Mon Sep 17 00:00:00 2001 From: sat0ken <15720506+sat0ken@users.noreply.github.com> Date: Sun, 20 Oct 2024 20:03:42 +0900 Subject: [PATCH 1/7] Add process test Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> --- tests/contest/contest/src/main.rs | 3 ++ tests/contest/contest/src/tests/mod.rs | 1 + .../contest/contest/src/tests/process/mod.rs | 2 ++ .../contest/src/tests/process/process_test.rs | 32 +++++++++++++++++++ tests/contest/runtimetest/src/main.rs | 1 + tests/contest/runtimetest/src/tests.rs | 18 +++++++++++ 6 files changed, 57 insertions(+) create mode 100644 tests/contest/contest/src/tests/process/mod.rs create mode 100644 tests/contest/contest/src/tests/process/process_test.rs diff --git a/tests/contest/contest/src/main.rs b/tests/contest/contest/src/main.rs index 8049457c0..fa77d96a9 100644 --- a/tests/contest/contest/src/main.rs +++ b/tests/contest/contest/src/main.rs @@ -20,6 +20,7 @@ use crate::tests::lifecycle::{ContainerCreate, ContainerLifecycle}; use crate::tests::linux_ns_itype::get_ns_itype_tests; use crate::tests::mounts_recursive::get_mounts_recursive_test; use crate::tests::pidfile::get_pidfile_test; +use crate::tests::process::get_process_test; use crate::tests::readonly_paths::get_ro_paths_test; use crate::tests::scheduler::get_scheduler_test; use crate::tests::seccomp::get_seccomp_test; @@ -113,6 +114,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(); tm.add_test_group(Box::new(cl)); tm.add_test_group(Box::new(cc)); @@ -136,6 +138,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(io_priority_test)); tm.add_cleanup(Box::new(cgroups::cleanup_v1)); diff --git a/tests/contest/contest/src/tests/mod.rs b/tests/contest/contest/src/tests/mod.rs index 1fee606b1..c3b57cc7f 100644 --- a/tests/contest/contest/src/tests/mod.rs +++ b/tests/contest/contest/src/tests/mod.rs @@ -10,6 +10,7 @@ pub mod lifecycle; pub mod linux_ns_itype; pub mod mounts_recursive; pub mod pidfile; +pub mod process; pub mod readonly_paths; pub mod scheduler; pub mod seccomp; 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..ce1911343 --- /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; \ No newline at end of file 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..4ced03228 --- /dev/null +++ b/tests/contest/contest/src/tests/process/process_test.rs @@ -0,0 +1,32 @@ +use crate::utils::test_inside_container; +use anyhow::{Context, Ok, Result}; +use oci_spec::runtime::{ + LinuxBuilder, LinuxDeviceBuilder, LinuxDeviceType, ProcessBuilder, Spec, SpecBuilder, +}; +use test_framework::{test_result, Test, TestGroup, TestResult}; + +fn create_spec() -> Result { + let spec = SpecBuilder::default().process( + ProcessBuilder::default() + .cwd("/test") + .env(vec!["testa", "valuea", "testb", "123"]) + .build() + .expect("error in creating process config"), + ).build().context("failed to build spec")?; + + Ok(spec) +} + +fn process_test() -> TestResult { + let spec = test_result!(create_spec()); + test_inside_container(spec, &|_| 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 +} \ No newline at end of file diff --git a/tests/contest/runtimetest/src/main.rs b/tests/contest/runtimetest/src/main.rs index 95780bd48..81d7a998a 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), _ => eprintln!("error due to unexpected execute test name: {execute_test}"), } } diff --git a/tests/contest/runtimetest/src/tests.rs b/tests/contest/runtimetest/src/tests.rs index 40f5ad29c..b70ccf383 100644 --- a/tests/contest/runtimetest/src/tests.rs +++ b/tests/contest/runtimetest/src/tests.rs @@ -1,9 +1,11 @@ +use std::env; use std::fs::{self, read_dir}; use std::os::linux::fs::MetadataExt; use std::os::unix::fs::{FileTypeExt, PermissionsExt}; use std::path::Path; use anyhow::{bail, Result}; + use nix::errno::Errno; use nix::libc; use nix::sys::utsname; @@ -545,3 +547,19 @@ pub fn test_io_priority_class(spec: &Spec, io_priority_class: IOPriorityClass) { eprintln!("error ioprio_get expected priority {expected_priority:?}, got {priority}") } } + +pub fn validate_process(spec: &Spec) { + let process = spec.process().as_ref().unwrap(); + + if process.cwd().ne(&getcwd().unwrap()) { + eprintln!("error due to spec cwd want {:?}, got {:?}", process.cwd(), getcwd().unwrap()) + } + + if env::var("testa").unwrap().to_string().ne("valuea") { + eprintln!("error due to spec environment value of testa want {:?}, got {:?}", "valuea", env::var("testa")) + } + + if env::var("testb").unwrap().to_string().ne("123") { + eprintln!("error due to spec environment value of testb want {:?}, got {:?}", "123", env::var("testb")) + } +} \ No newline at end of file From 78dfb54fed7c745d5b6d59aea2bd0d5f6dbec75b Mon Sep 17 00:00:00 2001 From: sat0ken <15720506+sat0ken@users.noreply.github.com> Date: Wed, 23 Oct 2024 06:05:07 +0900 Subject: [PATCH 2/7] rm unused imports Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> --- tests/contest/contest/src/tests/process/process_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/contest/contest/src/tests/process/process_test.rs b/tests/contest/contest/src/tests/process/process_test.rs index 4ced03228..d54899729 100644 --- a/tests/contest/contest/src/tests/process/process_test.rs +++ b/tests/contest/contest/src/tests/process/process_test.rs @@ -1,7 +1,7 @@ use crate::utils::test_inside_container; use anyhow::{Context, Ok, Result}; use oci_spec::runtime::{ - LinuxBuilder, LinuxDeviceBuilder, LinuxDeviceType, ProcessBuilder, Spec, SpecBuilder, + ProcessBuilder, Spec, SpecBuilder, }; use test_framework::{test_result, Test, TestGroup, TestResult}; From aef1f4522dd485e261e1ee0cca6d92e66c446ef5 Mon Sep 17 00:00:00 2001 From: sat0ken <15720506+sat0ken@users.noreply.github.com> Date: Wed, 23 Oct 2024 06:16:24 +0900 Subject: [PATCH 3/7] fix err Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> --- tests/contest/contest/src/tests/process/process_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/contest/contest/src/tests/process/process_test.rs b/tests/contest/contest/src/tests/process/process_test.rs index d54899729..942b64a43 100644 --- a/tests/contest/contest/src/tests/process/process_test.rs +++ b/tests/contest/contest/src/tests/process/process_test.rs @@ -9,7 +9,7 @@ fn create_spec() -> Result { let spec = SpecBuilder::default().process( ProcessBuilder::default() .cwd("/test") - .env(vec!["testa", "valuea", "testb", "123"]) + .env(vec!["testa".to_string(), "valuea".to_string(), "testb".to_string(), "123".to_string()]) .build() .expect("error in creating process config"), ).build().context("failed to build spec")?; From 3d52b59ef266f412d2ef41de3652d76378647e37 Mon Sep 17 00:00:00 2001 From: sat0ken <15720506+sat0ken@users.noreply.github.com> Date: Wed, 30 Oct 2024 22:31:25 +0900 Subject: [PATCH 4/7] fix set env Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> --- tests/contest/contest/src/tests/process/process_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/contest/contest/src/tests/process/process_test.rs b/tests/contest/contest/src/tests/process/process_test.rs index 942b64a43..9fac143cf 100644 --- a/tests/contest/contest/src/tests/process/process_test.rs +++ b/tests/contest/contest/src/tests/process/process_test.rs @@ -9,7 +9,7 @@ fn create_spec() -> Result { let spec = SpecBuilder::default().process( ProcessBuilder::default() .cwd("/test") - .env(vec!["testa".to_string(), "valuea".to_string(), "testb".to_string(), "123".to_string()]) + .env(vec!["testa=valuea".into(), "testb=123".into()]) .build() .expect("error in creating process config"), ).build().context("failed to build spec")?; From ef26e24248e07901aed22ac94fda87beb7e289fb Mon Sep 17 00:00:00 2001 From: sat0ken <15720506+sat0ken@users.noreply.github.com> Date: Wed, 30 Oct 2024 22:38:42 +0900 Subject: [PATCH 5/7] fix format err Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> --- .../contest/src/tests/process/process_test.rs | 22 ++++++++++++------- tests/contest/runtimetest/src/tests.rs | 19 ++++++++++++---- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/tests/contest/contest/src/tests/process/process_test.rs b/tests/contest/contest/src/tests/process/process_test.rs index 9fac143cf..75930319a 100644 --- a/tests/contest/contest/src/tests/process/process_test.rs +++ b/tests/contest/contest/src/tests/process/process_test.rs @@ -6,13 +6,19 @@ use oci_spec::runtime::{ use test_framework::{test_result, Test, TestGroup, TestResult}; fn create_spec() -> Result { - let spec = SpecBuilder::default().process( - ProcessBuilder::default() - .cwd("/test") - .env(vec!["testa=valuea".into(), "testb=123".into()]) - .build() - .expect("error in creating process config"), - ).build().context("failed to build spec")?; + let spec = SpecBuilder::default() + .process( + ProcessBuilder::default() + .cwd("/test") + .env(vec![ + "testa=valuea".into(), + "testb=123".into() + ]) + .build() + .expect("error in creating process config"), + ) + .build() + .context("failed to build spec")?; Ok(spec) } @@ -29,4 +35,4 @@ pub fn get_process_test() -> TestGroup { process_test_group.add(vec![Box::new(test)]); process_test_group -} \ No newline at end of file +} diff --git a/tests/contest/runtimetest/src/tests.rs b/tests/contest/runtimetest/src/tests.rs index a0f666cf4..8162773a0 100644 --- a/tests/contest/runtimetest/src/tests.rs +++ b/tests/contest/runtimetest/src/tests.rs @@ -548,20 +548,31 @@ 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(); if process.cwd().ne(&getcwd().unwrap()) { - eprintln!("error due to spec cwd want {:?}, got {:?}", process.cwd(), getcwd().unwrap()) + eprintln!( + "error due to spec cwd want {:?}, got {:?}", + process.cwd(), + getcwd().unwrap() + ) } if env::var("testa").unwrap().to_string().ne("valuea") { - eprintln!("error due to spec environment value of testa want {:?}, got {:?}", "valuea", env::var("testa")) + eprintln!( + "error due to spec environment value of testa want {:?}, got {:?}", + "valuea", + env::var("testa") + ) } if env::var("testb").unwrap().to_string().ne("123") { - eprintln!("error due to spec environment value of testb want {:?}, got {:?}", "123", env::var("testb")) + eprintln!( + "error due to spec environment value of testb want {:?}, got {:?}", + "123", + env::var("testb") + ) } } From 846df14e36999cbf7ba4a264f259e25ce449014c Mon Sep 17 00:00:00 2001 From: sat0ken <15720506+sat0ken@users.noreply.github.com> Date: Wed, 30 Oct 2024 23:07:16 +0900 Subject: [PATCH 6/7] add mkdir for test Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> --- .../contest/contest/src/tests/process/mod.rs | 2 +- .../contest/src/tests/process/process_test.rs | 22 +++++++++++-------- tests/contest/runtimetest/src/tests.rs | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/contest/contest/src/tests/process/mod.rs b/tests/contest/contest/src/tests/process/mod.rs index ce1911343..8237bcd5f 100644 --- a/tests/contest/contest/src/tests/process/mod.rs +++ b/tests/contest/contest/src/tests/process/mod.rs @@ -1,2 +1,2 @@ mod process_test; -pub use process_test::get_process_test; \ No newline at end of file +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 index 75930319a..2e91b363e 100644 --- a/tests/contest/contest/src/tests/process/process_test.rs +++ b/tests/contest/contest/src/tests/process/process_test.rs @@ -1,8 +1,7 @@ use crate::utils::test_inside_container; -use anyhow::{Context, Ok, Result}; -use oci_spec::runtime::{ - ProcessBuilder, Spec, SpecBuilder, -}; +use anyhow::{bail, Context, Ok, Result}; +use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; +use std::fs; use test_framework::{test_result, Test, TestGroup, TestResult}; fn create_spec() -> Result { @@ -10,10 +9,7 @@ fn create_spec() -> Result { .process( ProcessBuilder::default() .cwd("/test") - .env(vec![ - "testa=valuea".into(), - "testb=123".into() - ]) + .env(vec!["testa=valuea".into(), "testb=123".into()]) .build() .expect("error in creating process config"), ) @@ -25,7 +21,15 @@ fn create_spec() -> Result { fn process_test() -> TestResult { let spec = test_result!(create_spec()); - test_inside_container(spec, &|_| Ok(())) + test_inside_container(spec, &|_| { + match fs::create_dir("/test") { + Result::Ok(_) => { /*This is expected*/ } + Err(e) => { + bail!(e) + } + } + Ok(()) + }) } pub fn get_process_test() -> TestGroup { diff --git a/tests/contest/runtimetest/src/tests.rs b/tests/contest/runtimetest/src/tests.rs index 8162773a0..52e57a5d0 100644 --- a/tests/contest/runtimetest/src/tests.rs +++ b/tests/contest/runtimetest/src/tests.rs @@ -608,4 +608,4 @@ pub fn validate_rootfs() { if entries != expected { eprintln!("error due to rootfs want {expected:?}, got {entries:?}"); } -} \ No newline at end of file +} From 94526b2b86230bad75932627e895c1d431735d3e Mon Sep 17 00:00:00 2001 From: sat0ken <15720506+sat0ken@users.noreply.github.com> Date: Fri, 1 Nov 2024 07:35:59 +0900 Subject: [PATCH 7/7] set permision to /test Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> --- tests/contest/contest/src/tests/process/process_test.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/contest/contest/src/tests/process/process_test.rs b/tests/contest/contest/src/tests/process/process_test.rs index 2e91b363e..d605e5c30 100644 --- a/tests/contest/contest/src/tests/process/process_test.rs +++ b/tests/contest/contest/src/tests/process/process_test.rs @@ -2,6 +2,7 @@ use crate::utils::test_inside_container; use anyhow::{bail, Context, Ok, Result}; use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; use std::fs; +use std::os::unix::fs::PermissionsExt; use test_framework::{test_result, Test, TestGroup, TestResult}; fn create_spec() -> Result { @@ -28,6 +29,10 @@ fn process_test() -> TestResult { bail!(e) } } + let metadata = fs::metadata("/test")?; + let mut permissions = metadata.permissions(); + permissions.set_mode(0o700); + Ok(()) }) }