Skip to content

Commit

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

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Jul 15, 2021
1 parent 6a314ae commit 4d77f91
Show file tree
Hide file tree
Showing 2 changed files with 58 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
38 changes: 38 additions & 0 deletions tests/integration/update.bats
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,44 @@ EOF
check_cpu_quota 30000 100000 "300ms"
}

@test "update cpu period in a pod cgroup with pod limit set" {
requires cgroups_v1 root # FIXME for rootless
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup

set_cgroups_path "pod_${RANDOM}"

# Create parent/pod cgroup with CPU quota limit of 50%.
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 checks.
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 ]
# 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 * 6 / 10)) 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 4d77f91

Please sign in to comment.