mod common;
use std::os::fd::AsFd;
use std::path::Path;
use std::process::{Command, Stdio};
use common::{CHAIN_C, TmpDir, file_inventory, write_chain};
use ostrya::{
Checksum, CommitModifier, CommitModifierFlags, CommitOptions, CreateOptions, DictBuilder,
Error, FsckOptions, MediaExportOptions, MutableTree, ObjectType, Ref, Repo, RepoMode, Summary,
SummaryOptions, Type, Value,
};
use ostrya_rt::block_on;
const ID: &str = "org.example.Os";
const OTHER: &str = "org.example.Other";
const REF: &str = "os/stable";
const REQUIRE_MEDIA_FS: &str = "OSTRYA_REQUIRE_MEDIA_FS";
#[derive(Clone, Copy)]
struct Bind<'a> {
collection: Option<&'a str>,
refs: Option<&'a [&'a str]>,
}
const BOUND: Bind<'static> = Bind {
collection: Some(ID),
refs: Some(&[REF]),
};
fn entry(key: &str, ty: &str, value: Value) -> Value {
Value::Tuple(vec![
Value::Str(key.to_owned()),
Value::Variant(Box::new((Type::parse(ty).unwrap(), value))),
])
}
async fn create(path: &Path, mode: RepoMode, own: Option<&str>) -> Repo {
let mut opts = CreateOptions::new(mode);
if let Some(id) = own {
opts = opts.collection_id(id);
}
Repo::create(path, opts).await.unwrap()
}
async fn source(dir: &Path, own: Option<&str>) -> Repo {
create(&dir.join("src"), RepoMode::Archive, own).await
}
fn mount_dir(dir: &Path) -> std::path::PathBuf {
let mount = dir.join("mnt");
std::fs::create_dir(&mount).unwrap();
mount
}
async fn commit(
repo: &Repo,
dir: &Path,
marker: &str,
target: Option<&Ref>,
bind: Bind<'_>,
related: &[(&str, Checksum)],
) -> Checksum {
let sub = format!("tree-{marker}");
std::fs::create_dir_all(dir.join(&sub).join("sub")).unwrap();
std::fs::write(dir.join(&sub).join("hello.txt"), marker).unwrap();
std::fs::write(dir.join(&sub).join("sub/nested.txt"), b"nested\n").unwrap();
let txn = repo.transaction().await.unwrap();
let mut mtree = MutableTree::new();
let mut modifier = CommitModifier::new(
CommitModifierFlags::SKIP_XATTRS | CommitModifierFlags::CANONICAL_PERMISSIONS,
);
let dfd = std::fs::File::open(dir).unwrap();
txn.write_dfd_to_mtree(
dfd.as_fd(),
Path::new(&sub),
&mut mtree,
Some(&mut modifier),
)
.await
.unwrap();
let root = txn.write_mtree(&mut mtree).await.unwrap();
let mut metadata = Vec::new();
if let Some(id) = bind.collection {
metadata.push(entry(
"ostree.collection-binding",
"s",
Value::Str(id.to_owned()),
));
}
if let Some(refs) = bind.refs {
let refs = refs.iter().map(|r| Value::Str((*r).to_owned())).collect();
metadata.push(entry("ostree.ref-binding", "as", Value::Array(refs)));
}
let checksum = txn
.write_commit(
CommitOptions {
subject: Some(marker.to_owned()),
timestamp: Some(1_700_000_000),
metadata: Some(Value::Array(metadata)),
attachments: related
.iter()
.map(|(name, checksum)| ((*name).to_owned(), *checksum))
.collect(),
..CommitOptions::default()
},
&root,
)
.await
.unwrap();
if let Some(target) = target {
txn.set_ref(target, Some(&checksum));
}
txn.commit().await.unwrap();
checksum
}
async fn export(src: &Repo, mount: &Path, refs: &[Ref]) -> ostrya::Result<Vec<(Ref, Checksum)>> {
src.export_media(mount, refs, MediaExportOptions::default())
.await
.map(|report| report.refs)
}
fn media(mount: &Path) -> std::path::PathBuf {
mount.join(".ostree/repo")
}
fn media_ref(mount: &Path, rel: &str) -> Option<Checksum> {
let text = std::fs::read_to_string(media(mount).join("refs").join(rel)).ok()?;
Some(text.trim_end().parse().unwrap())
}
fn media_summary(mount: &Path) -> Summary {
Summary::parse(&std::fs::read(media(mount).join("summary")).unwrap()).unwrap()
}
fn assert_untouched(mount: &Path) {
assert!(!mount.join(".ostree").exists(), "{mount:?}");
}
async fn assert_fsck_clean(path: &Path) {
let repo = Repo::open(path).await.unwrap();
let report = repo.fsck(&FsckOptions::new()).await.unwrap();
assert!(report.errors.is_empty(), "{:?}", report.errors);
}
#[test]
fn a_stale_source_summary_is_not_read() {
let tmp = TmpDir::new("media-stale-summary");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let old = commit(&src, tmp.path(), "old", Some(&Ref::local(REF)), BOUND, &[]).await;
src.regenerate_summary(&SummaryOptions::default())
.await
.unwrap();
let new = commit(&src, tmp.path(), "new", Some(&Ref::local(REF)), BOUND, &[]).await;
assert_ne!(old, new);
let mount = mount_dir(tmp.path());
let refs = export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
assert_eq!(refs, [(Ref::collection(ID, REF), new)]);
assert_eq!(media_ref(&mount, &format!("mirrors/{ID}/{REF}")), Some(new));
});
}
#[test]
fn a_ref_that_only_a_remote_holds_is_not_found() {
let tmp = TmpDir::new("media-remote-only");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let config = src.path().join("config");
let text = std::fs::read_to_string(&config).unwrap();
std::fs::write(
&config,
format!("{text}[remote \"origin\"]\nurl=file:///nowhere\ncollection-id={OTHER}\n"),
)
.unwrap();
let src = Repo::open(src.path()).await.unwrap();
let bind = Bind {
collection: Some(OTHER),
refs: Some(&[REF]),
};
commit(
&src,
tmp.path(),
"remote",
Some(&Ref::remote("origin", REF)),
bind,
&[],
)
.await;
let mount = mount_dir(tmp.path());
let err = export(&src, &mount, &[Ref::collection(OTHER, REF)])
.await
.unwrap_err();
assert!(
matches!(&err, Error::RefNotFound(text) if *text == format!("@{OTHER}:{REF}")),
"{err:?}"
);
assert_untouched(&mount);
});
}
#[test]
fn an_operand_that_names_a_ref_directory_is_not_found() {
let tmp = TmpDir::new("media-ref-directory");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
let under = format!("{REF}/x");
for (operand, name, directory) in [
(Ref::local("os"), "os", true),
(Ref::collection(ID, "os"), "os", true),
(Ref::local(&under), under.as_str(), false),
(Ref::collection(ID, &under), under.as_str(), false),
] {
let err = export(&src, &mount, &[operand]).await.unwrap_err();
let text = format!("@{ID}:{name}");
if directory {
assert!(
matches!(&err, Error::NotARef { name, .. } if *name == text),
"{err:?}"
);
} else {
assert!(
matches!(&err, Error::RefNotFound(name) if *name == text),
"{err:?}"
);
}
assert_untouched(&mount);
}
});
}
#[test]
fn a_remote_ref_is_refused() {
let tmp = TmpDir::new("media-remote-ref");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let mount = mount_dir(tmp.path());
let err = export(&src, &mount, &[Ref::remote("origin", REF)])
.await
.unwrap_err();
assert!(
matches!(&err, Error::InvalidRefspec(text) if *text == format!("origin:{REF}")),
"{err:?}"
);
assert_untouched(&mount);
});
}
#[test]
fn a_local_name_goes_under_the_own_collection_id() {
let tmp = TmpDir::new("media-local-name");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let tip = commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
let refs = export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
assert_eq!(refs, [(Ref::collection(ID, REF), tip)]);
assert_eq!(media_ref(&mount, &format!("mirrors/{ID}/{REF}")), Some(tip));
assert_eq!(media_ref(&mount, &format!("heads/{REF}")), None);
});
}
#[test]
fn a_local_name_from_a_source_with_no_collection_id_is_refused() {
let tmp = TmpDir::new("media-no-own-id");
block_on(async {
let src = source(tmp.path(), None).await;
commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
let err = export(&src, &mount, &[Ref::local(REF)]).await.unwrap_err();
assert!(matches!(err, Error::InvalidInput(_)), "{err:?}");
assert_untouched(&mount);
});
}
#[test]
fn a_commit_bound_to_another_collection_is_refused() {
let tmp = TmpDir::new("media-other-binding");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let target = Ref::collection(OTHER, REF);
commit(&src, tmp.path(), "a", Some(&target), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
let err = export(&src, &mount, &[target]).await.unwrap_err();
assert!(matches!(err, Error::Pull(_)), "{err:?}");
assert_untouched(&mount);
});
}
#[test]
fn a_commit_with_no_collection_binding_is_refused() {
let tmp = TmpDir::new("media-no-binding");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let bind = Bind {
collection: None,
refs: Some(&[REF]),
};
commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), bind, &[]).await;
let mount = mount_dir(tmp.path());
let err = export(&src, &mount, &[Ref::local(REF)]).await.unwrap_err();
assert!(matches!(err, Error::Pull(_)), "{err:?}");
assert_untouched(&mount);
});
}
#[test]
fn a_name_and_its_own_collection_form_are_refused() {
let tmp = TmpDir::new("media-two-forms");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
let err = export(&src, &mount, &[Ref::local(REF), Ref::collection(ID, REF)])
.await
.unwrap_err();
assert!(matches!(err, Error::InvalidInput(_)), "{err:?}");
assert_untouched(&mount);
});
}
#[test]
fn a_duplicate_operand_is_exported_once() {
let tmp = TmpDir::new("media-duplicate");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let tip = commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
let operand = Ref::collection(ID, REF);
let refs = export(&src, &mount, &[operand.clone(), operand])
.await
.unwrap();
assert_eq!(refs, [(Ref::collection(ID, REF), tip)]);
});
}
#[test]
fn the_refs_of_two_collections_go_onto_one_media() {
let tmp = TmpDir::new("media-two-collections");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let main = commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let app_ref = Ref::collection(OTHER, "app");
let app_bind = Bind {
collection: Some(OTHER),
refs: Some(&["app"]),
};
let app = commit(&src, tmp.path(), "b", Some(&app_ref), app_bind, &[]).await;
let mount = mount_dir(tmp.path());
let refs = export(&src, &mount, &[app_ref.clone(), Ref::local(REF)])
.await
.unwrap();
assert_eq!(refs, [(app_ref, app), (Ref::collection(ID, REF), main)]);
assert_eq!(
media_ref(&mount, &format!("mirrors/{OTHER}/app")),
Some(app)
);
assert_eq!(
media_ref(&mount, &format!("mirrors/{ID}/{REF}")),
Some(main)
);
assert_fsck_clean(&media(&mount)).await;
});
}
#[test]
fn a_new_destination_is_an_archive_repository() {
let tmp = TmpDir::new("media-new");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let tip = commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mut dict = DictBuilder::new();
dict.insert_str("example.key", "value");
src.write_commit_detached_metadata(&tip, Some(&dict.build()))
.await
.unwrap();
let mount = mount_dir(tmp.path());
let report = src
.export_media(&mount, &[Ref::local(REF)], MediaExportOptions::default())
.await
.unwrap();
let repo = media(&mount);
assert_eq!(report.repo, repo);
assert_eq!(
std::fs::read_to_string(repo.join("config")).unwrap(),
"[core]\nrepo_version=1\nmode=archive-z2\nindexed-deltas=true\n"
);
assert_eq!(media_ref(&mount, &format!("mirrors/{ID}/{REF}")), Some(tip));
assert!(file_inventory(&repo, "refs/heads").is_empty());
let meta = format!(
"objects/{}/{}.commitmeta",
&tip.to_string()[..2],
&tip.to_string()[2..]
);
assert_eq!(
std::fs::read(repo.join(&meta)).unwrap(),
std::fs::read(src.path().join(&meta)).unwrap()
);
assert!(file_inventory(&repo, "deltas").is_empty());
assert!(!repo.join("summary.sig").exists());
assert!(!repo.join("extensions/ostrya/summary").exists());
let summary = media_summary(&mount);
assert!(summary.refs.is_empty());
let map = summary.collection_map().unwrap();
assert_eq!(map.len(), 1);
assert_eq!(map[0].0, ID);
assert_eq!(map[0].1.len(), 1);
assert_eq!(map[0].1[0].name, REF);
assert_eq!(map[0].1[0].commit, tip);
assert!(
summary
.metadata_value("ostree.summary.collection-id")
.is_none()
);
assert_eq!(
summary.metadata_value("ostree.summary.indexed-deltas"),
Some(&Value::Bool(true))
);
assert_fsck_clean(&repo).await;
});
}
#[test]
fn an_existing_destination_keeps_its_mode_and_collection_id() {
let tmp = TmpDir::new("media-existing");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let tip = commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
std::fs::create_dir(mount.join(".ostree")).unwrap();
create(&media(&mount), RepoMode::BareUserOnly, Some(OTHER)).await;
export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
let repo = Repo::open(&media(&mount)).await.unwrap();
assert_eq!(repo.mode(), RepoMode::BareUserOnly);
assert_eq!(repo.config().collection_id(), Some(OTHER));
assert_eq!(media_ref(&mount, &format!("mirrors/{ID}/{REF}")), Some(tip));
assert_fsck_clean(&media(&mount)).await;
});
}
#[test]
fn an_existing_destination_gets_indexed_deltas() {
let tmp = TmpDir::new("media-indexed-deltas");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
std::fs::create_dir(mount.join(".ostree")).unwrap();
create(&media(&mount), RepoMode::Archive, None).await;
let config = media(&mount).join("config");
std::fs::write(
&config,
"[core]\nrepo_version=1\nmode=archive-z2\nindexed-deltas=false\n",
)
.unwrap();
export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
assert_eq!(
std::fs::read_to_string(&config).unwrap(),
"[core]\nrepo_version=1\nmode=archive-z2\nindexed-deltas=true\n"
);
assert_eq!(
media_summary(&mount).metadata_value("ostree.summary.indexed-deltas"),
Some(&Value::Bool(true))
);
});
}
#[test]
fn an_existing_destination_with_a_bad_indexed_deltas_gets_true() {
let tmp = TmpDir::new("media-bad-indexed-deltas");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
std::fs::create_dir(mount.join(".ostree")).unwrap();
create(&media(&mount), RepoMode::Archive, None).await;
let config = media(&mount).join("config");
std::fs::write(
&config,
"[core]\nrepo_version=1\nmode=archive-z2\nindexed-deltas=maybe\n",
)
.unwrap();
export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
assert_eq!(
std::fs::read_to_string(&config).unwrap(),
"[core]\nrepo_version=1\nmode=archive-z2\nindexed-deltas=true\n"
);
assert_eq!(
media_summary(&mount).metadata_value("ostree.summary.indexed-deltas"),
Some(&Value::Bool(true))
);
});
}
#[test]
fn an_existing_summary_sig_is_deleted() {
let tmp = TmpDir::new("media-summary-sig");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
std::fs::write(media(&mount).join("summary.sig"), b"stale").unwrap();
export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
assert!(!media(&mount).join("summary.sig").exists());
});
}
#[test]
fn a_missing_or_non_directory_mount_is_refused() {
let tmp = TmpDir::new("media-bad-mount");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let missing = tmp.path().join("missing");
let err = export(&src, &missing, &[Ref::local(REF)])
.await
.unwrap_err();
assert!(
matches!(&err, Error::Io(e) if e.kind() == std::io::ErrorKind::NotFound),
"{err:?}"
);
assert!(!missing.exists());
let file = tmp.path().join("file");
std::fs::write(&file, b"").unwrap();
let err = export(&src, &file, &[Ref::local(REF)]).await.unwrap_err();
assert!(
matches!(&err, Error::Io(e) if e.kind() == std::io::ErrorKind::NotADirectory),
"{err:?}"
);
assert_eq!(std::fs::read(&file).unwrap(), b"");
});
}
#[test]
fn the_source_repository_is_refused_as_the_media() {
let tmp = TmpDir::new("media-source-is-media");
block_on(async {
let mount = mount_dir(tmp.path());
std::fs::create_dir(mount.join(".ostree")).unwrap();
let src = create(&media(&mount), RepoMode::Archive, Some(ID)).await;
commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
src.regenerate_summary(&SummaryOptions::default())
.await
.unwrap();
std::fs::write(media(&mount).join("summary.sig"), b"sig").unwrap();
let before = file_inventory(&media(&mount), "");
let err = export(&src, &mount, &[Ref::local(REF)]).await.unwrap_err();
assert!(matches!(&err, Error::InvalidInput(_)), "{err:?}");
assert_eq!(file_inventory(&media(&mount), ""), before);
let link = tmp.path().join("link");
std::fs::create_dir_all(link.join(".ostree")).unwrap();
std::os::unix::fs::symlink(media(&mount), link.join(".ostree/repo")).unwrap();
let err = export(&src, &link, &[Ref::local(REF)]).await.unwrap_err();
assert!(matches!(&err, Error::InvalidInput(_)), "{err:?}");
assert_eq!(file_inventory(&media(&mount), ""), before);
});
}
#[test]
fn a_destination_of_the_refs_collection_writes_local_refs_and_field_0() {
let tmp = TmpDir::new("media-own-id");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let tip = commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
std::fs::create_dir(mount.join(".ostree")).unwrap();
create(&media(&mount), RepoMode::Archive, Some(ID)).await;
export(&src, &mount, &[Ref::collection(ID, REF)])
.await
.unwrap();
assert_eq!(media_ref(&mount, &format!("heads/{REF}")), Some(tip));
assert!(file_inventory(&media(&mount), "refs/mirrors").is_empty());
assert_eq!(media_ref(&mount, "heads/ostree-metadata"), None);
let summary = media_summary(&mount);
let names: Vec<&str> = summary.refs.iter().map(|r| r.name.as_str()).collect();
assert_eq!(names, [REF]);
assert!(summary.collection_map().unwrap().is_empty());
assert_eq!(
summary.metadata_value("ostree.summary.collection-id"),
Some(&Value::Str(ID.to_owned()))
);
});
}
#[test]
fn a_field_2_attachment_lands_as_loose_objects() {
let tmp = TmpDir::new("media-field-2");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let att = commit(&src, tmp.path(), "dep", None, BOUND, &[]).await;
let tip = commit(
&src,
tmp.path(),
"main",
Some(&Ref::local(REF)),
BOUND,
&[("dep", att)],
)
.await;
let mount = mount_dir(tmp.path());
export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
let repo = Repo::open(&media(&mount)).await.unwrap();
assert!(repo.has_object(ObjectType::Commit, &att).await.unwrap());
assert!(repo.has_object(ObjectType::Commit, &tip).await.unwrap());
assert_eq!(
file_inventory(&media(&mount), "refs"),
[(
format!("refs/mirrors/{ID}/{REF}"),
format!("{tip}\n").into_bytes()
)]
);
assert!(file_inventory(&media(&mount), "deltas").is_empty());
assert_fsck_clean(&media(&mount)).await;
});
}
#[test]
fn a_chain_of_attachments_lands_on_the_media() {
let tmp = TmpDir::new("media-field-2-chain");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let chain = write_chain(&src, tmp.path(), "chain").await;
let tip = commit(
&src,
tmp.path(),
"main",
Some(&Ref::local(REF)),
BOUND,
&[(CHAIN_C, chain.c)],
)
.await;
let mount = mount_dir(tmp.path());
export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
let repo = Repo::open(&media(&mount)).await.unwrap();
for commit in [tip, chain.c, chain.d] {
for name in src.traverse_commit(&commit, 0).await.unwrap() {
assert!(repo.has_object(name.ty, &name.checksum).await.unwrap());
}
}
assert!(file_inventory(&media(&mount), "state").is_empty());
assert_eq!(
file_inventory(&media(&mount), "refs"),
[(
format!("refs/mirrors/{ID}/{REF}"),
format!("{tip}\n").into_bytes()
)]
);
assert_fsck_clean(&media(&mount)).await;
});
}
#[test]
fn the_commit_option_pins_an_older_commit() {
let tmp = TmpDir::new("media-commit-option");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let old = commit(&src, tmp.path(), "old", Some(&Ref::local(REF)), BOUND, &[]).await;
let new = commit(&src, tmp.path(), "new", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
assert_eq!(media_ref(&mount, &format!("mirrors/{ID}/{REF}")), Some(new));
let report = src
.export_media(
&mount,
&[Ref::local(REF)],
MediaExportOptions { commit: Some(old) },
)
.await
.unwrap();
assert_eq!(report.refs, [(Ref::collection(ID, REF), old)]);
assert_eq!(media_ref(&mount, &format!("mirrors/{ID}/{REF}")), Some(old));
});
}
#[test]
fn the_commit_option_takes_one_ref() {
let tmp = TmpDir::new("media-commit-one-ref");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let tip = commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let mount = mount_dir(tmp.path());
let err = src
.export_media(
&mount,
&[Ref::local(REF), Ref::local("other")],
MediaExportOptions { commit: Some(tip) },
)
.await
.unwrap_err();
assert!(matches!(err, Error::InvalidInput(_)), "{err:?}");
let err = export(&src, &mount, &[]).await.unwrap_err();
assert!(matches!(err, Error::InvalidInput(_)), "{err:?}");
assert_untouched(&mount);
});
}
#[test]
fn an_unknown_commit_fails_before_the_destination() {
let tmp = TmpDir::new("media-unknown-commit");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let mount = mount_dir(tmp.path());
let err = src
.export_media(
&mount,
&[Ref::local(REF)],
MediaExportOptions {
commit: Some(Checksum::from_bytes([7; 32])),
},
)
.await
.unwrap_err();
assert!(matches!(err, Error::ObjectNotFound { .. }), "{err:?}");
assert_untouched(&mount);
});
}
#[test]
fn a_tip_bound_to_another_ref_is_refused() {
let tmp = TmpDir::new("media-tip-ref-binding");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let bind = Bind {
collection: Some(ID),
refs: Some(&["other"]),
};
commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), bind, &[]).await;
let mount = mount_dir(tmp.path());
let err = export(&src, &mount, &[Ref::local(REF)]).await.unwrap_err();
assert!(matches!(err, Error::Pull(_)), "{err:?}");
assert_untouched(&mount);
});
}
#[test]
fn a_commit_bound_to_another_ref_is_refused() {
let tmp = TmpDir::new("media-ref-binding");
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let bind = Bind {
collection: Some(ID),
refs: Some(&["other"]),
};
let tip = commit(&src, tmp.path(), "a", None, bind, &[]).await;
let mount = mount_dir(tmp.path());
let err = src
.export_media(
&mount,
&[Ref::local(REF)],
MediaExportOptions { commit: Some(tip) },
)
.await
.unwrap_err();
assert!(matches!(err, Error::Pull(_)), "{err:?}");
assert_untouched(&mount);
});
}
fn runs(program: &str, args: &[&std::ffi::OsStr]) -> bool {
Command::new(program)
.args(args)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok_and(|status| status.success())
}
fn skip(reason: &str) {
assert!(
std::env::var_os(REQUIRE_MEDIA_FS).is_none(),
"{REQUIRE_MEDIA_FS} is set and {reason}"
);
eprintln!("skipped: {reason}");
}
struct Unmount(std::path::PathBuf);
impl Drop for Unmount {
fn drop(&mut self) {
let _ = Command::new("umount").arg(&self.0).status();
}
}
#[test]
fn exports_onto_vfat_and_exfat() {
if !rustix::process::geteuid().is_root() {
return skip("the test needs root");
}
let volumes: [(&str, &str, &[&str]); 2] = [
("vfat", "mkfs.vfat", &["-F", "32"]),
("exfat", "mkfs.exfat", &[]),
];
for (fs, mkfs, options) in volumes {
let tmp = TmpDir::new(&format!("media-{fs}"));
let image = tmp.path().join("image");
std::fs::File::create(&image)
.unwrap()
.set_len(64 << 20)
.unwrap();
let mut args: Vec<&std::ffi::OsStr> = options.iter().map(|o| o.as_ref()).collect();
args.push(image.as_os_str());
if !runs(mkfs, &args) {
return skip(&format!("{mkfs} cannot make a volume"));
}
let mount = mount_dir(tmp.path());
let mounted = runs(
"mount",
&[
"-t".as_ref(),
fs.as_ref(),
"-o".as_ref(),
"loop".as_ref(),
image.as_os_str(),
mount.as_os_str(),
],
);
if !mounted {
return skip(&format!("the {fs} volume does not mount"));
}
let _unmount = Unmount(mount.clone());
block_on(async {
let src = source(tmp.path(), Some(ID)).await;
let tip = commit(&src, tmp.path(), "a", Some(&Ref::local(REF)), BOUND, &[]).await;
let refs = export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
assert_eq!(refs, [(Ref::collection(ID, REF), tip)], "{fs}");
export(&src, &mount, &[Ref::local(REF)]).await.unwrap();
assert_eq!(media_ref(&mount, &format!("mirrors/{ID}/{REF}")), Some(tip));
assert_fsck_clean(&media(&mount)).await;
});
}
}