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
87 changes: 86 additions & 1 deletion tests/contest/contest/src/tests/cgroups/cpu/v1.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use std::fs;
use std::path::Path;
use std::string::ToString;

use anyhow::{Context, Result};
YJDoc2 marked this conversation as resolved.
Show resolved Hide resolved
use libcgroups::common;
use num_cpus;
use test_framework::{test_result, ConditionalTest, TestGroup, TestResult};
use tracing::debug;
YJDoc2 marked this conversation as resolved.
Show resolved Hide resolved

use super::{create_cpu_spec, create_empty_spec, create_spec};
use crate::utils::test_outside_container;
use crate::utils::test_utils::check_container_created;
use crate::utils::test_utils::{check_container_created, CGROUP_ROOT};

const CPU_CGROUP_PREFIX: &str = "/sys/fs/cgroup/cpu,cpuacct";
const DEFAULT_REALTIME_PERIOD: u64 = 1000000;
Expand Down Expand Up @@ -219,6 +223,81 @@ fn test_cpu_cgroups() -> TestResult {
TestResult::Passed
}

fn check_cgroup_subsystem(
cgroup_name: &str,
subsystem: &str,
filename: &str,
expected: &dyn ToString,
) -> Result<()> {
let cgroup_path = Path::new(CGROUP_ROOT)
.join(subsystem)
.join("runtime-test")
.join(cgroup_name)
.join(filename);

debug!("reading value from {:?}", cgroup_path);
let content = fs::read_to_string(&cgroup_path)
.with_context(|| format!("failed to read {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()));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please correct me if I am wrong. After checking the definition of the create_spec function, seems like I already specify the cgroups_path by calling it.

fn create_spec(cgroup_name: &str, case: LinuxCpu) -> Result<Spec> {
    let spec = SpecBuilder::default()
        .linux(
            LinuxBuilder::default()
                .cgroups_path(Path::new("/runtime-test").join(cgroup_name))
                .resources(
                    LinuxResourcesBuilder::default()
                        .cpu(case)
                        .build()
                        .context("failed to build resource spec")?,
                )
                .build()
                .context("failed to build linux spec")?,
        )
        .build()
        .context("failed to build spec")?;

    Ok(spec)
}

If that's the case, I guess I have no need to modify create_cpu_spec interface. Am I right?

Copy link
Member

Choose a reason for hiding this comment

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

Is your intention to edit Spec after calling create_spec? I prefer passing a cgroup path to create_cpu_spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for reply. I totally miss the point. I plan to modify create_spec and add an optional argument, since the original code already calls the builder there. Is it better than modify create_cpu_spec?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it'd be better because create_cpu_spec should only be editing related CPU field,s not the cgroup path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've add a is_relative argument in create_spec. But I have no idea why the CI fail.


test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
test_result!(check_cgroup_subsystem(
"test_relative_cpus",
YJDoc2 marked this conversation as resolved.
Show resolved Hide resolved
"cpu,cpuacct",
"cpu.shares",
&test_result!(case.shares().context("no shares value in cpu spec")),
));
test_result!(check_cgroup_subsystem(
"test_relative_cpus",
"cpu,cpuacct",
"cpu.cfs_period_us",
&test_result!(case.period().context("no period value in cpu spec")),
));
test_result!(check_cgroup_subsystem(
"test_relative_cpus",
"cpu,cpuacct",
"cpu.cfs_quota_us",
&test_result!(case.quota().context("no period value in cpu spec")),
));
test_result!(check_cgroup_subsystem(
"test_relative_cpus",
"cpuset",
"cpuset.cpus",
&test_result!(case
.cpus()
.to_owned()
.context("no period value in cpu spec"))
));
test_result!(check_cgroup_subsystem(
"test_relative_cpus",
"cpuset",
"cpuset.mems",
&test_result!(case
.mems()
.to_owned()
.context("no period value in cpu spec")),
YJDoc2 marked this conversation as resolved.
Show resolved Hide resolved
));
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 @@ -317,12 +396,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
Loading