forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.08.8 MiBc8ea10cTahinli27 days ago292 commits

runner_subcommand.rs114 lines, 3.3 KiBHistoryRawDownload
1//! `forge runner` is the forge CI runner, the same command line as `forge-runner`. These
2//! tests check that the subcommand exists and that the sandbox helper, which the runner starts as
3//! `<this executable> __sandbox`, is dispatched by this binary.
4
5use std::fs;
6use std::path::Path;
7use std::process::Command;
8
9use tempfile::TempDir;
10
11use crate::common::bin;
12
13fn forge() -> Command {
14 Command::new(bin())
15}
16
17/// Unprivileged user namespaces, as `crates/forge-runner/tests/sandbox.rs` checks them.
18fn userns_available() -> bool {
19 let max = fs::read_to_string("/proc/sys/user/max_user_namespaces")
20 .ok()
21 .and_then(|s| s.trim().parse::<u64>().ok())
22 .unwrap_or(0);
23 if max == 0 {
24 return false;
25 }
26 Command::new("unshare")
27 .args(["--user", "--map-root-user", "true"])
28 .status()
29 .is_ok_and(|s| s.success())
30}
31
32/// Gives every directory under `dir` owner access. Overlay work dirs are left without
33/// permissions, and `TempDir` cannot remove them otherwise. Same approach as the executor.
34fn make_removable(dir: &Path) {
35 let _ = fs::set_permissions(dir, std::os::unix::fs::PermissionsExt::from_mode(0o700));
36 let Ok(entries) = fs::read_dir(dir) else {
37 return;
38 };
39 for entry in entries.flatten() {
40 if entry.file_type().is_ok_and(|t| t.is_dir()) {
41 make_removable(&entry.path());
42 }
43 }
44}
45
46/// A work dir under the cargo target dir, removed on drop even when a job left overlay dirs
47/// behind.
48struct WorkDir(TempDir);
49
50impl WorkDir {
51 fn new() -> Self {
52 let dir = tempfile::Builder::new()
53 .prefix("runner-check-")
54 .tempdir_in(env!("CARGO_TARGET_TMPDIR"))
55 .expect("create work dir under the target dir");
56 Self(dir)
57 }
58}
59
60impl Drop for WorkDir {
61 fn drop(&mut self) {
62 make_removable(self.0.path());
63 }
64}
65
66#[test]
67fn runner_help_lists_subcommands() {
68 let out = forge()
69 .args(["runner", "--help"])
70 .output()
71 .expect("run forge runner --help");
72 let stdout = String::from_utf8_lossy(&out.stdout);
73 assert!(
74 out.status.success(),
75 "stderr: {}",
76 String::from_utf8_lossy(&out.stderr)
77 );
78 assert!(
79 stdout.contains("forge runner"),
80 "help should name forge runner: {stdout}"
81 );
82 for word in ["register", "run", "check"] {
83 assert!(
84 stdout.contains(word),
85 "help should mention {word}: {stdout}"
86 );
87 }
88}
89
90#[test]
91fn sandbox_helper_runs_through_forge() {
92 if !userns_available() {
93 eprintln!("skipping: unprivileged user namespaces are not available on this host");
94 return;
95 }
96 let work = WorkDir::new();
97 // `runner check` runs one trivial job through the real sandbox. It is started as
98 // `forge __sandbox`, so a successful exit proves that dispatch works in this binary.
99 let out = forge()
100 .args(["runner", "check", "--work-dir"])
101 .arg(work.0.path())
102 .output()
103 .expect("run forge runner check");
104 let stdout = String::from_utf8_lossy(&out.stdout);
105 let stderr = String::from_utf8_lossy(&out.stderr);
106 assert!(
107 out.status.success(),
108 "runner check failed\nstdout: {stdout}\nstderr: {stderr}"
109 );
110 assert!(
111 stdout.contains("ok"),
112 "expected ok lines in the report: {stdout}"
113 );
114}