diff options
| author | aur-build <aur-build@localhost> | 2026-11-20 20:19:54 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-20 20:19:54 +0000 |
| commit | 8426843087178cac55c3dcea52aea3baccc29b69 (patch) | |
| tree | d430dd5588d05c1d0543979fa8626cfa79c77d05 | |
| parent | 68ec6971b872d8be561e8c2c2748c6c143bfef7d (diff) | |
| download | aur-build-8426843087178cac55c3dcea52aea3baccc29b69.tar.gz aur-build-8426843087178cac55c3dcea52aea3baccc29b69.zip | |
config: move the parsing and validation tests into the module
Eleven tests in tests/config.rs exercised how a config parses, what it
refuses, and what it refuses to print. They reached those behaviours
through the crate's public API only because that was the only way in, and
that had been keeping two items public that nothing outside config names:
DEFAULT_SANDBOX_IMAGE resolved at config.rs:187 and :374, nowhere else
Sandbox::KINDS never read by production at all — serde builds
the unknown-kind message itself
KINDS is now `#[cfg(test)]` rather than merely private, and
`cargo check --release` without the test-support feature compiles with
zero warnings, which is the proof that nothing production needed was
gated away by mistake.
One honest caveat, recorded in the file: these tests still create a
tempdir, because `Config::load` takes a directory and so even a pure
parse needs a file on disk. That is plumbing for the signature, not the
subject of the test — none of them touches the database, the fixture
corpus, or a subprocess. What remains in tests/config.rs genuinely does:
root resolution against real paths under target/, and two tests that run
the built binary.
config: 31 pub / 5 test-only -> 29 pub / 3 test-only.
Suite: 281 passed, 0 failed.
| -rw-r--r-- | src/config.rs | 244 | ||||
| -rw-r--r-- | tests/architecture-budget.json | 22 | ||||
| -rw-r--r-- | tests/config.rs | 215 |
3 files changed, 261 insertions, 220 deletions
diff --git a/src/config.rs b/src/config.rs index d577cdf..cd43b97 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,9 @@ use std::path::{Path, PathBuf}; use serde::{Deserialize, Serialize}; -pub const DEFAULT_SANDBOX_IMAGE: &str = "aur-build-sandbox:latest"; +/// The image a build runs in when the config does not say. Only this module +/// resolves it, so it is not part of the crate's interface. +const DEFAULT_SANDBOX_IMAGE: &str = "aur-build-sandbox:latest"; #[derive(Clone, Default, Serialize, Deserialize)] #[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)] @@ -96,7 +98,11 @@ impl Sandbox { } } - pub const KINDS: [&'static str; 3] = ["loopback", "remote", "agent-sandbox"]; + /// The accepted values, so a test can assert that an unknown kind names + /// the whole valid set rather than one example of it. Serde builds the + /// rejection message itself, so nothing in production reads this list. + #[cfg(test)] + const KINDS: [&'static str; 3] = ["loopback", "remote", "agent-sandbox"]; } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -424,3 +430,237 @@ fn slug(url: &str) -> String { .take(32) .collect() } +#[cfg(test)] +mod tests { + use super::*; + + // `Config::load` takes a directory, so parsing a config means having a file + // on disk somewhere. The tempdir below is plumbing for that signature, not + // something these tests are about: none of them touches the database, the + // fixture corpus, or a subprocess. + fn load_sandbox(sandbox: &str) -> Result<Config, String> { + let tmp = tempfile::tempdir().unwrap(); + std::fs::write( + tmp.path().join("config.json"), + format!("{{\n \"sandbox\": {sandbox}\n}}\n"), + ) + .unwrap(); + Config::load(tmp.path()).map_err(|e| e.to_string()) + } + + fn load_raw(body: &str) -> Result<Config, String> { + let tmp = tempfile::tempdir().unwrap(); + std::fs::write(tmp.path().join("config.json"), body).unwrap(); + Config::load(tmp.path()).map_err(|e| e.to_string()) + } + + // Moved verbatim from tests/config.rs: these eleven exercise parsing, + // validation and Debug redaction of the config itself. + + #[test] + fn each_sandbox_kind_parses_with_its_required_fields() { + match load_sandbox(r#"{"kind":"loopback"}"#).unwrap().sandbox { + Sandbox::Loopback => {} + other => panic!("expected loopback, got {other:?}"), + } + + let remote = load_sandbox( + r#"{"kind":"remote","listen":"127.0.0.1:8787","token":"s3cr3t"}"#, + ) + .unwrap(); + match &remote.sandbox { + Sandbox::Remote { listen, token, shared_workspace } => { + assert_eq!(listen, "127.0.0.1:8787"); + assert_eq!(token, "s3cr3t"); + assert!(!*shared_workspace, "sharing the filesystem is asserted, never assumed"); + } + other => panic!("expected remote, got {other:?}"), + } + + let abs = load_sandbox( + r#"{"kind":"agent-sandbox","api":"https://k8s:6443","namespace":"builds", + "bearer":"kube-token","image":"aur-build-sandbox:latest", + "listen":"0.0.0.0:8787","token":"s3cr3t","shared_workspace":true}"#, + ) + .unwrap(); + match &abs.sandbox { + Sandbox::AgentSandbox { api, namespace, bearer, image, .. } => { + assert_eq!(api, "https://k8s:6443"); + assert_eq!(namespace, "builds"); + assert_eq!(bearer, "kube-token"); + assert_eq!(image, "aur-build-sandbox:latest"); + } + other => panic!("expected agent-sandbox, got {other:?}"), + } + assert_eq!(abs.sandbox_image(), "aur-build-sandbox:latest"); + } + + #[test] + fn a_missing_required_field_names_the_field() { + let err = load_sandbox(r#"{"kind":"remote","listen":"127.0.0.1:8787"}"#).unwrap_err(); + assert!(err.contains("token"), "the error must name the missing field: {err}"); + + let err = load_sandbox(r#"{"kind":"remote","token":"t"}"#).unwrap_err(); + assert!(err.contains("listen"), "the error must name the missing field: {err}"); + + for missing in ["api", "namespace", "bearer", "image", "listen", "token"] { + let mut fields = vec![ + r#""api":"https://k8s:6443""#.to_string(), + r#""namespace":"builds""#.to_string(), + r#""bearer":"kube""#.to_string(), + r#""image":"img""#.to_string(), + r#""listen":"0.0.0.0:1""#.to_string(), + r#""token":"t""#.to_string(), + ]; + fields.retain(|f| !f.starts_with(&format!("\"{missing}\""))); + let err = load_sandbox(&format!("{{\"kind\":\"agent-sandbox\",{}}}", fields.join(","))) + .unwrap_err(); + assert!(err.contains(missing), "dropping {missing} must name it: {err}"); + } + } + + #[test] + fn a_misspelled_field_is_refused_not_ignored() { + let err = load_sandbox(r#"{"kind":"remote","listn":"127.0.0.1:1","token":"t"}"#).unwrap_err(); + assert!(err.contains("listn"), "a typo'd field name must be named: {err}"); + } + + #[test] + fn an_unknown_kind_names_the_value_and_the_valid_set() { + let err = load_sandbox(r#"{"kind":"kata"}"#).unwrap_err(); + assert!(err.contains("kata"), "must name the bad value: {err}"); + for kind in Sandbox::KINDS { + assert!(err.contains(kind), "must name the valid set ({kind} missing): {err}"); + } + } + + #[test] + fn a_config_with_no_sandbox_object_is_the_local_backend() { + let cfg = load_raw("{\n \"repo_name\": \"aur\"\n}\n").unwrap(); + assert!(matches!(cfg.sandbox, Sandbox::Loopback)); + assert_eq!(cfg.repo_name, "aur", "the rest of the config is untouched"); + assert_eq!(cfg.sandbox_image(), DEFAULT_SANDBOX_IMAGE); + assert!(cfg.sandbox.shared_workspace(), "loopback shares the filesystem by construction"); + } + + #[test] + fn a_config_init_wrote_before_the_enum_still_loads() { + let cfg = load_raw( + r#"{ + "root": "/var/lib/aur-build", + "repo_name": "aur", + "arch": "x86_64", + "signing_key_id": "", + "bind": "127.0.0.1:8484", + "password": "", + "base_url": "http://127.0.0.1:8484", + "build_profile": "extra-x86_64", + "git_bin": "git", + "makepkg_bin": "makepkg", + "pacman_bin": "pacman", + "repo_add_bin": "repo-add", + "gpg_bin": "gpg", + "build_user": "", + "sandbox_backend": "loopback", + "agent_listen": "", + "agent_token": "", + "sandbox_image": "aur-build-sandbox:latest", + "max_concurrent_builds": 1, + "lease_secs": 90, + "min_free_disk_mb": 1024, + "max_transient_attempts": 3, + "dispatch_tick_secs": 2 + }"#, + ) + .unwrap(); + assert!(matches!(cfg.sandbox, Sandbox::Loopback)); + assert_eq!(cfg.lease_secs, 90); + } + + #[test] + fn retired_sandbox_keys_that_mean_something_are_refused() { + let err = load_raw(r#"{ "sandbox_backend": "remote", "agent_listen": "0.0.0.0:1", "agent_token": "t" }"#) + .unwrap_err(); + assert!(err.contains("sandbox_backend"), "must name the retired key: {err}"); + assert!(err.contains("sandbox"), "must point at the replacement: {err}"); + + let err = load_raw(r#"{ "sandbox_backend": "kata" }"#).unwrap_err(); + assert!(err.contains("sandbox_backend"), "{err}"); + + assert!(load_raw(r#"{ "sandbox_backend": "loopback", "agent_token": "" }"#).is_ok()); + } + + #[test] + fn the_rendezvous_secrets_never_reach_debug() { + let cfg = load_sandbox( + r#"{"kind":"agent-sandbox","api":"https://k8s:6443","namespace":"builds", + "bearer":"kube-token","image":"img","listen":"0.0.0.0:1","token":"s3cr3t"}"#, + ) + .unwrap(); + let shown = format!("{cfg:?}"); + assert!(!shown.contains("s3cr3t"), "the rendezvous token leaked into Debug"); + assert!(!shown.contains("kube-token"), "the cluster bearer leaked into Debug"); + assert!(shown.contains("builds"), "the non-secret fields are still visible: {shown}"); + } + + #[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}"); + + 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}"); + } + } + + #[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}"); + + 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() { + assert!(load_sandbox(r#"{"kind":"loopback"}"#).unwrap().validate().is_ok()); + assert!(load_raw("{}\n").unwrap().validate().is_ok()); + } +} diff --git a/tests/architecture-budget.json b/tests/architecture-budget.json index 93578fb..d361c54 100644 --- a/tests/architecture-budget.json +++ b/tests/architecture-budget.json @@ -78,15 +78,15 @@ "test": 0 }, "config": { - "dead": 5, + "dead": 3, "deadonly": 0, "glob": 0, - "largest": 427, + "largest": 667, "largest_file": "src/config.rs", - "lines": 427, + "lines": 667, "priv": 0, - "pub": 31, - "test": 5 + "pub": 29, + "test": 3 }, "db": { "dead": 21, @@ -310,15 +310,15 @@ } }, "totals": { - "dead": 100, + "dead": 98, "deadonly": 33, "edges": 87, "glob": 0, - "largest": 603, - "largest_file": "src/shims/mod.rs", - "lines": 14493, + "largest": 667, + "largest_file": "src/config.rs", + "lines": 14733, "priv": 109, - "pub": 499, - "test": 67 + "pub": 497, + "test": 65 } } diff --git a/tests/config.rs b/tests/config.rs index 080bbc7..a45ac1b 100644 --- a/tests/config.rs +++ b/tests/config.rs @@ -3,6 +3,14 @@ use std::process::Command; use aur_build::config::Config; +// What remains here needs a real directory or a real process: the root +// resolution tests create and remove paths under target/, and the CLI tests +// run the built binary. The parsing, validation and Debug-redaction tests +// moved into src/config.rs, where they are unit tests of the module. +// +// The one thing they could not shed is `Config::load`'s signature: it takes a +// directory, so even a pure parse needs a file on disk somewhere. + #[test] fn a_relative_root_is_absolutized_once_and_never_doubled() { let rel = PathBuf::from("target").join("config-root-test").join("runtime"); @@ -125,213 +133,6 @@ fn load_raw(body: &str) -> Result<Config, String> { } #[test] -fn each_sandbox_kind_parses_with_its_required_fields() { - match load_sandbox(r#"{"kind":"loopback"}"#).unwrap().sandbox { - aur_build::config::Sandbox::Loopback => {} - other => panic!("expected loopback, got {other:?}"), - } - - let remote = load_sandbox( - r#"{"kind":"remote","listen":"127.0.0.1:8787","token":"s3cr3t"}"#, - ) - .unwrap(); - match &remote.sandbox { - aur_build::config::Sandbox::Remote { listen, token, shared_workspace } => { - assert_eq!(listen, "127.0.0.1:8787"); - assert_eq!(token, "s3cr3t"); - assert!(!*shared_workspace, "sharing the filesystem is asserted, never assumed"); - } - other => panic!("expected remote, got {other:?}"), - } - - let abs = load_sandbox( - r#"{"kind":"agent-sandbox","api":"https://k8s:6443","namespace":"builds", - "bearer":"kube-token","image":"aur-build-sandbox:latest", - "listen":"0.0.0.0:8787","token":"s3cr3t","shared_workspace":true}"#, - ) - .unwrap(); - match &abs.sandbox { - aur_build::config::Sandbox::AgentSandbox { api, namespace, bearer, image, .. } => { - assert_eq!(api, "https://k8s:6443"); - assert_eq!(namespace, "builds"); - assert_eq!(bearer, "kube-token"); - assert_eq!(image, "aur-build-sandbox:latest"); - } - other => panic!("expected agent-sandbox, got {other:?}"), - } - assert_eq!(abs.sandbox_image(), "aur-build-sandbox:latest"); -} - -#[test] -fn a_missing_required_field_names_the_field() { - let err = load_sandbox(r#"{"kind":"remote","listen":"127.0.0.1:8787"}"#).unwrap_err(); - assert!(err.contains("token"), "the error must name the missing field: {err}"); - - let err = load_sandbox(r#"{"kind":"remote","token":"t"}"#).unwrap_err(); - assert!(err.contains("listen"), "the error must name the missing field: {err}"); - - for missing in ["api", "namespace", "bearer", "image", "listen", "token"] { - let mut fields = vec![ - r#""api":"https://k8s:6443""#.to_string(), - r#""namespace":"builds""#.to_string(), - r#""bearer":"kube""#.to_string(), - r#""image":"img""#.to_string(), - r#""listen":"0.0.0.0:1""#.to_string(), - r#""token":"t""#.to_string(), - ]; - fields.retain(|f| !f.starts_with(&format!("\"{missing}\""))); - let err = load_sandbox(&format!("{{\"kind\":\"agent-sandbox\",{}}}", fields.join(","))) - .unwrap_err(); - assert!(err.contains(missing), "dropping {missing} must name it: {err}"); - } -} - -#[test] -fn a_misspelled_field_is_refused_not_ignored() { - let err = load_sandbox(r#"{"kind":"remote","listn":"127.0.0.1:1","token":"t"}"#).unwrap_err(); - assert!(err.contains("listn"), "a typo'd field name must be named: {err}"); -} - -#[test] -fn an_unknown_kind_names_the_value_and_the_valid_set() { - let err = load_sandbox(r#"{"kind":"kata"}"#).unwrap_err(); - assert!(err.contains("kata"), "must name the bad value: {err}"); - for kind in aur_build::config::Sandbox::KINDS { - assert!(err.contains(kind), "must name the valid set ({kind} missing): {err}"); - } -} - -#[test] -fn a_config_with_no_sandbox_object_is_the_local_backend() { - let cfg = load_raw("{\n \"repo_name\": \"aur\"\n}\n").unwrap(); - assert!(matches!(cfg.sandbox, aur_build::config::Sandbox::Loopback)); - assert_eq!(cfg.repo_name, "aur", "the rest of the config is untouched"); - assert_eq!(cfg.sandbox_image(), aur_build::config::DEFAULT_SANDBOX_IMAGE); - assert!(cfg.sandbox.shared_workspace(), "loopback shares the filesystem by construction"); -} - -#[test] -fn a_config_init_wrote_before_the_enum_still_loads() { - let cfg = load_raw( - r#"{ - "root": "/var/lib/aur-build", - "repo_name": "aur", - "arch": "x86_64", - "signing_key_id": "", - "bind": "127.0.0.1:8484", - "password": "", - "base_url": "http://127.0.0.1:8484", - "build_profile": "extra-x86_64", - "git_bin": "git", - "makepkg_bin": "makepkg", - "pacman_bin": "pacman", - "repo_add_bin": "repo-add", - "gpg_bin": "gpg", - "build_user": "", - "sandbox_backend": "loopback", - "agent_listen": "", - "agent_token": "", - "sandbox_image": "aur-build-sandbox:latest", - "max_concurrent_builds": 1, - "lease_secs": 90, - "min_free_disk_mb": 1024, - "max_transient_attempts": 3, - "dispatch_tick_secs": 2 -}"#, - ) - .unwrap(); - assert!(matches!(cfg.sandbox, aur_build::config::Sandbox::Loopback)); - assert_eq!(cfg.lease_secs, 90); -} - -#[test] -fn retired_sandbox_keys_that_mean_something_are_refused() { - let err = load_raw(r#"{ "sandbox_backend": "remote", "agent_listen": "0.0.0.0:1", "agent_token": "t" }"#) - .unwrap_err(); - assert!(err.contains("sandbox_backend"), "must name the retired key: {err}"); - assert!(err.contains("sandbox"), "must point at the replacement: {err}"); - - let err = load_raw(r#"{ "sandbox_backend": "kata" }"#).unwrap_err(); - assert!(err.contains("sandbox_backend"), "{err}"); - - assert!(load_raw(r#"{ "sandbox_backend": "loopback", "agent_token": "" }"#).is_ok()); -} - -#[test] -fn the_rendezvous_secrets_never_reach_debug() { - let cfg = load_sandbox( - r#"{"kind":"agent-sandbox","api":"https://k8s:6443","namespace":"builds", - "bearer":"kube-token","image":"img","listen":"0.0.0.0:1","token":"s3cr3t"}"#, - ) - .unwrap(); - let shown = format!("{cfg:?}"); - assert!(!shown.contains("s3cr3t"), "the rendezvous token leaked into Debug"); - assert!(!shown.contains("kube-token"), "the cluster bearer leaked into Debug"); - assert!(shown.contains("builds"), "the non-secret fields are still visible: {shown}"); -} - -#[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}"); - - 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}"); - } -} - -#[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}"); - - 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() { - assert!(load_sandbox(r#"{"kind":"loopback"}"#).unwrap().validate().is_ok()); - assert!(load_raw("{}\n").unwrap().validate().is_ok()); -} - -#[test] fn serve_refuses_a_remote_config_that_never_asserted_the_workspace() { let tmp = tempfile::tempdir().unwrap(); std::fs::write( |
