Skip to content

Commit 75ec2cd

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 1ae3600 commit 75ec2cd

7 files changed

Lines changed: 268 additions & 2 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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
13+
tasks:
14+
- name: Publish CDI test variables
15+
ansible.builtin.include_tasks: test-env.yaml
16+
vars:
17+
test_env_name: cdi
18+
test_env_vars:
19+
OPENSHELL_TEST_CDI_SCENARIOS: "{{ cdi_fixture_root }}/scenarios.json"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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
15+
16+
# The CDI device kind of every scenario device. The spec and the published
17+
# expectations both take it from here, so the tests learn it from the latter.
18+
cdi_device_kind: tmachine.example/cdi-scenario
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
---
5+
- name: Require the environment's compute driver
6+
ansible.builtin.assert:
7+
that: [environment_driver is defined]
8+
fail_msg: environment_driver names the gateway driver and comes from the environment's base in config.nix
9+
10+
- name: Check the CDI spec path before creating the spec
11+
become: true
12+
ansible.builtin.stat:
13+
path: "{{ cdi_spec_path }}"
14+
follow: false
15+
register: cdi_spec_stat
16+
17+
- name: Require a guest without an existing CDI spec
18+
ansible.builtin.assert:
19+
that: [not cdi_spec_stat.stat.exists]
20+
fail_msg: "Refusing to replace existing CDI spec {{ cdi_spec_path }}"
21+
22+
- name: Create CDI fixture and spec directories
23+
become: true
24+
ansible.builtin.file:
25+
path: "{{ item }}"
26+
state: directory
27+
owner: root
28+
group: root
29+
mode: "0755"
30+
loop:
31+
- "{{ cdi_fixture_root }}"
32+
- "{{ cdi_fixture_files_root }}"
33+
- "{{ cdi_spec_path | dirname }}"
34+
35+
- name: Prepare named CDI scenarios
36+
ansible.builtin.include_tasks: scenarios.yaml
37+
38+
# The tests select the scenario device per sandbox through driver config, which
39+
# the gateway rejects unless an administrator allows it. The installers add
40+
# this to the active driver's settings.
41+
- name: Create the test settings directory
42+
become: true
43+
ansible.builtin.file:
44+
path: /etc/openshell-test
45+
state: directory
46+
owner: root
47+
group: root
48+
mode: "0755"
49+
50+
# The gateway must accept a per-sandbox CDI device, which it does only when the
51+
# driver allows caller driver config. Every installer merges this override into
52+
# the gateway's own configuration (see openshell_gateway/tasks/merge-settings.yaml).
53+
- name: Allow caller driver config for the CDI tests
54+
become: true
55+
ansible.builtin.copy:
56+
dest: /etc/openshell-test/gateway-overrides.toml
57+
content: |
58+
[openshell.drivers.{{ environment_driver }}]
59+
allow_driver_config = true
60+
owner: root
61+
group: root
62+
mode: "0644"
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
{{ {'kind': cdi_device_kind,
63+
'common_checks': cdi_scenario_common_checks,
64+
'scenarios': cdi_scenarios | items2dict(key_name='name', value_name='expect')}
65+
| to_nice_json }}
66+
owner: root
67+
group: root
68+
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: {{ cdi_device_kind }}
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 {{ cdi_device_kind }} kind, selected as
6+
# `{{ cdi_device_kind }}=<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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let
3636
};
3737
fedoraPodman = podman: {
3838
machine = "fedora";
39+
driver = "podman";
3940
use_galaxy = false;
4041
playbooks = [
4142
"ansible/playbooks/nextest.yaml"
@@ -49,6 +50,7 @@ let
4950
bases = {
5051
ubuntu-docker-rootful = {
5152
machine = "ubuntu";
53+
driver = "docker";
5254
use_galaxy = true;
5355
playbooks = [
5456
"ansible/playbooks/nextest.yaml"
@@ -74,7 +76,9 @@ let
7476
"fedora-podman-rootless"
7577
];
7678
# An environment named <base>-<suffix>: a base with more setup playbooks and
77-
# the inputs they read, run after the base's own.
79+
# the inputs they read, run after the base's own. A base with a compute driver
80+
# also passes it as `environment_driver`, for setup that configures the
81+
# gateway's driver table.
7882
withSetup =
7983
base: suffix: setup:
8084
let
@@ -86,7 +90,9 @@ let
8690
setup = {
8791
inherit (parent) use_galaxy;
8892
playbooks = parent.playbooks ++ setup.playbooks;
89-
inputs = setup.inputs or { };
93+
inputs =
94+
(if parent ? driver then { environment_driver = parent.driver; } else { })
95+
// (setup.inputs or { });
9096
};
9197
};
9298
# Feature environments, <base>-<feature>, one per base the feature supports.
@@ -101,6 +107,11 @@ let
101107
inputs.keycloak_realm_file = "../../scripts/keycloak-realm.json";
102108
};
103109
};
110+
# Hardware-free CDI fixtures, for the cdi suite.
111+
cdi = {
112+
bases = dockerBases ++ podmanBases;
113+
setup.playbooks = [ "ansible/playbooks/cdi.yaml" ];
114+
};
104115
};
105116
featureEnvironments = pkgs.lib.concatMap (
106117
feature: map (base: withSetup base feature features.${feature}.setup) features.${feature}.bases

0 commit comments

Comments
 (0)