summaryrefslogtreecommitdiffstats
path: root/tests/config.rs
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-19 15:23:23 +0000
committeraur-build <aur-build@localhost>2026-11-19 15:23:23 +0000
commit32eaaeae9e0fe2c2285fb79efa0e579150d8bd83 (patch)
treef363a135cf4a5cf48d5a5ab57f8a127e617feab2 /tests/config.rs
parent8912f71afb9697c8645209ec0e67f67098bb16a1 (diff)
downloadaur-build-32eaaeae9e0fe2c2285fb79efa0e579150d8bd83.tar.gz
aur-build-32eaaeae9e0fe2c2285fb79efa0e579150d8bd83.zip
config: refuse a sandbox configuration that cannot work, at startup
The enum makes the impossible unrepresentable, but two things survive any type: a field that is present and empty, and an assertion the type leaves defaulted on purpose. `Config::validate` catches both, and it runs at startup — before the watcher, the dispatcher or the UI are up and looking healthy — and again where a backend is constructed, so nothing that builds one can skip it. The one that matters is the workspace. `Run` frames carry a hub-side working directory and the srcdir is never shipped to the sandbox, so a `remote` backend on a machine that does not share the hub's filesystem would fail in `makepkg` with a missing directory — and, worse, would *succeed* in the case nobody wants: quietly building untrusted code on the hub. So a remote or agent-sandbox config must say `"shared_workspace": true`, and the refusal says why rather than just naming the field. Loopback needs no assertion because it shares the filesystem by construction, which is also why it stays the zero-config default. Empty `listen`, `token`, `api`, `namespace`, `bearer` and `image` are each refused by name. Two tests drive the compiled binary rather than the library, because a guard that only exists as a callable function is a guard the CLI can walk past.
Diffstat (limited to 'tests/config.rs')
-rw-r--r--tests/config.rs115
1 files changed, 115 insertions, 0 deletions
diff --git a/tests/config.rs b/tests/config.rs
index 30b687b..20010a7 100644
--- a/tests/config.rs
+++ b/tests/config.rs
@@ -322,3 +322,118 @@ fn the_rendezvous_secrets_never_reach_debug() {
assert!(!shown.contains("kube-token"), "the cluster bearer leaked into Debug");
assert!(shown.contains("builds"), "the non-secret fields are still visible: {shown}");
}
+
+// ---------------------------------------------------------------------------
+// The startup guard. The enum makes the impossible unrepresentable; this
+// refuses what a type cannot see — a field present but empty, and a remote
+// backend that was never told the workspace is shared.
+// ---------------------------------------------------------------------------
+
+/// A config that parses but cannot work must be refused by `validate`, not
+/// discovered at the step that needed the thing it is missing.
+#[test]
+fn a_field_that_is_present_but_empty_is_refused() {
+ let cfg = load_sandbox(r#"{"kind":"remote","listen":"","token":"t","shared_workspace":true}"#)
+ .unwrap();
+ let err = cfg.validate().unwrap_err().to_string();
+ assert!(err.contains("listen"), "must name the empty field: {err}");
+
+ let cfg = load_sandbox(r#"{"kind":"remote","listen":"127.0.0.1:1","token":" ","shared_workspace":true}"#)
+ .unwrap();
+ let err = cfg.validate().unwrap_err().to_string();
+ assert!(err.contains("token"), "whitespace is not a token: {err}");
+
+ // Every agent-sandbox field, one at a time.
+ for blank in ["api", "namespace", "bearer", "image", "listen", "token"] {
+ let mut fields = std::collections::BTreeMap::from([
+ ("api", "https://k8s:6443"),
+ ("namespace", "builds"),
+ ("bearer", "kube"),
+ ("image", "img"),
+ ("listen", "0.0.0.0:1"),
+ ("token", "t"),
+ ]);
+ fields.insert(blank, "");
+ let body: Vec<String> =
+ fields.iter().map(|(k, v)| format!("\"{k}\":\"{v}\"")).collect();
+ let cfg = load_sandbox(&format!("{{\"kind\":\"agent-sandbox\",{}}}", body.join(","))).unwrap();
+ let err = cfg.validate().unwrap_err().to_string();
+ assert!(err.contains(blank), "blank {blank} must be named: {err}");
+ }
+}
+
+/// The workspace gap, made impossible to hit by accident. The build workspace
+/// is not shipped to a remote sandbox, so "remote" without this assertion would
+/// silently build on the hub — the exact substitution this whole enum exists to
+/// prevent, one layer down.
+#[test]
+fn a_remote_backend_must_assert_the_shared_workspace() {
+ let cfg = load_sandbox(r#"{"kind":"remote","listen":"127.0.0.1:1","token":"t"}"#).unwrap();
+ let err = cfg.validate().unwrap_err().to_string();
+ assert!(err.contains("shared_workspace"), "must name the missing assertion: {err}");
+ assert!(err.contains("workspace"), "must explain what is missing: {err}");
+ assert!(err.contains("remote"), "must say which kind: {err}");
+
+ let cfg = load_sandbox(
+ r#"{"kind":"agent-sandbox","api":"https://k8s:6443","namespace":"b","bearer":"k",
+ "image":"img","listen":"0.0.0.0:1","token":"t"}"#,
+ )
+ .unwrap();
+ let err = cfg.validate().unwrap_err().to_string();
+ assert!(err.contains("shared_workspace"), "{err}");
+ assert!(err.contains("agent-sandbox"), "{err}");
+
+ // Asserting it is enough — the guard refuses the silence, not the kind.
+ assert!(load_sandbox(
+ r#"{"kind":"remote","listen":"127.0.0.1:1","token":"t","shared_workspace":true}"#
+ )
+ .unwrap()
+ .validate()
+ .is_ok());
+}
+
+#[test]
+fn the_local_backend_needs_no_assertion_at_all() {
+ // The whole point of keeping loopback the default: it shares the filesystem
+ // by construction, so there is nothing to promise.
+ assert!(load_sandbox(r#"{"kind":"loopback"}"#).unwrap().validate().is_ok());
+ assert!(load_raw("{}\n").unwrap().validate().is_ok());
+}
+
+/// The guard is wired to the process, not just available as a library call:
+/// `aur-build serve` with a remote config that never asserted the shared
+/// workspace must refuse rather than come up healthy and build on the hub.
+#[test]
+fn serve_refuses_a_remote_config_that_never_asserted_the_workspace() {
+ let tmp = tempfile::tempdir().unwrap();
+ std::fs::write(
+ tmp.path().join("config.json"),
+ r#"{ "sandbox": {"kind":"remote","listen":"127.0.0.1:0","token":"t"} }"#,
+ )
+ .unwrap();
+ let out = Command::new(env!("CARGO_BIN_EXE_aur-build"))
+ .args(["--root", &tmp.path().display().to_string(), "serve"])
+ .output()
+ .unwrap();
+ assert!(!out.status.success(), "serve must refuse, not start healthy");
+ let err = String::from_utf8_lossy(&out.stderr);
+ assert!(err.contains("shared_workspace"), "{err}");
+}
+
+/// …and the same for the CLI build path, which is where the build would
+/// actually have run.
+#[test]
+fn the_cli_build_path_refuses_an_unusable_sandbox_config() {
+ let tmp = tempfile::tempdir().unwrap();
+ std::fs::write(
+ tmp.path().join("config.json"),
+ r#"{ "sandbox": {"kind":"remote","listen":"127.0.0.1:0","token":""} }"#,
+ )
+ .unwrap();
+ let out = Command::new(env!("CARGO_BIN_EXE_aur-build"))
+ .args(["--root", &tmp.path().display().to_string(), "packages"])
+ .output()
+ .unwrap();
+ assert!(!out.status.success());
+ assert!(String::from_utf8_lossy(&out.stderr).contains("token"));
+}