142 lines
4.7 KiB
Rust
142 lines
4.7 KiB
Rust
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use rcom::domain::{EntryKind, HostAlias, Location};
|
|
use rcom::fs::{FsError, ReadFileSystem};
|
|
use rcom::test_support::{FaultInjectFs, FsOperation, MemoryFs, RecordingFs};
|
|
use tempfile::TempDir;
|
|
|
|
#[tokio::test]
|
|
async fn memory_fs_normalizes_paths_and_keeps_local_and_ssh_trees_separate() {
|
|
let fs = MemoryFs::new()
|
|
.with_file(Location::local("/work/local.txt"), "local")
|
|
.with_file(
|
|
Location::ssh(HostAlias::new("edge"), "/work/remote.txt"),
|
|
"remote",
|
|
);
|
|
|
|
let local = Location::local("work/./nested/..");
|
|
let remote = Location::ssh(HostAlias::new("edge"), "/work");
|
|
let local_entries = fs.list(&local).await.unwrap();
|
|
let remote_entries = fs.list(&remote).await.unwrap();
|
|
|
|
assert_eq!(local_entries.len(), 1);
|
|
assert_eq!(local_entries[0].name, "local.txt");
|
|
assert_eq!(remote_entries.len(), 1);
|
|
assert_eq!(remote_entries[0].name, "remote.txt");
|
|
assert_eq!(
|
|
fs.canonicalize(&local).await.unwrap(),
|
|
Location::local("/work")
|
|
);
|
|
assert_eq!(
|
|
fs.list(&Location::local("/missing")).await.unwrap_err(),
|
|
FsError::NotFound
|
|
);
|
|
assert_eq!(
|
|
fs.list(&Location::local("/work/local.txt"))
|
|
.await
|
|
.unwrap_err(),
|
|
FsError::NotADirectory
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn memory_fs_metadata_and_read_prefix_match_the_read_only_contract() {
|
|
let fs = MemoryFs::new().with_file(Location::local("/docs/readme.txt"), "abcd");
|
|
let file = Location::local("/docs/./readme.txt");
|
|
|
|
let metadata = fs.metadata(&file).await.unwrap();
|
|
let prefix = fs.read_prefix(&file, 3).await.unwrap();
|
|
let empty_prefix = fs.read_prefix(&file, 0).await.unwrap();
|
|
|
|
assert_eq!(metadata.name, "readme.txt");
|
|
assert_eq!(metadata.kind, EntryKind::File);
|
|
assert_eq!(metadata.size, Some(4));
|
|
assert_eq!(prefix.data, b"abc");
|
|
assert!(prefix.truncated);
|
|
assert!(empty_prefix.data.is_empty());
|
|
assert!(empty_prefix.truncated);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn fault_injection_returns_permission_denied_and_not_found_without_fixture_paths() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let location = Location::local(fixture.path().join("private").join("data"));
|
|
let fs = FaultInjectFs::new(Arc::new(MemoryFs::new()));
|
|
fs.fail_next(FsOperation::List, FsError::PermissionDenied);
|
|
fs.fail_next(FsOperation::Metadata, FsError::NotFound);
|
|
|
|
let list_error = fs.list(&location).await.unwrap_err();
|
|
let metadata_error = fs.metadata(&location).await.unwrap_err();
|
|
|
|
for error in [&list_error, &metadata_error] {
|
|
assert!(
|
|
!error
|
|
.to_string()
|
|
.contains(&fixture.path().display().to_string())
|
|
);
|
|
assert!(
|
|
!error
|
|
.user_message()
|
|
.contains(&fixture.path().display().to_string())
|
|
);
|
|
}
|
|
assert_eq!(list_error, FsError::PermissionDenied);
|
|
assert_eq!(metadata_error, FsError::NotFound);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn faults_are_consumed_in_order_and_delays_still_delegate() {
|
|
let fs = FaultInjectFs::new(Arc::new(
|
|
MemoryFs::new().with_file(Location::local("/file"), "ok"),
|
|
));
|
|
fs.delay_next(FsOperation::ReadPrefix, Duration::from_millis(1));
|
|
fs.fail_next(FsOperation::ReadPrefix, FsError::NotFound);
|
|
|
|
assert_eq!(
|
|
fs.read_prefix(&Location::local("/file"), 1)
|
|
.await
|
|
.unwrap()
|
|
.data,
|
|
b"o"
|
|
);
|
|
assert_eq!(
|
|
fs.read_prefix(&Location::local("/file"), 1)
|
|
.await
|
|
.unwrap_err(),
|
|
FsError::NotFound
|
|
);
|
|
assert_eq!(
|
|
fs.read_prefix(&Location::local("/file"), 1)
|
|
.await
|
|
.unwrap()
|
|
.data,
|
|
b"o"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn recording_fs_journals_every_read_only_operation_and_limit() {
|
|
let fs = RecordingFs::new(Arc::new(
|
|
MemoryFs::new().with_file(Location::local("/dir/file"), "contents"),
|
|
));
|
|
let directory = Location::local("/dir");
|
|
let file = Location::local("/dir/file");
|
|
|
|
fs.canonicalize(&directory).await.unwrap();
|
|
fs.list(&directory).await.unwrap();
|
|
fs.metadata(&file).await.unwrap();
|
|
fs.read_prefix(&file, 4).await.unwrap();
|
|
|
|
let operations = fs.operations();
|
|
assert_eq!(operations.len(), 4);
|
|
assert_eq!(operations[0].operation, FsOperation::Canonicalize);
|
|
assert_eq!(operations[1].operation, FsOperation::List);
|
|
assert_eq!(operations[2].operation, FsOperation::Metadata);
|
|
assert_eq!(operations[3].operation, FsOperation::ReadPrefix);
|
|
assert_eq!(operations[3].location, file);
|
|
assert_eq!(operations[3].limit, Some(4));
|
|
|
|
fs.clear();
|
|
assert!(fs.operations().is_empty());
|
|
}
|