use super::*;
use crate::archives;
const UNCHECKED: &str =
"actions/archives/actions_checkout/08eba0b27e820071cde6df949e0beb9ba4906955.tar.gz";
fn engine_with_archives(
github: &FakeGithub,
docker: &FakeDocker,
dir: &TestDir,
config: &str,
) -> (Engine<FakeGithub, FakeDocker>, Handle) {
let store = Store::open(&dir.0.join("history.db")).unwrap();
Engine::new(
Config::parse(config).unwrap(),
github.clone(),
docker.clone(),
Setup {
names: Names::new("runnerd"),
identity: identity("ci", ID_A),
memory_max: None,
cpus: None,
announced: store.announced().unwrap(),
paused: store.paused_repositories().unwrap(),
store,
paths: Paths {
cgroup: dir.0.join("cgroup"),
proc: dir.0.join("proc"),
sys: dir.0.join("sys"),
},
build_cache: None,
action_archives: Some(dir.0.join("actions")),
unsynced: dir.0.join("unsynced"),
durability: Durability::new(|| {}, |_| false),
},
)
}
async fn first_runner(
config: &str,
checked: bool,
) -> (Option<bollard::models::Mount>, Vec<String>) {
let (github, docker, dir) = (
FakeGithub::default(),
FakeDocker::default(),
TestDir::new("archives"),
);
let unchecked = dir.0.join(UNCHECKED);
std::fs::create_dir_all(unchecked.parent().unwrap()).unwrap();
std::fs::write(&unchecked, b"not what any check verified").unwrap();
boot(&dir, "boot-1");
let (mut engine, _handle) = engine_with_archives(&github, &docker, &dir, config);
let _task = checked.then(|| engine.archives.start(Arc::new(github.clone())).unwrap());
engine.recover().await.unwrap();
if checked && Config::parse(config).unwrap().caches.actions.enabled {
for _ in 0..1000 {
if engine.archives.runner().is_some() {
break;
}
tokio::time::sleep(Duration::from_millis(5)).await;
}
assert!(
!unchecked.exists(),
"the file no check saw went to quarantine before runners got the archives"
);
}
{
let mut state = github.0.lock().unwrap();
state.queued = vec![job(1)];
state.jobs.insert(1, details(1, "in_progress", None));
}
engine.tick(Shutdown::No).await;
let state = docker.0.lock().unwrap();
let (name, mounts) = state.mounts.iter().next().expect("a runner started");
let mount = mounts
.iter()
.find(|mount| mount.target.as_deref() == Some(archives::MOUNT))
.cloned();
let env = state.envs[name]
.iter()
.filter(|env| env.starts_with(archives::ENV))
.cloned()
.collect();
(mount, env)
}
#[tokio::test]
async fn a_runner_reads_the_action_archives_read_only_as_its_archive_cache() {
let (mount, env) = first_runner(CONFIG, true).await;
let mount = mount.expect("the archives are mounted");
assert_eq!(mount.read_only, Some(true), "a job cannot plant an archive");
assert!(
mount
.source
.as_deref()
.is_some_and(|source| source.ends_with("actions/archives")),
"only archives/ is mounted, never staging/ or quarantine/: {mount:?}"
);
assert_eq!(env, vec![format!("{}={}", archives::ENV, archives::MOUNT)]);
}
#[tokio::test]
async fn a_runner_gets_no_archives_while_they_are_off_or_before_their_first_check() {
let off = format!("{CONFIG}\n[caches.actions]\nenabled = false\n");
for (config, checked) in [(off.as_str(), true), (CONFIG, false)] {
let (mount, env) = first_runner(config, checked).await;
assert_eq!(mount, None, "no mount");
assert!(env.is_empty(), "no archive setting: {env:?}");
}
}
async fn until(
engine: &Engine<FakeGithub, FakeDocker>,
what: &str,
until: impl Fn(&archives::Link) -> bool,
) {
for _ in 0..1000 {
if until(&engine.archives) {
return;
}
tokio::time::sleep(Duration::from_millis(5)).await;
}
panic!("{what} never came");
}
fn reader_left_running(github: &FakeGithub, docker: &FakeDocker, output: &str) -> String {
let mut container = runner_container(1, 7, true);
container
.labels
.insert(archives::LABEL.to_owned(), "1".to_owned());
let name = container.name.clone();
{
let mut state = docker.0.lock().unwrap();
state.memory.insert(container.id.clone(), 4 << 30);
state.containers = vec![container];
state
.captures
.insert(("1".to_owned(), "0".to_owned()), output.to_owned());
}
github.0.lock().unwrap().runners = vec![runner(7, &name, true)];
name
}
#[tokio::test]
async fn a_restart_counts_the_runners_reading_the_archives_unchecked_and_names_them_if_the_first_check_moves_a_file()
{
for moved in [true, false] {
let (github, docker, dir) = (
FakeGithub::default(),
FakeDocker::default(),
TestDir::new("archives"),
);
if moved {
let unchecked = dir.0.join(UNCHECKED);
std::fs::create_dir_all(unchecked.parent().unwrap()).unwrap();
std::fs::write(&unchecked, b"not what any check verified").unwrap();
}
let reader = reader_left_running(&github, &docker, "");
boot(&dir, "boot-1");
let (mut engine, _handle) = engine_with_archives(&github, &docker, &dir, CONFIG);
engine.recover().await.unwrap();
let status = engine.archives.status().expect("a status before any check");
assert_eq!(
status.unchecked, 1,
"the adopted runner reads them unchecked"
);
assert!(
status
.line(0)
.ends_with("1 adopted runner reads them before a check"),
"{}",
status.line(0)
);
let _task = engine.archives.start(Arc::new(github.clone())).unwrap();
until(&engine, "the first check", |link| {
link.status().is_some_and(|status| status.unchecked == 0)
})
.await;
let status = engine.archives.status().unwrap();
let exposed: Vec<&str> = status
.failures
.iter()
.filter(|failure| failure.kind == api::ArchiveFailureKind::Exposed)
.map(|failure| failure.subject.as_str())
.collect();
if moved {
assert_eq!(exposed, vec![reader.as_str()], "named, and left running");
assert!(
status.attention_title().is_some_and(
|title| title.starts_with("a runner may have read an action archive")
),
"{:?}",
status.attention_title()
);
assert!(
docker
.0
.lock()
.unwrap()
.containers
.iter()
.any(|c| c.name == reader && c.running),
"it is not killed"
);
engine.archives.released(&reader);
assert!(
engine
.archives
.status()
.unwrap()
.failures
.iter()
.all(|failure| failure.kind != api::ArchiveFailureKind::Exposed),
"gone, it is no longer named"
);
} else {
assert!(exposed.is_empty(), "the check moved nothing: {exposed:?}");
}
}
}
#[tokio::test]
async fn every_check_withdraws_the_runners_view_until_it_returns_good() {
let (github, docker, dir) = (
FakeGithub::default(),
FakeDocker::default(),
TestDir::new("archives"),
);
boot(&dir, "boot-1");
archives::keep(
&dir.0.join("actions"),
"actions/checkout",
"08eba0b27e820071cde6df949e0beb9ba4906955",
b"an archive a refresh kept",
)
.unwrap();
let (mut engine, _handle) = engine_with_archives(&github, &docker, &dir, CONFIG);
let _task = engine.archives.start(Arc::new(github.clone())).unwrap();
engine.recover().await.unwrap();
until(&engine, "the first view", |link| link.runner().is_some()).await;
let mut ready = engine.archives.ready_changes();
ready.borrow_and_update();
github.hold_actions();
let reloaded = format!("{CONFIG}\n[caches.actions]\nkeep = \"7d\"\n");
engine.archives.reload(&Config::parse(&reloaded).unwrap());
let withdrawn = tokio::time::timeout(
Duration::from_secs(5),
async { ready.wait_for(Option::is_none).await.map(|_| ()) },
)
.await;
assert!(
matches!(withdrawn, Ok(Ok(()))),
"the check withdraws the view"
);
assert!(
engine.archives.runner().is_none(),
"a start during the check gets no view"
);
github.release_actions();
until(&engine, "the view again", |link| link.runner().is_some()).await;
}
#[tokio::test]
async fn a_job_log_that_shows_an_immutable_package_keeps_its_repository_out_of_the_archives() {
let line = "2026-11-20T10:00:00.0000000Z ##[group]Download immutable action package 'actions/checkout@v6'";
let (github, docker, dir) = (
FakeGithub::default(),
FakeDocker::default(),
TestDir::new("archives"),
);
reader_left_running(
&github,
&docker,
&format!("@@job 1 1\n@@page 1 0 {}\n{line}\n", line.len() + 1),
);
github
.0
.lock()
.unwrap()
.jobs
.insert(1, details(1, "in_progress", None));
boot(&dir, "boot-1");
let (mut engine, _handle) = engine_with_archives(&github, &docker, &dir, CONFIG);
let _task = engine.archives.start(Arc::new(github.clone())).unwrap();
engine.recover().await.unwrap();
until(&engine, "the package's report", |link| {
link.status().is_some_and(|status| {
status.failures.iter().any(|failure| {
failure.kind == api::ArchiveFailureKind::Package
&& failure.subject == "actions/checkout"
})
})
})
.await;
}