mod common; use std::io::Read; use std::sync::Arc; use std::time::Duration; use aur_build::pipeline::{self, Ctx}; use aur_build::sandbox::remote::RemoteBackend; use aur_build::sandbox::{SandboxBackend, SandboxSpec}; use common::*; fn agent_bin() -> std::path::PathBuf { shim_dir().join("sandbox-agent") } fn approve_fixture(env: &Env, name: &str) -> aur_build::db::model::CandidateRow { aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, name).unwrap(); let c = aur_build::review::candidates::pending_candidates(&env.db).unwrap().remove(0); let dd = aur_build::review::diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); aur_build::review::approve::approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap(); aur_build::review::candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap() } #[test] fn a_full_build_runs_over_the_agent_protocol() { let (env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); let mut ctx = Ctx::new(&env.cfg, &env.db, &id).unwrap(); let backend = RemoteBackend::bind(&env.cfg, "127.0.0.1:0", "test-token").unwrap(); let addr = backend.local_addr().to_string(); ctx.backend = Arc::new(backend); let mut agent = std::process::Command::new(agent_bin()) .args(["--hub", &addr, "--token", "test-token", "--ttl", "30"]) .spawn() .expect("spawn sandbox-agent"); let out = pipeline::run_with(&ctx).unwrap(); let _ = agent.kill(); let _ = agent.wait(); assert!(matches!(out, pipeline::Outcome::Succeeded { .. }), "{out:?}"); let log = std::fs::read_to_string(env.cfg.build_log_path(&id)).unwrap(); assert!(log.contains("Making package"), "agent-streamed output must reach the build log:\n{log}"); let shimlog = env.cfg.hooks.shim_dir.clone().unwrap(); assert_eq!(aur_build::shims::called_with(&shimlog, "makepkg", ""), 1); } #[test] fn blobs_round_trip_over_the_wire_and_a_bad_token_is_refused() { let cfg = aur_build::config::Config::default(); let backend = RemoteBackend::bind(&cfg, "127.0.0.1:0", "right-token").unwrap(); let addr = backend.local_addr().to_string(); let mut good = std::process::Command::new(agent_bin()) .args(["--hub", &addr, "--token", "right-token", "--ttl", "30"]) .spawn() .expect("spawn sandbox-agent"); let sbx = backend.acquire(&SandboxSpec::new("extra-x86_64", "blob-test", Duration::from_secs(10))).unwrap(); let bytes = b"pinned artifact bytes".to_vec(); let digest = aur_build::hash::hex(&bytes); sbx.put(&digest, &mut std::io::Cursor::new(bytes.clone())).unwrap(); let mut got = Vec::new(); sbx.get(&digest).unwrap().read_to_end(&mut got).unwrap(); assert_eq!(got, bytes, "blobs ride the wire by digest and come back byte-identical"); sbx.destroy().unwrap(); let _ = good.wait(); let mut bad = std::process::Command::new(agent_bin()) .args(["--hub", &addr, "--token", "wrong-token", "--ttl", "30"]) .spawn() .expect("spawn sandbox-agent"); let Err(err) = backend.acquire(&SandboxSpec::new("extra-x86_64", "nope", Duration::from_secs(10))) else { panic!("a stranger's box must not be leased") }; assert!(err.to_string().contains("token"), "{err}"); let _ = bad.wait(); } fn remote_cfg(listen: &str, token: &str) -> aur_build::config::Config { aur_build::config::Config { sandbox: aur_build::config::Sandbox::Remote { listen: listen.into(), token: token.into(), shared_workspace: true, }, ..aur_build::config::Config::default() } } fn free_port() -> u16 { let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let port = l.local_addr().unwrap().port(); drop(l); port } #[test] fn two_builds_share_one_agent_listener() { let addr = format!("127.0.0.1:{}", free_port()); let cfg = remote_cfg(&addr, "shared-token"); let a = aur_build::sandbox::backend_for(&cfg).unwrap(); let b = aur_build::sandbox::backend_for(&cfg).unwrap(); let (sa, sb) = (&a.status().unwrap()[0].id, &b.status().unwrap()[0].id); assert_eq!(sa, sb, "both must report the one listener: {sa} vs {sb}"); assert!(sa.contains(&addr), "and it must be the configured address: {sa}"); assert!( std::net::TcpListener::bind(&addr).is_err(), "a second bind on {addr} must fail, or nothing here proves anything" ); } #[test] fn a_dial_in_goes_to_whichever_backend_is_waiting() { let addr = format!("127.0.0.1:{}", free_port()); let cfg = remote_cfg(&addr, "shared-queue"); let _first = aur_build::sandbox::backend_for(&cfg).unwrap(); let second = aur_build::sandbox::backend_for(&cfg).unwrap(); let mut agent = std::process::Command::new(agent_bin()) .args(["--hub", &addr, "--token", "shared-queue", "--ttl", "30"]) .spawn() .expect("spawn sandbox-agent"); let spec = SandboxSpec::new("extra-x86_64", "task", Duration::from_secs(10)); let sbx = second.acquire(&spec); let _ = agent.kill(); let _ = agent.wait(); assert!(sbx.is_ok(), "the second backend must be able to take the dial-in: {:?}", sbx.err()); } #[test] fn a_different_token_on_the_same_address_is_refused_not_shared() { let addr = format!("127.0.0.1:{}", free_port()); assert!(aur_build::sandbox::backend_for(&remote_cfg(&addr, "one")).is_ok()); let second = aur_build::sandbox::backend_for(&remote_cfg(&addr, "two")); assert!(second.is_err(), "two tokens on one address must not silently share a listener"); } #[test] fn two_concurrent_builds_do_not_fight_over_the_listener() { let addr = format!("127.0.0.1:{}", free_port()); let (mut env, _fa, _fb) = env_with_two_fixtures("plain-make", "install-hooks"); env.cfg.max_concurrent_builds = 2; env.cfg.sandbox = aur_build::config::Sandbox::Remote { listen: addr.clone(), token: "concurrent".into(), shared_workspace: true }; let mut ids = Vec::new(); for name in ["plain-make", "install-hooks"] { let c = approve_fixture(&env, name); ids.push(pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap()); } assert_eq!(ids.len(), 2); let first = Ctx::new(&env.cfg, &env.db, &ids[0]); let second = Ctx::new(&env.cfg, &env.db, &ids[1]); assert!(first.is_ok(), "first build: {:?}", first.err()); assert!(second.is_ok(), "second concurrent build must not lose the port race"); }