370 lines
12 KiB
Rust
370 lines
12 KiB
Rust
use std::ffi::OsString;
|
|
use std::path::Path;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use rcom::actions::EditorCommand;
|
|
use rcom::app::SessionEpoch;
|
|
use rcom::domain::{EntryKind, HostAlias, Location, SourceId};
|
|
use rcom::fs::{FileOperations, FsError, ReadFileSystem, StagingOperations};
|
|
use rcom::test_support::{
|
|
EditorOutcome, ExternalEditor, FaultInjectEditor, MemoryFs, MemoryTransferFs,
|
|
TransferOperation, TrashPolicy, TrashPolicyError,
|
|
};
|
|
|
|
fn assert_stale_epoch(error: FsError) {
|
|
assert!(
|
|
matches!(&error, FsError::Other(message) if message.contains("stale session epoch")),
|
|
"unexpected error: {error}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn trash_policy_enforces_typed_endpoints_and_the_injected_sandbox() {
|
|
let sandbox = Location::local("/sandbox");
|
|
let policy = TrashPolicy::new(
|
|
Location::local("/sandbox/trash/../trash"),
|
|
Some(sandbox.clone()),
|
|
)
|
|
.unwrap();
|
|
|
|
assert_eq!(policy.root(), &Location::local("/sandbox/trash/../trash"));
|
|
assert_eq!(policy.accepted_sandbox_root(), Some(&sandbox));
|
|
policy
|
|
.validate_source(&Location::local("/sandbox/work/file"))
|
|
.unwrap();
|
|
assert_eq!(
|
|
policy.validate_source(&Location::ssh(HostAlias::new("edge"), "/sandbox/work/file")),
|
|
Err(TrashPolicyError::MixedEndpoints)
|
|
);
|
|
assert_eq!(
|
|
TrashPolicy::new(Location::local("/outside/trash"), Some(sandbox.clone())),
|
|
Err(TrashPolicyError::OutsideAcceptedSandbox)
|
|
);
|
|
assert_eq!(
|
|
TrashPolicy::new(Location::local("/sandbox-other/trash"), Some(sandbox)),
|
|
Err(TrashPolicyError::OutsideAcceptedSandbox)
|
|
);
|
|
assert_eq!(
|
|
TrashPolicy::new(
|
|
Location::local("/sandbox/trash"),
|
|
Some(Location::ssh(HostAlias::new("edge"), "/sandbox")),
|
|
),
|
|
Err(TrashPolicyError::MixedEndpoints)
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn create_and_mkdir_support_faults_create_new_and_epoch_journals() {
|
|
const EPOCH: SessionEpoch = 11;
|
|
let fs = MemoryTransferFs::new(SourceId::Local, EPOCH).with_dir(Location::local("/out"));
|
|
let file = Location::local("/out/new.txt");
|
|
let directory = Location::local("/out/new-dir");
|
|
|
|
fs.fail_next(TransferOperation::CreateFile, FsError::PermissionDenied);
|
|
assert_eq!(
|
|
fs.create_file(&file).await.unwrap_err(),
|
|
FsError::PermissionDenied
|
|
);
|
|
assert_eq!(fs.file_contents(&file), Err(FsError::NotFound));
|
|
|
|
fs.create_file(&file).await.unwrap();
|
|
assert_eq!(fs.file_contents(&file).unwrap(), b"");
|
|
fs.insert_file(file.clone(), b"keep me");
|
|
assert!(fs.create_file(&file).await.is_err());
|
|
assert_eq!(fs.file_contents(&file).unwrap(), b"keep me");
|
|
|
|
fs.fail_next(TransferOperation::CreateDir, FsError::Timeout);
|
|
assert_eq!(
|
|
fs.create_dir(&directory).await.unwrap_err(),
|
|
FsError::Timeout
|
|
);
|
|
assert_eq!(
|
|
FileOperations::metadata(&fs, &directory, false)
|
|
.await
|
|
.unwrap_err(),
|
|
FsError::NotFound
|
|
);
|
|
fs.create_dir(&directory).await.unwrap();
|
|
assert_eq!(
|
|
FileOperations::metadata(&fs, &directory, false)
|
|
.await
|
|
.unwrap()
|
|
.kind,
|
|
EntryKind::Directory
|
|
);
|
|
|
|
let operations = fs.operations();
|
|
let creates: Vec<_> = operations
|
|
.iter()
|
|
.filter(|entry| {
|
|
matches!(
|
|
entry.operation,
|
|
TransferOperation::CreateFile | TransferOperation::CreateDir
|
|
)
|
|
})
|
|
.collect();
|
|
assert_eq!(creates.len(), 5);
|
|
assert!(creates.iter().all(|entry| entry.epoch == EPOCH));
|
|
assert_eq!(creates[0].location, file);
|
|
assert_eq!(creates[0].destination, None);
|
|
assert!(creates.iter().any(|entry| entry.location == directory));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rename_and_remove_faults_are_journaled_without_mutations() {
|
|
const EPOCH: SessionEpoch = 4;
|
|
let from = Location::local("/work/from");
|
|
let to = Location::local("/work/to");
|
|
let file = Location::local("/work/delete-file");
|
|
let directory = Location::local("/work/delete-dir");
|
|
let fs = MemoryTransferFs::new(SourceId::Local, EPOCH)
|
|
.with_file(from.clone(), b"rename")
|
|
.with_file(file.clone(), b"delete")
|
|
.with_dir(directory.clone());
|
|
|
|
fs.fail_next(TransferOperation::Rename, FsError::Disconnected);
|
|
assert_eq!(
|
|
fs.rename(&from, &to).await.unwrap_err(),
|
|
FsError::Disconnected
|
|
);
|
|
assert_eq!(fs.file_contents(&from).unwrap(), b"rename");
|
|
assert_eq!(fs.file_contents(&to), Err(FsError::NotFound));
|
|
|
|
fs.fail_next(TransferOperation::RemoveFile, FsError::PermissionDenied);
|
|
assert_eq!(
|
|
fs.remove_file(&file).await.unwrap_err(),
|
|
FsError::PermissionDenied
|
|
);
|
|
assert_eq!(fs.file_contents(&file).unwrap(), b"delete");
|
|
|
|
fs.fail_next(TransferOperation::RemoveDir, FsError::Timeout);
|
|
assert_eq!(
|
|
fs.remove_dir(&directory).await.unwrap_err(),
|
|
FsError::Timeout
|
|
);
|
|
assert_eq!(
|
|
FileOperations::metadata(&fs, &directory, false)
|
|
.await
|
|
.unwrap()
|
|
.kind,
|
|
EntryKind::Directory
|
|
);
|
|
|
|
let operations = fs.operations();
|
|
assert_eq!(operations[0].operation, TransferOperation::Rename);
|
|
assert_eq!(operations[0].location, from);
|
|
assert_eq!(operations[0].destination, Some(to));
|
|
assert_eq!(operations[1].operation, TransferOperation::RemoveFile);
|
|
assert_eq!(operations[1].location, file);
|
|
assert_eq!(operations[2].operation, TransferOperation::RemoveDir);
|
|
assert_eq!(operations[2].location, directory);
|
|
assert!(operations[..3].iter().all(|entry| entry.epoch == EPOCH));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn stale_epoch_rejects_every_new_action_and_is_still_journaled() {
|
|
let from = Location::local("/from");
|
|
let file = Location::local("/file");
|
|
let directory = Location::local("/empty");
|
|
let stale = MemoryTransferFs::new(SourceId::Local, 7)
|
|
.with_file(from.clone(), b"from")
|
|
.with_file(file.clone(), b"file")
|
|
.with_dir(directory.clone())
|
|
.with_dir(Location::local("/out"));
|
|
let current = stale.reconnect(8);
|
|
|
|
assert_stale_epoch(
|
|
stale
|
|
.create_file(&Location::local("/out/new"))
|
|
.await
|
|
.unwrap_err(),
|
|
);
|
|
assert_stale_epoch(
|
|
stale
|
|
.create_dir(&Location::local("/out/new-dir"))
|
|
.await
|
|
.unwrap_err(),
|
|
);
|
|
assert_stale_epoch(
|
|
stale
|
|
.rename(&from, &Location::local("/to"))
|
|
.await
|
|
.unwrap_err(),
|
|
);
|
|
assert_stale_epoch(stale.remove_file(&file).await.unwrap_err());
|
|
assert_stale_epoch(stale.remove_dir(&directory).await.unwrap_err());
|
|
assert_stale_epoch(stale.open_read(&file).await.err().unwrap());
|
|
assert_stale_epoch(
|
|
stale
|
|
.open_part(&Location::local("/out/upload"))
|
|
.await
|
|
.err()
|
|
.unwrap(),
|
|
);
|
|
|
|
assert_eq!(stale.file_contents(&from).unwrap(), b"from");
|
|
assert_eq!(stale.file_contents(&file).unwrap(), b"file");
|
|
assert_eq!(
|
|
FileOperations::metadata(¤t, &directory, false)
|
|
.await
|
|
.unwrap()
|
|
.kind,
|
|
EntryKind::Directory
|
|
);
|
|
let operations = stale.operations();
|
|
assert_eq!(operations.len(), 8);
|
|
assert!(operations[..7].iter().all(|entry| entry.epoch == 7));
|
|
assert!(
|
|
operations[..7]
|
|
.iter()
|
|
.all(|entry| !entry.location.path.as_os_str().is_empty())
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn download_and_upload_faults_preserve_sources_and_journal_typed_locations() {
|
|
const EPOCH: SessionEpoch = 19;
|
|
let host = HostAlias::new("edge");
|
|
let source = Location::ssh(host.clone(), "/work/source.txt");
|
|
let fs = MemoryTransferFs::new(SourceId::Ssh(host), EPOCH).with_file(source.clone(), b"remote");
|
|
|
|
fs.fail_next(TransferOperation::Read, FsError::Timeout);
|
|
assert_eq!(fs.open_read(&source).await.err().unwrap(), FsError::Timeout);
|
|
assert_eq!(fs.file_contents(&source).unwrap(), b"remote");
|
|
|
|
let mut writer = fs.open_part(&source).await.unwrap();
|
|
let failed_part = writer.part().location().clone();
|
|
fs.fail_next(TransferOperation::Write, FsError::Disconnected);
|
|
assert_eq!(
|
|
fs.write_part(&mut writer, b"replacement")
|
|
.await
|
|
.unwrap_err(),
|
|
FsError::Disconnected
|
|
);
|
|
assert_eq!(fs.part_contents(&failed_part).unwrap(), b"");
|
|
assert_eq!(fs.file_contents(&source).unwrap(), b"remote");
|
|
|
|
let operations = fs.operations();
|
|
assert_eq!(operations[0].operation, TransferOperation::Read);
|
|
assert_eq!(operations[0].location, source);
|
|
assert_eq!(operations[0].epoch, EPOCH);
|
|
let write = operations
|
|
.iter()
|
|
.find(|entry| entry.operation == TransferOperation::Write)
|
|
.unwrap();
|
|
assert_eq!(write.location, failed_part);
|
|
assert_eq!(write.destination, Some(source));
|
|
assert_eq!(write.epoch, EPOCH);
|
|
assert_eq!(write.bytes, Some("replacement".len()));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn editor_double_records_direct_arguments_and_all_typed_outcomes() {
|
|
let editor = FaultInjectEditor::new();
|
|
let command = EditorCommand::parse("fake-editor --wait 'name with spaces'").unwrap();
|
|
let paths = [
|
|
Path::new("/owned/first.txt"),
|
|
Path::new("/owned/second.txt"),
|
|
Path::new("/owned/third.txt"),
|
|
];
|
|
editor.queue_success();
|
|
editor.queue_nonzero(Some(23));
|
|
editor.queue_nonzero(None);
|
|
editor.queue_spawn_error(FsError::NotFound);
|
|
|
|
assert_eq!(
|
|
editor.edit(&command, paths[0]).await.unwrap(),
|
|
EditorOutcome::Success
|
|
);
|
|
assert_eq!(
|
|
editor.edit(&command, paths[1]).await.unwrap(),
|
|
EditorOutcome::NonZero(Some(23))
|
|
);
|
|
assert_eq!(
|
|
editor.edit(&command, paths[2]).await.unwrap(),
|
|
EditorOutcome::NonZero(None)
|
|
);
|
|
assert_eq!(
|
|
editor.edit(&command, paths[0]).await.unwrap_err(),
|
|
FsError::NotFound
|
|
);
|
|
|
|
let invocations = editor.invocations();
|
|
assert_eq!(invocations.len(), 4);
|
|
for (invocation, path) in invocations.iter().zip(paths.iter().cycle()) {
|
|
assert_eq!(invocation.program, OsString::from("fake-editor"));
|
|
assert_eq!(
|
|
invocation.arguments,
|
|
[OsString::from("--wait"), OsString::from("name with spaces")]
|
|
);
|
|
assert_eq!(invocation.path, path.to_path_buf());
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn editor_gate_uses_notify_and_releases_without_a_sleep() {
|
|
let editor = FaultInjectEditor::new();
|
|
let command = EditorCommand::parse("fake-editor --flag").unwrap();
|
|
let gate = editor.gate_next();
|
|
let blocked = editor.clone();
|
|
let task =
|
|
tokio::spawn(async move { blocked.edit(&command, Path::new("/owned/gated.txt")).await });
|
|
|
|
tokio::time::timeout(Duration::from_secs(1), gate.wait_entered())
|
|
.await
|
|
.expect("editor did not reach its gate");
|
|
assert!(!task.is_finished());
|
|
assert_eq!(editor.invocations().len(), 1);
|
|
|
|
gate.release();
|
|
assert_eq!(
|
|
tokio::time::timeout(Duration::from_secs(1), task)
|
|
.await
|
|
.expect("released editor did not finish")
|
|
.unwrap()
|
|
.unwrap(),
|
|
EditorOutcome::Success
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn action_faults_do_not_mutate_the_read_only_memory_fs() {
|
|
let file = Location::local("/workspace/readme.txt");
|
|
let read_only = Arc::new(MemoryFs::new().with_file(file.clone(), b"read only"));
|
|
let listing_before = read_only
|
|
.list(&Location::local("/workspace"))
|
|
.await
|
|
.unwrap();
|
|
let contents_before = read_only.read_prefix(&file, 64).await.unwrap();
|
|
|
|
let mutations = MemoryTransferFs::local()
|
|
.with_file(Location::local("/workspace/other.txt"), b"other")
|
|
.with_dir(Location::local("/out"));
|
|
mutations.fail_next(TransferOperation::CreateFile, FsError::Disconnected);
|
|
mutations.fail_next(TransferOperation::RemoveFile, FsError::Disconnected);
|
|
assert!(
|
|
mutations
|
|
.create_file(&Location::local("/out/new"))
|
|
.await
|
|
.is_err()
|
|
);
|
|
assert!(
|
|
mutations
|
|
.remove_file(&Location::local("/workspace/other.txt"))
|
|
.await
|
|
.is_err()
|
|
);
|
|
|
|
assert_eq!(
|
|
read_only
|
|
.list(&Location::local("/workspace"))
|
|
.await
|
|
.unwrap(),
|
|
listing_before
|
|
);
|
|
assert_eq!(
|
|
read_only.read_prefix(&file, 64).await.unwrap(),
|
|
contents_before
|
|
);
|
|
}
|