Skip to content

Commit 01e7324

Browse files
committed
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 <[email protected]>
1 parent 910f0c5 commit 01e7324

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/jailer/src/env.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,27 +1138,38 @@ 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_name = format!(
1142+
"/tmp/firecracker/test/{}/jailer/dev",
1143+
rand::rand_alphanumerics(4).into_string().unwrap()
1144+
);
1145+
let mock_dev_dir = Path::new(mock_dev_dir_name.as_str());
1146+
11411147
// 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),
1148+
let mut dev_infos: Vec<(PathBuf, u32, u32)> = vec![
1149+
(
1150+
mock_dev_dir.join("net/tun-test"),
1151+
DEV_NET_TUN_MAJOR,
1152+
DEV_NET_TUN_MINOR,
1153+
),
1154+
(mock_dev_dir.join("kvm-test"), DEV_KVM_MAJOR, DEV_KVM_MINOR),
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.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
}
1171+
// Cleanup the tmp directory
1172+
fs::remove_dir_all(&mock_dev_dir).unwrap();
11621173
}
11631174

11641175
#[test]

0 commit comments

Comments
 (0)