Skip to content

Commit

Permalink
feat add rdev/rnodev recursive mount test
Browse files Browse the repository at this point in the history
Signed-off-by: lengrongfu <1275177125@qq.com>
  • Loading branch information
lengrongfu committed Mar 5, 2023
1 parent 74a499f commit da64622
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
17 changes: 17 additions & 0 deletions tests/rust-integration-tests/runtimetest/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
_ => {}
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/rust-integration-tests/runtimetest/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit da64622

Please sign in to comment.