From 3ae9358054cb381395c384155b3ba6a64ae6e1e7 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 25 Mar 2020 18:18:06 -0700 Subject: [PATCH] tests/integration: check_cgroup_value: simplify Consolidate two implementations of check_cgroup_value() into one, putting it into helpers. Remove the first parameter, deducing the variable to get the path from by the parameter name. This should help in future cgroupv2 support. Signed-off-by: Kir Kolyshkin --- tests/integration/cgroups.bats | 19 ++---- tests/integration/helpers.bash | 14 ++++ tests/integration/update.bats | 115 +++++++++++++++------------------ 3 files changed, 72 insertions(+), 76 deletions(-) diff --git a/tests/integration/cgroups.bats b/tests/integration/cgroups.bats index 17812abf..f925a0f8 100644 --- a/tests/integration/cgroups.bats +++ b/tests/integration/cgroups.bats @@ -14,17 +14,6 @@ function setup() { setup_busybox } -function check_cgroup_value() { - cgroup=$1 - source=$2 - expected=$3 - - current=$(cat $cgroup/$source) - echo $cgroup/$source - echo "current" $current "!?" "$expected" - [ "$current" -eq "$expected" ] -} - @test "runc update --kernel-memory (initialized)" { [[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup requires cgroups_kmem @@ -45,12 +34,14 @@ EOF runc run -d --console-socket $CONSOLE_SOCKET test_cgroups_kmem [ "$status" -eq 0 ] + check_cgroup_value "memory.kmem.limit_in_bytes" 16777216 + # update kernel memory limit runc update test_cgroups_kmem --kernel-memory 50331648 [ "$status" -eq 0 ] - # check the value - check_cgroup_value $CGROUP_MEMORY "memory.kmem.limit_in_bytes" 50331648 + # check the value + check_cgroup_value "memory.kmem.limit_in_bytes" 50331648 } @test "runc update --kernel-memory (uninitialized)" { @@ -71,7 +62,7 @@ EOF [ ! "$status" -eq 0 ] else [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_MEMORY "memory.kmem.limit_in_bytes" 50331648 + check_cgroup_value "memory.kmem.limit_in_bytes" 50331648 fi } diff --git a/tests/integration/helpers.bash b/tests/integration/helpers.bash index 8862dcb8..161fb84e 100644 --- a/tests/integration/helpers.bash +++ b/tests/integration/helpers.bash @@ -129,6 +129,20 @@ function set_cgroups_path() { sed -i 's#\("linux": {\)#\1\n "cgroupsPath": "'"${cgroups_path}"'",#' "$bundle/config.json" } +# Helper to check a value in cgroups. +function check_cgroup_value() { + source=$1 + expected=$2 + + ctrl=${source%%.*} + eval cgroup=\$CGROUP_${ctrl^^} + + current=$(cat $cgroup/$source) + echo $cgroup/$source + echo "current" $current "!?" "$expected" + [ "$current" -eq "$expected" ] +} + # Helper function to set a resources limit function set_resources_limit() { bundle="${1:-.}" diff --git a/tests/integration/update.bats b/tests/integration/update.bats index 549b2ece..bdfc1697 100644 --- a/tests/integration/update.bats +++ b/tests/integration/update.bats @@ -38,15 +38,6 @@ EOF sed -i "s/\(\"resources\": {\)/\1\n${DATA}/" ${BUSYBOX_BUNDLE}/config.json } -function check_cgroup_value() { - cgroup=$1 - source=$2 - expected=$3 - - current=$(cat $cgroup/$source) - [ "$current" == "$expected" ] -} - # TODO: test rt cgroup updating @test "update" { # XXX: Also, this test should be split into separate sections so that we @@ -67,52 +58,52 @@ function check_cgroup_value() { CGROUP_SYSTEM_MEMORY=$(grep "cgroup" /proc/self/mountinfo | gawk 'toupper($NF) ~ /\<'MEMORY'\>/ { print $5; exit }') # check that initial values were properly set - check_cgroup_value $CGROUP_CPU "cpu.cfs_period_us" 1000000 - check_cgroup_value $CGROUP_CPU "cpu.cfs_quota_us" 500000 - check_cgroup_value $CGROUP_CPU "cpu.shares" 100 - check_cgroup_value $CGROUP_CPUSET "cpuset.cpus" 0 - check_cgroup_value $CGROUP_MEMORY "memory.kmem.limit_in_bytes" 16777216 - check_cgroup_value $CGROUP_MEMORY "memory.kmem.tcp.limit_in_bytes" 11534336 - check_cgroup_value $CGROUP_MEMORY "memory.limit_in_bytes" 33554432 - check_cgroup_value $CGROUP_MEMORY "memory.soft_limit_in_bytes" 25165824 - check_cgroup_value $CGROUP_PIDS "pids.max" 20 + check_cgroup_value "cpu.cfs_period_us" 1000000 + check_cgroup_value "cpu.cfs_quota_us" 500000 + check_cgroup_value "cpu.shares" 100 + check_cgroup_value "cpuset.cpus" 0 + check_cgroup_value "memory.kmem.limit_in_bytes" 16777216 + check_cgroup_value "memory.kmem.tcp.limit_in_bytes" 11534336 + check_cgroup_value "memory.limit_in_bytes" 33554432 + check_cgroup_value "memory.soft_limit_in_bytes" 25165824 + check_cgroup_value "pids.max" 20 # update cpu-period runc update test_update --cpu-period 900000 [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_CPU "cpu.cfs_period_us" 900000 + check_cgroup_value "cpu.cfs_period_us" 900000 # update cpu-quota runc update test_update --cpu-quota 600000 [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_CPU "cpu.cfs_quota_us" 600000 + check_cgroup_value "cpu.cfs_quota_us" 600000 # update cpu-shares runc update test_update --cpu-share 200 [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_CPU "cpu.shares" 200 + check_cgroup_value "cpu.shares" 200 # update cpuset if supported (i.e. we're running on a multicore cpu) cpu_count=$(grep '^processor' /proc/cpuinfo | wc -l) if [ $cpu_count -gt 1 ]; then runc update test_update --cpuset-cpus "1" [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_CPUSET "cpuset.cpus" 1 + check_cgroup_value "cpuset.cpus" 1 fi # update memory limit runc update test_update --memory 67108864 [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_MEMORY "memory.limit_in_bytes" 67108864 + check_cgroup_value "memory.limit_in_bytes" 67108864 runc update test_update --memory 50M [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_MEMORY "memory.limit_in_bytes" 52428800 + check_cgroup_value "memory.limit_in_bytes" 52428800 # update memory soft limit runc update test_update --memory-reservation 33554432 [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_MEMORY "memory.soft_limit_in_bytes" 33554432 + check_cgroup_value "memory.soft_limit_in_bytes" 33554432 # Run swap memory tests if swap is available if [ -f "$CGROUP_MEMORY/memory.memsw.limit_in_bytes" ]; then @@ -121,12 +112,12 @@ function check_cgroup_value() { [ "$status" -eq 0 ] # Get System memory swap limit SYSTEM_MEMORY_SW=$(cat "${CGROUP_SYSTEM_MEMORY}/memory.memsw.limit_in_bytes") - check_cgroup_value $CGROUP_MEMORY "memory.memsw.limit_in_bytes" ${SYSTEM_MEMORY_SW} + check_cgroup_value "memory.memsw.limit_in_bytes" ${SYSTEM_MEMORY_SW} # update memory swap runc update test_update --memory-swap 96468992 [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_MEMORY "memory.memsw.limit_in_bytes" 96468992 + check_cgroup_value "memory.memsw.limit_in_bytes" 96468992 fi; # try to remove memory limit @@ -136,27 +127,27 @@ function check_cgroup_value() { # Get System memory limit SYSTEM_MEMORY=$(cat "${CGROUP_SYSTEM_MEMORY}/memory.limit_in_bytes") # check memory limited is gone - check_cgroup_value $CGROUP_MEMORY "memory.limit_in_bytes" ${SYSTEM_MEMORY} + check_cgroup_value "memory.limit_in_bytes" ${SYSTEM_MEMORY} # check swap memory limited is gone if [ -f "$CGROUP_MEMORY/memory.memsw.limit_in_bytes" ]; then - check_cgroup_value $CGROUP_MEMORY "memory.memsw.limit_in_bytes" ${SYSTEM_MEMORY} + check_cgroup_value "memory.memsw.limit_in_bytes" ${SYSTEM_MEMORY} fi # update kernel memory limit runc update test_update --kernel-memory 50331648 [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_MEMORY "memory.kmem.limit_in_bytes" 50331648 + check_cgroup_value "memory.kmem.limit_in_bytes" 50331648 # update kernel memory tcp limit runc update test_update --kernel-memory-tcp 41943040 [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_MEMORY "memory.kmem.tcp.limit_in_bytes" 41943040 + check_cgroup_value "memory.kmem.tcp.limit_in_bytes" 41943040 # update pids limit runc update test_update --pids-limit 10 [ "$status" -eq 0 ] - check_cgroup_value $CGROUP_PIDS "pids.max" 10 + check_cgroup_value "pids.max" 10 # Revert to the test initial value via json on stding runc update -r - test_update <