Skip to content

Commit e7776d2

Browse files
committed
test(features): add the remaining CDI scenarios
Add the scenarios for read-only and read-write mounts, library parent derivation, a device node, and a unix socket, with one test per scenario in the cdi suite. Each test delegates to the scenario wrapper, which checks the workload against the expectations recorded in the spec. Add the fixture machinery the scenarios need: a pipe fixture type, a host device node that a systemd service creates and labels on every boot, and the echo socket, which a systemd service serves on every boot. The node has the SELinux type of /dev/null so that a rootless runtime, which bind-mounts it, can use it. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 0ff81fa commit e7776d2

4 files changed

Lines changed: 379 additions & 8 deletions

File tree

‎tests/suites/features/cdi/tests/injection.rs‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,70 @@ use testkit::Context;
1919
async fn mount_under_read_only_baseline(cx: &mut Context) -> testkit::Result {
2020
cdi::scenario(cx, "mount-under-read-only-baseline").await
2121
}
22+
23+
/// A read-only file mounted outside any directory the sandbox baseline
24+
/// grants. The spec's expectations record whether the workload can read it.
25+
#[testkit::test]
26+
async fn read_only_file(cx: &mut Context) -> testkit::Result {
27+
cdi::scenario(cx, "read-only-file").await
28+
}
29+
30+
/// A read-only directory mounted outside the baseline grants. The
31+
/// expectations record whether its marker is readable and whether files can be
32+
/// created in it.
33+
#[testkit::test]
34+
async fn read_only_directory(cx: &mut Context) -> testkit::Result {
35+
cdi::scenario(cx, "read-only-directory").await
36+
}
37+
38+
/// A mount named like a shared library, `libfixture.so.1`, under a directory of
39+
/// its own. The expectations record whether the library's parent directory is
40+
/// granted for the workload.
41+
#[testkit::test]
42+
async fn library_parent(cx: &mut Context) -> testkit::Result {
43+
cdi::scenario(cx, "library-parent").await
44+
}
45+
46+
/// A mount named like a shared library directly under `/`. Granting its parent
47+
/// would grant the whole root, so the expectations record how that is handled.
48+
#[testkit::test]
49+
async fn broad_library_parent(cx: &mut Context) -> testkit::Result {
50+
cdi::scenario(cx, "broad-library-parent").await
51+
}
52+
53+
/// A file the CDI spec mounts read-write. The expectations record whether the
54+
/// workload can read or write it.
55+
#[testkit::test]
56+
async fn writable_file(cx: &mut Context) -> testkit::Result {
57+
cdi::scenario(cx, "writable-file").await
58+
}
59+
60+
/// A directory the CDI spec mounts read-write. The expectations record whether
61+
/// the workload can create files in it.
62+
#[testkit::test]
63+
async fn writable_directory(cx: &mut Context) -> testkit::Result {
64+
cdi::scenario(cx, "writable-directory").await
65+
}
66+
67+
/// A bind mount whose options omit both `ro` and `rw`, so the access it grants
68+
/// is implicit. The expectations record how the workload can use it.
69+
#[testkit::test]
70+
async fn omitted_access_file(cx: &mut Context) -> testkit::Result {
71+
cdi::scenario(cx, "omitted-access-file").await
72+
}
73+
74+
/// A character device node injected from `/dev/null` with read-write
75+
/// permissions. The expectations record whether a non-root workload can write
76+
/// it.
77+
#[testkit::test]
78+
async fn device_node(cx: &mut Context) -> testkit::Result {
79+
cdi::scenario(cx, "device-node").await
80+
}
81+
82+
/// A unix socket mounted on its own, without the directory it lives in. The
83+
/// expectations record that a sibling file in the host directory stays
84+
/// invisible.
85+
#[testkit::test]
86+
async fn unix_socket(cx: &mut Context) -> testkit::Result {
87+
cdi::scenario(cx, "unix-socket").await
88+
}

