Skip to content

Commit 79c8085

Browse files
committed
test(tmachine): add CDI fixtures and cdi feature environments
Add the cdi_fixtures role, which defines one CDI device per scenario of the tmachine.example/cdi-scenario class together with the host files each references, so CDI injection can be tested without GPU hardware. Run it in a cdi feature environment on each Docker and Podman base, such as ubuntu-docker-rootful-cdi, instead of the base environments. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent f0e1327 commit 79c8085

7 files changed

Lines changed: 239 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
---
5+
- name: Prepare hardware-free CDI fixtures in supported environments
6+
hosts: all
7+
gather_facts: false
8+
pre_tasks:
9+
- name: Wait for SSH
10+
ansible.builtin.wait_for_connection:
11+
roles:
12+
- cdi_fixtures
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
---
5+
# The published scenario expectations live under this root.
6+
cdi_fixture_root: /var/lib/openshell-test-inputs/cdi
7+
8+
# The files and the socket the specs mount into containers. Under /usr/local,
9+
# SELinux hosts give them a type containers can read, which /var/lib does not.
10+
cdi_fixture_files_root: /usr/local/share/openshell-test-inputs/cdi
11+
12+
# The container engine reads static CDI specs from /etc/cdi, which persists
13+
# across the setup, installation, and test disk layers.
14+
cdi_spec_path: /etc/cdi/openshell-tmachine-scenarios.yaml
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
---
5+
- name: Check the CDI spec path before creating the spec
6+
become: true
7+
ansible.builtin.stat:
8+
path: "{{ cdi_spec_path }}"
9+
follow: false
10+
register: cdi_spec_stat
11+
12+
- name: Require a guest without an existing CDI spec
13+
ansible.builtin.assert:
14+
that: [not cdi_spec_stat.stat.exists]
15+
fail_msg: "Refusing to replace existing CDI spec {{ cdi_spec_path }}"
16+
17+
- name: Create CDI fixture and spec directories
18+
become: true
19+
ansible.builtin.file:
20+
path: "{{ item }}"
21+
state: directory
22+
owner: root
23+
group: root
24+
mode: "0755"
25+
loop:
26+
- "{{ cdi_fixture_root }}"
27+
- "{{ cdi_fixture_files_root }}"
28+
- "{{ cdi_spec_path | dirname }}"
29+
30+
- name: Prepare named CDI scenarios
31+
ansible.builtin.include_tasks: scenarios.yaml
32+
33+
# The tests select the scenario device per sandbox through driver config, which
34+
# the gateway rejects unless an administrator allows it. The installers add
35+
# this to the active driver's settings.
36+
- name: Create the test settings directory
37+
become: true
38+
ansible.builtin.file:
39+
path: /etc/openshell-test
40+
state: directory
41+
owner: root
42+
group: root
43+
mode: "0755"
44+
45+
- name: Allow caller driver config for the CDI tests
46+
become: true
47+
ansible.builtin.copy:
48+
dest: /etc/openshell-test/driver.toml
49+
content: |
50+
allow_driver_config = true
51+
owner: root
52+
group: root
53+
mode: "0644"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
---
5+
- name: Load named CDI scenarios
6+
ansible.builtin.include_vars:
7+
file: scenarios.yaml
8+
9+
- name: Collect scenario fixtures
10+
ansible.builtin.set_fact:
11+
cdi_scenario_fixtures: >-
12+
{{ cdi_scenario_common_fixtures
13+
+ (cdi_scenarios | map(attribute='fixtures', default=[]) | flatten) }}
14+
15+
- name: Create the scenario fixture root
16+
become: true
17+
ansible.builtin.file:
18+
path: "{{ cdi_scenario_dir }}"
19+
state: directory
20+
owner: root
21+
group: root
22+
mode: "0755"
23+
24+
- name: Create scenario fixture directories
25+
become: true
26+
ansible.builtin.file:
27+
path: "{{ cdi_scenario_dir }}/{{ item.path }}"
28+
state: directory
29+
owner: "{{ item.owner | default('root') }}"
30+
group: "{{ item.group | default('root') }}"
31+
mode: "{{ item.mode }}"
32+
loop: "{{ cdi_scenario_fixtures | selectattr('type', 'eq', 'dir') | list }}"
33+
loop_control:
34+
label: "{{ item.path }}"
35+
36+
- name: Write scenario fixture files
37+
become: true
38+
ansible.builtin.copy:
39+
dest: "{{ cdi_scenario_dir }}/{{ item.path }}"
40+
content: "{{ item.content }}"
41+
owner: "{{ item.owner | default('root') }}"
42+
group: "{{ item.group | default('root') }}"
43+
mode: "{{ item.mode }}"
44+
loop: "{{ cdi_scenario_fixtures | selectattr('type', 'eq', 'file') | list }}"
45+
loop_control:
46+
label: "{{ item.path }}"
47+
48+
- name: Generate named CDI scenario devices
49+
become: true
50+
ansible.builtin.template:
51+
src: scenarios.yaml.j2
52+
dest: "{{ cdi_spec_path }}"
53+
owner: root
54+
group: root
55+
mode: "0644"
56+
57+
- name: Publish scenario expectations for the CDI feature tests
58+
become: true
59+
ansible.builtin.copy:
60+
dest: "{{ cdi_fixture_root }}/scenarios.json"
61+
content: >-
62+
{{ {'common_checks': cdi_scenario_common_checks,
63+
'scenarios': cdi_scenarios | items2dict(key_name='name', value_name='expect')}
64+
| to_nice_json }}
65+
owner: root
66+
group: root
67+
mode: "0644"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
# Generated from vars/scenarios.yaml. Scenarios are selected individually, so
4+
# negative scenarios never combine with positive ones.
5+
cdiVersion: "0.8.0"
6+
kind: tmachine.example/cdi-scenario
7+
containerEdits:
8+
{{ cdi_scenario_spec_edits | to_nice_yaml(indent=2) | indent(2, true) }}
9+
devices:
10+
{% for case in cdi_scenarios %}
11+
- name: {{ case.name }}
12+
containerEdits:
13+
{{ case.edits | to_nice_yaml(indent=2) | indent(6, true) }}
14+
{% endfor %}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
---
5+
# Each case is one device of the tmachine.example/cdi-scenario class, selected as
6+
# `tmachine.example/cdi-scenario=<name>`. A case declares the host fixtures it
7+
# needs and the CDI edits that reference them; tasks/scenarios.yaml creates the
8+
# fixtures and templates/scenarios.yaml.j2 renders the spec.
9+
#
10+
# Fixture `path` is relative to {{ cdi_scenario_dir }}. Types:
11+
# dir directory (parents must be declared first)
12+
# file regular file with `content`
13+
# Both accept `mode`, `owner` and `group`.
14+
#
15+
# `expect` records the behavior the CDI feature tests assert for the scenario,
16+
# and is rendered to {{ cdi_fixture_root }}/scenarios.json for them. It states
17+
# what the sandbox does today; a change that alters that behavior updates the
18+
# values here. Either:
19+
# rejected: <text> sandbox creation fails; <text> must appear in the
20+
# diagnostic (empty matches any failure). No workload
21+
# runs, so no checks apply.
22+
# checks: [...] sandbox creation succeeds and each check holds, where a
23+
# check is one of
24+
# {type: exists, path, expect} whether the path exists (true)
25+
# {type: contents, path, expect} the file's contents
26+
# {type: filetype, path, expect} `file`, `directory`, `character-device`,
27+
# `block-device`, `socket` or `fifo`
28+
# {type: read, path, expect} `ok` or `denied`
29+
# {type: write, path, expect} `ok` or `denied` (nothing is written)
30+
# {type: env, name, expect} value, null when unset
31+
# {type: non-root, expect} whether the workload is not uid 0 (true)
32+
# {type: in-group-of, path, expect}
33+
# whether the workload's groups include
34+
# the group that owns path (true)
35+
# `non-root` and `in-group-of` describe the workload process, not a file's
36+
# owner.
37+
cdi_scenario_dir: "{{ cdi_fixture_files_root }}/scenarios"
38+
39+
# Edits applied by every case, to check that spec and device edits merge. The
40+
# marker is mounted under /usr/share, which the sandbox already allows reading,
41+
# so these checks do not depend on grants derived from the CDI spec.
42+
cdi_scenario_spec_edits:
43+
env:
44+
- TMACHINE_CDI_SCENARIO_SPEC=fixture
45+
mounts:
46+
- hostPath: "{{ cdi_scenario_dir }}/spec-marker"
47+
containerPath: /usr/share/tmachine-cdi/common/spec-marker
48+
options: [bind, ro]
49+
50+
# Checked in every scenario that is created, to show that spec and device edits
51+
# merge.
52+
cdi_scenario_common_checks:
53+
- {type: env, name: TMACHINE_CDI_SCENARIO_SPEC, expect: fixture}
54+
- {type: contents, path: /usr/share/tmachine-cdi/common/spec-marker, expect: tmachine-cdi-spec-marker}
55+
- {type: write, path: /usr/share/tmachine-cdi/common/spec-marker, expect: denied}
56+
57+
cdi_scenario_common_fixtures:
58+
- {path: spec-marker, type: file, content: "tmachine-cdi-spec-marker\n", mode: "0644"}
59+
60+
cdi_scenarios:
61+
# The mount point is under a directory the sandbox baseline already makes
62+
# read-only, so nothing needs to be derived for the mount to be readable.
63+
- name: mount-under-read-only-baseline
64+
fixtures:
65+
- {path: baseline-read-only-file, type: file, content: "tmachine-cdi-baseline-read-only-file\n", mode: "0644"}
66+
edits:
67+
mounts:
68+
- hostPath: "{{ cdi_scenario_dir }}/baseline-read-only-file"
69+
containerPath: /usr/share/tmachine-cdi/baseline-read-only-file
70+
options: [bind, ro]
71+
expect:
72+
checks:
73+
- {type: contents, path: /usr/share/tmachine-cdi/baseline-read-only-file, expect: tmachine-cdi-baseline-read-only-file}
74+
- {type: write, path: /usr/share/tmachine-cdi/baseline-read-only-file, expect: denied}

‎tests/tmachine/config.nix‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ let
9898
inputs.keycloak_realm_file = "../../scripts/keycloak-realm.json";
9999
};
100100
};
101+
# Hardware-free CDI fixtures, for the cdi suite.
102+
cdi = {
103+
bases = dockerBases ++ podmanBases;
104+
setup.playbooks = [ "ansible/playbooks/cdi.yaml" ];
105+
};
101106
};
102107
featureEnvironments = pkgs.lib.concatMap (
103108
feature: map (base: withSetup base feature features.${feature}.setup) features.${feature}.bases

0 commit comments

Comments
 (0)