summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config.rs244
1 files changed, 242 insertions, 2 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());
+ }
+}