From 2c7dd43e45319883727e96aed8ddaff4319b2824 Mon Sep 17 00:00:00 2001 From: posutsai Date: Wed, 28 Aug 2024 09:59:38 +0000 Subject: [PATCH 1/9] check value from cgroupv1 fs and CpuSpec Signed-off-by: posutsai --- .../contest/src/tests/cgroups/cpu/v1.rs | 113 +++++++++++++++++- 1 file changed, 111 insertions(+), 2 deletions(-) diff --git a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs index d27de2c30..fba3da6e1 100644 --- a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs +++ b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs @@ -1,12 +1,20 @@ +use std::cmp::PartialEq; +use std::error::Error; +use std::fmt::Debug; +use std::fs; +use std::marker::{Send, Sync}; use std::path::Path; +use std::str::FromStr; +use anyhow::{Context, Result}; use libcgroups::common; use num_cpus; -use test_framework::{test_result, ConditionalTest, TestGroup, TestResult}; +use test_framework::{assert_result_eq, test_result, ConditionalTest, TestGroup, TestResult}; +use tracing::debug; 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; @@ -219,6 +227,101 @@ fn test_cpu_cgroups() -> TestResult { TestResult::Passed } +fn check_cgroup_numeric_subsystem( + cgroup_name: &str, + subsystem: &str, + filename: &str, + expected: T, +) -> Result<()> +where + ::Err: Send + Sync + Error + 'static, +{ + 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 observe = content.trim().to_owned().parse::()?; + assert_result_eq!(observe, expected) +} +fn check_cgroup_string_subsystem( + cgroup_name: &str, + subsystem: &str, + filename: &str, + expected: &str, +) -> 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 observe = content.trim().to_owned(); + assert_result_eq!(observe, expected) +} + +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())); + + test_outside_container(spec, &|data| { + test_result!(check_container_created(&data)); + test_result!(check_cgroup_numeric_subsystem( + "test_relative_cpus", + "cpu,cpuacct", + "cpu.shares", + test_result!(case.shares().context("no shares value in cpu spec")), + )); + test_result!(check_cgroup_numeric_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_numeric_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_string_subsystem( + "test_relative_cpus", + "cpuset", + "cpuset.cpus", + &test_result!(case + .cpus() + .to_owned() + .context("no period value in cpu spec")) + )); + test_result!(check_cgroup_string_subsystem( + "test_relative_cpus", + "cpuset", + "cpuset.mems", + &test_result!(case + .mems() + .to_owned() + .context("no period value in cpu spec")), + )); + TestResult::Passed + }) +} + fn test_empty_cpu() -> TestResult { let cgroup_name = "test_empty_cpu"; let spec = test_result!(create_empty_spec(cgroup_name)); @@ -317,12 +420,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 From febcff384cc58e22db935e872d5be6240233d026 Mon Sep 17 00:00:00 2001 From: posutsai Date: Fri, 30 Aug 2024 10:19:01 +0000 Subject: [PATCH 2/9] merge check function of multiple input types Signed-off-by: posutsai --- .../contest/src/tests/cgroups/cpu/v1.rs | 59 +++++++------------ 1 file changed, 20 insertions(+), 39 deletions(-) diff --git a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs index fba3da6e1..c600b0513 100644 --- a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs +++ b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs @@ -1,15 +1,11 @@ -use std::cmp::PartialEq; -use std::error::Error; -use std::fmt::Debug; +use std::any::Any; use std::fs; -use std::marker::{Send, Sync}; use std::path::Path; -use std::str::FromStr; use anyhow::{Context, Result}; use libcgroups::common; use num_cpus; -use test_framework::{assert_result_eq, test_result, ConditionalTest, TestGroup, TestResult}; +use test_framework::{test_result, ConditionalTest, TestGroup, TestResult}; use tracing::debug; use super::{create_cpu_spec, create_empty_spec, create_spec}; @@ -227,32 +223,11 @@ fn test_cpu_cgroups() -> TestResult { TestResult::Passed } -fn check_cgroup_numeric_subsystem( +fn check_cgroup_subsystem( cgroup_name: &str, subsystem: &str, filename: &str, - expected: T, -) -> Result<()> -where - ::Err: Send + Sync + Error + 'static, -{ - 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 observe = content.trim().to_owned().parse::()?; - assert_result_eq!(observe, expected) -} -fn check_cgroup_string_subsystem( - cgroup_name: &str, - subsystem: &str, - filename: &str, - expected: &str, + expected: &dyn Any, ) -> Result<()> { let cgroup_path = Path::new(CGROUP_ROOT) .join(subsystem) @@ -263,8 +238,14 @@ fn check_cgroup_string_subsystem( debug!("reading value from {:?}", cgroup_path); let content = fs::read_to_string(&cgroup_path) .with_context(|| format!("failed to read {cgroup_path:?}"))?; - let observe = content.trim().to_owned(); - assert_result_eq!(observe, expected) + if let Some(v) = expected.downcast_ref::() { + assert_eq!(&content.parse::()?, v); + } else if let Some(v) = expected.downcast_ref::() { + assert_eq!(&content.parse::()?, v); + } else if let Some(v) = expected.downcast_ref::() { + assert_eq!(&content, v); + } + Ok(()) } fn test_relative_cpus() -> TestResult { @@ -282,25 +263,25 @@ fn test_relative_cpus() -> TestResult { test_outside_container(spec, &|data| { test_result!(check_container_created(&data)); - test_result!(check_cgroup_numeric_subsystem( + test_result!(check_cgroup_subsystem( "test_relative_cpus", "cpu,cpuacct", "cpu.shares", - test_result!(case.shares().context("no shares value in cpu spec")), + &test_result!(case.shares().context("no shares value in cpu spec")), )); - test_result!(check_cgroup_numeric_subsystem( + 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!(case.period().context("no period value in cpu spec")), )); - test_result!(check_cgroup_numeric_subsystem( + 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!(case.quota().context("no period value in cpu spec")), )); - test_result!(check_cgroup_string_subsystem( + test_result!(check_cgroup_subsystem( "test_relative_cpus", "cpuset", "cpuset.cpus", @@ -309,7 +290,7 @@ fn test_relative_cpus() -> TestResult { .to_owned() .context("no period value in cpu spec")) )); - test_result!(check_cgroup_string_subsystem( + test_result!(check_cgroup_subsystem( "test_relative_cpus", "cpuset", "cpuset.mems", From 7b35e937a2ec438febcf090779c0ff195446c82e Mon Sep 17 00:00:00 2001 From: posutsai Date: Sat, 31 Aug 2024 07:19:18 +0000 Subject: [PATCH 3/9] fix parsing by trimming new line Signed-off-by: posutsai --- tests/contest/contest/src/tests/cgroups/cpu/v1.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs index c600b0513..60964e451 100644 --- a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs +++ b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs @@ -238,12 +238,13 @@ fn check_cgroup_subsystem( 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(); if let Some(v) = expected.downcast_ref::() { - assert_eq!(&content.parse::()?, v); + assert_eq!(&trimmed.parse::()?, v); } else if let Some(v) = expected.downcast_ref::() { - assert_eq!(&content.parse::()?, v); + assert_eq!(&trimmed.parse::()?, v); } else if let Some(v) = expected.downcast_ref::() { - assert_eq!(&content, v); + assert_eq!(&trimmed, v); } Ok(()) } From afbe94da4fc06cf3cd1b27f10e81ba9a643491d8 Mon Sep 17 00:00:00 2001 From: posutsai Date: Mon, 2 Sep 2024 12:15:42 +0000 Subject: [PATCH 4/9] Refine input type from Any to ToString Signed-off-by: posutsai --- tests/contest/contest/src/tests/cgroups/cpu/v1.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs index 60964e451..f5e8f4462 100644 --- a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs +++ b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs @@ -1,10 +1,10 @@ -use std::any::Any; use std::fs; use std::path::Path; use anyhow::{Context, Result}; use libcgroups::common; use num_cpus; +use std::string::ToString; use test_framework::{test_result, ConditionalTest, TestGroup, TestResult}; use tracing::debug; @@ -227,7 +227,7 @@ fn check_cgroup_subsystem( cgroup_name: &str, subsystem: &str, filename: &str, - expected: &dyn Any, + expected: &dyn ToString, ) -> Result<()> { let cgroup_path = Path::new(CGROUP_ROOT) .join(subsystem) @@ -239,13 +239,7 @@ fn check_cgroup_subsystem( let content = fs::read_to_string(&cgroup_path) .with_context(|| format!("failed to read {cgroup_path:?}"))?; let trimmed = content.trim(); - if let Some(v) = expected.downcast_ref::() { - assert_eq!(&trimmed.parse::()?, v); - } else if let Some(v) = expected.downcast_ref::() { - assert_eq!(&trimmed.parse::()?, v); - } else if let Some(v) = expected.downcast_ref::() { - assert_eq!(&trimmed, v); - } + assert_eq!(trimmed, expected.to_string()); Ok(()) } From d6cafd2adc7041c6443e5ca1de66c1ba321cfc65 Mon Sep 17 00:00:00 2001 From: posutsai Date: Mon, 2 Sep 2024 12:20:04 +0000 Subject: [PATCH 5/9] Fix lint Signed-off-by: posutsai --- tests/contest/contest/src/tests/cgroups/cpu/v1.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs index f5e8f4462..6e48f5d12 100644 --- a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs +++ b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs @@ -1,10 +1,10 @@ use std::fs; use std::path::Path; +use std::string::ToString; use anyhow::{Context, Result}; use libcgroups::common; use num_cpus; -use std::string::ToString; use test_framework::{test_result, ConditionalTest, TestGroup, TestResult}; use tracing::debug; From c1781f40a6c0e6979908cf344771720b5f00e821 Mon Sep 17 00:00:00 2001 From: posutsai Date: Fri, 6 Sep 2024 03:07:05 +0000 Subject: [PATCH 6/9] Fix code according to reviewer's comment Signed-off-by: posutsai --- .../contest/src/tests/cgroups/cpu/v1.rs | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs index 6e48f5d12..599ded4a9 100644 --- a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs +++ b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs @@ -2,11 +2,10 @@ use std::fs; use std::path::Path; use std::string::ToString; -use anyhow::{Context, Result}; +use anyhow::Result; use libcgroups::common; use num_cpus; use test_framework::{test_result, ConditionalTest, TestGroup, TestResult}; -use tracing::debug; use super::{create_cpu_spec, create_empty_spec, create_spec}; use crate::utils::test_outside_container; @@ -235,9 +234,7 @@ fn check_cgroup_subsystem( .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 content = fs::read_to_string(&cgroup_path)?; let trimmed = content.trim(); assert_eq!(trimmed, expected.to_string()); Ok(()) @@ -262,37 +259,31 @@ fn test_relative_cpus() -> TestResult { "test_relative_cpus", "cpu,cpuacct", "cpu.shares", - &test_result!(case.shares().context("no shares value in cpu spec")), + &case.shares().unwrap(), )); 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")), + &case.period().unwrap(), )); 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")), + &case.quota().unwrap(), )); test_result!(check_cgroup_subsystem( "test_relative_cpus", "cpuset", "cpuset.cpus", - &test_result!(case - .cpus() - .to_owned() - .context("no period value in cpu spec")) + &case.cpus().to_owned().unwrap(), )); test_result!(check_cgroup_subsystem( "test_relative_cpus", "cpuset", "cpuset.mems", - &test_result!(case - .mems() - .to_owned() - .context("no period value in cpu spec")), + &case.mems().to_owned().unwrap() )); TestResult::Passed }) From c7a1995344269ae80fe9d7811cb48e24fa94d485 Mon Sep 17 00:00:00 2001 From: posutsai Date: Wed, 18 Sep 2024 01:52:06 +0000 Subject: [PATCH 7/9] Extract common cgroup name into a variable Signed-off-by: posutsai --- tests/contest/contest/src/tests/cgroups/cpu/v1.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs index 599ded4a9..4446ee56a 100644 --- a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs +++ b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs @@ -255,32 +255,33 @@ fn test_relative_cpus() -> TestResult { test_outside_container(spec, &|data| { test_result!(check_container_created(&data)); + let cgroup_name = "test_relative_cpus"; test_result!(check_cgroup_subsystem( - "test_relative_cpus", + cgroup_name, "cpu,cpuacct", "cpu.shares", &case.shares().unwrap(), )); test_result!(check_cgroup_subsystem( - "test_relative_cpus", + cgroup_name, "cpu,cpuacct", "cpu.cfs_period_us", &case.period().unwrap(), )); test_result!(check_cgroup_subsystem( - "test_relative_cpus", + cgroup_name, "cpu,cpuacct", "cpu.cfs_quota_us", &case.quota().unwrap(), )); test_result!(check_cgroup_subsystem( - "test_relative_cpus", + cgroup_name, "cpuset", "cpuset.cpus", &case.cpus().to_owned().unwrap(), )); test_result!(check_cgroup_subsystem( - "test_relative_cpus", + cgroup_name, "cpuset", "cpuset.mems", &case.mems().to_owned().unwrap() From f21eea05789be2c599f04631b34798faffda6cf9 Mon Sep 17 00:00:00 2001 From: posutsai Date: Fri, 18 Oct 2024 09:47:49 +0000 Subject: [PATCH 8/9] Use get_subsystem_mount_point with relative path Signed-off-by: posutsai --- .../contest/src/tests/cgroups/cpu/v1.rs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs index 4446ee56a..8159e1479 100644 --- a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs +++ b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs @@ -2,15 +2,15 @@ use std::fs; use std::path::Path; use std::string::ToString; +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 anyhow::Result; use libcgroups::common; +use libcgroups::v1::{util, ControllerType}; use num_cpus; use test_framework::{test_result, ConditionalTest, TestGroup, TestResult}; -use super::{create_cpu_spec, create_empty_spec, create_spec}; -use crate::utils::test_outside_container; -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; const DEFAULT_REALTIME_RUNTIME: i64 = 950000; @@ -224,12 +224,12 @@ fn test_cpu_cgroups() -> TestResult { fn check_cgroup_subsystem( cgroup_name: &str, - subsystem: &str, + subsystem: &ControllerType, filename: &str, expected: &dyn ToString, ) -> Result<()> { - let cgroup_path = Path::new(CGROUP_ROOT) - .join(subsystem) + let mount_point = util::get_subsystem_mount_point(subsystem)?; + let cgroup_path = mount_point .join("runtime-test") .join(cgroup_name) .join(filename); @@ -258,31 +258,31 @@ fn test_relative_cpus() -> TestResult { let cgroup_name = "test_relative_cpus"; test_result!(check_cgroup_subsystem( cgroup_name, - "cpu,cpuacct", + &ControllerType::CpuAcct, "cpu.shares", &case.shares().unwrap(), )); test_result!(check_cgroup_subsystem( cgroup_name, - "cpu,cpuacct", + &ControllerType::CpuAcct, "cpu.cfs_period_us", &case.period().unwrap(), )); test_result!(check_cgroup_subsystem( cgroup_name, - "cpu,cpuacct", + &ControllerType::CpuAcct, "cpu.cfs_quota_us", &case.quota().unwrap(), )); test_result!(check_cgroup_subsystem( cgroup_name, - "cpuset", + &ControllerType::CpuSet, "cpuset.cpus", &case.cpus().to_owned().unwrap(), )); test_result!(check_cgroup_subsystem( cgroup_name, - "cpuset", + &ControllerType::CpuSet, "cpuset.mems", &case.mems().to_owned().unwrap() )); From 5adbd819131639908162c597babdfa927d748ac2 Mon Sep 17 00:00:00 2001 From: posutsai Date: Fri, 18 Oct 2024 09:52:22 +0000 Subject: [PATCH 9/9] Fix lint Signed-off-by: posutsai --- tests/contest/contest/src/tests/cgroups/cpu/v1.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs index 8159e1479..a7f9296a2 100644 --- a/tests/contest/contest/src/tests/cgroups/cpu/v1.rs +++ b/tests/contest/contest/src/tests/cgroups/cpu/v1.rs @@ -2,15 +2,16 @@ use std::fs; use std::path::Path; use std::string::ToString; -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 anyhow::Result; use libcgroups::common; use libcgroups::v1::{util, ControllerType}; use num_cpus; use test_framework::{test_result, ConditionalTest, TestGroup, TestResult}; +use super::{create_cpu_spec, create_empty_spec, create_spec}; +use crate::utils::test_outside_container; +use crate::utils::test_utils::check_container_created; + const CPU_CGROUP_PREFIX: &str = "/sys/fs/cgroup/cpu,cpuacct"; const DEFAULT_REALTIME_PERIOD: u64 = 1000000; const DEFAULT_REALTIME_RUNTIME: i64 = 950000;