Files
runc/tests/integration/events.bats
T
Kir Kolyshkin be00ae07c3 ci: shellcheck: update to 0.8.0, fix/suppress new warnings
1. This valid warning is reported by shellcheck v0.8.0:

	In tests/integration/helpers.bash line 38:
	KERNEL_MINOR="${KERNEL_VERSION#$KERNEL_MAJOR.}"
				       ^-----------^ SC2295 (info): Expansions inside ${..} need to be quoted separately, otherwise they match as patterns.

	Did you mean:
	KERNEL_MINOR="${KERNEL_VERSION#"$KERNEL_MAJOR".}"

Fix this.

2. These (invalid) warnings are also reported by the new version:

	In tests/integration/events.bats line 13:
	@test "events --stats" {
	^-- SC2030 (info): Modification of status is local (to subshell caused by @bats test).

	In tests/integration/events.bats line 41:
		[ "$status" -eq 0 ]
		   ^-----^ SC2031 (info): status was modified in a subshell. That change might be lost.

Basically, this is happening because shellcheck do not really track
the call tree and/or local variables. This is a known (and reported)
deficiency, and the alternative to disabling these warnings is moving
the code around, which is worse due to more changes in git history.

So we have to silence/disable these.

3. Update shellcheck to 0.8.0.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2022-02-10 17:24:04 -08:00

103 lines
2.6 KiB
Bash

#!/usr/bin/env bats
load helpers
function setup() {
setup_busybox
}
function teardown() {
teardown_bundle
}
# shellcheck disable=SC2030
@test "events --stats" {
# XXX: currently cgroups require root containers.
requires root
init_cgroup_paths
# run busybox detached
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
[ "$status" -eq 0 ]
# generate stats
runc events --stats test_busybox
[ "$status" -eq 0 ]
[[ "${lines[0]}" == [\{]"\"type\""[:]"\"stats\""[,]"\"id\""[:]"\"test_busybox\""[,]* ]]
[[ "${lines[0]}" == *"data"* ]]
}
function test_events() {
# XXX: currently cgroups require root containers.
requires root
init_cgroup_paths
local status interval retry_every=1
if [ $# -eq 2 ]; then
interval="$1"
retry_every="$2"
fi
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
# shellcheck disable=SC2031
[ "$status" -eq 0 ]
# Spawn two subshels:
# 1. Event logger that sends stats events to events.log.
(__runc events ${interval:+ --interval "$interval"} test_busybox >events.log) &
# 2. Waits for an event that includes test_busybox then kills the
# test_busybox container which causes the event logger to exit.
(
retry 10 "$retry_every" grep -q test_busybox events.log
__runc delete -f test_busybox
) &
wait # for both subshells to finish
[ -e events.log ]
output=$(head -1 events.log)
[[ "$output" == [\{]"\"type\""[:]"\"stats\""[,]"\"id\""[:]"\"test_busybox\""[,]* ]]
[[ "$output" == *"data"* ]]
}
@test "events --interval default" {
test_events
}
@test "events --interval 1s" {
test_events 1s 1
}
@test "events --interval 100ms" {
test_events 100ms 0.1
}
@test "events oom" {
# XXX: currently cgroups require root containers.
requires root cgroups_swap
init_cgroup_paths
# we need the container to hit OOM, so disable swap
update_config '(.. | select(.resources? != null)) .resources.memory |= {"limit": 33554432, "swap": 33554432}'
# run busybox detached
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
[ "$status" -eq 0 ]
# spawn two sub processes (shells)
# the first sub process is an event logger that sends stats events to events.log
# the second sub process exec a memory hog process to cause a oom condition
# and waits for an oom event
(__runc events test_busybox >events.log) &
(
retry 10 1 grep -q test_busybox events.log
# shellcheck disable=SC2016
__runc exec -d test_busybox sh -c 'test=$(dd if=/dev/urandom ibs=5120k)'
retry 10 1 grep -q oom events.log
__runc delete -f test_busybox
) &
wait # wait for the above sub shells to finish
grep -q '{"type":"oom","id":"test_busybox"}' events.log
}