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
|
|
|
|
DATA=$(cat <<EOF
|
|
|
|
"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
|
|
|
|
)
|
|
|
|
DATA=$(echo ${DATA} | sed 's/\n/\\n/g')
|
|
|
|
sed -i "s/\(\"resources\": {\)/\1\n${DATA}/" ${BUSYBOX_BUNDLE}/config.json
|
|
|
|
}
|
|
|
|
|
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" {
|
2017-09-15 17:39:35 +08:00
|
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
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"
|
|
|
|
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"
|
2020-04-04 08:15:07 +08:00
|
|
|
# checking swap is currently disabled for v2
|
|
|
|
#CGROUP_MEMORY=$CGROUP_PATH
|
2020-04-01 06:27:51 +08:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
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
|
|
|
|
check_cgroup_value $MEM_RESERVE 25165824
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "pids.max" 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-04-24 15:18:26 +08:00
|
|
|
if [[ -n "${RUNC_USE_SYSTEMD}" ]] ; then
|
|
|
|
if [ "$CGROUP_UNIFIED" != "yes" ]; then
|
|
|
|
check_systemd_value "runc-cgroups-integration-test.scope" "MemoryLimit=" "MemoryLimit=67108864"
|
|
|
|
else
|
|
|
|
check_systemd_value "runc-cgroups-integration-test.scope" "MemoryMax=" "MemoryMax=67108864"
|
|
|
|
fi
|
|
|
|
fi
|
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-04-24 15:18:26 +08:00
|
|
|
if [[ -n "${RUNC_USE_SYSTEMD}" ]] ; then
|
|
|
|
if [ "$CGROUP_UNIFIED" != "yes" ]; then
|
|
|
|
check_systemd_value "runc-cgroups-integration-test.scope" "MemoryLimit=" "MemoryLimit=52428800"
|
|
|
|
else
|
|
|
|
check_systemd_value "runc-cgroups-integration-test.scope" "MemoryMax=" "MemoryMax=52428800"
|
|
|
|
fi
|
|
|
|
fi
|
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
|
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-04-01 06:27:51 +08:00
|
|
|
if [ -f "$CGROUP_MEMORY/$MEM_SWAP" ]; 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-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value "$MEM_SWAP" $SYSTEM_MEM_SWAP
|
2016-10-19 18:15:55 +08:00
|
|
|
|
|
|
|
# update memory swap
|
2016-05-09 21:06:42 +08:00
|
|
|
runc update test_update --memory-swap 96468992
|
2016-04-28 22:30:50 +08:00
|
|
|
[ "$status" -eq 0 ]
|
2020-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value "$MEM_SWAP" 96468992
|
|
|
|
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
|
2016-10-19 18:15:55 +08:00
|
|
|
|
|
|
|
# check swap memory limited is gone
|
2020-04-01 06:27:51 +08:00
|
|
|
if [ -f "$CGROUP_MEMORY/$MEM_SWAP" ]; then
|
|
|
|
check_cgroup_value $MEM_SWAP $SYSTEM_MEM
|
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-04-24 15:18:26 +08:00
|
|
|
if [[ -n "${RUNC_USE_SYSTEMD}" ]] ; then
|
|
|
|
check_systemd_value "runc-cgroups-integration-test.scope" "TasksMax=" "TasksMax=10"
|
|
|
|
fi
|
2017-04-27 00:53:20 +08:00
|
|
|
|
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-04-01 06:27:51 +08:00
|
|
|
check_cgroup_value $MEM_LIMIT 33554432
|
|
|
|
check_cgroup_value $MEM_RESERVE 25165824
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "pids.max" 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
|
|
|
|
check_cgroup_value $MEM_RESERVE 33554432
|
2020-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "pids.max" 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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
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-03-26 09:18:06 +08:00
|
|
|
check_cgroup_value "cpu.cfs_period_us" 1000000
|
|
|
|
check_cgroup_value "cpu.cfs_quota_us" 500000
|
|
|
|
check_cgroup_value "cpu.shares" 100
|
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-03-31 05:00:35 +08:00
|
|
|
requires cgroups_rt
|
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
|
|
|
|
{
|
|
|
|
"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
|
|
|
}
|