pub mod fetch;
pub mod folders;
pub mod repo;
pub mod tar;
pub mod wip;
use std::collections::{BTreeMap, BTreeSet};
use std::io::Read;
use std::os::unix::fs::PermissionsExt;
use std::path::{Component, Path, PathBuf};
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;
use anyhow::{Context, Result, anyhow, bail};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use self::repo::Repo;
use super::transfer::{self, Header, Received, Receivers};
use crate::serve::api::ApiError;
use crate::config::{Build, Config, GpuNeed, MountContext, Placement, ResolvedSandbox};
use crate::worker_policy::{self, Allow, Policy, Refusal};
pub const ROOT_TAR_KIND: &str = "root-tar";
pub const BUNDLE_KIND: &str = "bundle";
pub const ROOT_TAR_LIMIT: u64 = 256 << 20;
pub const BUNDLE_LIMIT: u64 = 4 << 30;
pub const ROOT_SKIPS: [&str; 3] = [".worktrees", "shared-volumes", "shared-files"];
#[derive(Debug)]
pub enum Unprepared {
NotMovable(String),
Failed(anyhow::Error),
}
impl std::fmt::Display for Unprepared {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Unprepared::NotMovable(why) => write!(f, "runs where it is: {why}"),
Unprepared::Failed(e) => write!(f, "{e:#}"),
}
}
}
impl From<anyhow::Error> for Unprepared {
fn from(e: anyhow::Error) -> Self {
Unprepared::Failed(e)
}
}
fn not_movable<T>(why: impl Into<String>) -> Result<T, Unprepared> {
Err(Unprepared::NotMovable(why.into()))
}
pub const HAVES_METHOD: &str = "code.haves";
pub const HAVES_REPOS_MAX: usize = 32;
pub const ROOTS_CAP: usize = 1024;
pub const HAVES_HELLO_WAIT: Duration = Duration::from_secs(5);
pub const HAVES_WAIT: Duration = Duration::from_secs(30);
pub const SEND_WRITE_WAIT: Duration = Duration::from_secs(5 * 60);
pub const ANSWER_WAIT: Duration = Duration::from_secs(35 * 60);
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Haves {
#[serde(default)]
pub roots: BTreeSet<String>,
#[serde(default)]
pub tips: BTreeMap<String, Vec<String>>,
}
pub trait Transfer {
fn haves(&self, repos: &[String]) -> Result<Haves> {
let _ = repos;
Ok(Haves::default())
}
fn send(&self, header: &Header, body: &mut dyn Read) -> Result<()>;
fn pull(&self, kind: &str, name: &str, limit: u64, dest: &Path) -> Result<u64> {
let _ = (kind, limit, dest);
bail!("this transfer pulls nothing (asked for {name})")
}
}
pub struct LinkTransfer {
pub dir: PathBuf,
pub worker: String,
}
impl LinkTransfer {
pub fn for_worker(dir: &Path, worker: &str) -> LinkTransfer {
LinkTransfer { dir: dir.to_path_buf(), worker: worker.to_string() }
}
}
impl Transfer for LinkTransfer {
fn haves(&self, repos: &[String]) -> Result<Haves> {
use crate::serve::machines::{link, ssh::Duplex};
let stream = super::route::connect(&self.dir, &self.worker, crate::devsbd::proto::channel::API)
.with_context(|| format!("cannot reach the worker `{}`", self.worker))?;
stream.set_write_timeout(Some(HAVES_WAIT)).context("cannot set a write timeout")?;
let mut link = link::handshake(&self.worker, Duplex::socket(stream), HAVES_HELLO_WAIT, link::Over::Link)
.map_err(|e| anyhow!("{e}"))?;
let r = link.conn.call_within(HAVES_METHOD, json!({ "repos": repos }), HAVES_WAIT)?;
let r = r.with_context(|| format!("no `{HAVES_METHOD}` answer within {}s", HAVES_WAIT.as_secs()))?;
if let Some(e) = r.error {
bail!("{HAVES_METHOD}: {}", e.message);
}
serde_json::from_value(r.result.unwrap_or(Value::Null)).with_context(|| format!("bad {HAVES_METHOD} answer"))
}
fn send(&self, header: &Header, body: &mut dyn Read) -> Result<()> {
let stream = super::route::connect(&self.dir, &self.worker, crate::devsbd::proto::channel::TRANSFER)
.with_context(|| format!("cannot reach the worker `{}`", self.worker))?;
stream.set_write_timeout(Some(SEND_WRITE_WAIT)).context("cannot set a write timeout")?;
stream.set_read_timeout(Some(ANSWER_WAIT)).context("cannot set a read timeout")?;
transfer::send(stream, header, body)
}
fn pull(&self, kind: &str, name: &str, limit: u64, dest: &Path) -> Result<u64> {
let stream = super::route::connect(&self.dir, &self.worker, crate::devsbd::proto::channel::TRANSFER)
.with_context(|| format!("cannot reach the worker `{}`", self.worker))?;
stream.set_write_timeout(Some(SEND_WRITE_WAIT)).context("cannot set a write timeout")?;
stream.set_read_timeout(Some(SEND_WRITE_WAIT)).context("cannot set a read timeout")?;
transfer::pull(stream, kind, name, limit, dest)
}
}
impl LinkTransfer {
pub fn fetch_instance(&self, instance: &str, haves: &[String]) -> Result<fetch::Fetched> {
use crate::serve::machines::{link, ssh::Duplex};
let method = fetch::FETCH_METHOD;
let stream = super::route::connect(&self.dir, &self.worker, crate::devsbd::proto::channel::API)
.with_context(|| format!("cannot reach the worker `{}`", self.worker))?;
stream.set_write_timeout(Some(HAVES_WAIT)).context("cannot set a write timeout")?;
let mut link = link::handshake(&self.worker, Duplex::socket(stream), HAVES_HELLO_WAIT, link::Over::Link)
.map_err(|e| anyhow!("{e}"))?;
let r = link.conn.call_within(method, json!({ "instance": instance, "haves": haves }), fetch::FETCH_WAIT)?;
let r = r.with_context(|| format!("no `{method}` answer within {}s", fetch::FETCH_WAIT.as_secs()))?;
if let Some(e) = r.error {
bail!("{method} on {}: {}", self.worker, e.message);
}
serde_json::from_value(r.result.unwrap_or(Value::Null)).with_context(|| format!("bad {method} answer"))
}
pub fn folders(&self, root: &Path, sandbox: &str) -> Result<Result<folders::Folders, (String, String)>> {
use crate::serve::machines::{link, ssh::Duplex};
let method = folders::FOLDERS_METHOD;
let stream = super::route::connect(&self.dir, &self.worker, crate::devsbd::proto::channel::API)
.with_context(|| format!("cannot reach `{}`", self.worker))?;
stream.set_write_timeout(Some(HAVES_WAIT)).context("cannot set a write timeout")?;
let mut link = link::handshake(&self.worker, Duplex::socket(stream), HAVES_HELLO_WAIT, link::Over::Link)
.map_err(|e| anyhow!("{e}"))?;
let r = link.conn.call_within(method, json!({ "root": root, "sandbox": sandbox }), HAVES_WAIT)?;
let r = r.with_context(|| format!("no `{method}` answer within {}s", HAVES_WAIT.as_secs()))?;
if let Some(e) = r.error {
return Ok(Err((e.code, e.message)));
}
Ok(Ok(serde_json::from_value(r.result.unwrap_or(Value::Null)).with_context(|| format!("bad {method} answer"))?))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Layout {
pub root: PathBuf,
pub folders: Vec<PathBuf>,
}
pub fn layout(root: &Path, folders: &[PathBuf]) -> Result<Layout, String> {
let names = |p: &Path| -> Vec<String> {
p.components().filter_map(|c| match c {
Component::Normal(n) => Some(n.to_string_lossy().into_owned()),
_ => None,
})
.collect()
};
let root_names = names(root);
let mut climbs = Vec::new();
for f in folders {
let f_names = names(f);
let common = root_names.iter().zip(&f_names).take_while(|(a, b)| a == b).count();
let ups = root_names.len() - common;
let down = f_names[common..].to_vec();
if ups == 0 {
return Err(format!("folder {} is in the config root", f.display()));
}
if down.is_empty() {
return Err(format!("folder {} holds the config root", f.display()));
}
climbs.push((ups, down));
}
let depth = climbs.iter().map(|(ups, _)| *ups).max().unwrap_or(0).max(1);
if root_names.len() < depth {
return Err(format!("config root {} is too shallow for its folders", root.display()));
}
let root_at: Vec<String> = root_names[root_names.len() - depth..].to_vec();
let mut out = Vec::new();
for (ups, down) in climbs {
let at: PathBuf = root_at[..depth - ups].iter().chain(&down).collect();
out.push(at);
}
for (i, a) in out.iter().enumerate() {
for b in &out[i + 1..] {
if a.starts_with(b) || b.starts_with(a) {
return Err(format!("folders {} and {} nest", folders[i].display(), b.display()));
}
}
}
Ok(Layout { root: root_at.iter().collect(), folders: out })
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RepoOffer {
pub host: PathBuf,
pub at: PathBuf,
pub branch: String,
pub wip: bool,
pub id: String,
}
#[derive(Debug, Clone)]
pub struct Offer {
pub tar: Vec<u8>,
pub hash: String,
pub layout: Layout,
pub repos: Vec<RepoOffer>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Prepared {
pub remote_root: PathBuf,
pub folders: Vec<(PathBuf, PathBuf, String)>,
pub sent: Vec<Sent>,
pub notes: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Sent {
pub id: String,
pub host: PathBuf,
pub refs: BTreeMap<String, String>,
}
pub fn offer(config_root: &Path, sandbox: &str, branch: Option<&str>) -> Result<Offer, Unprepared> {
let root = config_root.canonicalize().with_context(|| format!("config root {} does not exist", config_root.display()))?;
let config = Config::load(&root)?;
let resolved = config.resolve_sandbox(sandbox)?;
let props = &resolved.properties;
let folder = props.folder.as_deref().with_context(|| format!("sandbox `{sandbox}` has no `folder`"))?;
let resolve = |f: &str| root.join(f).canonicalize().with_context(|| format!("sandbox folder `{f}` does not exist"));
let main = resolve(folder)?;
if main.starts_with(&root) {
return not_movable(format!("its folder {} is in the config root (the config root travels as a tar, without `.git`)", main.display()));
}
let mut outside = vec![main];
for entry in props.folders.iter().flat_map(|f| f.values()) {
if entry.path().contains("${") {
return not_movable(format!("`folders` entry `{}` uses a variable", entry.path()));
}
let host = resolve(entry.path())?;
if !host.starts_with(&root) && !outside.contains(&host) {
outside.push(host);
}
}
let tied = ties(&root, &config, &resolved);
if !tied.is_empty() {
return not_movable(tied.join(", "));
}
let layout = layout(&root, &outside).map_err(Unprepared::NotMovable)?;
let mut repos_out = Vec::new();
for (i, (host, at)) in outside.iter().zip(&layout.folders).enumerate() {
repos_out.push(repo_offer(host, at, if i == 0 { branch } else { None })?);
}
let tar = pack_root(&root, &layout.root)?;
let hash = short_hash(&tar);
Ok(Offer { tar, hash, layout, repos: repos_out })
}
pub fn repo_offer(host: &Path, at: &Path, branch: Option<&str>) -> Result<RepoOffer, Unprepared> {
let Some(checkout) = repo::probe(host)? else {
return not_movable(format!("{} isn't a git checkout", host.display()));
};
if let Some(b) = branch {
repo::check_branch(b)?;
}
let requested = match branch {
Some(b) if checkout.branch.as_deref() != Some(b) && repo::has_branch(Repo::Checkout(host), b)? => Some(b.to_string()),
_ => None,
};
if let Some(b) = requested {
let id = repo::identity(Repo::Checkout(host), &format!("refs/heads/{b}"))?;
return Ok(RepoOffer { host: host.to_path_buf(), at: at.to_path_buf(), branch: b, wip: false, id });
}
let Some(current) = checkout.branch else {
return not_movable(format!("{} has a detached HEAD (check out a branch first)", host.display()));
};
repo::check_branch(¤t)?;
let id = match repo::ensure_root(host)? {
Some(_) => repo::identity(Repo::Checkout(host), repo::ROOT_REF)?,
None => repo::identity(Repo::Checkout(host), "HEAD")?,
};
Ok(RepoOffer { host: host.to_path_buf(), at: at.to_path_buf(), branch: current, wip: true, id })
}
pub fn ties_of(config_root: &Path, sandbox: &str) -> Result<(Vec<String>, Option<GpuNeed>)> {
let root = config_root.canonicalize().with_context(|| format!("config root {} does not exist", config_root.display()))?;
let config = Config::load(&root)?;
let resolved = config.resolve_sandbox(sandbox)?;
Ok((ties(&root, &config, &resolved), super::placed::gpu_need(&config, &resolved)))
}
pub fn ties(root: &Path, config: &Config, sandbox: &ResolvedSandbox) -> Vec<String> {
let props = &sandbox.properties;
let at = |f: &str| {
let p = root.join(f);
worker_policy::resolve(&p).unwrap_or(p)
};
let main = props.folder.as_deref().map(at);
let mut moved: Vec<PathBuf> = main.iter().cloned().collect();
moved.extend(props.folders.iter().flat_map(|f| f.values()).filter(|e| !e.path().contains("${")).map(|e| at(e.path())));
moved.retain(|f| !f.starts_with(root));
let policy = Policy {
root: root.to_path_buf(),
data: root.to_path_buf(),
allow: Allow { binds: moved, ..Allow::default() },
sockets: worker_policy::host_sockets(),
};
let mut out: Vec<String> = Vec::new();
if props.placement == Some(Placement::Home) {
out.push("`placement = \"home\"`".into());
}
out.extend(
policy
.config_refusals(root, config, sandbox)
.into_iter()
.filter(|r| r.allow.as_deref() != Some("gpus = true"))
.map(tie),
);
let root_s = root.to_string_lossy();
let shared = root.join("shared-volumes");
let shared_s = shared.to_string_lossy();
let folder_s = main.as_deref().map(|m| m.to_string_lossy().into_owned()).unwrap_or_default();
let basename = main.as_deref().and_then(Path::file_name).map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
let ctx = MountContext {
config_dir: &root_s,
workspace_folder: &folder_s,
workspace_folder_basename: &basename,
shared_volumes: &shared_s,
instance: "<instance>",
container_env: None,
};
for mount in props.mounts.iter().flatten() {
let Ok(m) = mount.resolve(&ctx) else { continue };
let Some(source) = m.source.as_deref().filter(|_| m.kind == "bind") else { continue };
if source.contains("${") || !Path::new(source).is_absolute() || reads_local_env(mount) {
continue;
}
let source_path = Path::new(source);
if source_path.starts_with(&shared) && !source_path.components().any(|c| c == std::path::Component::ParentDir) {
continue;
}
if let Err(r) = policy.check_bind(source) {
out.push(tie(r));
}
}
let services: Vec<Build> = props.services.iter().flatten().filter_map(|n| config.resolve_service(n).ok()?.spec.build).collect();
for build in props.build.iter().chain(&services) {
let paths = [("build context", build.context.as_deref().or(Some("."))), ("dockerfile", build.dockerfile.as_deref())];
for (what, rel) in paths {
let Some(path) = rel.map(|r| root.join(r)).filter(|p| p.exists()) else { continue };
if let Err(r) = policy.check_path(&path, what) {
out.push(tie(r));
}
}
}
let mut seen = std::collections::HashSet::new();
out.retain(|t| seen.insert(t.clone()));
out
}
fn reads_local_env(mount: &crate::config::Mount) -> bool {
let raw = match mount {
crate::config::Mount::Shorthand(s) => s.as_str(),
crate::config::Mount::Object(o) => o.source.as_deref().unwrap_or_default(),
};
raw.contains("${localEnv:")
}
fn tie(r: Refusal) -> String {
r.what.replace("(outside the fleet root)", "(outside the config root)")
}
pub fn short_hash(bytes: &[u8]) -> String {
Sha256::digest(bytes).iter().take(8).map(|b| format!("{b:02x}")).collect()
}
pub fn pack_root(root: &Path, at: &Path) -> Result<Vec<u8>> {
let at = at.to_str().context("the root's name isn't UTF-8")?.to_string();
let mut w = tar::Writer::default();
let mut prefix = String::new();
for part in at.split('/') {
prefix = if prefix.is_empty() { part.to_string() } else { format!("{prefix}/{part}") };
w.dir(&prefix)?;
}
walk(root, "", &at, &mut w)?;
Ok(w.finish())
}
fn walk(dir: &Path, rel: &str, at: &str, w: &mut tar::Writer) -> Result<()> {
let mut entries: Vec<_> = std::fs::read_dir(dir)
.with_context(|| format!("cannot list {}", dir.display()))?
.collect::<std::io::Result<_>>()
.with_context(|| format!("cannot list {}", dir.display()))?;
entries.sort_by_key(|e| e.file_name());
for e in entries {
let name = e.file_name();
let name = name.to_str().with_context(|| format!("{} has a non-UTF-8 name", e.path().display()))?;
if name == ".git" || (rel.is_empty() && ROOT_SKIPS.contains(&name)) {
continue;
}
let rel = if rel.is_empty() { name.to_string() } else { format!("{rel}/{name}") };
let member = format!("{at}/{rel}");
let path = e.path();
let meta = path.symlink_metadata().with_context(|| format!("cannot read {}", path.display()))?;
let kind = meta.file_type();
if kind.is_symlink() {
let target = std::fs::read_link(&path).with_context(|| format!("cannot read {}", path.display()))?;
let target = target.to_str().with_context(|| format!("{} points at a non-UTF-8 path", path.display()))?;
tar::check_link(&rel, target)?;
w.link(&member, target)?;
} else if kind.is_dir() {
w.dir(&member)?;
walk(&path, &rel, at, w)?;
} else if kind.is_file() {
if w.len() as u64 + meta.len() > ROOT_TAR_LIMIT {
bail!("the config root is over {ROOT_TAR_LIMIT} bytes");
}
let data = std::fs::read(&path).with_context(|| format!("cannot read {}", path.display()))?;
w.file(&member, meta.permissions().mode() & 0o100 != 0, &data)?;
}
}
Ok(())
}
pub fn send(offer: &Offer, worker_root: &Path, t: &dyn Transfer) -> Result<Prepared> {
let ids: Vec<String> = offer.repos.iter().map(|r| r.id.clone()).collect::<BTreeSet<_>>().into_iter().collect();
let haves = t.haves(&ids).context("cannot ask the worker what it has")?;
if !haves.roots.contains(&offer.hash) {
let (size, sha256) = transfer::digest(&offer.tar[..])?;
let name = format!("root-{}.tar", offer.hash);
let header = Header { kind: ROOT_TAR_KIND.into(), name, size, sha256, to: None, repo: None, refs: None };
t.send(&header, &mut &offer.tar[..]).context("cannot send the config root")?;
}
let tree = worker_root.join("roots").join(&offer.hash);
let mut folders = Vec::new();
let mut sent = Vec::new();
for r in &offer.repos {
let tips = haves.tips.get(&r.id).map(Vec::as_slice).unwrap_or_default();
let refs = send_repo(r, &offer.hash, tips, t).with_context(|| format!("cannot send {}", r.host.display()))?;
folders.push((r.host.clone(), tree.join(&r.at), r.branch.clone()));
sent.push(Sent { id: r.id.clone(), host: r.host.clone(), refs });
}
Ok(Prepared { remote_root: tree.join(&offer.layout.root), folders, sent, notes: Vec::new() })
}
pub fn mirror(sent: &[Sent], t: &dyn Transfer) -> Vec<(PathBuf, String)> {
let ids: Vec<String> = sent.iter().map(|s| s.id.clone()).collect::<BTreeSet<_>>().into_iter().collect();
let haves = match t.haves(&ids) {
Ok(h) => h,
Err(e) => {
let why = format!("{:#}", e.context("cannot ask the control plane what it has"));
return sent.iter().map(|s| (s.host.clone(), why.clone())).collect();
}
};
let mut failed = Vec::new();
for s in sent {
let tips = haves.tips.get(&s.id).map(Vec::as_slice).unwrap_or_default();
if let Err(e) = mirror_repo(s, tips, t) {
failed.push((s.host.clone(), format!("{e:#}")));
}
}
failed
}
fn mirror_repo(s: &Sent, tips: &[String], t: &dyn Transfer) -> Result<()> {
let checkout = Repo::Checkout(&s.host);
let file = repo::TempPath::new(&checkout.git_dir()?, "bundle");
let temps = s.refs.values().map(|id| repo::TempRef::at(&s.host, id)).collect::<Result<Vec<_>>>()?;
let names: Vec<&str> = temps.iter().map(|r| r.name.as_str()).collect();
let new = repo::bundle_refs(checkout, &names, tips, &file.0)?;
drop(temps);
let open = || std::fs::File::open(&file.0).with_context(|| format!("cannot read {}", file.0.display()));
let mut body: Box<dyn Read> = if new { Box::new(open()?) } else { Box::new(std::io::empty()) };
let (size, sha256) = if new { transfer::digest(open()?)? } else { transfer::digest(std::io::empty())? };
let header = Header {
kind: BUNDLE_KIND.into(),
name: "repo.bundle".into(),
size,
sha256,
to: None,
repo: Some(s.id.clone()),
refs: Some(s.refs.clone()),
};
t.send(&header, &mut body)
}
fn send_repo(r: &RepoOffer, hash: &str, tips: &[String], t: &dyn Transfer) -> Result<BTreeMap<String, String>> {
let to = r.at.to_str().context("a folder's name isn't UTF-8")?;
send_repo_as(r, BUNDLE_KIND, &format!("{hash}/{to}"), tips, t)
}
fn send_repo_as(r: &RepoOffer, kind: &str, to: &str, tips: &[String], t: &dyn Transfer) -> Result<BTreeMap<String, String>> {
let full = format!("refs/heads/{}", r.branch);
let checkout = Repo::Checkout(&r.host);
let file = repo::TempPath::new(&checkout.git_dir()?, "bundle");
let mut refs = BTreeMap::new();
let new = if r.wip {
let temp = repo::TempRef::new(&r.host);
let snap = wip::snapshot(&r.host, &temp.name)?;
if snap.branch.as_deref() != Some(r.branch.as_str()) {
bail!("{} isn't on {} any more", r.host.display(), r.branch);
}
let base_ref = if repo::has_branch(checkout, &r.branch)? { full.as_str() } else { repo::ROOT_REF };
refs.insert(full.clone(), snap.base);
refs.insert(repo::wip_ref(&r.branch), snap.wip);
let new = repo::bundle_refs(checkout, &[base_ref, &temp.name], tips, &file.0)?;
drop(temp); new
} else {
let commit = repo::commit_at(checkout, &full)?.with_context(|| format!("no {full}"))?;
refs.insert(full.clone(), commit);
repo::bundle_refs(checkout, &[&full], tips, &file.0)?
};
let open = || std::fs::File::open(&file.0).with_context(|| format!("cannot read {}", file.0.display()));
let mut body: Box<dyn Read> = if new { Box::new(open()?) } else { Box::new(std::io::empty()) };
let (size, sha256) = if new { transfer::digest(open()?)? } else { transfer::digest(std::io::empty())? };
let header = Header {
kind: kind.into(),
name: "repo.bundle".into(),
size,
sha256,
to: Some(to.to_string()),
repo: Some(r.id.clone()),
refs: Some(refs.clone()),
};
t.send(&header, &mut body)?;
Ok(refs)
}
pub fn haves_call(fleet_root: &Path, params: &Value) -> Result<Value, ApiError> {
let repos = haves_params(params)?;
let haves = haves_of(fleet_root, &repos).map_err(ApiError::internal)?;
serde_json::to_value(haves).map_err(|e| ApiError::internal(e.into()))
}
pub fn haves_params(params: &Value) -> Result<Vec<String>, ApiError> {
let Some(list) = params.get("repos").and_then(Value::as_array) else {
return Err(ApiError::invalid(format!("`{HAVES_METHOD}` needs `repos`, a list of repo identities")));
};
if list.len() > HAVES_REPOS_MAX {
return Err(ApiError::invalid(format!("`{HAVES_METHOD}` asks about at most {HAVES_REPOS_MAX} repos")));
}
list.iter()
.map(|v| {
let id = v.as_str().ok_or_else(|| ApiError::invalid("`repos` holds repo identities (strings)"))?;
repo::check_id(id).map_err(|e| ApiError::invalid(format!("{e:#}")))?;
Ok(id.to_string())
})
.collect()
}
pub fn haves_of(fleet_root: &Path, repos: &[String]) -> Result<Haves> {
let mut out = Haves::default();
let roots = fleet_root.join("roots");
match std::fs::read_dir(&roots) {
Ok(entries) => {
let mut names: Vec<String> = entries
.flatten()
.filter(|e| e.file_type().is_ok_and(|t| t.is_dir()))
.filter_map(|e| e.file_name().into_string().ok())
.filter(|n| is_hash(n))
.collect();
names.sort_unstable();
names.truncate(ROOTS_CAP);
out.roots = names.into_iter().collect();
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(anyhow!(e).context(format!("cannot list {}", roots.display()))),
}
for id in repos {
repo::check_id(id)?;
let cache = cache_path(fleet_root, id);
if cache.symlink_metadata().is_ok_and(|m| m.is_dir()) && cache.join("HEAD").is_file() {
let tips = repo::tips(Repo::Bare(&cache))?;
if !tips.is_empty() {
out.tips.insert(id.clone(), tips);
}
}
}
Ok(out)
}
pub fn cache_path(fleet_root: &Path, id: &str) -> PathBuf {
fleet_root.join("repos").join(format!("{id}.git"))
}
fn make_cache(fleet_root: &Path, id: &str) -> Result<PathBuf> {
repo::check_id(id)?;
let cache = cache_path(fleet_root, id);
real_dir(cache.parent().context("a cache has a parent")?)?;
real_dir(&cache)?;
repo::ensure_bare(&cache)?;
Ok(cache)
}
fn real_dir(dir: &Path) -> Result<()> {
match std::fs::create_dir(dir) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
Err(e) => return Err(anyhow!(e).context(format!("cannot create {}", dir.display()))),
}
if !dir.symlink_metadata().is_ok_and(|m| m.is_dir()) {
bail!("{} isn't a directory", dir.display());
}
Ok(())
}
fn is_hash(s: &str) -> bool {
s.len() == 16 && s.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
}
pub fn prepare(t: &dyn Transfer, worker_root: &Path, config_root: &Path, sandbox: &str, branch: Option<&str>) -> Result<Prepared, Unprepared> {
let offer = offer(config_root, sandbox, branch)?;
Ok(send(&offer, worker_root, t)?)
}
fn next_seq() -> u32 {
static SEQ: AtomicU32 = AtomicU32::new(0);
SEQ.fetch_add(1, Ordering::Relaxed)
}
pub fn receivers(fleet_root: &Path, receivers: Receivers, in_use: InUse) -> Receivers {
let receivers = fetch::serves(fleet_root, receivers);
let log = receivers.log();
let roots = fleet_root.join("roots");
let (tar_in, tar_roots, bundle_in, bundle_roots) = (roots.clone(), roots.clone(), roots.clone(), roots);
let cache_in = fleet_root.join("repos").join(".in");
let fleet_root = fleet_root.to_path_buf();
receivers
.with(
ROOT_TAR_KIND,
ROOT_TAR_LIMIT,
move |h| {
let want = format!("root-{}.tar", &h.sha256[..16]);
if h.name != want || h.to.is_some() {
bail!("a root tar is named {want}, not {}", h.name);
}
Ok(tar_in.join(".in").join(&h.name))
},
|_| {},
)
.then(ROOT_TAR_KIND, move |r| {
let unpacked = unpack_root(&r.path, &tar_roots, &r.header.sha256[..16]);
let _ = std::fs::remove_file(&r.path);
unpacked
})
.with(
BUNDLE_KIND,
BUNDLE_LIMIT,
move |h| {
let name = format!("{}-{}.bundle", h.sha256, repo::nonce());
if h.to.is_none() {
let (id, _, _) = bundle_refs_of(h)?;
repo::check_id(id)?;
return Ok(cache_in.join(name));
}
let (hash, _) = bundle_target(h)?;
bundle_refs_of(h)?;
if !bundle_in.join(hash).is_dir() {
bail!("root {hash} isn't here (its tar goes first)");
}
Ok(bundle_in.join(".in").join(name))
},
|_| {},
)
.then(BUNDLE_KIND, move |r: &Received| {
let landed = (|| {
let (id, branch, wip) = bundle_refs_of(&r.header)?;
let refs = r.header.refs.as_ref().context("a bundle names its refs")?;
if r.header.to.is_none() {
let cache = make_cache(&fleet_root, id)?;
if r.header.size > 0 {
repo::unbundle(&cache, &r.path)?;
}
return repo::set_refs(&cache, refs);
}
let (hash, rest) = bundle_target(&r.header)?;
let tree = bundle_roots.join(hash);
let mut at = tree.clone();
for part in rest.split('/') {
at.push(part);
if at.symlink_metadata().is_ok_and(|m| !m.is_dir()) {
bail!("{} isn't a directory", at.display());
}
}
let cache = make_cache(&fleet_root, id)?;
if r.header.size > 0 {
repo::unbundle(&cache, &r.path)?;
}
repo::set_refs(&cache, refs)?;
let busy = at.join(".git").exists() && in_use(&at);
if repo::land_from(&cache, &at, branch, wip, busy)? == repo::Landed::Kept {
log(&format!(
"fleet: {} is in use (dirty, or not behind the sent tip): {branch} stays where it is there, the sent tip is in the repo cache",
at.display()
));
}
Ok(())
})();
let _ = std::fs::remove_file(&r.path);
landed
})
}
pub type InUse = std::sync::Arc<dyn Fn(&Path) -> bool + Send + Sync>;
pub fn mounted(state: &crate::state::State, checkout: &Path) -> bool {
let canon = |p: &Path| p.canonicalize().unwrap_or_else(|_| p.to_path_buf());
let checkout = canon(checkout);
state.instances.values().any(|i| {
let direct = i.folders.iter().filter(|f| f.worktree.is_none()).map(|f| &f.base);
std::iter::once(&i.folder).chain(direct).filter(|p| p.is_absolute()).map(|p| canon(p)).any(|p| p.starts_with(&checkout) || checkout.starts_with(&p))
})
}
fn bundle_target(h: &Header) -> Result<(&str, &str)> {
let to = h.to.as_deref().context("a bundle names where it goes (`to`)")?;
let (hash, rest) = to.split_once('/').with_context(|| format!("a bundle goes below a root, not to {to}"))?;
if !is_hash(hash) {
bail!("`{hash}` isn't a root hash");
}
Ok((hash, rest))
}
fn bundle_refs_of(h: &Header) -> Result<(&str, &str, Option<&str>)> {
let id = h.repo.as_deref().context("a bundle names its repo (`repo`)")?;
let refs = h.refs.as_ref().context("a bundle names its refs (`refs`)")?;
let mut branches = refs.keys().filter_map(|n| n.strip_prefix("refs/heads/"));
let (Some(branch), None) = (branches.next(), branches.next()) else {
bail!("a bundle carries one branch (`refs/heads/…`), not {}", refs.keys().cloned().collect::<Vec<_>>().join(", "));
};
let wip = repo::wip_ref(branch);
if let Some(other) = refs.keys().find(|n| !n.starts_with("refs/heads/") && **n != wip) {
bail!("a bundle carries its branch and that branch's WIP ref ({wip}), not {other}");
}
Ok((id, branch, refs.get(&wip).map(String::as_str)))
}
fn unpack_root(tar: &Path, roots: &Path, hash: &str) -> Result<()> {
let dest = roots.join(hash);
if dest.is_dir() {
return Ok(());
}
let bytes = std::fs::read(tar).with_context(|| format!("cannot read {}", tar.display()))?;
if short_hash(&bytes) != hash {
bail!("the tar isn't root {hash}");
}
let temp = roots.join(format!(".{hash}.{}-{}.part", std::process::id(), next_seq()));
let unpacked = tar::extract(&bytes, &temp).and_then(|()| match std::fs::rename(&temp, &dest) {
Ok(()) => Ok(()),
Err(_) if dest.is_dir() => Ok(()),
Err(e) => Err(anyhow!(e).context(format!("cannot move the root to {}", dest.display()))),
});
let _ = std::fs::remove_dir_all(&temp);
unpacked
}
#[cfg(test)]
mod tests {
use std::os::unix::net::UnixStream;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use super::repo::tests::{git, repo_with_branches, scratch};
use super::*;
use crate::serve::endpoint;
fn p(s: &str) -> PathBuf {
PathBuf::from(s)
}
#[test]
fn layout_keeps_each_folders_offset() {
let l = layout(&p("/w/.devsandboxes"), &[p("/w/devsandboxes"), p("/w/stackblitz/bolt")]).unwrap();
assert_eq!(l, Layout { root: p(".devsandboxes"), folders: vec![p("devsandboxes"), p("stackblitz/bolt")] });
let l = layout(&p("/home/u/src/cfg"), &[p("/home/u/src/web"), p("/home/u/lib")]).unwrap();
assert_eq!(l, Layout { root: p("src/cfg"), folders: vec![p("src/web"), p("lib")] });
assert_eq!(layout(&p("/w/cfg"), &[]).unwrap(), Layout { root: p("cfg"), folders: vec![] });
let err = |root: &str, f: &[&str]| layout(&p(root), &f.iter().map(|s| p(s)).collect::<Vec<_>>()).unwrap_err();
assert!(err("/w/cfg", &["/w/cfg"]).contains("is in the config root"));
assert!(err("/w/cfg", &["/w/cfg/app"]).contains("is in the config root"));
assert!(err("/w/cfg", &["/w"]).contains("holds the config root"));
assert!(err("/w/cfg", &["/w/a", "/w/a/b"]).contains("nest"));
assert!(err("/", &[]).contains("too shallow"));
assert_eq!(layout(&p("/w/cfg"), &[p("/x/y")]).unwrap(), Layout { root: p("w/cfg"), folders: vec![p("x/y")] });
}
fn fixture(dir: &Path) -> PathBuf {
repo_with_branches(dir, "web");
let cfg = dir.join("cfg");
for d in ["dockerfiles", ".worktrees/web-2", "shared-volumes/h", "shared-files/x", ".git/objects", "features/f/.git"] {
std::fs::create_dir_all(cfg.join(d)).unwrap();
}
std::fs::write(cfg.join("devsandboxes.toml"), "[sandbox.web]\nfolder = \"../web\"\nimage = \"img\"\n").unwrap();
std::fs::write(cfg.join("dockerfiles/web.Dockerfile"), "FROM x\n").unwrap();
std::fs::write(cfg.join("features/f/install.sh"), "#!/bin/sh\n").unwrap();
std::fs::set_permissions(cfg.join("features/f/install.sh"), std::fs::Permissions::from_mode(0o755)).unwrap();
std::fs::write(cfg.join(".worktrees/web-2/x"), "no").unwrap();
std::fs::write(cfg.join(".git/HEAD"), "no").unwrap();
std::fs::write(cfg.join("features/f/.git/HEAD"), "no").unwrap();
std::os::unix::fs::symlink("dockerfiles/web.Dockerfile", cfg.join("Dockerfile")).unwrap();
cfg
}
fn member_paths(tar: &[u8]) -> Vec<String> {
tar::read(tar).unwrap().into_iter().map(|m| m.path).collect()
}
#[test]
fn the_root_tar_skips_state_and_git_and_hashes_by_content() {
let dir = scratch("pack");
let cfg = fixture(&dir);
let tar = pack_root(&cfg, &p("cfg")).unwrap();
assert_eq!(
member_paths(&tar),
[
"cfg",
"cfg/Dockerfile",
"cfg/devsandboxes.toml",
"cfg/dockerfiles",
"cfg/dockerfiles/web.Dockerfile",
"cfg/features",
"cfg/features/f",
"cfg/features/f/install.sh",
]
);
std::thread::sleep(Duration::from_millis(20));
std::fs::write(cfg.join("dockerfiles/web.Dockerfile"), "FROM x\n").unwrap();
assert_eq!(short_hash(&pack_root(&cfg, &p("cfg")).unwrap()), short_hash(&tar));
assert_ne!(short_hash(&pack_root(&cfg, &p("w/cfg")).unwrap()), short_hash(&tar));
std::fs::write(cfg.join("dockerfiles/web.Dockerfile"), "FROM y\n").unwrap();
assert_ne!(short_hash(&pack_root(&cfg, &p("cfg")).unwrap()), short_hash(&tar));
std::os::unix::fs::symlink("../web", cfg.join("out")).unwrap();
let err = format!("{:#}", pack_root(&cfg, &p("cfg")).unwrap_err());
assert!(err.contains("symlink out points outside the config root"), "{err}");
let _ = std::fs::remove_dir_all(&dir);
}
fn deliver(r: &Arc<Receivers>, header: &Header, body: &[u8]) -> Result<()> {
transfer::send(r.accept().unwrap(), header, body)
}
fn tar_header(tar: &[u8]) -> Header {
let (size, sha256) = transfer::digest(tar).unwrap();
Header { kind: ROOT_TAR_KIND.into(), name: format!("root-{}.tar", short_hash(tar)), size, sha256, to: None, repo: None, refs: None }
}
fn bundle_header(body: &[u8], to: &str, repo: &str, refs: &[(&str, &str)]) -> Header {
let (size, sha256) = transfer::digest(body).unwrap();
Header {
kind: BUNDLE_KIND.into(),
name: "repo.bundle".into(),
size,
sha256,
to: Some(to.into()),
repo: Some(repo.into()),
refs: Some(refs.iter().map(|(n, id)| (n.to_string(), id.to_string())).collect()),
}
}
#[test]
fn the_worker_unpacks_a_root_once_and_refuses_bad_ones() {
let dir = scratch("recv-root");
let cfg = fixture(&dir);
let fleet = dir.join("fleet");
let r = Arc::new(receivers(&fleet, Receivers::new(|_| {}), free()));
let tar = pack_root(&cfg, &p("cfg")).unwrap();
let h = tar_header(&tar);
deliver(&r, &h, &tar).unwrap();
let tree = fleet.join("roots").join(short_hash(&tar));
assert_eq!(std::fs::read_to_string(tree.join("cfg/devsandboxes.toml")).unwrap(), "[sandbox.web]\nfolder = \"../web\"\nimage = \"img\"\n");
assert_eq!(std::fs::read_link(tree.join("cfg/Dockerfile")).unwrap(), p("dockerfiles/web.Dockerfile"));
assert!(std::fs::metadata(tree.join("cfg/features/f/install.sh")).unwrap().permissions().mode() & 0o100 != 0);
std::fs::write(tree.join("cfg/marker"), "").unwrap();
deliver(&r, &h, &tar).unwrap();
assert!(tree.join("cfg/marker").exists());
let leftovers: Vec<_> = std::fs::read_dir(fleet.join("roots")).unwrap().flatten().map(|e| e.file_name()).collect();
assert_eq!(leftovers.len(), 2, "the tree and `.in`: {leftovers:?}");
assert_eq!(std::fs::read_dir(fleet.join("roots/.in")).unwrap().count(), 0);
let err = format!("{:#}", deliver(&r, &Header { name: "root-0000000000000000.tar".into(), ..h.clone() }, &tar).unwrap_err());
assert!(err.contains("a root tar is named"), "{err}");
let mut evil = tar::Writer::default();
evil.dir("cfg").unwrap();
let mut bytes = evil.finish();
bytes.truncate(bytes.len() - 1024);
let mut raw = [0u8; 512];
raw[..9].copy_from_slice(b"../escape");
raw[100..108].copy_from_slice(b"0000644\0");
raw[124..136].copy_from_slice(b"00000000000\0");
raw[156] = b'0';
raw[148..156].copy_from_slice(b" ");
let sum: u32 = raw.iter().map(|&b| u32::from(b)).sum();
raw[148..156].copy_from_slice(format!("{sum:06o}\0 ").as_bytes());
bytes.extend_from_slice(&raw);
bytes.extend_from_slice(&[0; 1024]);
let err = format!("{:#}", deliver(&r, &tar_header(&bytes), &bytes).unwrap_err());
assert!(err.contains("unsafe path \"../escape\""), "{err}");
assert!(!dir.join("fleet/roots/../escape").exists() && !fleet.join("escape").exists());
assert!(!fleet.join("roots").join(short_hash(&bytes)).exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_bundle_needs_its_root_and_a_plain_target() {
let dir = scratch("recv-bundle");
let fleet = dir.join("fleet");
let r = Arc::new(receivers(&fleet, Receivers::new(|_| {}), free()));
let oid = "a".repeat(40);
let h = bundle_header(b"x", "0123456789abcdef/web", "fedcba9876543210", &[("refs/heads/main", &oid)]);
let err = format!("{:#}", deliver(&r, &h, b"x").unwrap_err());
assert!(err.contains("root 0123456789abcdef isn't here"), "{err}");
let err = format!("{:#}", deliver(&r, &Header { repo: None, ..h.clone() }, b"x").unwrap_err());
assert!(err.contains("names its repo"), "{err}");
let err = format!("{:#}", deliver(&r, &Header { refs: None, ..h.clone() }, b"x").unwrap_err());
assert!(err.contains("names its refs"), "{err}");
for refs in [
vec![("refs/heads/main", oid.as_str()), ("refs/heads/b", oid.as_str())],
vec![("refs/devsandbox/wip/main", oid.as_str())],
] {
let bad = bundle_header(b"x", "0123456789abcdef/web", "fedcba9876543210", &refs);
let err = format!("{:#}", deliver(&r, &bad, b"x").unwrap_err());
assert!(err.contains("carries one branch"), "{refs:?}: {err}");
}
for other in ["refs/devsandbox/wip/b", "refs/tags/v1", "refs/devsandbox/root"] {
let bad = bundle_header(b"x", "0123456789abcdef/web", "fedcba9876543210", &[("refs/heads/main", &oid), (other, &oid)]);
let err = format!("{:#}", deliver(&r, &bad, b"x").unwrap_err());
assert!(err.contains(&format!("not {other}")), "{other}: {err}");
}
let err = format!("{:#}", deliver(&r, &Header { to: Some("web".into()), ..h.clone() }, b"x").unwrap_err());
assert!(err.contains("below a root"), "{err}");
let tree = fleet.join("roots/0123456789abcdef");
std::fs::create_dir_all(&tree).unwrap();
std::os::unix::fs::symlink("/tmp", tree.join("web")).unwrap();
let err = format!("{:#}", deliver(&r, &h, b"x").unwrap_err());
assert!(err.contains("isn't a directory"), "{err}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn what_cant_move_says_why() {
let dir = scratch("not-movable");
let cfg = fixture(&dir);
let why = |toml: &str, branch: Option<&str>| {
std::fs::write(cfg.join("devsandboxes.toml"), toml).unwrap();
match offer(&cfg, "web", branch) {
Err(Unprepared::NotMovable(why)) => why,
other => panic!("{other:?}"),
}
};
std::fs::create_dir_all(dir.join("plain")).unwrap();
assert!(why("[sandbox.web]\nfolder = \"../plain\"\nimage = \"i\"\n", None).contains("isn't a git checkout"));
let inside = why("[sandbox.web]\nfolder = \"dockerfiles\"\nimage = \"i\"\n", None);
assert!(inside.contains("is in the config root (the config root travels as a tar"), "{inside}");
let web = "[sandbox.web]\nfolder = \"../web\"\nimage = \"i\"\n";
git(&dir.join("web"), &["checkout", "-q", "--detach"]);
assert!(why(web, None).contains("web has a detached HEAD (check out a branch first)"));
assert!(why(web, Some("nope")).contains("detached HEAD"), "a missing branch falls back to the current one");
let got = offer(&cfg, "web", Some("feature")).unwrap();
assert_eq!((got.repos[0].branch.as_str(), got.repos[0].wip), ("feature", false));
git(&dir.join("web"), &["checkout", "-q", "main"]);
assert!(why("[sandbox.web]\nfolder = \"../web\"\nimage = \"i\"\nfolders = { \"/w/x\" = \"../${localEnv:X}\" }\n", None)
.contains("uses a variable"));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn ties_name_what_keeps_a_sandbox_home() {
let dir = scratch("ties");
let cfg = dir.join("cfg");
for d in ["cfg/data", "web/sub", "lib", "elsewhere"] {
std::fs::create_dir_all(dir.join(d)).unwrap();
}
let ties_of_toml = |extra: &str| {
let text = format!(
"[services.g]\nimage = \"redis\"\nscope = \"global\"\n[services.i]\nimage = \"redis\"\n\
[services.gpu]\nimage = \"ollama\"\nhostRequirements.gpu = true\n\
[services.opt]\nimage = \"ollama\"\nhostRequirements.gpu = \"optional\"\n\
[sandbox.s]\nfolder = \"../web\"\nimage = \"alpine\"\nfolders = {{ \"/w/lib\" = \"../lib\" }}\n{extra}\n"
);
let config = Config::parse(&text).unwrap();
let s = config.resolve_sandbox("s").unwrap();
ties(&cfg, &config, &s)
};
let web = dir.join("web").display().to_string();
let lib = dir.join("lib").display().to_string();
let clean = format!(
"services = [\"i\"]\nmounts = [\"source=${{configDir}}/data,target=/d,type=bind\", \
\"source=${{localWorkspaceFolder}}/sub,target=/s\", \"source={web}/sub,target=/w\", \
\"source={lib},target=/l\", \"source=v-${{instance}},target=/v,type=volume\", \
{{ type = \"tmpfs\", target = \"/t\" }}]"
);
assert_eq!(ties_of_toml(&clean), Vec::<String>::new());
std::fs::write(cfg.join("Dockerfile"), "FROM alpine\n").unwrap();
assert_eq!(ties_of_toml("build = { dockerfile = \"Dockerfile\" }"), Vec::<String>::new());
assert_eq!(
ties_of_toml("build = { dockerfile = \"Dockerfile\", context = \"../elsewhere\" }"),
[format!("build context `{}` (outside the config root)", cfg.join("../elsewhere").display())]
);
assert_eq!(ties_of_toml("placement = \"home\""), ["`placement = \"home\"`"]);
assert_eq!(ties_of_toml("privileged = true\nplacement = \"home\""), ["`placement = \"home\"`", "privileged"]);
assert_eq!(ties_of_toml(""), Vec::<String>::new());
assert_eq!(ties_of_toml("hostRequirements.gpu = \"optional\""), Vec::<String>::new());
assert_eq!(ties_of_toml("services = [\"opt\"]"), Vec::<String>::new());
assert_eq!(ties_of_toml("hostRequirements.gpu = true"), Vec::<String>::new());
assert_eq!(ties_of_toml("services = [\"gpu\"]"), Vec::<String>::new());
assert_eq!(
ties_of_toml(
"caches = [\"cargo\"]\npersist-shell-history = true\nworktree-link = [\".env\"]\n\
mounts = [{ source = \"${sharedVolumes}/x\", target = \"/x\" }]"
),
Vec::<String>::new()
);
std::fs::create_dir_all(cfg.join("shared-volumes")).unwrap();
std::fs::create_dir_all(dir.join("gh")).unwrap();
std::os::unix::fs::symlink(dir.join("gh"), cfg.join("shared-volumes/gh")).unwrap();
assert_eq!(ties_of_toml("mounts = [\"source=${sharedVolumes}/gh,target=/g\"]"), Vec::<String>::new());
assert_eq!(ties_of_toml("mounts = [\"source=${sharedVolumes}/../../gh,target=/g\"]").len(), 1);
let out = dir.join("elsewhere").display().to_string();
for (extra, want) in [
(format!("mounts = [\"source={out},target=/o\"]"), format!("bind mount of `{out}` (outside the config root)")),
(
"mounts = [\"source=/var/run/docker.sock,target=/s\"]".into(),
"runtime socket mount `/var/run/docker.sock`".into(),
),
("containerEnv = { T = \"${localEnv:X}\" }".into(), "`${localEnv:X}` (this host's env)".into()),
("privileged = true".into(), "privileged".into()),
("initializeCommand = \"true\"".into(), "`initializeCommand`".into()),
("services = [\"i\", \"g\"]".into(), "global service `g`".into()),
] {
assert_eq!(ties_of_toml(&extra), [want], "{extra}");
}
let all = ties_of_toml(&format!(
"privileged = true\nmounts = [\"source=${{localEnv:HOME}}/.gh,target=/g\", \"source={out},target=/o\"]"
));
assert_eq!(
all,
[
"privileged".to_string(),
"`${localEnv:HOME}` (this host's env)".into(),
format!("bind mount of `{out}` (outside the config root)"),
]
);
assert_eq!(
ties_of_toml(&format!("mounts = [\"source={out},target=/a\", \"source=/var/run/docker.sock,target=/s\", \"source={out},target=/b\"]")),
[format!("bind mount of `{out}` (outside the config root)"), "runtime socket mount `/var/run/docker.sock`".into()]
);
assert_eq!(
ties_of_toml(&format!("mounts = [\"source={out}/${{instance}},target=/i\"]")),
[format!("bind mount of `{out}/<instance>` (outside the config root)")]
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn offer_refuses_a_tied_sandbox() {
let dir = scratch("offer-tied");
let cfg = fixture(&dir);
std::fs::write(cfg.join("devsandboxes.toml"), "[sandbox.web]\nfolder = \"../web\"\nimage = \"i\"\ninitializeCommand = \"true\"\nprivileged = true\n")
.unwrap();
match offer(&cfg, "web", None) {
Err(Unprepared::NotMovable(why)) => assert_eq!(why, "`initializeCommand`, privileged"),
other => panic!("{other:?}"),
}
assert_eq!(ties_of(&cfg, "web").unwrap(), (vec!["`initializeCommand`".to_string(), "privileged".into()], None));
assert!(ties_of(&cfg, "nope").is_err());
let _ = std::fs::remove_dir_all(&dir);
}
fn serve_api(stream: UnixStream, fleet: &Path) {
use std::io::{BufRead, BufReader, Write};
use crate::serve::proto::{HelloResult, PROTOCOL, Request, Response};
stream.set_read_timeout(Some(Duration::from_secs(30))).unwrap();
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut out = stream;
let mut line = String::new();
while reader.read_line(&mut line).is_ok_and(|n| n > 0) {
let req: Request = serde_json::from_str(&line).unwrap();
let resp = match req.method.as_str() {
"hello" => {
let hello = HelloResult { version: "0.0.0".into(), build: 0, protocol: PROTOCOL, handoff: false };
Response::ok(req.id, serde_json::to_value(hello).unwrap())
}
HAVES_METHOD => match haves_call(fleet, &req.params) {
Ok(v) => Response::ok(req.id, v),
Err(e) => Response::err(req.id, e.code, e.message),
},
fetch::FETCH_METHOD => {
let state = crate::state::State::load_at(&fleet.parent().unwrap().join("state.toml")).unwrap();
match fetch::fetch_call(&state, fleet, &req.params) {
Ok(v) => Response::ok(req.id, v),
Err(e) => Response::err(req.id, e.code, e.message),
}
}
m => Response::err(req.id, "unknown-method", m.to_string()),
};
if writeln!(out, "{}", serde_json::to_string(&resp).unwrap()).is_err() {
return;
}
line.clear();
}
}
fn free() -> InUse {
Arc::new(|_| false)
}
fn link(dir: &Path, fleet: &Path) -> PathBuf {
link_with(dir, fleet, free())
}
fn link_with(dir: &Path, fleet: &Path, in_use: InUse) -> PathBuf {
use crate::devsbd::proto::channel;
use crate::serve::fleet::link::{Endpoint, Serves, answerer_with, opener_with};
let control_plane_dir = dir.join("cp");
std::fs::create_dir_all(&control_plane_dir).unwrap();
let socket = endpoint::fleet_socket(&control_plane_dir, "w");
let ours = Serves::default().transfers(Arc::new(Receivers::new(|_| {})));
let api_fleet = fleet.to_path_buf();
let api = move || {
let (mine, theirs) = UnixStream::pair().ok()?;
let fleet = api_fleet.clone();
std::thread::spawn(move || serve_api(mine, &fleet));
Some(theirs.into())
};
let theirs = Serves::default().with(channel::API, api).transfers(Arc::new(receivers(fleet, Receivers::new(|_| {}), in_use)));
let (a_r, o_w) = std::io::pipe().unwrap();
let (o_r, a_w) = std::io::pipe().unwrap();
let path = socket.clone();
std::thread::spawn(move || {
opener_with(&super::super::LINK, o_r, o_w, Endpoint { socket: Some(&path), serves: ours, outlet: None, auth: None }, |_| Ok(()))
});
std::thread::spawn(move || {
answerer_with(&super::super::LINK, a_r, a_w, Endpoint { serves: theirs, ..Endpoint::default() }, |_| Ok(()), || {})
});
let start = std::time::Instant::now();
while UnixStream::connect(&socket).is_err() {
assert!(start.elapsed() < Duration::from_secs(5), "the link socket accepts");
std::thread::sleep(Duration::from_millis(10));
}
socket
}
fn source_state(repo: &Path) -> Vec<String> {
vec![
std::fs::read(repo.join(".git/index")).map(|b| short_hash(&b)).unwrap_or_default(),
git(repo, &["status", "--porcelain=v1", "-uall"]),
git(repo, &["symbolic-ref", "HEAD"]),
git(repo, &["for-each-ref", "refs/heads"]),
git(repo, &["stash", "list"]),
git(repo, &["for-each-ref", "refs/devsandbox"]),
]
}
fn assert_no_leftovers(repo: &Path) {
assert!(!git(repo, &["for-each-ref", "--format=%(refname)", "refs/devsandbox"]).contains("/wip/send-"));
let temps: Vec<_> = std::fs::read_dir(repo.join(".git"))
.unwrap()
.filter_map(|e| e.unwrap().file_name().into_string().ok())
.filter(|n| n.starts_with("devsandbox-"))
.collect();
assert_eq!(temps, Vec::<String>::new());
}
#[test]
fn prepare_moves_a_sandbox_onto_a_worker_over_its_link() {
let dir = scratch("prepare");
let cfg = fixture(&dir);
std::fs::write(
cfg.join("devsandboxes.toml"),
"[sandbox.web]\nfolder = \"../web\"\nimage = \"img\"\nfolders = { \"/workspaces/lib\" = \"../lib\", \"/workspaces/cfg\" = \".\" }\n",
)
.unwrap();
let (web, lib) = (dir.join("web"), repo_with_branches(&dir, "lib"));
git(&lib, &["checkout", "-q", "feature"]);
std::fs::write(web.join("README"), "dirty\n").unwrap();
let before = (source_state(&web), source_state(&lib));
let fleet = dir.join("worker/fleet/control_plane");
let t = LinkTransfer::for_worker(link(&dir, &fleet).parent().unwrap(), "w");
let got = prepare(&t, &fleet, &cfg, "web", Some("feature")).unwrap();
let tree = got.remote_root.parent().unwrap().to_path_buf();
assert_eq!(got.remote_root, tree.join("cfg"));
assert_eq!(tree.parent().unwrap(), fleet.join("roots"));
assert_eq!(got.folders, [(web.clone(), tree.join("web"), "feature".to_string()), (lib.clone(), tree.join("lib"), "feature".to_string())]);
let moved = got.remote_root.join("../web").canonicalize().unwrap();
assert_eq!(moved, tree.join("web").canonicalize().unwrap());
assert_eq!(std::fs::read_to_string(moved.join("F")).unwrap(), "feature\n");
assert_eq!(std::fs::read_to_string(moved.join("README")).unwrap(), "main\n");
assert_eq!(git(&moved, &["symbolic-ref", "--short", "HEAD"]), "feature");
assert_eq!(git(&moved, &["status", "--porcelain=v1", "-uall"]), "");
assert_eq!(git(&moved, &["remote"]), "");
assert!(got.remote_root.join("dockerfiles/web.Dockerfile").exists());
assert!(!got.remote_root.join(".worktrees").exists() && !got.remote_root.join(".git").exists());
let again = prepare(&t, &fleet, &cfg, "web", None).unwrap();
assert_eq!(again.remote_root, got.remote_root);
assert_eq!(again.folders[0].2, "main");
assert_eq!(git(&moved, &["rev-parse", "main"]), git(&web, &["rev-parse", "main"]));
assert_eq!(git(&moved, &["symbolic-ref", "--short", "HEAD"]), "main");
assert_eq!(std::fs::read_to_string(moved.join("README")).unwrap(), "dirty\n");
let new = prepare(&t, &fleet, &cfg, "web", Some("new-one")).unwrap();
assert_eq!(new.folders[0].2, "main");
let ask = crate::serve::fleet::placed::Ask { branch: Some("new-one"), base: None };
assert_eq!(crate::serve::fleet::placed::start_point(&ask, &new).as_deref(), Some("main"));
assert_eq!((source_state(&web), source_state(&lib)), before, "the sources are untouched");
assert_no_leftovers(&web);
assert_no_leftovers(&lib);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_checkout_moves_with_its_unpushed_and_uncommitted_work() {
let dir = scratch("prepare-wip");
let cfg = fixture(&dir);
let web = dir.join("web");
git(&web, &["checkout", "-q", "-b", "local-only"]);
for (f, body) in [("keep", "k\n"), ("edit", "e\n"), ("gone", "g\n"), ("staged", "s\n"), (".gitignore", "*.log\n")] {
std::fs::write(web.join(f), body).unwrap();
}
git(&web, &["add", "-A"]);
git(&web, &["commit", "-q", "-m", "local"]);
std::fs::write(web.join("keep"), "stashed\n").unwrap();
git(&web, &["stash", "-q"]);
std::fs::write(web.join("edit"), "e2\n").unwrap();
std::fs::remove_file(web.join("gone")).unwrap();
std::fs::write(web.join("staged"), "s2\n").unwrap();
git(&web, &["add", "staged"]);
std::fs::create_dir_all(web.join("sub")).unwrap();
std::fs::write(web.join("sub/new"), "n\n").unwrap();
std::fs::write(web.join("x.log"), "ignored\n").unwrap();
assert_eq!(git(&web, &["remote"]), "", "no origin");
let before = source_state(&web);
let fleet = dir.join("worker/fleet/control_plane");
let t = LinkTransfer::for_worker(link(&dir, &fleet).parent().unwrap(), "w");
let got = prepare(&t, &fleet, &cfg, "web", None).unwrap();
let moved = got.folders[0].1.clone();
assert_eq!(got.folders[0].2, "local-only");
assert_eq!(git(&moved, &["symbolic-ref", "--short", "HEAD"]), "local-only");
assert_eq!(git(&moved, &["rev-parse", "HEAD"]), git(&web, &["rev-parse", "local-only"]));
assert_eq!(git(&moved, &["status", "--porcelain=v1", "-uall"]), "M edit\n D gone\n M staged\n?? sub/new");
for f in ["keep", "edit", "staged", "sub/new", ".gitignore"] {
assert_eq!(std::fs::read(web.join(f)).unwrap(), std::fs::read(moved.join(f)).unwrap(), "{f}");
}
assert!(!moved.join("gone").exists() && !moved.join("x.log").exists(), "deleted stays deleted, ignored doesn't travel");
assert_eq!(git(&moved, &["for-each-ref", "refs/devsandbox"]), "");
assert_eq!(source_state(&web), before, "the source is untouched");
assert_no_leftovers(&web);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn an_unborn_repo_moves_its_untracked_files() {
let dir = scratch("prepare-unborn");
let cfg = fixture(&dir);
let web = dir.join("fresh");
std::fs::create_dir_all(web.join("sub")).unwrap();
git(&web, &["init", "-q", "-b", "main"]);
std::fs::write(web.join("a"), "a\n").unwrap();
std::fs::write(web.join("sub/b"), "b\n").unwrap();
std::fs::write(cfg.join("devsandboxes.toml"), "[sandbox.web]\nfolder = \"../fresh\"\nimage = \"img\"\n").unwrap();
let fleet = dir.join("worker/fleet/control_plane");
let t = LinkTransfer::for_worker(link(&dir, &fleet).parent().unwrap(), "w");
let got = prepare(&t, &fleet, &cfg, "web", None).unwrap();
let moved = got.folders[0].1.clone();
let root = git(&web, &["rev-parse", repo::ROOT_REF]);
assert_eq!(git(&web, &["for-each-ref", "--format=%(refname)", "refs/devsandbox"]), repo::ROOT_REF, "the one ref kept");
assert_eq!(git(&web, &["status", "--porcelain=v1", "-uall"]), "?? a\n?? sub/b", "still unborn");
assert_eq!(git(&web, &["symbolic-ref", "HEAD"]), "refs/heads/main");
assert_eq!(git(&moved, &["rev-parse", "HEAD"]), root, "the worker's main is the devsandbox root");
assert_eq!(git(&moved, &["status", "--porcelain=v1", "-uall"]), "?? a\n?? sub/b");
assert_eq!(std::fs::read_to_string(moved.join("sub/b")).unwrap(), "b\n");
prepare(&t, &fleet, &cfg, "web", None).unwrap();
assert_eq!(git(&web, &["rev-parse", repo::ROOT_REF]), root);
assert_no_leftovers(&web);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_refused_transfer_fails_prepare() {
struct Refuse(&'static [&'static str], Mutex<Vec<String>>);
impl Transfer for Refuse {
fn send(&self, header: &Header, body: &mut dyn Read) -> Result<()> {
std::io::copy(body, &mut std::io::sink())?;
self.1.lock().unwrap().push(header.kind.clone());
if self.0.contains(&header.kind.as_str()) {
bail!("transfer refused: full")
}
Ok(())
}
}
let dir = scratch("refused");
let cfg = fixture(&dir);
let web = dir.join("web");
std::fs::write(web.join("README"), "dirty\n").unwrap();
let before = source_state(&web);
let t = Refuse(&[ROOT_TAR_KIND], Mutex::default());
match prepare(&t, &dir.join("fleet"), &cfg, "web", None) {
Err(Unprepared::Failed(e)) => assert!(format!("{e:#}").contains("cannot send the config root: transfer refused: full")),
other => panic!("{other:?}"),
}
assert_eq!(*t.1.lock().unwrap(), ["root-tar"]);
let t = Refuse(&[BUNDLE_KIND], Mutex::default());
match prepare(&t, &dir.join("fleet"), &cfg, "web", None) {
Err(Unprepared::Failed(e)) => {
assert!(format!("{e:#}").contains(&format!("cannot send {}: transfer refused: full", web.display())), "{e:#}")
}
other => panic!("{other:?}"),
}
assert_eq!(*t.1.lock().unwrap(), ["root-tar", "bundle"]);
assert_eq!(source_state(&web), before);
assert_no_leftovers(&web);
let _ = std::fs::remove_dir_all(&dir);
}
struct Recorded<'a> {
inner: &'a dyn Transfer,
sent: Mutex<Vec<(String, u64)>>,
}
impl Transfer for Recorded<'_> {
fn haves(&self, repos: &[String]) -> Result<Haves> {
self.inner.haves(repos)
}
fn send(&self, header: &Header, body: &mut dyn Read) -> Result<()> {
self.sent.lock().unwrap().push((header.kind.clone(), header.size));
self.inner.send(header, body)
}
}
impl Recorded<'_> {
fn take(&self) -> Vec<(String, u64)> {
std::mem::take(&mut *self.sent.lock().unwrap())
}
}
#[test]
fn repeat_sends_carry_only_what_the_worker_lacks() {
let dir = scratch("repeat");
let cfg = fixture(&dir);
let web = dir.join("web");
let bulk: String = (0..20_000).map(|i| format!("line {i}\n")).collect();
std::fs::write(web.join("bulk"), bulk).unwrap();
git(&web, &["add", "bulk"]);
git(&web, &["commit", "-q", "-m", "bulk"]);
let fleet = dir.join("worker/fleet/control_plane");
let link = LinkTransfer::for_worker(link(&dir, &fleet).parent().unwrap(), "w");
let t = Recorded { inner: &link, sent: Mutex::default() };
let id = repo::identity(Repo::Checkout(&web), "HEAD").unwrap();
assert_eq!(link.haves(std::slice::from_ref(&id)).unwrap(), Haves::default());
let got = prepare(&t, &fleet, &cfg, "web", None).unwrap();
let first = t.take();
assert_eq!(first.iter().map(|(k, _)| k.as_str()).collect::<Vec<_>>(), ["root-tar", "bundle"]);
let checkout = got.folders[0].1.clone();
assert_eq!(git(&checkout, &["symbolic-ref", "--short", "HEAD"]), "main");
assert_eq!(git(&checkout, &["remote"]), "", "no remote on the worker");
let cache = cache_path(&fleet, &id);
let main = git(&web, &["rev-parse", "main"]);
assert_eq!(git(&cache, &["rev-parse", "refs/heads/main"]), main);
assert_eq!(git(&cache, &["rev-parse", &format!("{}^", repo::wip_ref("main"))]), main, "the snapshot sits on main");
let haves = link.haves(std::slice::from_ref(&id)).unwrap();
let hash = got.remote_root.parent().unwrap().file_name().unwrap().to_str().unwrap().to_string();
assert_eq!(haves.roots, [hash].into());
assert!(haves.tips[&id].contains(&main), "{haves:?}");
std::fs::write(web.join("README"), "two\n").unwrap();
git(&web, &["commit", "-q", "-am", "two"]);
std::fs::write(web.join("later"), "wip\n").unwrap();
prepare(&t, &fleet, &cfg, "web", None).unwrap();
let second = t.take();
assert_eq!(second.len(), 1, "the root isn't sent again: {second:?}");
assert!(second[0].1 > 0 && second[0].1 < first[1].1, "a delta: {second:?} after {first:?}");
assert_eq!(std::fs::read_to_string(checkout.join("README")).unwrap(), "two\n");
assert_eq!(std::fs::read_to_string(checkout.join("later")).unwrap(), "wip\n", "the new WIP applied");
let wip = git(&cache, &["rev-parse", &repo::wip_ref("main")]);
assert_eq!(git(&cache, &["show", &format!("{wip}:later")]), "wip");
prepare(&t, &fleet, &cfg, "web", Some("feature")).unwrap();
t.take();
prepare(&t, &fleet, &cfg, "web", Some("feature")).unwrap();
assert_eq!(t.take(), [("bundle".to_string(), 0)]);
assert_eq!(git(&checkout, &["rev-parse", "feature"]), git(&web, &["rev-parse", "feature"]));
assert_eq!(std::fs::read_dir(fleet.join("roots/.in")).unwrap().count(), 0, "no body left behind");
assert_no_leftovers(&web);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn the_control_plane_mirror_fills_the_control_planes_cache_only() {
let dir = scratch("mirror");
let cfg = fixture(&dir);
let web = dir.join("web");
let bulk: String = (0..20_000).map(|i| format!("line {i}\n")).collect();
std::fs::write(web.join("bulk"), bulk).unwrap();
git(&web, &["add", "bulk"]);
git(&web, &["commit", "-q", "-m", "bulk"]);
std::fs::write(web.join("README"), "dirty\n").unwrap();
let before = source_state(&web);
let (worker, control_plane) = (dir.join("worker/fleet/m"), dir.join("cp/fleet/m"));
let to_worker = LinkTransfer::for_worker(link(&dir.join("w"), &worker).parent().unwrap(), "w");
let to_control_plane = LinkTransfer::for_worker(link(&dir.join("k"), &control_plane).parent().unwrap(), "w");
let t = Recorded { inner: &to_control_plane, sent: Mutex::default() };
let none = Vec::<(PathBuf, String)>::new();
let got = prepare(&to_worker, &worker, &cfg, "web", None).unwrap();
assert_eq!(got.notes, Vec::<String>::new());
let [sent] = got.sent.as_slice() else { panic!("{:?}", got.sent) };
assert_eq!(sent.host, web);
assert_eq!(mirror(&got.sent, &t), none);
let first = t.take();
assert!(matches!(first.as_slice(), [(k, n)] if k == "bundle" && *n > 0), "{first:?}");
let (control_plane_cache, worker_cache) = (cache_path(&control_plane, &sent.id), cache_path(&worker, &sent.id));
let refs = |c: &Path| git(c, &["for-each-ref", "--format=%(refname) %(objectname)"]);
assert_eq!(refs(&control_plane_cache), refs(&worker_cache));
assert_eq!(git(&control_plane_cache, &["rev-parse", "refs/heads/main"]), git(&web, &["rev-parse", "main"]));
let wip = git(&control_plane_cache, &["rev-parse", &repo::wip_ref("main")]);
assert_eq!(git(&control_plane_cache, &["show", &format!("{wip}:README")]), "dirty");
assert!(!control_plane.join("roots").exists(), "no root, no checkout");
let mut entries: Vec<String> = std::fs::read_dir(control_plane.join("repos")).unwrap().map(|e| e.unwrap().file_name().into_string().unwrap()).collect();
entries.sort();
assert_eq!(entries, [".in".to_string(), format!("{}.git", sent.id)]);
assert_eq!(std::fs::read_dir(control_plane.join("repos/.in")).unwrap().count(), 0, "no body left behind");
assert_eq!(mirror(&got.sent, &t), none);
assert_eq!(t.take(), [("bundle".to_string(), 0)]);
assert_eq!(source_state(&web), before, "the source is untouched");
assert_no_leftovers(&web);
std::fs::write(web.join("two"), "two\n").unwrap();
git(&web, &["add", "two"]);
git(&web, &["commit", "-q", "-m", "two"]);
let again = prepare(&to_worker, &worker, &cfg, "web", None).unwrap();
assert_eq!(mirror(&again.sent, &t), none);
let second = t.take();
assert!(second.len() == 1 && second[0].1 > 0 && second[0].1 < first[0].1, "a delta: {second:?} after {first:?}");
assert_eq!(refs(&control_plane_cache), refs(&worker_cache));
assert_no_leftovers(&web);
let gone = Sent { refs: [("refs/heads/main".to_string(), "1".repeat(40))].into(), ..sent.clone() };
let failed = mirror(std::slice::from_ref(&gone), &t);
assert!(matches!(failed.as_slice(), [(h, why)] if *h == web && why.contains("cannot point a ref at")), "{failed:?}");
assert_no_leftovers(&web);
struct NoHaves;
impl Transfer for NoHaves {
fn haves(&self, _: &[String]) -> Result<Haves> {
bail!("unreachable")
}
fn send(&self, _: &Header, _: &mut dyn Read) -> Result<()> {
unreachable!()
}
}
assert_eq!(mirror(&got.sent, &NoHaves), [(web.clone(), "cannot ask the control plane what it has: unreachable".to_string())]);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_placed_instance_is_fetched_back_over_the_link_into_hidden_refs() {
let dir = scratch("fetch-back-link");
let cfg = fixture(&dir);
let web = dir.join("web");
std::fs::write(web.join("README"), "dirty\n").unwrap();
let fleet = dir.join("worker/fleet/control_plane");
let t = LinkTransfer::for_worker(link(&dir, &fleet).parent().unwrap(), "w");
let got = prepare(&t, &fleet, &cfg, "web", None).unwrap();
let moved = crate::serve::fleet::placed::MovedFolder::of(&got).unwrap();
assert_eq!((moved.local.as_path(), moved.branch.as_str()), (web.as_path(), "main"));
let there = moved.remote.clone();
let instance = format!(
"[instance.web-2]\nsandbox = \"web\"\ninstance_id = \"web-2\"\ncontainer = \"c\"\nworkspace = \"/w\"\ncreated_unix = 0\nfolder = {:?}\n",
there.to_str().unwrap()
);
std::fs::write(fleet.parent().unwrap().join("state.toml"), instance).unwrap();
git(&there, &["commit", "-q", "-am", "on the worker"]);
std::fs::write(there.join("later"), "wip\n").unwrap();
let before = source_state(&web);
let entry = crate::serve::fleet::placements::Entry {
machine: "w".into(),
instance: "web-2".into(),
sandbox: "web".into(),
root: cfg.clone(),
folder: web.clone(),
remote: there.clone(),
repo: moved.repo.clone(),
branch: moved.branch.clone(),
};
let back = fetch::fetch_back(&entry, |haves| t.fetch_instance("web-2", haves), &t).unwrap();
assert!(back.fetched > 0);
let heads = "refs/devsandbox/w/web-2/heads/main";
assert_eq!(git(&web, &["rev-parse", heads]), git(&there, &["rev-parse", "main"]));
assert_eq!(git(&web, &["show", &format!("{heads}:README")]), "dirty", "the worker's commit");
assert_eq!(git(&web, &["show", "refs/devsandbox/w/web-2/wip:later"]), "wip");
let after = source_state(&web);
assert_eq!(after[..5], before[..5], "the user's index, status, HEAD, branches and stash untouched");
assert_eq!(git(&there, &["rev-parse", "refs/devsandbox/wip/web-2"]), back.refs["refs/devsandbox/w/web-2/wip"], "the WIP stays there");
assert_eq!(std::fs::read_dir(fleet.join(fetch::OUT_DIR)).unwrap().count(), 0, "pulled, so removed");
assert_no_leftovers(&web);
let again = fetch::fetch_back(&entry, |haves| t.fetch_instance("web-2", haves), &t).unwrap();
assert_eq!((again.fetched, again.refs), (0, back.refs));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_cache_only_bundle_keeps_every_check() {
let dir = scratch("recv-cache-only");
let fleet = dir.join("fleet");
let r = Arc::new(receivers(&fleet, Receivers::new(|_| {}), free()));
let oid = "a".repeat(40);
let cache_only = |refs: &[(&str, &str)]| Header { to: None, ..bundle_header(b"", "x/y", "fedcba9876543210", refs) };
let err = format!("{:#}", deliver(&r, &cache_only(&[("refs/heads/main", &oid)]), b"").unwrap_err());
assert!(err.contains("is neither in the bundle nor already here"), "{err}");
let err = format!("{:#}", deliver(&r, &cache_only(&[("refs/heads/main", &oid), ("refs/tags/v1", &oid)]), b"").unwrap_err());
assert!(err.contains("not refs/tags/v1"), "{err}");
let err = format!("{:#}", deliver(&r, &cache_only(&[("refs/devsandbox/wip/main", &oid)]), b"").unwrap_err());
assert!(err.contains("carries one branch"), "{err}");
let err = format!("{:#}", deliver(&r, &Header { repo: None, ..cache_only(&[("refs/heads/main", &oid)]) }, b"").unwrap_err());
assert!(err.contains("names its repo"), "{err}");
assert!(!fleet.join("roots").exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_wip_snapshot_lands_in_a_new_checkout_only() {
let dir = scratch("recv-wip");
let fleet = dir.join("fleet");
std::fs::create_dir_all(fleet.join("roots/0123456789abcdef")).unwrap();
let r = Arc::new(receivers(&fleet, Receivers::new(|_| {}), Arc::new(|_| true)));
let src = dir.join("src");
std::fs::create_dir_all(&src).unwrap();
git(&src, &["init", "-q", "-b", "main"]);
std::fs::write(src.join("f"), "one\n").unwrap();
git(&src, &["add", "f"]);
git(&src, &["commit", "-q", "-m", "one"]);
std::fs::write(src.join("f"), "changed\n").unwrap();
std::fs::write(src.join("u"), "untracked\n").unwrap();
let id = repo::identity(Repo::Checkout(&src), "HEAD").unwrap();
let wip_ref = repo::wip_ref("main");
let send = |name: &str, haves: &[String]| {
let snap = wip::snapshot(&src, &wip_ref).unwrap();
let file = dir.join(name);
let body = if repo::bundle_refs(Repo::Checkout(&src), &["refs/heads/main", &wip_ref], haves, &file).unwrap() {
std::fs::read(&file).unwrap()
} else {
Vec::new()
};
let h = bundle_header(&body, "0123456789abcdef/a", &id, &[("refs/heads/main", &snap.base), (&wip_ref, &snap.wip)]);
deliver(&r, &h, &body).unwrap();
snap
};
let first = send("1.bundle", &[]);
let at = fleet.join("roots/0123456789abcdef/a");
assert_eq!(git(&at, &["rev-parse", "HEAD"]), first.base);
assert_eq!(git(&at, &["symbolic-ref", "--short", "HEAD"]), "main");
assert_eq!(std::fs::read_to_string(at.join("f")).unwrap(), "changed\n");
assert_eq!(git(&at, &["status", "--porcelain=v1", "-uall"]), "M f\n?? u");
assert_eq!(git(&at, &["for-each-ref", "refs/devsandbox"]), "", "the WIP ref isn't kept in the checkout");
let cache = cache_path(&fleet, &id);
assert_eq!(git(&cache, &["rev-parse", &wip_ref]), first.wip);
std::fs::write(src.join("f"), "changed again\n").unwrap();
let tips = repo::tips(Repo::Bare(&cache)).unwrap();
let second = send("2.bundle", &tips);
assert_ne!(second.wip, first.wip);
assert_eq!(git(&cache, &["rev-parse", &wip_ref]), second.wip);
assert_eq!(std::fs::read_to_string(at.join("f")).unwrap(), "changed\n");
let missing = "1".repeat(40);
let h = bundle_header(b"", "0123456789abcdef/a", &id, &[("refs/heads/main", &missing)]);
let err = format!("{:#}", deliver(&r, &h, b"").unwrap_err());
assert!(err.contains("is neither in the bundle nor already here"), "{err}");
assert_eq!(git(&cache, &["rev-parse", "refs/heads/main"]), second.base, "no ref moved");
let other = dir.join("elsewhere");
std::fs::create_dir_all(&other).unwrap();
std::os::unix::fs::symlink(&other, cache_path(&fleet, "fedcba9876543210")).unwrap();
let h = bundle_header(b"", "0123456789abcdef/b", "fedcba9876543210", &[("refs/heads/main", &second.base)]);
let err = format!("{:#}", deliver(&r, &h, b"").unwrap_err());
assert!(err.contains("isn't a directory"), "{err}");
assert_eq!(std::fs::read_dir(&other).unwrap().count(), 0);
let _ = std::fs::remove_dir_all(&dir);
}
fn place_twice(tag: &str, in_use: bool) -> (PathBuf, PathBuf, String, Result<Prepared, Unprepared>, PathBuf, PathBuf) {
let dir = scratch(tag);
let cfg = fixture(&dir);
let web = dir.join("web");
std::fs::write(web.join(".gitignore"), "*.log\n").unwrap();
git(&web, &["add", ".gitignore"]);
git(&web, &["commit", "-q", "-m", "ignore"]);
let fleet = dir.join("worker/fleet/control_plane");
let t = LinkTransfer::for_worker(link_with(&dir, &fleet, Arc::new(move |_| in_use)).parent().unwrap(), "w");
std::fs::write(web.join("README"), "wip 1\n").unwrap();
std::fs::write(web.join("U"), "u\n").unwrap();
let got = prepare(&t, &fleet, &cfg, "web", None).unwrap();
let checkout = got.folders[0].1.clone();
assert_eq!(std::fs::read_to_string(checkout.join("README")).unwrap(), "wip 1\n");
let first = git(&checkout, &["rev-parse", "HEAD"]);
std::fs::write(checkout.join("x.log"), "built\n").unwrap();
std::fs::remove_file(web.join("U")).unwrap();
std::fs::write(web.join("README"), "two\n").unwrap();
git(&web, &["commit", "-q", "-am", "two"]);
std::fs::write(web.join("README"), "wip 2\n").unwrap();
let again = prepare(&t, &fleet, &cfg, "web", None);
let cache = cache_path(&fleet, &repo::identity(Repo::Checkout(&web), "HEAD").unwrap());
(dir, checkout, first, again, cache, web)
}
#[test]
fn a_checkout_no_instance_mounts_is_recut_to_what_was_sent() {
let (dir, checkout, _, again, _, web) = place_twice("recut", false);
again.unwrap();
assert_eq!(git(&checkout, &["rev-parse", "HEAD"]), git(&web, &["rev-parse", "main"]));
assert_eq!(git(&checkout, &["symbolic-ref", "--short", "HEAD"]), "main");
assert_eq!(std::fs::read_to_string(checkout.join("README")).unwrap(), "wip 2\n");
assert!(!checkout.join("U").exists(), "WIP-1's untracked file is gone");
assert_eq!(std::fs::read_to_string(checkout.join("x.log")).unwrap(), "built\n", "ignored files stay");
assert_eq!(git(&checkout, &["status", "--porcelain=v1", "-uall"]), "M README");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_checkout_an_instance_mounts_keeps_its_tree_unless_it_can_fast_forward() {
let (dir, checkout, first, again, cache, web) = place_twice("in-use", true);
again.unwrap();
assert_eq!(git(&checkout, &["rev-parse", "HEAD"]), first);
assert_eq!(std::fs::read_to_string(checkout.join("README")).unwrap(), "wip 1\n");
assert!(checkout.join("U").exists());
assert_eq!(git(&cache, &["rev-parse", "refs/heads/main"]), git(&web, &["rev-parse", "main"]), "the new tip is in the cache");
git(&checkout, &["checkout", "-q", "--", "README"]);
std::fs::remove_file(checkout.join("U")).unwrap();
std::fs::write(web.join("README"), "three\n").unwrap();
git(&web, &["commit", "-q", "-am", "three"]);
let cfg = dir.join("cfg");
let fleet = dir.join("worker/fleet/control_plane");
let t = LinkTransfer::for_worker(&dir.join("cp"), "w");
prepare(&t, &fleet, &cfg, "web", None).unwrap();
assert_eq!(git(&checkout, &["rev-parse", "HEAD"]), git(&web, &["rev-parse", "main"]));
assert_eq!(std::fs::read_to_string(checkout.join("README")).unwrap(), "three\n");
assert_eq!(git(&checkout, &["status", "--porcelain=v1", "-uall"]), "", "no WIP applied");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_checkout_is_in_use_when_an_instance_mounts_it_directly() {
let dir = scratch("mounted");
let (at, other) = (dir.join("roots/h/web"), dir.join("roots/h/lib"));
std::fs::create_dir_all(&at).unwrap();
std::fs::create_dir_all(&other).unwrap();
let state = |body: &str| -> crate::state::State {
toml::from_str(&format!("[instance.a]\nsandbox = \"web\"\ncontainer = \"c\"\nworkspace = \"/w\"\ncreated_unix = 0\n{body}")).unwrap()
};
let q = |p: &Path| p.display().to_string();
assert!(!mounted(&crate::state::State::default(), &at));
assert!(mounted(&state(&format!("folder = \"{}\"\n", q(&at))), &at));
std::os::unix::fs::symlink(&at, dir.join("link")).unwrap();
assert!(mounted(&state(&format!("folder = \"{}\"\n", q(&dir.join("link")))), &at));
assert!(!mounted(&state(&format!("folder = \"{}\"\n", q(&other))), &at));
let wt = format!("folder = \"{}\"\nbase_folder = \"{}\"\nworktree = \"{0}\"\n", q(&dir.join("wt")), q(&at));
assert!(!mounted(&state(&wt), &at));
let entry = |worktree: &str| format!("folder = \"{}\"\n[[instance.a.folders]]\ntarget = \"/w/web\"\nbase = \"{}\"\n{worktree}", q(&other), q(&at));
assert!(mounted(&state(&entry("")), &at));
assert!(!mounted(&state(&entry(&format!("worktree = \"{}\"\n", q(&dir.join("wt2"))))), &at));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn haves_answer_the_fleet_roots_roots_and_caches() {
let dir = scratch("haves");
let fleet = dir.join("fleet");
assert_eq!(haves_of(&fleet, &[]).unwrap(), Haves::default(), "no fleet root yet");
for d in ["roots/0123456789abcdef", "roots/.in", "roots/.0123456789abcdef.1-2.part", "roots/not-a-hash"] {
std::fs::create_dir_all(fleet.join(d)).unwrap();
}
std::fs::write(fleet.join("roots/fedcba9876543210"), "a file").unwrap();
let cache = cache_path(&fleet, "00000000000000aa");
std::fs::create_dir_all(&cache).unwrap();
git(&cache, &["init", "-q", "--bare"]);
let ids = ["00000000000000aa".to_string(), "00000000000000bb".to_string()];
let got = haves_of(&fleet, &ids).unwrap();
assert_eq!(got, Haves { roots: ["0123456789abcdef".to_string()].into(), tips: BTreeMap::new() }, "an empty cache has no tips");
let src = dir.join("src");
std::fs::create_dir_all(&src).unwrap();
git(&src, &["init", "-q", "-b", "main"]);
git(&src, &["commit", "-q", "--allow-empty", "-m", "x"]);
git(&src, &["push", "-q", cache.to_str().unwrap(), "main"]);
let got = haves_call(&fleet, &json!({ "repos": ids })).unwrap();
assert_eq!(got, json!({"roots": ["0123456789abcdef"], "tips": {"00000000000000aa": [git(&src, &["rev-parse", "HEAD"])]}}));
crate::serve::dts::assert_matches("CodeHaves", &got);
let code = |params: Value| haves_call(&fleet, ¶ms).unwrap_err().code;
assert_eq!(code(json!({})), "invalid");
assert_eq!(code(json!({"repos": "x"})), "invalid");
assert_eq!(code(json!({"repos": [1]})), "invalid");
assert_eq!(code(json!({"repos": ["../../etc"]})), "invalid");
assert_eq!(code(json!({"repos": vec!["0123456789abcdef"; HAVES_REPOS_MAX + 1]})), "invalid");
let _ = std::fs::remove_dir_all(&dir);
}
}