mod common; use std::sync::Arc; use std::time::Duration; use aur_build::config::Config; use aur_build::sandbox::kata::{KataBackend, LocalAgentProvisioner}; use aur_build::sandbox::{Cmd, SandboxBackend, SandboxSpec}; use common::{env_with_fixture, shim_dir}; fn agent_sandbox_config() -> Config { let tmp = tempfile::tempdir().unwrap(); std::fs::write( tmp.path().join("config.json"), r#"{ "sandbox": { "kind": "agent-sandbox", "api": "https://k8s.test:6443", "namespace": "builds", "bearer": "kube-bearer", "image": "aur-build-sandbox:test", "listen": "127.0.0.1:0", "token": "kata-cfg-token", "shared_workspace": true } }"#, ) .unwrap(); let cfg = Config::load(tmp.path()).unwrap(); cfg.validate().expect("the config must survive the startup guard"); cfg } #[test] fn a_provisioned_sandbox_rendezvous_and_runs_a_command() { let cfg = Config::default(); let prov = Arc::new(LocalAgentProvisioner::new(shim_dir().join("sandbox-agent"))); let backend = KataBackend::bind(&cfg, "127.0.0.1:0", "kata-token", prov.clone()).unwrap(); let sbx = backend .acquire(&SandboxSpec::new("extra-x86_64", "build-1", Duration::from_secs(15))) .expect("the provisioned agent should dial in and rendezvous"); let out = sbx.run(&Cmd::new(["/bin/echo", "hi from the sandbox"]), &mut |_| {}).unwrap(); assert!(out.success()); assert_eq!(String::from_utf8_lossy(&out.stdout), "hi from the sandbox\n"); sbx.destroy().unwrap(); } #[test] fn agent_sandbox_is_a_backend_not_a_fallthrough_to_loopback() { let cfg = agent_sandbox_config(); let backend = aur_build::sandbox::backend_for(&cfg).unwrap(); let id = backend.status().unwrap()[0].id.clone(); assert!(id.contains("kata@"), "expected a kata backend, got {id}"); let again = aur_build::sandbox::backend_for(&cfg).unwrap(); assert_eq!(again.status().unwrap()[0].id, id, "two builds must share one kata backend"); } #[test] fn a_loopback_config_never_becomes_agent_sandbox_or_vice_versa() { let local = Config::default(); let id = aur_build::sandbox::backend_for(&local).unwrap().status().unwrap()[0].id.clone(); assert!(!id.contains("kata@"), "loopback must not report kata: {id}"); assert!(!id.contains("remote@"), "loopback must not report remote: {id}"); } #[test] fn a_fixture_builds_and_publishes_through_the_kata_lease_path() { let (env, _fx) = env_with_fixture("plain-make"); aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, "plain-make").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(); let c = aur_build::review::candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); let id = aur_build::pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); let mut ctx = aur_build::pipeline::Ctx::new(&env.cfg, &env.db, &id).unwrap(); let prov = Arc::new(LocalAgentProvisioner::new(shim_dir().join("sandbox-agent"))); let backend = KataBackend::bind(&env.cfg, "127.0.0.1:0", "kata-e2e", prov).unwrap(); ctx.backend = Arc::new(backend); let out = aur_build::pipeline::run_with(&ctx).unwrap(); let release = match out { aur_build::pipeline::Outcome::Succeeded { release_id } => release_id, other => panic!("the kata path must publish, got {other:?}"), }; assert!(release > 0); let world = aur_build::pipeline::publish::index_world(&env.db).unwrap(); assert!( world.iter().any(|w| w.pkgbase == "plain-make" && w.version == "1.0-1"), "the index must serve what the kata build produced: {world:?}" ); let log = std::fs::read_to_string(env.cfg.build_log_path(&id)).unwrap(); assert!(log.contains("Making package"), "agent output must reach the log:\n{log}"); let shimlog = env.cfg.hooks.shim_dir.clone().unwrap(); assert_eq!(aur_build::shims::called_with(&shimlog, "makepkg", ""), 1); }