Skip to content

Commit

Permalink
tests/int: test case for #3084
Browse files Browse the repository at this point in the history
Testing that it fails without the fix.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Jul 15, 2021
1 parent 4e2ef3b commit 0763c54
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
25 changes: 20 additions & 5 deletions tests/integration/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 47 additions & 0 deletions tests/integration/update.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0763c54

Please sign in to comment.