...

Text file src/github.com/opencontainers/runc/tests/integration/events.bats

Documentation: github.com/opencontainers/runc/tests/integration

     1#!/usr/bin/env bats
     2
     3load helpers
     4
     5function setup() {
     6	setup_busybox
     7}
     8
     9function teardown() {
    10	teardown_bundle
    11}
    12
    13# shellcheck disable=SC2030
    14@test "events --stats" {
    15	# XXX: currently cgroups require root containers.
    16	requires root
    17	init_cgroup_paths
    18
    19	# run busybox detached
    20	runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
    21	[ "$status" -eq 0 ]
    22
    23	# generate stats
    24	runc events --stats test_busybox
    25	[ "$status" -eq 0 ]
    26	[[ "${lines[0]}" == [\{]"\"type\""[:]"\"stats\""[,]"\"id\""[:]"\"test_busybox\""[,]* ]]
    27	[[ "${lines[0]}" == *"data"* ]]
    28}
    29
    30function test_events() {
    31	# XXX: currently cgroups require root containers.
    32	requires root
    33	init_cgroup_paths
    34
    35	local status interval retry_every=1
    36	if [ $# -eq 2 ]; then
    37		interval="$1"
    38		retry_every="$2"
    39	fi
    40
    41	runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
    42	# shellcheck disable=SC2031
    43	[ "$status" -eq 0 ]
    44
    45	# Spawn two subshels:
    46	# 1. Event logger that sends stats events to events.log.
    47	(__runc events ${interval:+ --interval "$interval"} test_busybox >events.log) &
    48	# 2. Waits for an event that includes test_busybox then kills the
    49	#    test_busybox container which causes the event logger to exit.
    50	(
    51		retry 10 "$retry_every" grep -q test_busybox events.log
    52		__runc delete -f test_busybox
    53	) &
    54	wait # for both subshells to finish
    55
    56	[ -e events.log ]
    57
    58	output=$(head -1 events.log)
    59	[[ "$output" == [\{]"\"type\""[:]"\"stats\""[,]"\"id\""[:]"\"test_busybox\""[,]* ]]
    60	[[ "$output" == *"data"* ]]
    61}
    62
    63@test "events --interval default" {
    64	test_events
    65}
    66
    67@test "events --interval 1s" {
    68	test_events 1s 1
    69}
    70
    71@test "events --interval 100ms" {
    72	test_events 100ms 0.1
    73}
    74
    75@test "events oom" {
    76	# XXX: currently cgroups require root containers.
    77	requires root cgroups_swap
    78	init_cgroup_paths
    79
    80	# we need the container to hit OOM, so disable swap
    81	update_config '(.. | select(.resources? != null)) .resources.memory |= {"limit": 33554432, "swap": 33554432}'
    82
    83	# run busybox detached
    84	runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
    85	[ "$status" -eq 0 ]
    86
    87	# spawn two sub processes (shells)
    88	# the first sub process is an event logger that sends stats events to events.log
    89	# the second sub process exec a memory hog process to cause a oom condition
    90	# and waits for an oom event
    91	(__runc events test_busybox >events.log) &
    92	(
    93		retry 10 1 grep -q test_busybox events.log
    94		# shellcheck disable=SC2016
    95		__runc exec -d test_busybox sh -c 'test=$(dd if=/dev/urandom ibs=5120k)'
    96		retry 30 1 grep -q oom events.log
    97		__runc delete -f test_busybox
    98	) &
    99	wait # wait for the above sub shells to finish
   100
   101	grep -q '{"type":"oom","id":"test_busybox"}' events.log
   102}

View as plain text