github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/tests/integration/events.bats (about) 1 #!/usr/bin/env bats 2 3 load helpers 4 5 function setup() { 6 setup_busybox 7 } 8 9 function teardown() { 10 teardown_bundle 11 } 12 13 # This needs to be placed at the top of the bats file to work around 14 # a shellcheck bug. See <https://github.com/koalaman/shellcheck/issues/2873>. 15 function test_events() { 16 # XXX: currently cgroups require root containers. 17 requires root 18 init_cgroup_paths 19 20 local status interval retry_every=1 21 if [ $# -eq 2 ]; then 22 interval="$1" 23 retry_every="$2" 24 fi 25 26 runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox 27 [ "$status" -eq 0 ] 28 29 # Spawn two subshels: 30 # 1. Event logger that sends stats events to events.log. 31 (__runc events ${interval:+ --interval "$interval"} test_busybox >events.log) & 32 # 2. Waits for an event that includes test_busybox then kills the 33 # test_busybox container which causes the event logger to exit. 34 ( 35 retry 10 "$retry_every" grep -q test_busybox events.log 36 __runc delete -f test_busybox 37 ) & 38 wait # for both subshells to finish 39 40 [ -e events.log ] 41 42 output=$(head -1 events.log) 43 [[ "$output" == [\{]"\"type\""[:]"\"stats\""[,]"\"id\""[:]"\"test_busybox\""[,]* ]] 44 [[ "$output" == *"data"* ]] 45 } 46 47 @test "events --stats" { 48 # XXX: currently cgroups require root containers. 49 requires root 50 init_cgroup_paths 51 52 # run busybox detached 53 runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox 54 [ "$status" -eq 0 ] 55 56 # generate stats 57 runc events --stats test_busybox 58 [ "$status" -eq 0 ] 59 [[ "${lines[0]}" == [\{]"\"type\""[:]"\"stats\""[,]"\"id\""[:]"\"test_busybox\""[,]* ]] 60 [[ "${lines[0]}" == *"data"* ]] 61 } 62 63 @test "events --stats with psi data" { 64 requires root cgroups_v2 psi 65 init_cgroup_paths 66 67 update_config '.linux.resources.cpu |= { "quota": 1000 }' 68 69 runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox 70 [ "$status" -eq 0 ] 71 72 # Stress the CPU a bit. Need something that runs for more than 10s. 73 runc exec test_busybox dd if=/dev/zero bs=1 count=128K of=/dev/null 74 [ "$status" -eq 0 ] 75 76 runc exec test_busybox sh -c 'tail /sys/fs/cgroup/*.pressure' 77 78 runc events --stats test_busybox 79 [ "$status" -eq 0 ] 80 81 # Check PSI metrics. 82 jq '.data.cpu.psi' <<<"${lines[0]}" 83 for psi_type in some full; do 84 for psi_metric in avg10 avg60 avg300 total; do 85 echo -n "checking .data.cpu.psi.$psi_type.$psi_metric != 0: " 86 jq -e '.data.cpu.psi.'$psi_type.$psi_metric' != 0' <<<"${lines[0]}" 87 done 88 done 89 } 90 91 @test "events --interval default" { 92 test_events 93 } 94 95 @test "events --interval 1s" { 96 test_events 1s 1 97 } 98 99 @test "events --interval 100ms" { 100 test_events 100ms 0.1 101 } 102 103 @test "events oom" { 104 # XXX: currently cgroups require root containers. 105 requires root cgroups_swap 106 init_cgroup_paths 107 108 # we need the container to hit OOM, so disable swap 109 update_config '(.. | select(.resources? != null)) .resources.memory |= {"limit": 33554432, "swap": 33554432}' 110 111 # run busybox detached 112 runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox 113 [ "$status" -eq 0 ] 114 115 # spawn two sub processes (shells) 116 # the first sub process is an event logger that sends stats events to events.log 117 # the second sub process exec a memory hog process to cause a oom condition 118 # and waits for an oom event 119 (__runc events test_busybox >events.log) & 120 ( 121 retry 10 1 grep -q test_busybox events.log 122 # shellcheck disable=SC2016 123 __runc exec -d test_busybox sh -c 'test=$(dd if=/dev/urandom ibs=5120k)' 124 retry 30 1 grep -q oom events.log 125 __runc delete -f test_busybox 126 ) & 127 wait # wait for the above sub shells to finish 128 129 grep -q '{"type":"oom","id":"test_busybox"}' events.log 130 }