| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::fs; |
| 6 | use std::path::Path; |
| 7 | use std::process::Command; |
| 8 | |
| 9 | use tempfile::TempDir; |
| 10 | |
| 11 | use crate::common::bin; |
| 12 | |
| 13 | fn forge() -> Command { |
| 14 | Command::new(bin()) |
| 15 | } |
| 16 | |
| 17 | |
| 18 | fn 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 | |
| 33 | |
| 34 | fn 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 | |
| 47 | |
| 48 | struct WorkDir(TempDir); |
| 49 | |
| 50 | impl 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 | |
| 60 | impl Drop for WorkDir { |
| 61 | fn drop(&mut self) { |
| 62 | make_removable(self.0.path()); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn 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] |
| 91 | fn 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 | |
| 98 | |
| 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 | } |