Skip to content

Commit

Permalink
tests/integration: add some cgroup v2 tests
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kolyshkin committed Apr 13, 2020
1 parent 3dfa543 commit 483f9a0
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 55 deletions.
22 changes: 19 additions & 3 deletions tests/integration/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}."
;;
Expand Down
164 changes: 112 additions & 52 deletions tests/integration/update.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -78,52 +82,48 @@ 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
runc update test_update --pids-limit 10
[ "$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 <<EOF
{
"memory": {
Expand All @@ -142,12 +142,9 @@ EOF
}
EOF
[ "$status" -eq 0 ]
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

# redo all the changes at once
Expand All @@ -156,11 +153,8 @@ EOF
--memory 67108864 --memory-reservation 33554432 \
--pids-limit 10
[ "$status" -eq 0 ]
check_cgroup_value "cpu.cfs_period_us" 900000
check_cgroup_value "cpu.cfs_quota_us" 600000
check_cgroup_value "cpu.shares" 200
check_cgroup_value "memory.limit_in_bytes" 67108864
check_cgroup_value "memory.soft_limit_in_bytes" 33554432
check_cgroup_value $MEM_LIMIT 67108864
check_cgroup_value $MEM_RESERVE 33554432
check_cgroup_value "pids.max" 10

# reset to initial test value via json file
Expand All @@ -180,17 +174,83 @@ EOF
"limit": 20
}
}
EOF

runc update -r $BATS_TMPDIR/runc-cgroups-integration-test.json test_update
[ "$status" -eq 0 ]
check_cgroup_value "cpuset.cpus" 0
check_cgroup_value $MEM_LIMIT 33554432
check_cgroup_value $MEM_RESERVE 25165824
check_cgroup_value "pids.max" 20
}

@test "update cgroup v1 cpu limits" {
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
requires cgroups_v1

# run a few busyboxes detached
runc run -d --console-socket $CONSOLE_SOCKET test_update
[ "$status" -eq 0 ]

# 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

# 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

# Revert to the test initial value via json on stding
runc update -r - test_update <<EOF
{
"cpu": {
"shares": 100,
"quota": 500000,
"period": 1000000
}
}
EOF
[ "$status" -eq 0 ]
check_cgroup_value "cpu.cfs_period_us" 1000000
check_cgroup_value "cpu.cfs_quota_us" 500000
check_cgroup_value "cpu.shares" 100

# redo all the changes at once
runc update test_update \
--cpu-period 900000 --cpu-quota 600000 --cpu-share 200
[ "$status" -eq 0 ]
check_cgroup_value "cpu.cfs_period_us" 900000
check_cgroup_value "cpu.cfs_quota_us" 600000
check_cgroup_value "cpu.shares" 200

# reset to initial test value via json file
cat << EOF > $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
[ "$status" -eq 0 ]
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" {
Expand Down

0 comments on commit 483f9a0

Please sign in to comment.