2016-04-28 22:30:50 +08:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
|
|
|
|
load helpers
|
|
|
|
|
|
|
|
function teardown() {
|
2017-09-15 17:39:35 +08:00
|
|
|
rm -f $BATS_TMPDIR/runc-cgroups-integration-test.json
|
2016-05-11 14:17:32 +08:00
|
|
|
teardown_running_container test_update
|
2016-11-28 16:30:54 +08:00
|
|
|
teardown_running_container test_update_rt
|
2016-04-28 22:30:50 +08:00
|
|
|
teardown_busybox
|
|
|
|
}
|
|
|
|
|
|
|
|
function setup() {
|
|
|
|
teardown
|
|
|
|
setup_busybox
|
|
|
|
|
2017-09-15 17:39:35 +08:00
|
|
|
set_cgroups_path "$BUSYBOX_BUNDLE"
|
2016-04-28 22:30:50 +08:00
|
|
|
|
|
|
|
# Set some initial known values
|
2020-05-25 08:54:13 +08:00
|
|
|
update_config ' .linux.resources.memory |= {"limit": 33554432, "reservation": 25165824}
|
|
|
|
| .linux.resources.cpu |= {"shares": 100, "quota": 500000, "period": 1000000, "cpus": "0"}
|
|
|
|
| .linux.resources.pids |= {"limit": 20}' ${BUSYBOX_BUNDLE}
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
# 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" {
|
2020-04-01 09:47:06 +08:00
|
|
|
[[ "$ROOTLESS" -ne 0 && -z "$RUNC_USE_SYSTEMD" ]] && requires rootless_cgroup
|
|
|
|
if [[ "$ROOTLESS" -ne 0 && -n "$RUNC_USE_SYSTEMD" ]]; then
|
|
|
|
file="/sys/fs/cgroup/user.slice/user-$(id -u).slice/user@$(id -u).service/cgroup.controllers"
|
|
|
|
# NOTE: delegation of cpuset requires systemd >= 244 (Fedora >= 32, Ubuntu >= 20.04).
|
|
|
|
for f in memory pids cpuset; do
|
|
|
|
if grep -qwv $f $file; then
|
|
|
|
skip "$f is not enabled in $file"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2020-04-01 06:27:51 +08:00
|
|
|
init_cgroup_paths
|
2016-05-10 20:22:13 +08:00
|
|
|
|
2016-05-14 06:49:45 +08:00
|
|
|
# run a few busyboxes detached
|
2016-09-06 20:40:01 +08:00
|
|
|
runc run -d --console-socket $CONSOLE_SOCKET test_update
|
2016-04-28 22:30:50 +08:00
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
# 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"
|
2020-05-20 10:19:59 +08:00
|
|
|
SD_MEM_LIMIT="MemoryLimit"
|
2020-04-01 06:27:51 +08:00
|
|
|
MEM_RESERVE="memory.soft_limit_in_bytes"
|
2020-05-20 10:45:25 +08:00
|
|
|
SD_MEM_RESERVE="unsupported"
|
2020-04-01 06:27:51 +08:00
|
|
|
MEM_SWAP="memory.memsw.limit_in_bytes"
|
2020-05-20 10:45:25 +08:00
|
|
|
SD_MEM_SWAP="unsupported"
|
2020-04-01 06:27:51 +08:00
|
|
|
SYSTEM_MEM=$(cat "${CGROUP_MEMORY_BASE_PATH}/${MEM_LIMIT}")
|
2020-05-31 05:44:16 +08:00
|
|
|
HAVE_SWAP="no"
|
|
|
|
if [ -f "${CGROUP_MEMORY_BASE_PATH}/${MEM_SWAP}" ]; then
|
|
|
|
HAVE_SWAP="yes"
|
|
|
|
fi
|
2020-04-01 06:27:51 +08:00
|
|
|
;;
|
|
|
|
yes)
|
|
|
|
MEM_LIMIT="memory.max"
|
2020-05-20 10:19:59 +08:00
|
|
|
SD_MEM_LIMIT="MemoryMax"
|
2020-04-01 06:27:51 +08:00
|
|
|
MEM_RESERVE="memory.low"
|
2020-05-20 10:45:25 +08:00
|
|
|
SD_MEM_RESERVE="MemoryLow"
|
2020-04-01 06:27:51 +08:00
|
|
|
MEM_SWAP="memory.swap.max"
|
2020-05-20 10:45:25 +08:00
|
|
|
SD_MEM_SWAP="MemorySwapMax"
|
2020-04-01 06:27:51 +08:00
|
|
|
SYSTEM_MEM="max"
|
2020-05-31 05:44:16 +08:00
|
|
|
HAVE_SWAP="yes"
|
2020-04-01 06:27:51 +08:00
|
|
|
;;
|
|
|
|
esac
|
2020-05-20 10:45:25 +08:00
|
|
|
SD_UNLIMITED="infinity"
|
2020-06-03 16:49:35 +08:00
|
|
|
SD_VERSION=$(systemctl --version | awk '{print $2; exit}')
|
|
|
|
if [ $SD_VERSION -lt 227 ]; then
|
|
|
|
SD_UNLIMITED="18446744073709551615"
|
|
|
|
fi
|
2020-04-01 06:27:51 +08:00
|
|
|
|
2016-04-28 22:30:50 +08:00
|
|
|
# check that initial values were properly set
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "cpuset.cpus" 0
|
2020-04-21 23:19:36 +08:00
|
|
|
if [[ "$CGROUP_UNIFIED" = "yes" ]] && ! grep -qw memory "$CGROUP_PATH/cgroup.controllers"; then
|
|
|
|
# This happen on containerized environment because "echo +memory > /sys/fs/cgroup/cgroup.subtree_control" fails with EINVAL
|
|
|
|
skip "memory controller not available"
|
|
|
|
fi
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_LIMIT 33554432
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value $SD_MEM_LIMIT 33554432
|
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_RESERVE 25165824
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value $SD_MEM_RESERVE 25165824
|
|
|
|
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "pids.max" 20
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value "TasksMax" 20
|
2016-04-28 22:30:50 +08:00
|
|
|
|
|
|
|
# update cpuset if supported (i.e. we're running on a multicore cpu)
|
2020-04-01 06:27:51 +08:00
|
|
|
cpu_count=$(grep -c '^processor' /proc/cpuinfo)
|
2016-09-18 10:44:46 +08:00
|
|
|
if [ $cpu_count -gt 1 ]; then
|
2016-05-09 21:06:42 +08:00
|
|
|
runc update test_update --cpuset-cpus "1"
|
2016-04-28 22:30:50 +08:00
|
|
|
[ "$status" -eq 0 ]
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "cpuset.cpus" 1
|
2016-04-28 22:30:50 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# update memory limit
|
2016-05-09 21:06:42 +08:00
|
|
|
runc update test_update --memory 67108864
|
2016-04-28 22:30:50 +08:00
|
|
|
[ "$status" -eq 0 ]
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_LIMIT 67108864
|
2020-05-20 10:19:59 +08:00
|
|
|
check_systemd_value $SD_MEM_LIMIT 67108864
|
2016-04-28 22:30:50 +08:00
|
|
|
|
2016-05-30 18:56:10 +08:00
|
|
|
runc update test_update --memory 50M
|
|
|
|
[ "$status" -eq 0 ]
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_LIMIT 52428800
|
2020-05-20 10:19:59 +08:00
|
|
|
check_systemd_value $SD_MEM_LIMIT 52428800
|
2016-05-30 18:56:10 +08:00
|
|
|
|
2016-04-28 22:30:50 +08:00
|
|
|
# update memory soft limit
|
2016-05-09 21:06:42 +08:00
|
|
|
runc update test_update --memory-reservation 33554432
|
2016-04-28 22:30:50 +08:00
|
|
|
[ "$status" -eq 0 ]
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value "$MEM_RESERVE" 33554432
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value "$SD_MEM_RESERVE" 33554432
|
2016-04-28 22:30:50 +08:00
|
|
|
|
2018-09-07 11:37:40 +08:00
|
|
|
# Run swap memory tests if swap is available
|
2020-05-31 05:44:16 +08:00
|
|
|
if [ "$HAVE_SWAP" = "yes" ]; then
|
2016-10-19 18:15:55 +08:00
|
|
|
# try to remove memory swap limit
|
|
|
|
runc update test_update --memory-swap -1
|
|
|
|
[ "$status" -eq 0 ]
|
2020-05-31 05:09:07 +08:00
|
|
|
check_cgroup_value "$MEM_SWAP" $SYSTEM_MEM
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value "$SD_MEM_SWAP" $SD_UNLIMITED
|
2016-10-19 18:15:55 +08:00
|
|
|
|
|
|
|
# update memory swap
|
2020-05-21 03:12:44 +08:00
|
|
|
if [ "$CGROUP_UNIFIED" = "yes" ]; then
|
|
|
|
# for cgroupv2, memory and swap can only be set together
|
|
|
|
runc update test_update --memory 52428800 --memory-swap 96468992
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
# for cgroupv2, swap is a separate limit (it does not include mem)
|
|
|
|
check_cgroup_value "$MEM_SWAP" $((96468992-52428800))
|
|
|
|
check_systemd_value "$SD_MEM_SWAP" $((96468992-52428800))
|
|
|
|
else
|
|
|
|
runc update test_update --memory-swap 96468992
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
check_cgroup_value "$MEM_SWAP" 96468992
|
|
|
|
check_systemd_value "$SD_MEM_SWAP" 96468992
|
|
|
|
fi
|
2020-04-01 06:27:51 +08:00
|
|
|
fi
|
2016-10-19 18:15:55 +08:00
|
|
|
|
|
|
|
# try to remove memory limit
|
|
|
|
runc update test_update --memory -1
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
# check memory limit is gone
|
|
|
|
check_cgroup_value $MEM_LIMIT $SYSTEM_MEM
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value $SD_MEM_LIMIT $SD_UNLIMITED
|
2016-10-19 18:15:55 +08:00
|
|
|
|
|
|
|
# check swap memory limited is gone
|
2020-05-31 05:44:16 +08:00
|
|
|
if [ "$HAVE_SWAP" = "yes" ]; then
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_SWAP $SYSTEM_MEM
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value "$SD_MEM_SWAP" $SD_UNLIMITED
|
2016-04-28 22:30:50 +08:00
|
|
|
fi
|
|
|
|
|
2017-04-27 00:53:20 +08:00
|
|
|
# update pids limit
|
|
|
|
runc update test_update --pids-limit 10
|
|
|
|
[ "$status" -eq 0 ]
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "pids.max" 10
|
2020-05-20 10:19:59 +08:00
|
|
|
check_systemd_value "TasksMax" 10
|
2017-04-27 00:53:20 +08:00
|
|
|
|
2020-05-05 10:19:46 +08:00
|
|
|
# unlimited
|
|
|
|
runc update test_update --pids-limit -1
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
check_cgroup_value "pids.max" max
|
|
|
|
check_systemd_value "TasksMax" $SD_UNLIMITED
|
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
# Revert to the test initial value via json on stdin
|
2016-05-09 21:06:42 +08:00
|
|
|
runc update -r - test_update <<EOF
|
2016-04-28 22:30:50 +08:00
|
|
|
{
|
|
|
|
"memory": {
|
|
|
|
"limit": 33554432,
|
2020-03-31 05:00:35 +08:00
|
|
|
"reservation": 25165824
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
|
|
|
"cpu": {
|
|
|
|
"shares": 100,
|
|
|
|
"quota": 500000,
|
|
|
|
"period": 1000000,
|
|
|
|
"cpus": "0"
|
|
|
|
},
|
2017-04-27 00:53:20 +08:00
|
|
|
"pids": {
|
|
|
|
"limit": 20
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
[ "$status" -eq 0 ]
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "cpuset.cpus" 0
|
2020-05-20 10:45:25 +08:00
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_LIMIT 33554432
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value $SD_MEM_LIMIT 33554432
|
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_RESERVE 25165824
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value $SD_MEM_RESERVE 25165824
|
|
|
|
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "pids.max" 20
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value "TasksMax" 20
|
2016-04-28 22:30:50 +08:00
|
|
|
|
|
|
|
# redo all the changes at once
|
2019-07-08 17:07:48 +08:00
|
|
|
runc update test_update \
|
2020-03-31 05:00:35 +08:00
|
|
|
--cpu-period 900000 --cpu-quota 600000 --cpu-share 200 \
|
|
|
|
--memory 67108864 --memory-reservation 33554432 \
|
2017-04-27 00:53:20 +08:00
|
|
|
--pids-limit 10
|
2016-04-28 22:30:50 +08:00
|
|
|
[ "$status" -eq 0 ]
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_LIMIT 67108864
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value $SD_MEM_LIMIT 67108864
|
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_RESERVE 33554432
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value $SD_MEM_RESERVE 33554432
|
|
|
|
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "pids.max" 10
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value "TasksMax" 10
|
2016-04-28 22:30:50 +08:00
|
|
|
|
|
|
|
# reset to initial test value via json file
|
2020-03-31 06:04:12 +08:00
|
|
|
cat << EOF > $BATS_TMPDIR/runc-cgroups-integration-test.json
|
2016-04-28 22:30:50 +08:00
|
|
|
{
|
|
|
|
"memory": {
|
|
|
|
"limit": 33554432,
|
2020-03-31 05:00:35 +08:00
|
|
|
"reservation": 25165824
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
|
|
|
"cpu": {
|
|
|
|
"shares": 100,
|
|
|
|
"quota": 500000,
|
|
|
|
"period": 1000000,
|
|
|
|
"cpus": "0"
|
|
|
|
},
|
2017-04-27 00:53:20 +08:00
|
|
|
"pids": {
|
|
|
|
"limit": 20
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-01 06:27:51 +08:00
|
|
|
EOF
|
|
|
|
|
|
|
|
runc update -r $BATS_TMPDIR/runc-cgroups-integration-test.json test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
check_cgroup_value "cpuset.cpus" 0
|
2020-05-20 10:45:25 +08:00
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_LIMIT 33554432
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value $SD_MEM_LIMIT 33554432
|
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_RESERVE 25165824
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value $SD_MEM_RESERVE 25165824
|
|
|
|
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value "pids.max" 20
|
2020-05-20 10:45:25 +08:00
|
|
|
check_systemd_value "TasksMax" 20
|
2020-04-01 06:27:51 +08:00
|
|
|
}
|
|
|
|
|
2020-06-04 08:02:15 +08:00
|
|
|
function check_cpu_quota() {
|
|
|
|
local quota=$1
|
|
|
|
local period=$2
|
|
|
|
local sd_quota=$3
|
|
|
|
|
|
|
|
if [ "$CGROUP_UNIFIED" = "yes" ]; then
|
|
|
|
if [ "$quota" = "-1" ]; then
|
|
|
|
quota="max"
|
|
|
|
fi
|
|
|
|
check_cgroup_value "cpu.max" "$quota $period"
|
|
|
|
else
|
|
|
|
check_cgroup_value "cpu.cfs_quota_us" $quota
|
|
|
|
check_cgroup_value "cpu.cfs_period_us" $period
|
|
|
|
fi
|
2020-06-10 10:19:46 +08:00
|
|
|
# systemd values are the same for v1 and v2
|
2020-06-04 08:02:15 +08:00
|
|
|
check_systemd_value "CPUQuotaPerSecUSec" $sd_quota
|
2020-06-10 10:19:46 +08:00
|
|
|
|
|
|
|
# CPUQuotaPeriodUSec requires systemd >= v242
|
|
|
|
[ "$(systemd_version)" -lt 242 ] && return
|
|
|
|
|
|
|
|
local sd_period=$(( period/1000 ))ms
|
|
|
|
[ "$sd_period" = "1000ms" ] && sd_period="1s"
|
|
|
|
local sd_infinity=""
|
|
|
|
# 100ms is the default value, and if not set, shown as infinity
|
|
|
|
[ "$sd_period" = "100ms" ] && sd_infinity="infinity"
|
|
|
|
check_systemd_value "CPUQuotaPeriodUSec" $sd_period $sd_infinity
|
2020-06-04 08:02:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function check_cpu_shares() {
|
|
|
|
local shares=$1
|
|
|
|
|
|
|
|
if [ "$CGROUP_UNIFIED" = "yes" ]; then
|
|
|
|
local weight=$((1 + ((shares - 2) * 9999) / 262142))
|
|
|
|
check_cgroup_value "cpu.weight" $weight
|
|
|
|
check_systemd_value "CPUWeight" $weight
|
|
|
|
else
|
|
|
|
check_cgroup_value "cpu.shares" $shares
|
|
|
|
check_systemd_value "CPUShares" $shares
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "update cgroup cpu limits" {
|
2020-04-01 06:27:51 +08:00
|
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
|
|
|
|
|
|
# run a few busyboxes detached
|
|
|
|
runc run -d --console-socket $CONSOLE_SOCKET test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
|
|
|
# check that initial values were properly set
|
2020-06-04 08:02:15 +08:00
|
|
|
check_cpu_quota 500000 1000000 "500ms"
|
|
|
|
check_cpu_shares 100
|
2020-04-01 06:27:51 +08:00
|
|
|
|
2020-06-04 08:02:15 +08:00
|
|
|
# update cpu period
|
|
|
|
runc update test_update --cpu-period 900000
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
check_cpu_quota 500000 900000 "560ms"
|
2020-04-01 06:27:51 +08:00
|
|
|
|
2020-06-04 08:02:15 +08:00
|
|
|
# update cpu quota
|
|
|
|
runc update test_update --cpu-quota 600000
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
check_cpu_quota 600000 900000 "670ms"
|
2020-05-27 02:02:16 +08:00
|
|
|
|
2020-06-09 01:09:18 +08:00
|
|
|
# remove cpu quota
|
|
|
|
runc update test_update --cpu-quota -1
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
check_cpu_quota -1 900000 "infinity"
|
2020-04-01 06:27:51 +08:00
|
|
|
|
|
|
|
# update cpu-shares
|
|
|
|
runc update test_update --cpu-share 200
|
|
|
|
[ "$status" -eq 0 ]
|
2020-06-04 08:02:15 +08:00
|
|
|
check_cpu_shares 200
|
2020-04-01 06:27:51 +08:00
|
|
|
|
|
|
|
# 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 ]
|
2020-06-04 08:02:15 +08:00
|
|
|
check_cpu_quota 500000 1000000 "500ms"
|
2020-04-01 06:27:51 +08:00
|
|
|
|
|
|
|
# redo all the changes at once
|
|
|
|
runc update test_update \
|
|
|
|
--cpu-period 900000 --cpu-quota 600000 --cpu-share 200
|
|
|
|
[ "$status" -eq 0 ]
|
2020-06-04 08:02:15 +08:00
|
|
|
check_cpu_quota 600000 900000 "670ms"
|
|
|
|
check_cpu_shares 200
|
2020-05-20 12:41:41 +08:00
|
|
|
|
2020-06-09 01:09:18 +08:00
|
|
|
# remove cpu quota and reset the period
|
|
|
|
runc update test_update --cpu-quota -1 --cpu-period 100000
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
check_cpu_quota -1 100000 "infinity"
|
2020-04-01 06:27:51 +08:00
|
|
|
|
|
|
|
# reset to initial test value via json file
|
|
|
|
cat << EOF > $BATS_TMPDIR/runc-cgroups-integration-test.json
|
|
|
|
{
|
|
|
|
"cpu": {
|
|
|
|
"shares": 100,
|
|
|
|
"quota": 500000,
|
|
|
|
"period": 1000000
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 22:30:50 +08:00
|
|
|
EOF
|
|
|
|
|
2017-09-15 17:39:35 +08:00
|
|
|
runc update -r $BATS_TMPDIR/runc-cgroups-integration-test.json test_update
|
2016-04-28 22:30:50 +08:00
|
|
|
[ "$status" -eq 0 ]
|
2020-06-04 08:02:15 +08:00
|
|
|
check_cpu_quota 500000 1000000 "500ms"
|
|
|
|
check_cpu_shares 100
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
2016-11-28 16:30:54 +08:00
|
|
|
|
2020-06-09 08:35:02 +08:00
|
|
|
@test "set cpu period with no quota" {
|
|
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
|
|
|
|
|
|
update_config '.linux.resources.cpu |= { "period": 1000000 }' ${BUSYBOX_BUNDLE}
|
|
|
|
|
|
|
|
runc run -d --console-socket $CONSOLE_SOCKET test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
|
|
|
check_cpu_quota -1 1000000 "infinity"
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "set cpu quota with no period" {
|
|
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
|
|
|
|
|
|
update_config '.linux.resources.cpu |= { "quota": 5000 }' ${BUSYBOX_BUNDLE}
|
|
|
|
|
|
|
|
runc run -d --console-socket $CONSOLE_SOCKET test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
check_cpu_quota 5000 100000 "50ms"
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "update cpu period with no previous period/quota set" {
|
|
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
|
|
|
|
|
|
update_config '.linux.resources.cpu |= {}' ${BUSYBOX_BUNDLE}
|
2020-05-20 12:41:41 +08:00
|
|
|
|
2020-06-09 08:35:02 +08:00
|
|
|
runc run -d --console-socket $CONSOLE_SOCKET test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
|
|
|
# update the period alone, no old values were set
|
|
|
|
runc update --cpu-period 50000 test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
check_cpu_quota -1 50000 "infinity"
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "update cpu quota with no previous period/quota set" {
|
|
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
|
|
|
|
|
|
update_config '.linux.resources.cpu |= {}' ${BUSYBOX_BUNDLE}
|
|
|
|
|
|
|
|
runc run -d --console-socket $CONSOLE_SOCKET test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
|
|
|
# update the quota alone, no old values were set
|
|
|
|
runc update --cpu-quota 30000 test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
check_cpu_quota 30000 100000 "300ms"
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
2016-11-28 16:30:54 +08:00
|
|
|
|
|
|
|
@test "update rt period and runtime" {
|
2017-09-15 17:39:35 +08:00
|
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
2020-06-03 15:10:57 +08:00
|
|
|
requires cgroups_v1 cgroups_rt no_systemd
|
|
|
|
|
|
|
|
# By default, "${CGROUP_CPU}/cpu.rt_runtime_us" is set to 0, which inhibits
|
|
|
|
# setting the container's realtimeRuntime. (#2046)
|
|
|
|
#
|
|
|
|
# When $CGROUP_CPU is "/sys/fs/cgroup/cpu,cpuacct/runc-cgroups-integration-test/test-cgroup",
|
|
|
|
# we write the values of /sys/fs/cgroup/cpu,cpuacct/cpu.rt_{period,runtime}_us to:
|
|
|
|
# - sys/fs/cgroup/cpu,cpuacct/runc-cgroups-integration-test/cpu.rt_{period,runtime}_us
|
|
|
|
# - sys/fs/cgroup/cpu,cpuacct/runc-cgroups-integration-test/test-cgroup/cpu.rt_{period,runtime}_us
|
|
|
|
#
|
|
|
|
# Typically period=1000000 runtime=950000 .
|
|
|
|
#
|
|
|
|
# TODO: support systemd
|
|
|
|
mkdir -p "$CGROUP_CPU"
|
|
|
|
local root_period=$(cat "${CGROUP_CPU_BASE_PATH}/cpu.rt_period_us")
|
|
|
|
local root_runtime=$(cat "${CGROUP_CPU_BASE_PATH}/cpu.rt_runtime_us")
|
|
|
|
# the following IFS magic sets dirs=("runc-cgroups-integration-test" "test-cgroup")
|
|
|
|
IFS='/' read -r -a dirs <<< $(echo ${CGROUP_CPU} | sed -e s@^${CGROUP_CPU_BASE_PATH}/@@)
|
|
|
|
for (( i = 0; i < ${#dirs[@]}; i++ )); do
|
|
|
|
local target="$CGROUP_CPU_BASE_PATH"
|
|
|
|
for (( j = 0; j <= i; j++ )); do
|
|
|
|
target="${target}/${dirs[$j]}"
|
|
|
|
done
|
|
|
|
target_period="${target}/cpu.rt_period_us"
|
|
|
|
echo "Writing ${root_period} to ${target_period}"
|
|
|
|
echo "$root_period" > "$target_period"
|
|
|
|
target_runtime="${target}/cpu.rt_runtime_us"
|
|
|
|
echo "Writing ${root_runtime} to ${target_runtime}"
|
|
|
|
echo "$root_runtime" > "$target_runtime"
|
|
|
|
done
|
2016-11-28 16:30:54 +08:00
|
|
|
|
|
|
|
# run a detached busybox
|
|
|
|
runc run -d --console-socket $CONSOLE_SOCKET test_update_rt
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
|
|
|
runc update -r - test_update_rt <<EOF
|
|
|
|
{
|
2020-06-03 15:10:57 +08:00
|
|
|
"cpu": {
|
|
|
|
"realtimeRuntime": 500001
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
check_cgroup_value "cpu.rt_period_us" "$root_period"
|
|
|
|
check_cgroup_value "cpu.rt_runtime_us" 500001
|
|
|
|
|
|
|
|
runc update -r - test_update_rt <<EOF
|
|
|
|
{
|
2016-11-28 16:30:54 +08:00
|
|
|
"cpu": {
|
|
|
|
"realtimePeriod": 800001,
|
|
|
|
"realtimeRuntime": 500001
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "cpu.rt_period_us" 800001
|
|
|
|
check_cgroup_value "cpu.rt_runtime_us" 500001
|
2016-11-28 16:30:54 +08:00
|
|
|
|
|
|
|
runc update test_update_rt --cpu-rt-period 900001 --cpu-rt-runtime 600001
|
|
|
|
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "cpu.rt_period_us" 900001
|
|
|
|
check_cgroup_value "cpu.rt_runtime_us" 600001
|
2016-11-28 16:30:54 +08:00
|
|
|
}
|
2020-05-10 00:41:14 +08:00
|
|
|
|
|
|
|
@test "update devices [minimal transition rules]" {
|
|
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
|
|
# This test currently only makes sense on cgroupv1.
|
|
|
|
requires cgroups_v1
|
|
|
|
|
|
|
|
# Run a basic shell script that tries to write to /dev/null. If "runc
|
|
|
|
# update" makes use of minimal transition rules, updates should not cause
|
|
|
|
# writes to fail at any point.
|
2020-05-25 08:54:13 +08:00
|
|
|
update_config '.process.args |= ["sh", "-c", "while true; do echo >/dev/null; done"]'
|
2020-05-10 00:41:14 +08:00
|
|
|
|
|
|
|
# Set up a temporary console socket and recvtty so we can get the stdio.
|
|
|
|
TMP_RECVTTY_DIR="$(mktemp -d "$BATS_TMPDIR/runc-tmp-recvtty.XXXXXX")"
|
|
|
|
TMP_RECVTTY_PID="$TMP_RECVTTY_DIR/recvtty.pid"
|
|
|
|
TMP_CONSOLE_SOCKET="$TMP_RECVTTY_DIR/console.sock"
|
|
|
|
CONTAINER_OUTPUT="$TMP_RECVTTY_DIR/output"
|
|
|
|
("$RECVTTY" --no-stdin --pid-file "$TMP_RECVTTY_PID" \
|
|
|
|
--mode single "$TMP_CONSOLE_SOCKET" &>"$CONTAINER_OUTPUT" ) &
|
|
|
|
retry 10 0.1 [ -e "$TMP_CONSOLE_SOCKET" ]
|
|
|
|
|
|
|
|
# Run the container in the background.
|
|
|
|
runc run -d --console-socket "$TMP_CONSOLE_SOCKET" test_update
|
|
|
|
cat "$CONTAINER_OUTPUT"
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
|
|
|
# Trigger an update. This update doesn't actually change the device rules,
|
|
|
|
# but it will trigger the devices cgroup code to reapply the current rules.
|
|
|
|
# We trigger the update a few times to make sure we hit the race.
|
|
|
|
for _ in {1..12}
|
|
|
|
do
|
|
|
|
# TODO: Update "runc update" so we can change the device rules.
|
|
|
|
runc update --pids-limit 30 test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
done
|
|
|
|
|
|
|
|
# Kill recvtty.
|
|
|
|
kill -9 "$(<"$TMP_RECVTTY_PID")"
|
|
|
|
|
|
|
|
# There should've been no output from the container.
|
|
|
|
cat "$CONTAINER_OUTPUT"
|
|
|
|
[ -z "$(<"$CONTAINER_OUTPUT")" ]
|
|
|
|
}
|
2020-05-11 13:32:01 +08:00
|
|
|
|
|
|
|
@test "update paused container" {
|
|
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
|
|
requires cgroups_freezer
|
|
|
|
|
|
|
|
# Run the container in the background.
|
|
|
|
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
|
|
|
# Pause the container.
|
|
|
|
runc pause test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
|
|
|
# Trigger an unrelated update.
|
|
|
|
runc update --pids-limit 30 test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
|
|
|
|
# The container should still be paused.
|
|
|
|
testcontainer test_update paused
|
|
|
|
|
|
|
|
# Resume the container.
|
|
|
|
runc resume test_update
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
}
|