Skip to content

Commit

Permalink
add test for Cgroup/cpu.idle
Browse files Browse the repository at this point in the history
Signed-off-by: wineway <wangyuweihx@gmail.com>
  • Loading branch information
wineway committed May 10, 2022
1 parent 0657971 commit 8cbd30e
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 90 deletions.
95 changes: 8 additions & 87 deletions Cargo.lock

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

27 changes: 27 additions & 0 deletions crates/libcgroups/src/v1/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,33 @@ mod tests {
assert_eq!(content, RUNTIME.to_string());
}

#[test]
fn test_set_cpu_idle() {
// arrange
const IDLE: i64 = 1;
const CPU: &str = "cpu";

if !Path::new(common::DEFAULT_CGROUP_ROOT)
.join(CPU)
.join(CGROUP_CPU_IDLE)
.exists()
{
// skip test_set_cpu_idle due to not found cpu.idle, maybe due to old kernel version
return;
}

let (tmp, max) = setup("test_set_cpu_idle", CGROUP_CPU_IDLE);
let cpu = LinuxCpuBuilder::default().idle(IDLE).build().unwrap();

// act
Cpu::apply(&tmp, &cpu).expect("apply cpu");

// assert
let content = fs::read_to_string(max)
.unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_IDLE));
assert_eq!(content, IDLE.to_string());
}

#[test]
fn test_set_rt_period() {
// arrange
Expand Down
27 changes: 27 additions & 0 deletions crates/libcgroups/src/v2/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,33 @@ mod tests {
assert_eq!(content, 840.to_string());
}

#[test]
fn test_set_cpu_idle() {
// arrange
const IDLE: i64 = 1;
const CPU: &str = "cpu";

if !Path::new(common::DEFAULT_CGROUP_ROOT)
.join(CPU)
.join(CGROUP_CPU_IDLE)
.exists()
{
// skip test_set_cpu_idle due to not found cpu.idle, maybe due to old kernel version
return;
}

let (tmp, max) = setup("test_set_cpu_idle", CGROUP_CPU_IDLE);
let cpu = LinuxCpuBuilder::default().idle(IDLE).build().unwrap();

// act
Cpu::apply(&tmp, &cpu).expect("apply cpu");

// assert
let content = fs::read_to_string(max)
.unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_IDLE));
assert_eq!(content, format!("{}", IDLE))
}

#[test]
fn test_set_positive_quota() {
// arrange
Expand Down
2 changes: 1 addition & 1 deletion tests/rust-integration-tests/integration_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ libcontainer = { path = "../../../crates/libcontainer" }
log = { version = "0.4", features = ["std"] }
nix = "0.24.1"
num_cpus = "1.13"
oci-spec = "0.5.5"
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e" }
once_cell = "1.10.0"
pnet = "0.29.0"
procfs = "0.12.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn create_cpu_spec(
shares: u64,
quota: i64,
period: u64,
idle_opt: Option<i64>,
cpus: &str,
mems: &str,
realtime_period_opt: Option<u64>,
Expand All @@ -24,6 +25,10 @@ fn create_cpu_spec(
.cpus(cpus)
.mems(mems);

if let Some(idle) = idle_opt {
builder = builder.idle(idle);
}

if let Some(realtime_period) = realtime_period_opt {
builder = builder.realtime_period(realtime_period);
}
Expand Down
Loading

0 comments on commit 8cbd30e

Please sign in to comment.