diff options
Diffstat (limited to 'tests/config.rs')
| -rw-r--r-- | tests/config.rs | 215 |
1 files changed, 8 insertions, 207 deletions
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( |
