From 6dcf9c1e7fb4c6bb46de0e0ed984aa2f4b116602 Mon Sep 17 00:00:00 2001 From: lengrongfu <1275177125@qq.com> Date: Sun, 5 Mar 2023 20:16:24 +0800 Subject: [PATCH] feat add rdev/rnodev recursive mount test Signed-off-by: lengrongfu <1275177125@qq.com> --- .../src/tests/mounts_recursive/mod.rs | 44 +++++++++++++++++++ .../integration_test/src/utils/test_utils.rs | 7 +++ .../runtimetest/src/tests.rs | 17 +++++++ .../runtimetest/src/utils.rs | 18 ++++++++ 4 files changed, 86 insertions(+) diff --git a/tests/rust-integration-tests/integration_test/src/tests/mounts_recursive/mod.rs b/tests/rust-integration-tests/integration_test/src/tests/mounts_recursive/mod.rs index 3f1725499..279c84c0f 100644 --- a/tests/rust-integration-tests/integration_test/src/tests/mounts_recursive/mod.rs +++ b/tests/rust-integration-tests/integration_test/src/tests/mounts_recursive/mod.rs @@ -290,12 +290,54 @@ fn check_recursive_rnodiratime() -> TestResult { result } +fn check_recursive_rdev() -> TestResult { + let rdev_base_dir = PathBuf::from_str("/dev").unwrap(); + let mount_dest_path = PathBuf::from_str("/rdev").unwrap(); + + let mount_options = vec!["rbind".to_string(), "rdev".to_string()]; + let mut mount_spec = Mount::default(); + mount_spec + .set_destination(mount_dest_path) + .set_typ(None) + .set_source(Some(rdev_base_dir.clone())) + .set_options(Some(mount_options)); + let spec = get_spec( + vec![mount_spec], + vec!["runtimetest".to_string(), "mounts_recursive".to_string()], + ); + + let result = test_inside_container(spec, &|_| Ok(())); + result +} + +fn check_recursive_rnodev() -> TestResult { + let rnodev_base_dir = PathBuf::from_str("/dev").unwrap(); + let mount_dest_path = PathBuf::from_str("/rnodev").unwrap(); + + let mount_options = vec!["rbind".to_string(), "rnodev".to_string()]; + let mut mount_spec = Mount::default(); + mount_spec + .set_destination(mount_dest_path) + .set_typ(None) + .set_source(Some(rnodev_base_dir.clone())) + .set_options(Some(mount_options)); + let spec = get_spec( + vec![mount_spec], + vec!["runtimetest".to_string(), "mounts_recursive".to_string()], + ); + + let result = test_inside_container(spec, &|_| Ok(())); + result +} + pub fn get_mounts_recursive_test() -> TestGroup { let rro_test = Test::new("rro_test", Box::new(check_recursive_readonly)); let rnosuid_test = Test::new("rnosuid_test", Box::new(check_recursive_nosuid)); let rnoexec_test = Test::new("rnoexec_test", Box::new(check_recursive_noexec)); let rnodiratime_test = Test::new("rnodiratime_test", Box::new(check_recursive_rnodiratime)); let rdiratime_test = Test::new("rdiratime_test", Box::new(check_recursive_rdiratime)); + let rdev_test = Test::new("rdev_test", Box::new(check_recursive_rdev)); + let rnodev_test = Test::new("rnodev_test", Box::new(check_recursive_rnodev)); let mut tg = TestGroup::new("mounts_recursive"); tg.add(vec![ @@ -304,6 +346,8 @@ pub fn get_mounts_recursive_test() -> TestGroup { Box::new(rnoexec_test), Box::new(rdiratime_test), Box::new(rnodiratime_test), + Box::new(rdev_test), + Box::new(rnodev_test), ]); tg diff --git a/tests/rust-integration-tests/integration_test/src/utils/test_utils.rs b/tests/rust-integration-tests/integration_test/src/utils/test_utils.rs index ca8fd89ec..b888c1d70 100644 --- a/tests/rust-integration-tests/integration_test/src/utils/test_utils.rs +++ b/tests/rust-integration-tests/integration_test/src/utils/test_utils.rs @@ -199,6 +199,13 @@ pub fn test_inside_container( .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!( diff --git a/tests/rust-integration-tests/runtimetest/src/tests.rs b/tests/rust-integration-tests/runtimetest/src/tests.rs index 3122e3136..4ca6eae43 100644 --- a/tests/rust-integration-tests/runtimetest/src/tests.rs +++ b/tests/rust-integration-tests/runtimetest/src/tests.rs @@ -149,6 +149,23 @@ pub fn validate_mounts_recursive(spec: &Spec) { eprintln!("error in testing rnodiratime recursive mounting: {e}"); } } + "rdev" => { + println!("test_device_access: {:?}", mount); + let rest = + utils::test_device_access(mount.destination().to_str().unwrap()); + if let Err(e) = rest { + eprintln!("error in testing rdev recursive mounting: {e}"); + } + } + "rnodev" => { + println!("test_device_unaccess: {:?}", mount); + let rest = + utils::test_device_unaccess(mount.destination().to_str().unwrap()); + if rest.is_ok() { + // beacuse /rnodev/null device not access,so rest is err + eprintln!("error in testing rnodev recursive mounting"); + } + } _ => {} } } diff --git a/tests/rust-integration-tests/runtimetest/src/utils.rs b/tests/rust-integration-tests/runtimetest/src/utils.rs index 9790cd285..bd78b4c6f 100644 --- a/tests/rust-integration-tests/runtimetest/src/utils.rs +++ b/tests/rust-integration-tests/runtimetest/src/utils.rs @@ -147,3 +147,21 @@ pub fn test_dir_not_update_access_time(path: &str) -> Result<(), std::io::Error> } Ok(()) } + +pub fn test_device_access(path: &str) -> Result<(), std::io::Error> { + println!("test_device_access path: {:?}", path); + let _ = std::fs::OpenOptions::new() + .create(true) + .write(true) + .open(PathBuf::from(path).join("null"))?; + Ok(()) +} + +pub fn test_device_unaccess(path: &str) -> Result<(), std::io::Error> { + println!("test_device_unaccess path: {:?}", path); + let _ = std::fs::OpenOptions::new() + .create(true) + .write(true) + .open(PathBuf::from(path).join("null"))?; + Ok(()) +}