‎tests/tmachine/ansible/roles/cdi_fixtures/tasks/scenarios.yaml‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,153 @@
4545
loop_control:
4646
label: "{{ item.path }}"
4747

48+
- name: Create scenario fixture named pipes
49+
become: true
50+
ansible.builtin.command:
51+
argv: [mkfifo, "--mode={{ item.mode }}", "{{ cdi_scenario_dir }}/{{ item.path }}"]
52+
creates: "{{ cdi_scenario_dir }}/{{ item.path }}"
53+
loop: "{{ cdi_scenario_fixtures | selectattr('type', 'eq', 'fifo') | list }}"
54+
loop_control:
55+
label: "{{ item.path }}"
56+
57+
- name: Set scenario fixture pipe ownership
58+
become: true
59+
ansible.builtin.file:
60+
path: "{{ cdi_scenario_dir }}/{{ item.path }}"
61+
owner: "{{ item.owner | default('root') }}"
62+
group: "{{ item.group | default('root') }}"
63+
mode: "{{ item.mode }}"
64+
loop: "{{ cdi_scenario_fixtures | selectattr('type', 'eq', 'fifo') | list }}"
65+
loop_control:
66+
label: "{{ item.path }}"
67+
68+
# A service that creates the socket, not a socket unit: on SELinux hosts systemd
69+
# labels a socket it creates from the policy's default for the path, which
70+
# denies /var/lib.
71+
# A process creates a file with its directory's type, so the socket the service
72+
# creates inherits this one, and does so again on every boot. On SELinux hosts
73+
# (Fedora) a container cannot stat a socket of the default /usr/local type. The
74+
# file module ignores the type where SELinux is not enabled.
75+
- name: Label the socket fixture directory for containers
76+
become: true
77+
ansible.builtin.file:
78+
path: "{{ cdi_scenario_dir }}/socket"
79+
state: directory
80+
setype: container_file_t
81+
82+
- name: Check for systemd-socket-activate
83+
ansible.builtin.stat:
84+
path: /usr/bin/systemd-socket-activate
85+
register: cdi_socket_activate
86+
87+
- name: Require systemd-socket-activate for the socket fixture
88+
ansible.builtin.assert:
89+
that: [cdi_socket_activate.stat.exists]
90+
fail_msg: systemd-socket-activate is not installed; the socket fixture needs it
91+
92+
- name: Install the socket fixture unit
93+
become: true
94+
ansible.builtin.template:
95+
src: openshell-cdi-echo.service.j2
96+
dest: /etc/systemd/system/openshell-cdi-echo.service
97+
owner: root
98+
group: root
99+
mode: "0644"
100+
101+
- name: Start the persistent socket fixture
102+
block:
103+
- name: Start the socket fixture service
104+
become: true
105+
ansible.builtin.systemd_service:
106+
name: openshell-cdi-echo.service
107+
daemon_reload: true
108+
enabled: true
109+
state: restarted
110+
111+
- name: Wait for the socket fixture
112+
become: true
113+
ansible.builtin.wait_for:
114+
path: "{{ cdi_scenario_dir }}/socket/echo.sock"
115+
state: present
116+
timeout: 15
117+
118+
# Connect as an unprivileged user, as a sandbox workload would. A fixture
119+
# that exists but does not echo would otherwise pass the scenario, which
120+
# only checks the socket's file type.
121+
- name: Check that the socket fixture echoes
122+
ansible.builtin.command:
123+
argv:
124+
- "{{ ansible_python_interpreter | default('/usr/bin/python3') }}"
125+
- -c
126+
- |
127+
import socket, sys
128+
payload = b"openshell-cdi-echo\n"
129+
s = socket.socket(socket.AF_UNIX)
130+
s.settimeout(5)
131+
s.connect(sys.argv[1])
132+
s.sendall(payload)
133+
s.shutdown(socket.SHUT_WR)
134+
data = b""
135+
while True:
136+
chunk = s.recv(1024)
137+
if not chunk:
138+
break
139+
data += chunk
140+
sys.exit(0 if data == payload else 1)
141+
- "{{ cdi_scenario_dir }}/socket/echo.sock"
142+
changed_when: false
143+
144+
rescue:
145+
- name: Collect socket fixture diagnostics
146+
become: true
147+
ansible.builtin.shell: |
148+
systemctl status openshell-cdi-echo.service --no-pager -l
149+
journalctl -u openshell-cdi-echo.service --no-pager -n 40
150+
ls -lZd {{ cdi_scenario_dir }}/socket
151+
(ausearch -m avc,user_avc -ts recent 2>&1 || true) | tail -20
152+
changed_when: false
153+
failed_when: false
154+
register: cdi_socket_diagnostics
155+
156+
- name: Report why the socket fixture did not start
157+
ansible.builtin.fail:
158+
msg: "openshell-cdi-echo.service did not start:\n{{ cdi_socket_diagnostics.stdout }}\n{{ cdi_socket_diagnostics.stderr }}"
159+
160+
# The device-node scenario injects /dev/tmachine-cdi-null, a copy of /dev/null,
161+
# from a host node at the same path. A rootless runtime cannot create nodes, so
162+
# it bind-mounts the host node, and the container sees the host node's SELinux
163+
# type: it needs the type of /dev/null, which containers may use. /dev is
164+
# recreated on every boot, so a service creates and labels the node each time.
165+
# The label is ignored where SELinux is not enabled.
166+
- name: Install the host device node unit
167+
become: true
168+
ansible.builtin.copy:
169+
dest: /etc/systemd/system/openshell-cdi-device.service
170+
content: |
171+
[Unit]
172+
Description=OpenShell tmachine CDI host device node fixture
173+
174+
[Service]
175+
Type=oneshot
176+
RemainAfterExit=yes
177+
ExecStart=/bin/rm -f /dev/tmachine-cdi-null
178+
ExecStart=/usr/bin/mknod --mode=0666 /dev/tmachine-cdi-null c 1 3
179+
ExecStart=-/usr/bin/chcon --type=null_device_t /dev/tmachine-cdi-null
180+
181+
[Install]
182+
WantedBy=multi-user.target
183+
owner: root
184+
group: root
185+
mode: "0644"
186+
187+
- name: Create the host device node
188+
become: true
189+
ansible.builtin.systemd_service:
190+
name: openshell-cdi-device.service
191+
daemon_reload: true
192+
enabled: true
193+
state: restarted
194+
48195
- name: Generate named CDI scenario devices
49196
become: true
50197
ansible.builtin.template:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. #}
2+
{# SPDX-License-Identifier: Apache-2.0 #}
3+
[Unit]
4+
Description=OpenShell tmachine CDI Unix socket fixture
5+
After=local-fs.target
6+
7+
[Service]
8+
# The socket is created by this process, not by PID 1, and `cat` echoes each
9+
# connection. `--inetd` puts the accepted connection on stdin and stdout, which
10+
# is where `cat` reads and writes.
11+
# On SELinux hosts a container may connect only to sockets of certain domains,
12+
# which does not include the default one for a service. Serve the socket as a
13+
# container, so the container that mounts it can connect. It is ignored where
14+
# SELinux is off.
15+
SELinuxContext=system_u:system_r:container_t:s0
16+
ExecStartPre=/bin/rm -f {{ cdi_scenario_dir }}/socket/echo.sock
17+
ExecStart=/usr/bin/systemd-socket-activate --inetd --listen={{ cdi_scenario_dir }}/socket/echo.sock --accept /bin/cat
18+
# The process creates the socket with mode 0644 whatever the unit's UMask, which
19+
# only root can connect to. Open it to any user once it exists. A service
20+
# start, including the one at every boot, creates the socket again.
21+
ExecStartPost=/bin/sh -c 'until [ -S "$1" ]; do sleep 0.1; done; chmod 0666 "$1"' sh {{ cdi_scenario_dir }}/socket/echo.sock
22+
Restart=always
23+
24+
[Install]
25+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)