Skip to content

Commit 2022d51

Browse files
committed
test(cdi): check the standard sbin paths in CDI workloads
Add a scenario whose spec narrows PATH to directories without sbin and a path-has check, and expect the standard sbin directories to be appended for the workload. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 6160d3b commit 2022d51

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎tests/suites/features/cdi/src/lib.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ enum Check {
8686
NonRoot { expect: bool },
8787
/// Whether the workload's groups include the group that owns `path`.
8888
InGroupOf { path: String, expect: bool },
89+
/// Whether a directory is an entry of the workload's `PATH`.
90+
PathHas { dir: String, expect: bool },
8991
/// Connecting to the unix socket at `path` works and it echoes what is
9092
/// sent. The probe needs `perl`, which the default workload image has.
9193
EchoSocket { path: String, expect: Access },
@@ -160,6 +162,10 @@ impl Check {
160162
path: text("path")?,
161163
expect: flag("expect")?,
162164
}),
165+
Some("path-has") => Ok(Self::PathHas {
166+
dir: text("dir")?,
167+
expect: flag("expect")?,
168+
}),
163169
Some("echo-socket") => Ok(Self::EchoSocket {
164170
path: text("path")?,
165171
expect: access()?,
@@ -181,6 +187,7 @@ impl Check {
181187
Self::Write { path, .. } => format!("write:{path}"),
182188
Self::NonRoot { .. } => "non-root".to_string(),
183189
Self::InGroupOf { path, .. } => format!("in-group-of:{path}"),
190+
Self::PathHas { dir, .. } => format!("path-has:{dir}"),
184191
Self::EchoSocket { path, .. } => format!("echo-socket:{path}"),
185192
}
186193
}
@@ -219,6 +226,9 @@ impl Check {
219226
Self::InGroupOf { path, .. } => format!(
220227
r#"set -- $(ls -ln '{path}' 2>/dev/null); case " $(id -G) " in *" $4 "*) printf ok ;; *) printf missing-group ;; esac"#
221228
),
229+
Self::PathHas { dir, .. } => format!(
230+
r#"case ":$PATH:" in *:'{dir}':*) printf ok ;; *) printf absent ;; esac"#
231+
),
222232
// `no-client` and `no-echo` are not expected by any scenario, so a
223233
// workload without perl or a socket that does not echo fails the
224234
// check with a result that says why.
@@ -244,6 +254,7 @@ impl Check {
244254
Self::InGroupOf { expect, .. } => {
245255
if *expect { "ok" } else { "missing-group" }.to_string()
246256
}
257+
Self::PathHas { expect, .. } => if *expect { "ok" } else { "absent" }.to_string(),
247258
}
248259
}
249260
}
@@ -766,4 +777,19 @@ mod tests {
766777
"missing-group"
767778
);
768779
}
780+
781+
#[test]
782+
fn probe_shell_observes_path_entries() {
783+
let fx = Fixtures::new("path-has");
784+
let observe = |value: Value| fx.observe(&checks(&[value]).remove(0));
785+
786+
assert_eq!(
787+
observe(json!({"type": "path-has", "dir": "/bin", "expect": true})),
788+
"ok"
789+
);
790+
assert_eq!(
791+
observe(json!({"type": "path-has", "dir": "/no/such/directory", "expect": false})),
792+
"absent"
793+
);
794+
}
769795
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ async fn mps_pipe_directory_without_noexec(cx: &mut Context) -> testkit::Result
8787
cdi::scenario(cx, "mps-pipe-directory-without-noexec").await
8888
}
8989

90+
/// A spec that narrows `PATH` to directories without sbin. The standard sbin
91+
/// directories are appended for the workload, where tools such as `ldconfig`
92+
/// and `nvidia-smi` can live.
93+
#[testkit::test]
94+
async fn sbin_path(cx: &mut Context) -> testkit::Result {
95+
cdi::scenario(cx, "sbin-path").await
96+
}
97+
9098
/// A character device node injected from `/dev/null` with read-write
9199
/// permissions. The expectations record whether a non-root workload can write
92100
/// it.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
# needs perl in the workload image
4141
# {type: env, name, expect} value, null when unset
4242
# {type: non-root, expect} whether the workload is not uid 0 (true)
43+
# {type: path-has, dir, expect} whether dir is an entry of PATH (true)
4344
# {type: in-group-of, path, expect}
4445
# whether the workload's groups include
4546
# the group that owns path (true)
@@ -179,6 +180,19 @@ cdi_scenarios:
179180
- {type: non-root}
180181
- {type: write, path: /dev/tmachine-cdi-null, expect: ok}
181182

183+
# The spec narrows PATH to directories without sbin. The standard sbin
184+
# directories are appended for workloads of CDI sandboxes, where tools such
185+
# as ldconfig and nvidia-smi can live.
186+
- name: sbin-path
187+
edits:
188+
env:
189+
- PATH=/usr/local/bin:/usr/bin:/bin
190+
expect:
191+
checks:
192+
- {type: path-has, dir: /usr/local/sbin, expect: true}
193+
- {type: path-has, dir: /usr/sbin, expect: true}
194+
- {type: path-has, dir: /sbin, expect: true}
195+
182196
# NVIDIA MPS clients open pipes under /tmp/nvidia-mps. The fixture uses a
183197
# regular file in place of a pipe so the checks cannot block on open.
184198
- name: mps-pipe-directory

0 commit comments

Comments
 (0)