Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: remove the shared state from the tests #34

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ memmap2 = "0.5.5"
memfd = "0.6"
bincode = "1.3"
thiserror = "1.0"
bytesize = {version = "1.1.0", features = ["serde"]}
bytesize = {version = "1.1.0", features = ["serde"]}

[dev-dependencies]
rand = "0.8.5"
43 changes: 29 additions & 14 deletions core/src/cgroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,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();
Expand All @@ -274,7 +275,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();

Expand All @@ -286,15 +287,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();
Expand All @@ -308,8 +312,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();
Expand All @@ -323,8 +327,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());
Expand Down Expand Up @@ -352,7 +356,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);
Expand All @@ -370,7 +374,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());
Expand All @@ -397,7 +401,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();
Expand Down Expand Up @@ -432,4 +436,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;
}
}
}
}