Skip to content

Commit afab3c8

Browse files
zzzzzzzzzy9mxpv
authored andcommitted
add some test for mount_rootfs and umount_recursive and setup_loop
Signed-off-by: zzzzzzzzzy9 <[email protected]>
1 parent 11e9780 commit afab3c8

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

crates/shim/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ prctl.workspace = true
5454
signal-hook = "0.3.13"
5555
serde.workspace = true
5656
serde_json.workspace = true
57+
tempfile.workspace = true
5758
thiserror.workspace = true
5859
time.workspace = true
5960

crates/shim/src/mount_linux.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,4 +1090,66 @@ mod tests {
10901090
assert_eq!(options, expected_options);
10911091
}
10921092
}
1093+
1094+
#[cfg(feature = "async")]
1095+
#[test]
1096+
fn test_mount_rootfs_umount_recursive() {
1097+
let target = tempfile::tempdir().expect("create target dir error");
1098+
let lower1 = tempfile::tempdir().expect("create lower1 dir error");
1099+
let lower2 = tempfile::tempdir().expect("create lower2 dir error");
1100+
let upperdir = tempfile::tempdir().expect("create upperdir dir error");
1101+
let workdir = tempfile::tempdir().expect("create workdir dir error");
1102+
let options = vec![
1103+
"lowerdir=".to_string()
1104+
+ lower1.path().to_str().expect("lower1 path to str error")
1105+
+ ":"
1106+
+ lower2.path().to_str().expect("lower2 path to str error"),
1107+
"upperdir=".to_string()
1108+
+ upperdir
1109+
.path()
1110+
.to_str()
1111+
.expect("upperdir path to str error"),
1112+
"workdir=".to_string() + workdir.path().to_str().expect("workdir path to str error"),
1113+
];
1114+
// mount target.
1115+
let result = mount_rootfs(Some("overlay"), Some("overlay"), &options, &target);
1116+
assert!(result.is_ok());
1117+
let mut mountinfo = get_mounts(Some(prefix_filter(
1118+
target
1119+
.path()
1120+
.to_str()
1121+
.expect("target path to str error")
1122+
.to_string(),
1123+
)))
1124+
.expect("get_mounts error");
1125+
// make sure the target has been mounted.
1126+
assert_ne!(0, mountinfo.len());
1127+
// umount target.
1128+
let result = umount_recursive(target.path().to_str(), 0);
1129+
assert!(result.is_ok());
1130+
mountinfo = get_mounts(Some(prefix_filter(
1131+
target
1132+
.path()
1133+
.to_str()
1134+
.expect("target path to str error")
1135+
.to_string(),
1136+
)))
1137+
.expect("get_mounts error");
1138+
// make sure the target has been unmounted.
1139+
assert_eq!(0, mountinfo.len());
1140+
}
1141+
1142+
#[cfg(feature = "async")]
1143+
#[test]
1144+
fn test_setup_loop_dev() {
1145+
let path = tempfile::NamedTempFile::new().expect("cannot create tempfile");
1146+
let backing_file = path.path().to_str();
1147+
let params = LoopParams {
1148+
readonly: false,
1149+
auto_clear: true,
1150+
direct: false,
1151+
};
1152+
let result = setup_loop(backing_file, params);
1153+
assert!(result.is_ok());
1154+
}
10931155
}

0 commit comments

Comments
 (0)