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

Integration test: cgroup v1 relative-cpus tests #2898

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
9 changes: 7 additions & 2 deletions tests/contest/contest/src/tests/cgroups/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ fn create_cpu_spec(
builder.build().context("failed to build cpu spec")
}

fn create_spec(cgroup_name: &str, case: LinuxCpu) -> Result<Spec> {
fn create_spec(cgroup_name: &str, case: LinuxCpu, is_relative: bool) -> Result<Spec> {
let cgroups_path = if is_relative {
Path::new("testdir/runtime-test").join(cgroup_name)
} else {
Path::new("/runtime-test").join(cgroup_name)
};
Comment on lines +45 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we pass the cgroup path itself?

let spec = SpecBuilder::default()
.linux(
LinuxBuilder::default()
.cgroups_path(Path::new("/runtime-test").join(cgroup_name))
.cgroups_path(cgroups_path)
.resources(
LinuxResourcesBuilder::default()
.cpu(case)
Expand Down
84 changes: 81 additions & 3 deletions tests/contest/contest/src/tests/cgroups/cpu/v1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::fs;
use std::path::Path;
use std::string::ToString;

use anyhow::Result;
use libcgroups::common;
use libcgroups::v1::{util, ControllerType};
use num_cpus;
use test_framework::{test_result, ConditionalTest, TestGroup, TestResult};

Expand Down Expand Up @@ -204,7 +208,7 @@ fn test_cpu_cgroups() -> TestResult {
];

for case in cases.into_iter() {
let spec = test_result!(create_spec(cgroup_name, case));
let spec = test_result!(create_spec(cgroup_name, case, false));
let test_result = test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));

Expand All @@ -219,6 +223,74 @@ fn test_cpu_cgroups() -> TestResult {
TestResult::Passed
}

fn check_cgroup_subsystem(
cgroup_name: &str,
subsystem: &ControllerType,
filename: &str,
expected: &dyn ToString,
) -> Result<()> {
let mount_point = util::get_subsystem_mount_point(subsystem)?;
let cgroup_path = mount_point
.join("testdir/runtime-test")
.join(cgroup_name)
.join(filename);

let content = fs::read_to_string(&cgroup_path)?;
let trimmed = content.trim();
assert_eq!(trimmed, expected.to_string());
Ok(())
}

fn test_relative_cpus() -> TestResult {
let case = test_result!(create_cpu_spec(
1024,
100000,
50000,
None,
"0-1",
"0",
get_realtime_period(),
get_realtime_runtime(),
));
let spec = test_result!(create_spec("test_relative_cpus", case.clone(), true));

test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
let cgroup_name = "test_relative_cpus";
test_result!(check_cgroup_subsystem(
cgroup_name,
&ControllerType::CpuAcct,
"cpu.shares",
&case.shares().unwrap(),
));
test_result!(check_cgroup_subsystem(
cgroup_name,
&ControllerType::CpuAcct,
"cpu.cfs_period_us",
&case.period().unwrap(),
));
test_result!(check_cgroup_subsystem(
cgroup_name,
&ControllerType::CpuAcct,
"cpu.cfs_quota_us",
&case.quota().unwrap(),
));
test_result!(check_cgroup_subsystem(
cgroup_name,
&ControllerType::CpuSet,
"cpuset.cpus",
&case.cpus().to_owned().unwrap(),
));
test_result!(check_cgroup_subsystem(
cgroup_name,
&ControllerType::CpuSet,
"cpuset.mems",
&case.mems().to_owned().unwrap()
));
TestResult::Passed
})
}

fn test_empty_cpu() -> TestResult {
let cgroup_name = "test_empty_cpu";
let spec = test_result!(create_empty_spec(cgroup_name));
Expand Down Expand Up @@ -248,7 +320,7 @@ fn test_cpu_idle_set() -> TestResult {
realtime_runtime,
));

let spec = test_result!(create_spec(cgroup_name, cpu));
let spec = test_result!(create_spec(cgroup_name, cpu, false));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
TestResult::Passed
Expand All @@ -272,7 +344,7 @@ fn test_cpu_idle_default() -> TestResult {
realtime_period,
realtime_runtime,
));
let spec = test_result!(create_spec(cgroup_name, cpu));
let spec = test_result!(create_spec(cgroup_name, cpu, false));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
TestResult::Passed
Expand Down Expand Up @@ -317,12 +389,18 @@ pub fn get_test_group() -> TestGroup {
Box::new(can_run_idle),
Box::new(test_cpu_idle_set),
);
let relative_cpus = ConditionalTest::new(
"test_relative_cpus",
Box::new(can_run),
Box::new(test_relative_cpus),
);

