Skip to content

Commit 14af14d

Browse files
committed
tests/int: simplify using check_cpu_quota
Instead of providing systemd CPU quota value (CPUQuotaPerSec), calculate it based on how opencontainers/cgroups/systemd handles it (see addCPUQuota). Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 8d90e3d commit 14af14d

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

tests/integration/cgroups.bats

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ convert_hugetlb_size() {
328328
check_systemd_value "MemoryHigh" 5242880
329329
check_systemd_value "MemoryMax" 10485760
330330
check_systemd_value "TasksMax" 99
331-
check_cpu_quota 10000 100000 "100ms"
331+
check_cpu_quota 10000 100000
332332
check_cpu_weight 42
333333
}
334334

@@ -390,7 +390,7 @@ convert_hugetlb_size() {
390390
[ "$output" = '42' ]
391391
check_systemd_value "TasksMax" 42
392392

393-
check_cpu_quota 5000 50000 "100ms"
393+
check_cpu_quota 5000 50000
394394

395395
check_cpu_weight 42
396396
}

tests/integration/helpers.bash

+22-3
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,28 @@ function check_systemd_value() {
286286
function check_cpu_quota() {
287287
local quota=$1
288288
local period=$2
289-
local sd_quota=$3
289+
local sd_quota
290+
291+
if [ -v RUNC_USE_SYSTEMD ]; then
292+
if [ "$quota" = "-1" ]; then
293+
sd_quota="infinity"
294+
else
295+
# In systemd world, quota (CPUQuotaPerSec) is measured in ms
296+
# (per second), and systemd rounds it up to 10ms. For example,
297+
# given quota=4000 and period=10000, systemd value is 400ms.
298+
#
299+
# Calculate milliseconds (quota/period * 1000).
300+
# First multiply by 1000 to get milliseconds,
301+
# then add half of period for proper rounding.
302+
local ms=$(((quota * 1000 + period / 2) / period))
303+
# Round up to nearest 10ms.
304+
ms=$(((ms + 5) / 10 * 10))
305+
sd_quota="${ms}ms"
306+
fi
307+
308+
# Systemd values are the same for v1 and v2.
309+
check_systemd_value "CPUQuotaPerSecUSec" "$sd_quota"
310+
fi
290311

291312
if [ -v CGROUP_V2 ]; then
292313
if [ "$quota" = "-1" ]; then
@@ -297,8 +318,6 @@ function check_cpu_quota() {
297318
check_cgroup_value "cpu.cfs_quota_us" "$quota"
298319
check_cgroup_value "cpu.cfs_period_us" "$period"
299320
fi
300-
# systemd values are the same for v1 and v2
301-
check_systemd_value "CPUQuotaPerSecUSec" "$sd_quota"
302321

303322
# CPUQuotaPeriodUSec requires systemd >= v242
304323
[ "$(systemd_version)" -lt 242 ] && return

tests/integration/update.bats

+28-28
Original file line numberDiff line numberDiff line change
@@ -263,26 +263,26 @@ EOF
263263
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
264264
[ "$status" -eq 0 ]
265265

266-
# check that initial values were properly set
267-
check_cpu_quota 500000 1000000 "500ms"
266+
# Check that initial values were properly set.
267+
check_cpu_quota 500000 1000000
268268
check_cpu_shares 100
269269

270-
# update cpu period
270+
# Update cpu period.
271271
runc update test_update --cpu-period 900000
272272
[ "$status" -eq 0 ]
273-
check_cpu_quota 500000 900000 "560ms"
273+
check_cpu_quota 500000 900000
274274

275-
# update cpu quota
275+
# Update cpu quota.
276276
runc update test_update --cpu-quota 600000
277277
[ "$status" -eq 0 ]
278-
check_cpu_quota 600000 900000 "670ms"
278+
check_cpu_quota 600000 900000
279279

280-
# remove cpu quota
280+
# Remove cpu quota.
281281
runc update test_update --cpu-quota -1
282282
[ "$status" -eq 0 ]
283-
check_cpu_quota -1 900000 "infinity"
283+
check_cpu_quota -1 900000
284284

285-
# update cpu-shares
285+
# Update cpu-shares.
286286
runc update test_update --cpu-share 200
287287
[ "$status" -eq 0 ]
288288
check_cpu_shares 200
@@ -298,21 +298,21 @@ EOF
298298
}
299299
EOF
300300
[ "$status" -eq 0 ]
301-
check_cpu_quota 500000 1000000 "500ms"
301+
check_cpu_quota 500000 1000000
302302

303-
# redo all the changes at once
303+
# Redo all the changes at once.
304304
runc update test_update \
305305
--cpu-period 900000 --cpu-quota 600000 --cpu-share 200
306306
[ "$status" -eq 0 ]
307-
check_cpu_quota 600000 900000 "670ms"
307+
check_cpu_quota 600000 900000
308308
check_cpu_shares 200
309309

310-
# remove cpu quota and reset the period
310+
# Remove cpu quota and reset the period.
311311
runc update test_update --cpu-quota -1 --cpu-period 100000
312312
[ "$status" -eq 0 ]
313-
check_cpu_quota -1 100000 "infinity"
313+
check_cpu_quota -1 100000
314314

315-
# reset to initial test value via json file
315+
# Reset to initial test values via json file.
316316
cat <<EOF >"$BATS_RUN_TMPDIR"/runc-cgroups-integration-test.json
317317
{
318318
"cpu": {
@@ -326,7 +326,7 @@ EOF
326326

327327
runc update -r "$BATS_RUN_TMPDIR"/runc-cgroups-integration-test.json test_update
328328
[ "$status" -eq 0 ]
329-
check_cpu_quota 500000 1000000 "500ms"
329+
check_cpu_quota 500000 1000000
330330
check_cpu_shares 100
331331
}
332332

@@ -363,7 +363,7 @@ EOF
363363
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
364364
[ "$status" -eq 0 ]
365365

366-
check_cpu_quota -1 1000000 "infinity"
366+
check_cpu_quota -1 1000000
367367
}
368368

369369
@test "set cpu period with no quota (invalid period)" {
@@ -382,7 +382,7 @@ EOF
382382

383383
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
384384
[ "$status" -eq 0 ]
385-
check_cpu_quota 5000 100000 "50ms"
385+
check_cpu_quota 5000 100000
386386
}
387387

388388
@test "update cpu period with no previous period/quota set" {
@@ -393,10 +393,10 @@ EOF
393393
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
394394
[ "$status" -eq 0 ]
395395

396-
# update the period alone, no old values were set
396+
# Update the period alone, no old values were set.
397397
runc update --cpu-period 50000 test_update
398398
[ "$status" -eq 0 ]
399-
check_cpu_quota -1 50000 "infinity"
399+
check_cpu_quota -1 50000
400400
}
401401

402402
@test "update cpu quota with no previous period/quota set" {
@@ -407,10 +407,10 @@ EOF
407407
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
408408
[ "$status" -eq 0 ]
409409

410-
# update the quota alone, no old values were set
410+
# Update the quota alone, no old values were set.
411411
runc update --cpu-quota 30000 test_update
412412
[ "$status" -eq 0 ]
413-
check_cpu_quota 30000 100000 "300ms"
413+
check_cpu_quota 30000 100000
414414
}
415415

416416
@test "update cpu period in a pod cgroup with pod limit set" {
@@ -445,7 +445,7 @@ EOF
445445
# Finally, the test itself: set 30% limit but with lower period.
446446
runc update --cpu-period 10000 --cpu-quota 3000 test_update
447447
[ "$status" -eq 0 ]
448-
check_cpu_quota 3000 10000 "300ms"
448+
check_cpu_quota 3000 10000
449449
}
450450

451451
@test "update cgroup cpu.idle" {
@@ -545,9 +545,9 @@ EOF
545545
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
546546
[ "$status" -eq 0 ]
547547

548-
# check that initial values were properly set
549-
check_cpu_quota 500000 1000000 "500ms"
550-
# initial cpu shares of 100 corresponds to weight of 4
548+
# Check that initial values were properly set.
549+
check_cpu_quota 500000 1000000
550+
# Initial cpu shares of 100 corresponds to weight of 4.
551551
check_cpu_weight 4
552552
check_systemd_value "TasksMax" 20
553553

@@ -561,8 +561,8 @@ EOF
561561
}
562562
EOF
563563

564-
# check the updated systemd unit properties
565-
check_cpu_quota -1 100000 "infinity"
564+
# Check the updated systemd unit properties.
565+
check_cpu_quota -1 100000
566566
check_cpu_weight 16
567567
check_systemd_value "TasksMax" 10
568568
}

0 commit comments

Comments
 (0)