diff --git a/Cargo.lock b/Cargo.lock index e966582..0a398d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -342,6 +342,17 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "wasi", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -524,6 +535,7 @@ dependencies = [ "nix", "polling", "procfs", + "rand", "serde", "thiserror", "walkdir", @@ -747,6 +759,12 @@ dependencies = [ "serde", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "pretty_env_logger" version = "0.4.0" @@ -835,6 +853,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "redox_syscall" version = "0.2.16" @@ -1105,6 +1153,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.83" diff --git a/core/Cargo.toml b/core/Cargo.toml index 2b8429d..08c121c 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -21,4 +21,7 @@ memmap2 = "0.5.5" memfd = "0.6" bincode = "1.3" thiserror = "1.0" -bytesize = {version = "1.1.0", features = ["serde"]} \ No newline at end of file +bytesize = {version = "1.1.0", features = ["serde"]} + +[dev-dependencies] +rand = "0.8.5" \ No newline at end of file diff --git a/core/src/cgroup.rs b/core/src/cgroup.rs index 24cc80e..28d8ef9 100644 --- a/core/src/cgroup.rs +++ b/core/src/cgroup.rs @@ -260,10 +260,11 @@ mod tests { #[test] fn new_root() { - let path = get_path().join("cgroup_test"); + let name = gen_name(); + let path = get_path().join(&name); assert!(!path.exists()); // Ensure, that it does not already exist - let cg = CGroup::new_root(get_path(), "cgroup_test").unwrap(); + let cg = CGroup::new_root(get_path(), &name).unwrap(); assert!(path.exists() && path.is_dir()); cg.rm().unwrap(); @@ -272,7 +273,7 @@ mod tests { #[test] fn import_root() { - let path = get_path().join("cgroup_test"); + let path = get_path().join(gen_name()); assert!(!path.exists()); // Ensure, that it does not already exist fs::create_dir(&path).unwrap(); @@ -284,15 +285,18 @@ mod tests { #[test] fn new() { - let path_cg1 = get_path().join("cgroup_test"); - let path_cg2 = path_cg1.join("cgroup_test2"); + let name1 = gen_name(); + let name2 = gen_name(); + + let path_cg1 = get_path().join(&name1); + let path_cg2 = path_cg1.join(&name2); assert!(!path_cg1.exists()); // Ensure, that it does not already exist - let cg1 = CGroup::new_root(get_path(), "cgroup_test").unwrap(); + let cg1 = CGroup::new_root(get_path(), &name1).unwrap(); assert!(path_cg1.exists() && path_cg1.is_dir()); assert!(!path_cg2.exists()); - let _cg2 = cg1.new("cgroup_test2").unwrap(); + let _cg2 = cg1.new(&name2).unwrap(); assert!(path_cg2.exists() && path_cg2.is_dir()); cg1.rm().unwrap(); @@ -306,8 +310,8 @@ mod tests { let mut proc = spawn_proc().unwrap(); let pid = Pid::from_raw(proc.id() as i32); - let cg1 = CGroup::new_root(get_path(), "cgroup_test").unwrap(); - let cg2 = cg1.new("cgroup_test2").unwrap(); + let cg1 = CGroup::new_root(get_path(), &gen_name()).unwrap(); + let cg2 = cg1.new(&gen_name()).unwrap(); cg1.mv(pid).unwrap(); cg2.mv(pid).unwrap(); @@ -321,8 +325,8 @@ mod tests { let mut proc = spawn_proc().unwrap(); let pid = Pid::from_raw(proc.id() as i32); - let cg1 = CGroup::new_root(get_path(), "cgroup_test").unwrap(); - let cg2 = cg1.new("cgroup_test2").unwrap(); + let cg1 = CGroup::new_root(get_path(), &gen_name()).unwrap(); + let cg2 = cg1.new(&gen_name()).unwrap(); assert!(cg1.get_pids().unwrap().is_empty()); assert!(cg2.get_pids().unwrap().is_empty()); @@ -350,7 +354,7 @@ mod tests { fn populated() { let mut proc = spawn_proc().unwrap(); let pid = Pid::from_raw(proc.id() as i32); - let cg = CGroup::new_root(get_path(), "cgroup_test").unwrap(); + let cg = CGroup::new_root(get_path(), &gen_name()).unwrap(); assert!(!cg.populated().unwrap()); assert_eq!(cg.populated().unwrap(), cg.get_pids().unwrap().len() > 0); @@ -368,7 +372,7 @@ mod tests { fn frozen() { let mut proc = spawn_proc().unwrap(); let pid = Pid::from_raw(proc.id() as i32); - let cg = CGroup::new_root(get_path(), "cgroup_test").unwrap(); + let cg = CGroup::new_root(get_path(), &gen_name()).unwrap(); // Freeze an empty cgroup assert!(!cg.frozen().unwrap()); @@ -395,7 +399,7 @@ mod tests { fn kill() { let proc = spawn_proc().unwrap(); let pid = Pid::from_raw(proc.id() as i32); - let cg = CGroup::new_root(get_path(), "cgroup_test").unwrap(); + let cg = CGroup::new_root(get_path(), &gen_name()).unwrap(); // Kill an empty cgroup cg.kill().unwrap(); @@ -430,4 +434,15 @@ mod tests { .unwrap() .join(super::current_cgroup().unwrap()) } + + /// Generates a name for the current cgroup + fn gen_name() -> String { + loop { + let val: u64 = rand::random(); + let str = format!("apex-test-{val}"); + if !Path::new(&str).exists() { + return str; + } + } + } }