Skip to content

Commit

Permalink
add ratime mount_setattr test
Browse files Browse the repository at this point in the history
Signed-off-by: lengrongfu <[email protected]>
  • Loading branch information
lengrongfu committed Mar 15, 2024
1 parent 0b89e1d commit cc7c436
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/libcontainer/src/syscall/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl FromStr for MountAttrOption {
MOUNT_ATTR_RELATIME,
)),
"rnoatime" => Ok(MountAttrOption::MountAttrNoatime(false, MOUNT_ATTR_NOATIME)),
"ratime" => Ok(MountAttrOption::MountAttrNoatime(true, MOUNT_ATTR_NOATIME)),
"ratime" => Ok(MountAttrOption::MountAttrNoatime(true, MOUNT_ATTR__ATIME)),
"rstrictatime" => Ok(MountAttrOption::MountAttrStrictAtime(
false,
MOUNT_ATTR_STRICTATIME,
Expand Down
29 changes: 27 additions & 2 deletions tests/contest/contest/src/tests/mounts_recursive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ fn check_recursive_nosuid() -> TestResult {
Some(Uid::from_raw(1200)),

Check warning on line 182 in tests/contest/contest/src/tests/mounts_recursive/mod.rs

View workflow job for this annotation

GitHub Actions / check (aarch64, gnu)

Diff in /home/runner/work/youki/youki/tests/contest/contest/src/tests/mounts_recursive/mod.rs

Check warning on line 182 in tests/contest/contest/src/tests/mounts_recursive/mod.rs

View workflow job for this annotation

GitHub Actions / check (aarch64, gnu)

Diff in /home/runner/work/youki/youki/tests/contest/contest/src/tests/mounts_recursive/mod.rs
None,
)
.unwrap();
.unwrap();
chown(
&in_container_executable_subdir_file_path,
Some(Uid::from_raw(1200)),
None,

Check warning on line 189 in tests/contest/contest/src/tests/mounts_recursive/mod.rs

View workflow job for this annotation

GitHub Actions / check (aarch64, gnu)

Diff in /home/runner/work/youki/youki/tests/contest/contest/src/tests/mounts_recursive/mod.rs

Check warning on line 189 in tests/contest/contest/src/tests/mounts_recursive/mod.rs

View workflow job for this annotation

GitHub Actions / check (aarch64, gnu)

Diff in /home/runner/work/youki/youki/tests/contest/contest/src/tests/mounts_recursive/mod.rs
)
.unwrap();
.unwrap();
in_container_executable_file_perm
.set_mode(in_container_executable_file_perm.mode() | Mode::S_ISUID.bits());
in_container_executable_subdir_file_perm
Expand Down Expand Up @@ -503,6 +503,29 @@ fn check_recursive_rnoatime() -> TestResult {
result
}

fn check_recursive_ratime() -> TestResult {
let ratime_dir_path = PathBuf::from_str("/tmp/ratime_dir").unwrap();
let mount_dest_path = PathBuf::from_str("/ratime").unwrap();
fs::create_dir(ratime_dir_path.clone()).unwrap();

let mount_options = vec!["rbind".to_string(), "ratime".to_string()];
let mut mount_spec = Mount::default();
mount_spec
.set_destination(mount_dest_path)
.set_typ(None)
.set_source(Some(ratime_dir_path.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(()));

fs::remove_dir_all(ratime_dir_path).unwrap();
result
}

fn check_recursive_rstrictatime() -> TestResult {
let rstrictatime_base_dir = PathBuf::from_str("/tmp").unwrap();
let rstrictatime_dir_path = rstrictatime_base_dir.join("rstrictatime_dir");
Expand Down Expand Up @@ -622,6 +645,7 @@ pub fn get_mounts_recursive_test() -> TestGroup {
let rrelatime_test = Test::new("rrelatime_test", Box::new(check_recursive_rrelatime));
let rnorelatime_test = Test::new("rnorelatime_test", Box::new(check_recursive_rnorelatime));
let rnoatime_test = Test::new("rnoatime_test", Box::new(check_recursive_rnoatime));
let ratime_test = Test::new("ratime_test", Box::new(check_recursive_ratime));
let rstrictatime_test = Test::new("rstrictatime_test", Box::new(check_recursive_rstrictatime));
let rnosymfollow_test = Test::new("rnosymfollow_test", Box::new(check_recursive_rnosymfollow));
let rsymfollow_test = Test::new("rsymfollow_test", Box::new(check_recursive_rsymfollow));
Expand All @@ -641,6 +665,7 @@ pub fn get_mounts_recursive_test() -> TestGroup {
Box::new(rrelatime_test),
Box::new(rnorelatime_test),
Box::new(rnoatime_test),
Box::new(ratime_test),
Box::new(rstrictatime_test),
Box::new(rnosymfollow_test),
Box::new(rsymfollow_test),
Expand Down
10 changes: 10 additions & 0 deletions tests/contest/runtimetest/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ pub fn validate_mounts_recursive(spec: &Spec) {
);
}
}
"ratime" => {
println!("ratime: {mount:?}");
if let Err(e) = utils::test_mount_ratime_option(
mount.destination().to_str().unwrap(),
) {
eprintln!(
"path expected to be ratime, found not ratime, error: {e}"
);
}
}
"rstrictatime" => {
println!("rstrictatime: {mount:?}");
if let Err(e) = utils::test_mount_rstrictatime_option(
Expand Down
40 changes: 40 additions & 0 deletions tests/contest/runtimetest/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,46 @@ pub fn test_mount_rnoatime_option(path: &str) -> Result<(), std::io::Error> {
Ok(())
}

pub fn test_mount_ratime_option(path: &str) -> Result<(), std::io::Error> {
let test_file_path = PathBuf::from(path).join("ratime.txt");
Command::new("touch")
.arg(test_file_path.to_str().unwrap())
.output()?;
let one_metadata = fs::metadata(test_file_path.clone())?;
println!(
"{:?} file one atime is {:?},mtime is {:?}, current time is {:?}",
test_file_path,
one_metadata.atime(),
one_metadata.mtime(),
std::time::SystemTime::now()
);
std::thread::sleep(std::time::Duration::from_millis(1000));

// execute touch command to update access time
Command::new("touch")
.arg(test_file_path.to_str().unwrap())
.output()
.expect("execute touch command error");
let two_metadata = fs::metadata(test_file_path.clone())?;
println!(
"{:?} file two atime is {:?},mtime is {:?},current time is {:?}",
test_file_path,
two_metadata.atime(),
two_metadata.mtime(),
std::time::SystemTime::now()
);
if one_metadata.atime() == two_metadata.atime() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"update access time for file {:?}, expected update",
test_file_path.to_str()
),
));
}
Ok(())
}

// Always update the last access time (atime) when files are accessed on this mount.
pub fn test_mount_rstrictatime_option(path: &str) -> Result<(), std::io::Error> {
let test_file_path = PathBuf::from(path).join("rstrictatime.txt");
Expand Down

0 comments on commit cc7c436

Please sign in to comment.