From 0763c54c8132106940cf50e3e2ad54944f1f77cc Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 14 Jul 2021 18:13:04 -0700 Subject: [PATCH] tests/int: test case for #3084 Testing that it fails without the fix. Signed-off-by: Kir Kolyshkin --- tests/integration/helpers.bash | 25 ++++++++++++++---- tests/integration/update.bats | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/tests/integration/helpers.bash b/tests/integration/helpers.bash index e095953a1f8..b13cc5e6fe8 100644 --- a/tests/integration/helpers.bash +++ b/tests/integration/helpers.bash @@ -133,22 +133,37 @@ function init_cgroup_paths() { # Randomize cgroup path(s), and update cgroupsPath in config.json. # This function sets a few cgroup-related variables. +# +# Optional parameter $1 is a pod/parent name. If set, a parent/pod cgroup is +# created, and variables $REL_PARENT_PATH and $SD_PARENT_NAME can be used to +# operate it. function set_cgroups_path() { init_cgroup_paths + local pod dash_pod slash_pod pod_slice + if [ "$#" -ne 0 -a "$1" != "" ]; then + # Set up a parent/pod cgroup. + pod="$1" + dash_pod="-$pod" + slash_pod="/$pod" + SD_PARENT_NAME="machine-${pod}.slice" + pod_slice="/$SD_PARENT_NAME" + fi local rnd="$RANDOM" if [ -n "${RUNC_USE_SYSTEMD}" ]; then SD_UNIT_NAME="runc-cgroups-integration-test-${rnd}.scope" if [ "$(id -u)" = "0" ]; then - REL_CGROUPS_PATH="/machine.slice/$SD_UNIT_NAME" - OCI_CGROUPS_PATH="machine.slice:runc-cgroups:integration-test-${rnd}" + REL_PARENT_PATH="/machine.slice${pod_slice}" + OCI_CGROUPS_PATH="machine${dash_pod}.slice:runc-cgroups:integration-test-${rnd}" else - REL_CGROUPS_PATH="/user.slice/user-$(id -u).slice/user@$(id -u).service/machine.slice/$SD_UNIT_NAME" + REL_PARENT_PATH="/user.slice/user-$(id -u).slice/user@$(id -u).service/machine.slice${pod_slice}" # OCI path doesn't contain "/user.slice/user-$(id -u).slice/user@$(id -u).service/" prefix - OCI_CGROUPS_PATH="machine.slice:runc-cgroups:integration-test-${rnd}" + OCI_CGROUPS_PATH="machine${dash_pod}.slice:runc-cgroups:integration-test-${rnd}" fi + REL_CGROUPS_PATH="$REL_PARENT_PATH/$SD_UNIT_NAME" else - REL_CGROUPS_PATH="/runc-cgroups-integration-test/test-cgroup-${rnd}" + REL_PARENT_PATH="/runc-cgroups-integration-test${slash_pod}" + REL_CGROUPS_PATH="$REL_PARENT_PATH/test-cgroup-${rnd}" OCI_CGROUPS_PATH=$REL_CGROUPS_PATH fi diff --git a/tests/integration/update.bats b/tests/integration/update.bats index d51e93d0d47..4c2df75bea6 100644 --- a/tests/integration/update.bats +++ b/tests/integration/update.bats @@ -383,6 +383,53 @@ EOF check_cpu_quota 30000 100000 "300ms" } +@test "update cpu period in a pod cgroup with pod limit set" { + requires cgroups_v1 + [[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup + + set_cgroups_path "pod_${RANDOM}" + + echo "REL_PARENT_PATH=$REL_PARENT_PATH" + echo "REL_CGROUPS_PATH=$REL_CGROUPS_PATH" + echo "OCI_CGROUPS_PATH=$OCI_CGROUPS_PATH" + echo "SD_UNIT_NAME=$SD_UNIT_NAME" + echo "SD_PARENT_NAME=$SD_PARENT_NAME" + + # Create pod cgroup, set pod CPU quota limit. + if [ -n "${RUNC_USE_SYSTEMD}" ]; then + local user + [ $(id -u) != "0" ] && user="--user" + systemctl set-property $user $SD_PARENT_NAME CPUQuota="50%" + else + mkdir /sys/fs/cgroup/cpu/$REL_PARENT_PATH + echo 50000 > /sys/fs/cgroup/cpu/$REL_PARENT_PATH/cpu.cfs_quota_us + fi + # Sanity check + tail /sys/fs/cgroup/cpu$REL_PARENT_PATH/cpu.cfs* + local cur + cur=$(cat /sys/fs/cgroup/cpu$REL_PARENT_PATH/cpu.cfs_period_us) + [ "$cur" -eq 100000 ] + cur=$(cat /sys/fs/cgroup/cpu$REL_PARENT_PATH/cpu.cfs_quota_us) + [ "$cur" -eq 50000 ] + + runc run -d --console-socket "$CONSOLE_SOCKET" test_update + [ "$status" -eq 0 ] + # Container current values + tail /sys/fs/cgroup/cpu$REL_CGROUPS_PATH/cpu.cfs* + # Get the current period + cur=$(get_cgroup_value cpu.cfs_period_us) + + # Sanity check: as the parent cgroup sets the limit to 50%, + # setting a higher limit (e.g. 60%) is expected to fail. + runc update --cpu-quota $((cur/10*6)) test_update + [ "$status" -eq 1 ] + + # Finally, the test itself: set 30% limit but with lower period. + runc update --cpu-period 10000 --cpu-quota 3000 test_update + [ "$status" -eq 0 ] + check_cpu_quota 3000 10000 "300ms" +} + @test "update cgroup v2 resources via unified map" { [[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup requires cgroups_v2