...
1#!/usr/bin/env bats
2
3load helpers
4
5function setup() {
6 setup_busybox
7}
8
9function teardown() {
10 teardown_bundle
11}
12
13@test "runc delete" {
14 # Need a permission to create a cgroup.
15 # XXX(@kolyshkin): currently this test does not handle rootless when
16 # fs cgroup driver is used, because in this case cgroup (with a
17 # predefined name) is created by tests/rootless.sh, not by runc.
18 [[ "$ROOTLESS" -ne 0 ]] && requires systemd
19 set_resources_limit
20
21 runc run -d --console-socket "$CONSOLE_SOCKET" testbusyboxdelete
22 [ "$status" -eq 0 ]
23
24 testcontainer testbusyboxdelete running
25 # Ensure the find statement used later is correct.
26 output=$(find /sys/fs/cgroup -name testbusyboxdelete -o -name \*-testbusyboxdelete.scope 2>/dev/null || true)
27 if [ -z "$output" ]; then
28 fail "expected cgroup not found"
29 fi
30
31 runc kill testbusyboxdelete KILL
32 [ "$status" -eq 0 ]
33 wait_for_container 10 1 testbusyboxdelete stopped
34
35 runc delete testbusyboxdelete
36 [ "$status" -eq 0 ]
37
38 runc state testbusyboxdelete
39 [ "$status" -ne 0 ]
40
41 output=$(find /sys/fs/cgroup -name testbusyboxdelete -o -name \*-testbusyboxdelete.scope 2>/dev/null || true)
42 [ "$output" = "" ] || fail "cgroup not cleaned up correctly: $output"
43}
44
45@test "runc delete --force" {
46 # run busybox detached
47 runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
48 [ "$status" -eq 0 ]
49
50 # check state
51 testcontainer test_busybox running
52
53 # force delete test_busybox
54 runc delete --force test_busybox
55
56 runc state test_busybox
57 [ "$status" -ne 0 ]
58}
59
60@test "runc delete --force ignore not exist" {
61 runc delete --force notexists
62 [ "$status" -eq 0 ]
63}
64
65@test "runc delete --force [paused container]" {
66 runc run -d --console-socket "$CONSOLE_SOCKET" ct1
67 [ "$status" -eq 0 ]
68 testcontainer ct1 running
69
70 runc pause ct1
71 runc delete --force ct1
72 [ "$status" -eq 0 ]
73}
74
75@test "runc delete --force in cgroupv1 with subcgroups" {
76 requires cgroups_v1 root cgroupns
77 set_cgroups_path
78 set_cgroup_mount_writable
79 # enable cgroupns
80 update_config '.linux.namespaces += [{"type": "cgroup"}]'
81
82 local subsystems="memory freezer"
83
84 runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
85 [ "$status" -eq 0 ]
86
87 testcontainer test_busybox running
88
89 __runc exec -d test_busybox sleep 1d
90
91 # find the pid of sleep
92 pid=$(__runc exec test_busybox ps -a | grep 1d | awk '{print $1}')
93 [[ ${pid} =~ [0-9]+ ]]
94
95 # create a sub-cgroup
96 cat <<EOF | runc exec test_busybox sh
97set -e -u -x
98for s in ${subsystems}; do
99 cd /sys/fs/cgroup/\$s
100 mkdir foo
101 cd foo
102 echo ${pid} > tasks
103 cat tasks
104done
105EOF
106 [ "$status" -eq 0 ]
107 [[ "$output" =~ [0-9]+ ]]
108
109 for s in ${subsystems}; do
110 name=CGROUP_${s^^}_BASE_PATH
111 eval path=\$"${name}${REL_CGROUPS_PATH}/foo"
112 # shellcheck disable=SC2154
113 [ -d "${path}" ] || fail "test failed to create memory sub-cgroup ($path not found)"
114 done
115
116 runc delete --force test_busybox
117
118 runc state test_busybox
119 [ "$status" -ne 0 ]
120
121 output=$(find /sys/fs/cgroup -wholename '*testbusyboxdelete*' -type d 2>/dev/null || true)
122 [ "$output" = "" ] || fail "cgroup not cleaned up correctly: $output"
123}
124
125@test "runc delete --force in cgroupv2 with subcgroups" {
126 requires cgroups_v2 root
127 set_cgroups_path
128 set_cgroup_mount_writable
129
130 # run busybox detached
131 runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
132 [ "$status" -eq 0 ]
133
134 # check state
135 testcontainer test_busybox running
136
137 # create a sub process
138 __runc exec -d test_busybox sleep 1d
139
140 # find the pid of sleep
141 pid=$(__runc exec test_busybox ps -a | grep 1d | awk '{print $1}')
142 [[ ${pid} =~ [0-9]+ ]]
143
144 # create subcgroups
145 cat <<EOF >nest.sh
146 set -e -u -x
147 cd /sys/fs/cgroup
148 echo +pids > cgroup.subtree_control
149 mkdir foo
150 cd foo
151 echo threaded > cgroup.type
152 echo ${pid} > cgroup.threads
153 cat cgroup.threads
154EOF
155 runc exec test_busybox sh <nest.sh
156 [ "$status" -eq 0 ]
157 [[ "$output" =~ [0-9]+ ]]
158
159 # check create subcgroups success
160 [ -d "$CGROUP_PATH"/foo ]
161
162 # force delete test_busybox
163 runc delete --force test_busybox
164
165 runc state test_busybox
166 [ "$status" -ne 0 ]
167
168 # check delete subcgroups success
169 [ ! -d "$CGROUP_PATH"/foo ]
170}
171
172@test "runc delete removes failed systemd unit" {
173 requires systemd_v244 # Older systemd lacks RuntimeMaxSec support.
174
175 set_cgroups_path
176 # shellcheck disable=SC2016
177 update_config ' .annotations += {
178 "org.systemd.property.RuntimeMaxSec": "2",
179 "org.systemd.property.TimeoutStopSec": "1"
180 }
181 | .process.args |= ["/bin/sleep", "10"]'
182
183 runc run -d --console-socket "$CONSOLE_SOCKET" test-failed-unit
184 [ "$status" -eq 0 ]
185
186 wait_for_container 10 1 test-failed-unit stopped
187
188 local user=""
189 [ $EUID -ne 0 ] && user="--user"
190
191 # Expect "unit is not active" exit code.
192 run -3 systemctl status $user "$SD_UNIT_NAME"
193
194 runc delete test-failed-unit
195 # Expect "no such unit" exit code.
196 run -4 systemctl status $user "$SD_UNIT_NAME"
197}
View as plain text