Skip to content

Commit a4b0857

Browse files
authored
Merge pull request #4188 from kolyshkin/ci-swap
tests/int: swap-related fixes
2 parents 8fc5be4 + 3083bd4 commit a4b0857

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

libcontainer/cgroups/fs2/memory.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ func setMemory(dirPath string, r *configs.Resources) error {
5757
// never write empty string to `memory.swap.max`, it means set to 0.
5858
if swapStr != "" {
5959
if err := cgroups.WriteFile(dirPath, "memory.swap.max", swapStr); err != nil {
60-
return err
60+
// If swap is not enabled, silently ignore setting to max or disabling it.
61+
if !(errors.Is(err, os.ErrNotExist) && (swapStr == "max" || swapStr == "0")) {
62+
return err
63+
}
6164
}
6265
}
6366

tests/integration/cgroups.bats

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ convert_hugetlb_size() {
272272
"memory.low": "524288",
273273
"memory.high": "5242880",
274274
"memory.max": "20484096",
275-
"memory.swap.max": "20971520",
276275
"pids.max": "99",
277276
"cpu.max": "10000 100000",
278277
"cpu.weight": "42"
@@ -289,20 +288,41 @@ convert_hugetlb_size() {
289288
echo "$output" | grep -q '^memory.low:524288$'
290289
echo "$output" | grep -q '^memory.high:5242880$'
291290
echo "$output" | grep -q '^memory.max:20484096$'
292-
echo "$output" | grep -q '^memory.swap.max:20971520$'
293291
echo "$output" | grep -q '^pids.max:99$'
294292
echo "$output" | grep -q '^cpu.max:10000 100000$'
295293

296294
check_systemd_value "MemoryMin" 131072
297295
check_systemd_value "MemoryLow" 524288
298296
check_systemd_value "MemoryHigh" 5242880
299297
check_systemd_value "MemoryMax" 20484096
300-
check_systemd_value "MemorySwapMax" 20971520
301298
check_systemd_value "TasksMax" 99
302299
check_cpu_quota 10000 100000 "100ms"
303300
check_cpu_weight 42
304301
}
305302

303+
@test "runc run (cgroup v2 resources.unified swap)" {
304+
requires root cgroups_v2 cgroups_swap
305+
306+
set_cgroups_path
307+
update_config ' .linux.resources.unified |= {
308+
"memory.max": "20484096",
309+
"memory.swap.max": "20971520"
310+
}'
311+
312+
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_unified
313+
[ "$status" -eq 0 ]
314+
315+
runc exec test_cgroups_unified sh -c 'cd /sys/fs/cgroup && grep . *.max'
316+
[ "$status" -eq 0 ]
317+
echo "$output"
318+
319+
echo "$output" | grep -q '^memory.max:20484096$'
320+
echo "$output" | grep -q '^memory.swap.max:20971520$'
321+
322+
check_systemd_value "MemoryMax" 20484096
323+
check_systemd_value "MemorySwapMax" 20971520
324+
}
325+
306326
@test "runc run (cgroup v2 resources.unified override)" {
307327
requires root cgroups_v2
308328

tests/integration/helpers.bash

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function init_cgroup_paths() {
121121
CGROUP_BASE_PATH=/sys/fs/cgroup
122122

123123
# Find any cgroup.freeze files...
124-
if [ -n "$(find "$CGROUP_BASE_PATH" -type f -name "cgroup.freeze" -print -quit)" ]; then
124+
if [ -n "$(find "$CGROUP_BASE_PATH" -maxdepth 2 -type f -name "cgroup.freeze" -print -quit)" ]; then
125125
CGROUP_SUBSYSTEMS+=" freezer"
126126
fi
127127
else
@@ -451,16 +451,22 @@ function requires() {
451451
;;
452452
cgroups_swap)
453453
init_cgroup_paths
454-
if [ -v CGROUP_V1 ] && [ ! -e "${CGROUP_MEMORY_BASE_PATH}/memory.memsw.limit_in_bytes" ]; then
455-
skip_me=1
454+
if [ -v CGROUP_V1 ]; then
455+
if [ ! -e "${CGROUP_MEMORY_BASE_PATH}/memory.memsw.limit_in_bytes" ]; then
456+
skip_me=1
457+
fi
458+
elif [ -v CGROUP_V2 ]; then
459+
if [ -z "$(find "$CGROUP_BASE_PATH" -maxdepth 2 -type f -name memory.swap.max -print -quit)" ]; then
460+
skip_me=1
461+
fi
456462
fi
457463
;;
458464
cgroups_cpu_idle)
459465
local p
460466
init_cgroup_paths
461467
[ -v CGROUP_V1 ] && p="$CGROUP_CPU_BASE_PATH"
462468
[ -v CGROUP_V2 ] && p="$CGROUP_BASE_PATH"
463-
if [ -z "$(find "$p" -name cpu.idle -print -quit)" ]; then
469+
if [ -z "$(find "$p" -maxdepth 2 -type f -name cpu.idle -print -quit)" ]; then
464470
skip_me=1
465471
fi
466472
;;
@@ -476,7 +482,7 @@ function requires() {
476482
p="$CGROUP_BASE_PATH"
477483
f="cpu.max.burst"
478484
fi
479-
if [ -z "$(find "$p" -name "$f" -print -quit)" ]; then
485+
if [ -z "$(find "$p" -maxdepth 2 -type f -name "$f" -print -quit)" ]; then
480486
skip_me=1
481487
fi
482488
;;

tests/integration/update.bats

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ function setup() {
3838
MEM_SWAP="memory.memsw.limit_in_bytes"
3939
SD_MEM_SWAP="unsupported"
4040
SYSTEM_MEM=$(cat "${CGROUP_MEMORY_BASE_PATH}/${MEM_LIMIT}")
41-
HAVE_SWAP="no"
42-
if [ -f "${CGROUP_MEMORY_BASE_PATH}/${MEM_SWAP}" ]; then
43-
HAVE_SWAP="yes"
44-
fi
4541
else
4642
MEM_LIMIT="memory.max"
4743
SD_MEM_LIMIT="MemoryMax"
@@ -50,9 +46,11 @@ function setup() {
5046
MEM_SWAP="memory.swap.max"
5147
SD_MEM_SWAP="MemorySwapMax"
5248
SYSTEM_MEM="max"
53-
HAVE_SWAP="yes"
5449
fi
5550

51+
unset HAVE_SWAP
52+
get_cgroup_value $MEM_SWAP >/dev/null && HAVE_SWAP=yes
53+
5654
SD_UNLIMITED="infinity"
5755
SD_VERSION=$(systemctl --version | awk '{print $2; exit}')
5856
if [ "$SD_VERSION" -lt 227 ]; then
@@ -94,8 +92,8 @@ function setup() {
9492
check_cgroup_value "$MEM_RESERVE" 33554432
9593
check_systemd_value "$SD_MEM_RESERVE" 33554432
9694

97-
# Run swap memory tests if swap is available
98-
if [ "$HAVE_SWAP" = "yes" ]; then
95+
# Run swap memory tests if swap is available.
96+
if [ -v HAVE_SWAP ]; then
9997
# try to remove memory swap limit
10098
runc update test_update --memory-swap -1
10199
[ "$status" -eq 0 ]
@@ -127,7 +125,7 @@ function setup() {
127125
check_systemd_value "$SD_MEM_LIMIT" "$SD_UNLIMITED"
128126

129127
# check swap memory limited is gone
130-
if [ "$HAVE_SWAP" = "yes" ]; then
128+
if [ -v HAVE_SWAP ]; then
131129
check_cgroup_value "$MEM_SWAP" "$SYSTEM_MEM"
132130
check_systemd_value "$SD_MEM_SWAP" "$SD_UNLIMITED"
133131
fi
@@ -221,7 +219,7 @@ EOF
221219
check_cgroup_value "pids.max" 20
222220
check_systemd_value "TasksMax" 20
223221

224-
if [ "$HAVE_SWAP" = "yes" ]; then
222+
if [ -v HAVE_SWAP ]; then
225223
# Test case for https://github.com/opencontainers/runc/pull/592,
226224
# checking libcontainer/cgroups/fs/memory.go:setMemoryAndSwap.
227225

0 commit comments

Comments
 (0)