test_group.add(vec![
Box::new(linux_cgroups_cpus),
Box::new(empty_cpu),
Box::new(cpu_idle_set),
Box::new(cpu_idle_default),
Box::new(relative_cpus),
]);

test_group
Expand Down
35 changes: 24 additions & 11 deletions tests/contest/contest/src/tests/cgroups/cpu/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn test_cpu_idle_set() -> TestResult {
.build()
.context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_idle_set", cpu));
let spec = test_result!(create_spec("test_cpu_idle_set", cpu, false));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
test_result!(check_cpu_idle("test_cpu_idle_set", idle));
Expand All @@ -47,7 +47,7 @@ fn test_cpu_idle_default() -> TestResult {
let default_idle = 0;
let cpu = test_result!(LinuxCpuBuilder::default().build().context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_idle_default", cpu));
let spec = test_result!(create_spec("test_cpu_idle_default", cpu, false));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
test_result!(check_cpu_idle("test_cpu_idle_default", default_idle));
Expand All @@ -64,7 +64,7 @@ fn test_cpu_weight_valid_set() -> TestResult {
.build()
.context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_weight_valid_set", cpu));
let spec = test_result!(create_spec("test_cpu_weight_valid_set", cpu, false));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
test_result!(check_cpu_weight(
Expand All @@ -84,7 +84,7 @@ fn test_cpu_weight_zero_ignored() -> TestResult {
.build()
.context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_weight_zero_ignored", cpu));
let spec = test_result!(create_spec("test_cpu_weight_zero_ignored", cpu, false));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
test_result!(check_cpu_weight(
Expand All @@ -104,7 +104,11 @@ fn test_cpu_weight_too_high_maximum_set() -> TestResult {
.build()
.context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_weight_too_high_maximum_set", cpu));
let spec = test_result!(create_spec(
"test_cpu_weight_too_high_maximum_set",
cpu,
false
));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
test_result!(check_cpu_weight(
Expand All @@ -123,7 +127,7 @@ fn test_cpu_quota_valid_set() -> TestResult {
.build()
.context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_quota_valid_set", cpu));
let spec = test_result!(create_spec("test_cpu_quota_valid_set", cpu, false));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
test_result!(check_cpu_max(
Expand All @@ -143,7 +147,7 @@ fn test_cpu_quota_zero_default_set() -> TestResult {
.build()
.context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_quota_zero_default_set", cpu));
let spec = test_result!(create_spec("test_cpu_quota_zero_default_set", cpu, false));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
test_result!(check_cpu_max(
Expand All @@ -165,7 +169,8 @@ fn test_cpu_quota_negative_default_set() -> TestResult {

let spec = test_result!(create_spec(
"test_cpu_quota_negative_value_default_set",
cpu
cpu,
false
));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
Expand All @@ -188,7 +193,7 @@ fn test_cpu_period_valid_set() -> TestResult {
.build()
.context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_period_valid_set", cpu));
let spec = test_result!(create_spec("test_cpu_period_valid_set", cpu, false));
test_result!(prepare_cpu_max(
&spec,
&quota.to_string(),
Expand All @@ -213,7 +218,11 @@ fn test_cpu_quota_period_unspecified_unchanged() -> TestResult {
let expected_period = 250_000;
let cpu = test_result!(LinuxCpuBuilder::default().build().context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_period_unspecified_unchanged", cpu));
let spec = test_result!(create_spec(
"test_cpu_period_unspecified_unchanged",
cpu,
false
));
test_result!(prepare_cpu_max(
&spec,
&quota.to_string(),
Expand All @@ -240,7 +249,11 @@ fn test_cpu_period_and_quota_valid_set() -> TestResult {
.build()
.context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_period_and_quota_valid_set", cpu));
let spec = test_result!(create_spec(
"test_cpu_period_and_quota_valid_set",
cpu,
false
));

test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
Expand Down
Loading