Skip to content

Commit 0f68aa8

Browse files
committed
test: Add regression test for user namespace cgroup delegation
Add integration test to verify that containers joining a user namespace via path still have UID/GID mappings in their OCI spec. This ensures proper cgroup delegation for systemd containers. The test verifies: - User namespace path is set (joining sandbox's userns) - uidMappings and gidMappings are present in config.json - Container can start successfully - Container can access cgroups This prevents regression of the issue fixed in the previous commit where missing mappings caused systemd containers to fail with "Permission denied" when creating cgroups. Regression test for: #9705 Signed-off-by: Sascha Grunert <[email protected]>
1 parent e826ac1 commit 0f68aa8

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

test/cgroups.bats

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,68 @@ EOF
249249
output=$(crictl inspect "$ctr_id" | jq -r '.status.state')
250250
[[ "$output" == "CONTAINER_RUNNING" ]]
251251
}
252+
253+
@test "user namespace containers include UID/GID mappings for cgroup delegation" {
254+
if test -n "$CONTAINER_UID_MAPPINGS"; then
255+
skip "userNS already enabled globally"
256+
fi
257+
if ! is_cgroup_v2; then
258+
skip "test requires cgroup v2"
259+
fi
260+
261+
start_crio
262+
263+
# Create a pod with user namespace enabled (hostUsers: false)
264+
jq ' .linux.security_context.namespace_options.userns_options = {
265+
"mode": 0,
266+
"uids": [{
267+
"host_id": 100000,
268+
"container_id": 0,
269+
"length": 65536
270+
}],
271+
"gids": [{
272+
"host_id": 100000,
273+
"container_id": 0,
274+
"length": 65536
275+
}]
276+
}' "$TESTDATA"/sandbox_config.json > "$TESTDIR"/sandbox_userns.json
277+
278+
pod_id=$(crictl runp "$TESTDIR"/sandbox_userns.json)
279+
280+
# Create a container in the user namespace pod
281+
ctr_id=$(crictl create "$pod_id" "$TESTDATA"/container_sleep.json "$TESTDIR"/sandbox_userns.json)
282+
283+
# Start the container and verify it can run successfully
284+
# Without proper UID/GID mappings, systemd containers would fail to create cgroups
285+
crictl start "$ctr_id"
286+
287+
output=$(crictl inspect "$ctr_id" | jq -r '.status.state')
288+
[[ "$output" == "CONTAINER_RUNNING" ]]
289+
290+
# Get container info including the runtime spec
291+
container_info=$(crictl inspect --output json "$ctr_id")
292+
293+
# Verify that BOTH user namespace path AND uidMappings/gidMappings are present
294+
# in the container's runtime information
295+
296+
# Check that user namespace has a path (joining the sandbox's userns)
297+
user_ns_path=$(echo "$container_info" | jq -r '.info.runtimeSpec.linux.namespaces[] | select(.type == "user") | .path')
298+
[[ -n "$user_ns_path" ]]
299+
300+
# Check that uidMappings and gidMappings are present in the spec
301+
# These are required for proper cgroup delegation even when joining an existing userns
302+
uid_mappings=$(echo "$container_info" | jq -r '.info.runtimeSpec.linux.uidMappings')
303+
gid_mappings=$(echo "$container_info" | jq -r '.info.runtimeSpec.linux.gidMappings')
304+
[[ "$uid_mappings" != "null" ]]
305+
[[ "$gid_mappings" != "null" ]]
306+
307+
# Verify the mappings contain the expected values
308+
uid_host_id=$(echo "$container_info" | jq -r '.info.runtimeSpec.linux.uidMappings[0].hostID')
309+
gid_host_id=$(echo "$container_info" | jq -r '.info.runtimeSpec.linux.gidMappings[0].hostID')
310+
[[ "$uid_host_id" == "100000" ]]
311+
[[ "$gid_host_id" == "100000" ]]
312+
313+
# Verify the container can access its cgroup (regression test for #9705)
314+
# The container should be able to read its cgroup files
315+
crictl exec --sync "$ctr_id" cat /proc/self/cgroup
316+
}

0 commit comments

Comments
 (0)