2016-05-05 10:12:25 +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_cgroups_kmem
2017-09-15 17:39:35 +08:00
teardown_running_container test_cgroups_permissions
2016-05-05 10:12:25 +08:00
teardown_busybox
}
function setup() {
teardown
setup_busybox
}
2020-03-31 05:00:35 +08:00
@test "runc update --kernel-memory{,-tcp} (initialized)" {
2017-09-15 17:39:35 +08:00
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
requires cgroups_kmem
2016-05-10 20:22:13 +08:00
2017-09-15 17:39:35 +08:00
set_cgroups_path "$BUSYBOX_BUNDLE"
2016-05-05 10:12:25 +08:00
# Set some initial known values
DATA=$(cat <<-EOF
"memory": {
2020-03-31 05:00:35 +08:00
"kernel": 16777216,
"kernelTCP": 11534336
2016-05-05 10:12:25 +08:00
},
EOF
)
DATA=$(echo ${DATA} | sed 's/\n/\\n/g')
sed -i "s/\(\"resources\": {\)/\1\n${DATA}/" ${BUSYBOX_BUNDLE}/config.json
2016-05-14 06:49:45 +08:00
# run a detached busybox to work with
2016-09-06 20:40:01 +08:00
runc run -d --console-socket $CONSOLE_SOCKET test_cgroups_kmem
2016-05-05 10:12:25 +08:00
[ "$status" -eq 0 ]
2020-03-26 09:18:06 +08:00
check_cgroup_value "memory.kmem.limit_in_bytes" 16777216
2020-03-31 05:00:35 +08:00
check_cgroup_value "memory.kmem.tcp.limit_in_bytes" 11534336
2020-03-26 09:18:06 +08:00
2016-05-05 10:12:25 +08:00
# update kernel memory limit
2016-05-09 21:06:42 +08:00
runc update test_cgroups_kmem --kernel-memory 50331648
2016-05-05 10:12:25 +08:00
[ "$status" -eq 0 ]
2020-03-26 09:18:06 +08:00
check_cgroup_value "memory.kmem.limit_in_bytes" 50331648
2020-03-31 05:00:35 +08:00
# update kernel memory tcp limit
runc update test_cgroups_kmem --kernel-memory-tcp 41943040
[ "$status" -eq 0 ]
check_cgroup_value "memory.kmem.tcp.limit_in_bytes" 41943040
2016-05-05 10:12:25 +08:00
}
2016-05-11 14:17:32 +08:00
@test "runc update --kernel-memory (uninitialized)" {
2017-09-15 17:39:35 +08:00
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
requires cgroups_kmem
2016-05-10 20:22:13 +08:00
2017-09-15 17:39:35 +08:00
set_cgroups_path "$BUSYBOX_BUNDLE"
2016-05-05 10:12:25 +08:00
2016-05-14 06:49:45 +08:00
# run a detached busybox to work with
2016-09-06 20:40:01 +08:00
runc run -d --console-socket $CONSOLE_SOCKET test_cgroups_kmem
2016-05-05 10:12:25 +08:00
[ "$status" -eq 0 ]
# update kernel memory limit
2016-05-09 21:06:42 +08:00
runc update test_cgroups_kmem --kernel-memory 50331648
2016-05-18 08:51:02 +08:00
# Since kernel 4.6, we can update kernel memory without initialization
# because it's accounted by default.
if [ "$KERNEL_MAJOR" -lt 4 ] || [ "$KERNEL_MAJOR" -eq 4 -a "$KERNEL_MINOR" -le 5 ]; then
[ ! "$status" -eq 0 ]
else
[ "$status" -eq 0 ]
2020-03-26 09:18:06 +08:00
check_cgroup_value "memory.kmem.limit_in_bytes" 50331648
2016-05-18 08:51:02 +08:00
fi
2016-05-05 10:12:25 +08:00
}
2017-09-15 17:39:35 +08:00
@test "runc create (no limits + no cgrouppath + no permission) succeeds" {
runc run -d --console-socket $CONSOLE_SOCKET test_cgroups_permissions
[ "$status" -eq 0 ]
}
@test "runc create (rootless + no limits + cgrouppath + no permission) fails with permission error" {
requires rootless
requires rootless_no_cgroup
2020-04-01 09:47:06 +08:00
# systemd controls the permission, so error does not happen
requires no_systemd
2017-09-15 17:39:35 +08:00
set_cgroups_path "$BUSYBOX_BUNDLE"
runc run -d --console-socket $CONSOLE_SOCKET test_cgroups_permissions
[ "$status" -eq 1 ]
[[ ${lines[1]} == *"permission denied"* ]]
}
@test "runc create (rootless + limits + no cgrouppath + no permission) fails with informative error" {
requires rootless
requires rootless_no_cgroup
2020-04-01 09:47:06 +08:00
# systemd controls the permission, so error does not happen
requires no_systemd
2017-09-15 17:39:35 +08:00
set_resources_limit "$BUSYBOX_BUNDLE"
runc run -d --console-socket $CONSOLE_SOCKET test_cgroups_permissions
[ "$status" -eq 1 ]
2020-04-21 23:19:36 +08:00
[[ ${lines[1]} == *"rootless needs no limits + no cgrouppath when no permission is granted for cgroups"* ]] || [[ ${lines[1]} == *"cannot set pids limit: container could not join or create cgroup"* ]]
2017-09-15 17:39:35 +08:00
}
@test "runc create (limits + cgrouppath + permission on the cgroup dir) succeeds" {
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
set_cgroups_path "$BUSYBOX_BUNDLE"
set_resources_limit "$BUSYBOX_BUNDLE"
runc run -d --console-socket $CONSOLE_SOCKET test_cgroups_permissions
[ "$status" -eq 0 ]
}
@test "runc exec (limits + cgrouppath + permission on the cgroup dir) succeeds" {
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
set_cgroups_path "$BUSYBOX_BUNDLE"
set_resources_limit "$BUSYBOX_BUNDLE"
runc run -d --console-socket $CONSOLE_SOCKET test_cgroups_permissions
[ "$status" -eq 0 ]
runc exec test_cgroups_permissions echo "cgroups_exec"
[ "$status" -eq 0 ]
[[ ${lines[0]} == *"cgroups_exec"* ]]
}