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 492f446a7a..50b2a5978b 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 @@ -243,16 +243,60 @@ fn check_recursive_noexec() -> 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 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![ Box::new(rro_test), Box::new(rnosuid_test), Box::new(rnoexec_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 ca8fd89ec0..b888c1d704 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 c1ca542458..a601fb9b5c 100644 --- a/tests/rust-integration-tests/runtimetest/src/tests.rs +++ b/tests/rust-integration-tests/runtimetest/src/tests.rs @@ -131,6 +131,23 @@ pub fn validate_mounts_recursive(spec: &Spec) { eprintln!("error in testing rnoexec 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 e9ba305c17..e007a22045 100644 --- a/tests/rust-integration-tests/runtimetest/src/utils.rs +++ b/tests/rust-integration-tests/runtimetest/src/utils.rs @@ -85,3 +85,21 @@ pub fn test_file_executable(path: &str) -> Result<(), std::io::Error> { format!("{path:?} is directory, so cannot execute"), )) } + +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(()) +}