Skip to content

Commit 245057f

Browse files
Manciukicroypat
authored andcommitted
test(jailer): use tmp dir for mknod test
We seldom have failures in the CI where the test_mknod_and_own_dev fails because a file already exists. The test is using the actual /dev to create tmp devices. As there's no reason to use the actual /dev, move it to use a random folder and clean it up after the test. Signed-off-by: Riccardo Mancini <mancio@amazon.com>
1 parent 010f119 commit 245057f

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/jailer/src/env.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,26 +1138,35 @@ mod tests {
11381138
mock_cgroups.add_v1_mounts().unwrap();
11391139
let env = create_env(mock_cgroups.proc_mounts_path.as_str());
11401140

1141+
let mock_dev_dir = TempDir::new().unwrap();
1142+
11411143
// Ensure device nodes are created with correct major/minor numbers and permissions.
1142-
let mut dev_infos: Vec<(&CStr, u32, u32)> = vec![
1143-
(c"/dev/net/tun-test", DEV_NET_TUN_MAJOR, DEV_NET_TUN_MINOR),
1144-
(c"/dev/kvm-test", DEV_KVM_MAJOR, DEV_KVM_MINOR),
1144+
let mut dev_infos: Vec<(PathBuf, u32, u32)> = vec![
1145+
(
1146+
mock_dev_dir.as_path().join("net/tun-test"),
1147+
DEV_NET_TUN_MAJOR,
1148+
DEV_NET_TUN_MINOR,
1149+
),
1150+
(
1151+
mock_dev_dir.as_path().join("kvm-test"),
1152+
DEV_KVM_MAJOR,
1153+
DEV_KVM_MINOR,
1154+
),
11451155
];
11461156

11471157
if let Some(uffd_dev_minor) = env.uffd_dev_minor {
1148-
dev_infos.push((c"/dev/userfaultfd-test", DEV_UFFD_MAJOR, uffd_dev_minor));
1158+
dev_infos.push((
1159+
mock_dev_dir.as_path().join("userfaultfd-test"),
1160+
DEV_UFFD_MAJOR,
1161+
uffd_dev_minor,
1162+
));
11491163
}
11501164

11511165
for (dev, major, minor) in dev_infos {
1152-
// Checking this just to be super sure there's no file at `dev_str` path (though
1153-
// it shouldn't be as we deleted it at the end of the previous test run).
1154-
if Path::new(dev.to_str().unwrap()).exists() {
1155-
fs::remove_file(dev.to_str().unwrap()).unwrap();
1156-
}
1157-
1158-
ensure_mknod_and_own_dev(&env, dev, major, minor);
1159-
// Remove the device node.
1160-
fs::remove_file(dev.to_str().unwrap()).expect("Could not remove file.");
1166+
// Ensure the folder where we are creating the node exists
1167+
fs::create_dir_all(dev.parent().unwrap()).unwrap();
1168+
let dev_path = dev.to_str().map(CString::new).unwrap().unwrap();
1169+
ensure_mknod_and_own_dev(&env, &dev_path, major, minor);
11611170
}
11621171
}
11631172

0 commit comments

Comments
 (0)