From 483f9a0c50d11fd87e066c0b322e4858fad21499 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 31 Mar 2020 15:27:51 -0700 Subject: [PATCH] tests/integration: add some cgroup v2 tests 1. Add `cgroups_v1` and `cgroups_v2` options to `requires`. 2. Modify `check_cgroup_value` to be able to work with v2. 3. Split `test "update"` into two: - (1) testing cgroupv1-only cpu shares and cfs - (2) testing limits that are more or less common between v1 and v2: memory/swap, pids, cpusets. Signed-off-by: Kir Kolyshkin --- tests/integration/helpers.bash | 22 ++++- tests/integration/update.bats | 164 ++++++++++++++++++++++----------- 2 files changed, 131 insertions(+), 55 deletions(-) diff --git a/tests/integration/helpers.bash b/tests/integration/helpers.bash index 0fc78f77ee7..de8be4ba53a 100644 --- a/tests/integration/helpers.bash +++ b/tests/integration/helpers.bash @@ -150,13 +150,17 @@ function check_cgroup_value() { source=$1 expected=$2 - ctrl=${source%%.*} - eval cgroup=\$CGROUP_${ctrl^^} + if [ "x$CGROUP_UNIFIED" = "xyes" ] ; then + cgroup=$CGROUP_PATH + else + ctrl=${source%%.*} + eval cgroup=\$CGROUP_${ctrl^^} + fi current=$(cat $cgroup/$source) echo $cgroup/$source echo "current" $current "!?" "$expected" - [ "$current" -eq "$expected" ] + [ "$current" = "$expected" ] } # Helper function to set a resources limit @@ -218,6 +222,18 @@ function requires() { skip "Test requires ${var}" fi ;; + cgroups_v1) + init_cgroup_paths + if [ "$CGROUP_UNIFIED" != "no" ]; then + skip "Test requires cgroups v1" + fi + ;; + cgroups_v2) + init_cgroup_paths + if [ "$CGROUP_UNIFIED" != "yes" ]; then + skip "Test requires cgroups v2 (unified)" + fi + ;; *) fail "BUG: Invalid requires ${var}." ;; diff --git a/tests/integration/update.bats b/tests/integration/update.bats index 8d9f5f8d733..6c82a065418 100644 --- a/tests/integration/update.bats +++ b/tests/integration/update.bats @@ -36,39 +36,43 @@ EOF sed -i "s/\(\"resources\": {\)/\1\n${DATA}/" ${BUSYBOX_BUNDLE}/config.json } -@test "update" { +# Tests whatever limits are (more or less) common between cgroup +# v1 and v2: memory/swap, pids, and cpuset. +@test "update cgroup v1/v2 common limits" { [[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup + init_cgroup_paths # run a few busyboxes detached runc run -d --console-socket $CONSOLE_SOCKET test_update [ "$status" -eq 0 ] + # Set a few variables to make the code below work for both v1 and v2 + case $CGROUP_UNIFIED in + no) + MEM_LIMIT="memory.limit_in_bytes" + MEM_RESERVE="memory.soft_limit_in_bytes" + MEM_SWAP="memory.memsw.limit_in_bytes" + SYSTEM_MEM=$(cat "${CGROUP_MEMORY_BASE_PATH}/${MEM_LIMIT}") + SYSTEM_MEM_SWAP=$(cat "${CGROUP_MEMORY_BASE_PATH}/$MEM_SWAP") + ;; + yes) + MEM_LIMIT="memory.max" + MEM_RESERVE="memory.low" + MEM_SWAP="memory.swap.max" + SYSTEM_MEM="max" + SYSTEM_MEM_SWAP="max" + CGROUP_MEMORY=$CGROUP_PATH + ;; + esac + # check that initial values were properly set - 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.limit_in_bytes" 33554432 - check_cgroup_value "memory.soft_limit_in_bytes" 25165824 + check_cgroup_value $MEM_LIMIT 33554432 + check_cgroup_value $MEM_RESERVE 25165824 check_cgroup_value "pids.max" 20 - # update cpu-period - runc update test_update --cpu-period 900000 - [ "$status" -eq 0 ] - check_cgroup_value "cpu.cfs_period_us" 900000 - - # update cpu-quota - runc update test_update --cpu-quota 600000 - [ "$status" -eq 0 ] - check_cgroup_value "cpu.cfs_quota_us" 600000 - - # update cpu-shares - runc update test_update --cpu-share 200 - [ "$status" -eq 0 ] - 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) + cpu_count=$(grep -c '^processor' /proc/cpuinfo) if [ $cpu_count -gt 1 ]; then runc update test_update --cpuset-cpus "1" [ "$status" -eq 0 ] @@ -78,44 +82,40 @@ EOF # update memory limit runc update test_update --memory 67108864 [ "$status" -eq 0 ] - check_cgroup_value "memory.limit_in_bytes" 67108864 + check_cgroup_value $MEM_LIMIT 67108864 runc update test_update --memory 50M [ "$status" -eq 0 ] - check_cgroup_value "memory.limit_in_bytes" 52428800 + check_cgroup_value $MEM_LIMIT 52428800 # update memory soft limit runc update test_update --memory-reservation 33554432 [ "$status" -eq 0 ] - check_cgroup_value "memory.soft_limit_in_bytes" 33554432 + check_cgroup_value "$MEM_RESERVE" 33554432 # Run swap memory tests if swap is available - if [ -f "$CGROUP_MEMORY/memory.memsw.limit_in_bytes" ]; then + if [ -f "$CGROUP_MEMORY/$MEM_SWAP" ]; then # try to remove memory swap limit runc update test_update --memory-swap -1 [ "$status" -eq 0 ] - # Get System memory swap limit - SYSTEM_MEMORY_SW=$(cat "${CGROUP_MEMORY_BASE_PATH}/memory.memsw.limit_in_bytes") - check_cgroup_value "memory.memsw.limit_in_bytes" ${SYSTEM_MEMORY_SW} + check_cgroup_value "$MEM_SWAP" $SYSTEM_MEM_SWAP # update memory swap runc update test_update --memory-swap 96468992 [ "$status" -eq 0 ] - check_cgroup_value "memory.memsw.limit_in_bytes" 96468992 - fi; + check_cgroup_value "$MEM_SWAP" 96468992 + fi # try to remove memory limit runc update test_update --memory -1 [ "$status" -eq 0 ] - # Get System memory limit - SYSTEM_MEMORY=$(cat "${CGROUP_MEMORY_BASE_PATH}/memory.limit_in_bytes") - # check memory limited is gone - check_cgroup_value "memory.limit_in_bytes" ${SYSTEM_MEMORY} + # check memory limit is gone + check_cgroup_value $MEM_LIMIT $SYSTEM_MEM # check swap memory limited is gone - if [ -f "$CGROUP_MEMORY/memory.memsw.limit_in_bytes" ]; then - check_cgroup_value "memory.memsw.limit_in_bytes" ${SYSTEM_MEMORY} + if [ -f "$CGROUP_MEMORY/$MEM_SWAP" ]; then + check_cgroup_value $MEM_SWAP $SYSTEM_MEM fi # update pids limit @@ -123,7 +123,7 @@ EOF [ "$status" -eq 0 ] check_cgroup_value "pids.max" 10 - # Revert to the test initial value via json on stding + # Revert to the test initial value via json on stdin runc update -r - test_update < $BATS_TMPDIR/runc-cgroups-integration-test.json +{ + "cpu": { + "shares": 100, + "quota": 500000, + "period": 1000000 + } +} EOF runc update -r $BATS_TMPDIR/runc-cgroups-integration-test.json test_update @@ -187,10 +251,6 @@ EOF 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.limit_in_bytes" 33554432 - check_cgroup_value "memory.soft_limit_in_bytes" 25165824 - check_cgroup_value "pids.max" 20 } @test "update rt period and runtime" {