264 lines
8.2 KiB
Rust
264 lines
8.2 KiB
Rust
|
|
use std::collections::BTreeMap;
|
||
|
|
use std::path::Path;
|
||
|
|
use std::sync::Arc;
|
||
|
|
|
||
|
|
use rcom::domain::{EntryKind, HostAlias, Location, SourceId};
|
||
|
|
use rcom::fs::{BackendRegistry, FsError, LocalFs, execute_prepared_copy_move};
|
||
|
|
use rcom::operations::{
|
||
|
|
CollisionPolicy, CommitOutcome, ItemId, ItemPhase, OperationKind, OperationPlan,
|
||
|
|
OperationPlanner, OperationRequest, PlanGeneration, PlanId, SourceCleanupOutcome, SourceItem,
|
||
|
|
};
|
||
|
|
use rcom::ssh::{ConnectionManager, OpenSshConnector};
|
||
|
|
use tempfile::TempDir;
|
||
|
|
use tokio_util::sync::CancellationToken;
|
||
|
|
use uuid::Uuid;
|
||
|
|
|
||
|
|
fn remote_plan(
|
||
|
|
id: u64,
|
||
|
|
kind: OperationKind,
|
||
|
|
sources: Vec<Location>,
|
||
|
|
destination: Location,
|
||
|
|
policy: CollisionPolicy,
|
||
|
|
epoch: u64,
|
||
|
|
) -> OperationPlan {
|
||
|
|
let sources = sources
|
||
|
|
.into_iter()
|
||
|
|
.enumerate()
|
||
|
|
.map(|(index, source)| SourceItem::new(ItemId::new(index as u64 + 1), source))
|
||
|
|
.collect();
|
||
|
|
OperationPlan::new(
|
||
|
|
PlanId::new(id),
|
||
|
|
PlanGeneration::new(id),
|
||
|
|
kind,
|
||
|
|
sources,
|
||
|
|
destination,
|
||
|
|
policy,
|
||
|
|
None,
|
||
|
|
None,
|
||
|
|
BTreeMap::from([(SourceId::Ssh(HostAlias::new("hermesVM")), epoch)]),
|
||
|
|
)
|
||
|
|
.unwrap()
|
||
|
|
}
|
||
|
|
|
||
|
|
fn assert_remote_paths_inside(plan: &OperationPlan, sandbox: &Path) {
|
||
|
|
for location in plan
|
||
|
|
.sources()
|
||
|
|
.iter()
|
||
|
|
.map(SourceItem::location)
|
||
|
|
.chain(std::iter::once(plan.destination()))
|
||
|
|
.filter(|location| location.source == SourceId::Ssh(HostAlias::new("hermesVM")))
|
||
|
|
{
|
||
|
|
assert!(
|
||
|
|
location.path().starts_with(sandbox),
|
||
|
|
"remote mutation escaped the acceptance sandbox"
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn assert_committed(results: &[rcom::fs::TransferItemResult], moving: bool) {
|
||
|
|
assert!(!results.is_empty());
|
||
|
|
for result in results {
|
||
|
|
assert_eq!(result.outcome().phase(), ItemPhase::Succeeded);
|
||
|
|
assert_eq!(result.outcome().commit(), CommitOutcome::Committed);
|
||
|
|
assert_eq!(
|
||
|
|
result.outcome().source_cleanup(),
|
||
|
|
if moving {
|
||
|
|
SourceCleanupOutcome::Removed
|
||
|
|
} else {
|
||
|
|
SourceCleanupOutcome::NotRequired
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn execute_checked(
|
||
|
|
plan: &OperationPlan,
|
||
|
|
source: &rcom::fs::BackendHandle,
|
||
|
|
destination: &rcom::fs::BackendHandle,
|
||
|
|
sandbox: &Path,
|
||
|
|
moving: bool,
|
||
|
|
) {
|
||
|
|
assert_remote_paths_inside(plan, sandbox);
|
||
|
|
let request = OperationRequest::new(
|
||
|
|
plan.id(),
|
||
|
|
plan.generation(),
|
||
|
|
plan.kind(),
|
||
|
|
plan.sources()
|
||
|
|
.iter()
|
||
|
|
.map(|source| source.location().clone())
|
||
|
|
.collect(),
|
||
|
|
plan.destination().clone(),
|
||
|
|
plan.policy(),
|
||
|
|
plan.endpoint_epochs().clone(),
|
||
|
|
);
|
||
|
|
let prepared = OperationPlanner::new(Arc::new(source.clone()), Arc::new(destination.clone()))
|
||
|
|
.prepare(request)
|
||
|
|
.await
|
||
|
|
.unwrap();
|
||
|
|
let results =
|
||
|
|
execute_prepared_copy_move(&prepared, source, destination, &CancellationToken::new()).await;
|
||
|
|
assert_committed(&results, moving);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[tokio::test]
|
||
|
|
#[ignore = "write acceptance only on hermesVM inside a fresh UUID sandbox"]
|
||
|
|
async fn hermesvm_write_acceptance_stays_inside_a_fresh_sandbox() {
|
||
|
|
let host = HostAlias::new("hermesVM");
|
||
|
|
let registry = BackendRegistry::new(Arc::new(LocalFs::new()));
|
||
|
|
let connections = ConnectionManager::new(Arc::new(OpenSshConnector::default()), registry);
|
||
|
|
let connection = connections.connect(&host).await.unwrap();
|
||
|
|
let epoch = connection.epoch;
|
||
|
|
let remote_tmp = Location::ssh(host.clone(), "/tmp");
|
||
|
|
let remote = connections
|
||
|
|
.resolve_operations(&remote_tmp, Some(epoch))
|
||
|
|
.await
|
||
|
|
.unwrap();
|
||
|
|
let local = connections
|
||
|
|
.resolve_operations(&Location::local("/"), None)
|
||
|
|
.await
|
||
|
|
.unwrap();
|
||
|
|
|
||
|
|
let sandbox_path = std::path::PathBuf::from(format!("/tmp/rcom-acceptance-{}", Uuid::new_v4()));
|
||
|
|
let sandbox = Location::ssh(host.clone(), sandbox_path.clone());
|
||
|
|
match remote.transfer.metadata(&sandbox, false).await {
|
||
|
|
Err(FsError::NotFound) => {}
|
||
|
|
Ok(_) => panic!("refusing to reuse a pre-existing acceptance sandbox"),
|
||
|
|
Err(error) => panic!("cannot inspect fresh sandbox: {error}"),
|
||
|
|
}
|
||
|
|
remote.transfer.create_dir(&sandbox).await.unwrap();
|
||
|
|
let incoming = sandbox.join("incoming");
|
||
|
|
let moved = sandbox.join("moved");
|
||
|
|
remote.transfer.create_dir(&incoming).await.unwrap();
|
||
|
|
remote.transfer.create_dir(&moved).await.unwrap();
|
||
|
|
|
||
|
|
let fixture = TempDir::new().unwrap();
|
||
|
|
let local_output = fixture.path().join("output");
|
||
|
|
std::fs::create_dir(&local_output).unwrap();
|
||
|
|
let local_a = fixture.path().join("a.txt");
|
||
|
|
let local_b = fixture.path().join("b.txt");
|
||
|
|
std::fs::write(&local_a, vec![b'a'; 70 * 1024]).unwrap();
|
||
|
|
std::fs::write(&local_b, b"second").unwrap();
|
||
|
|
|
||
|
|
let copy = remote_plan(
|
||
|
|
1,
|
||
|
|
OperationKind::Copy,
|
||
|
|
vec![Location::local(&local_a), Location::local(&local_b)],
|
||
|
|
incoming.clone(),
|
||
|
|
CollisionPolicy::Ask,
|
||
|
|
epoch,
|
||
|
|
);
|
||
|
|
execute_checked(©, &local, &remote, &sandbox_path, false).await;
|
||
|
|
assert_eq!(
|
||
|
|
remote
|
||
|
|
.transfer
|
||
|
|
.metadata(&incoming.join("a.txt"), false)
|
||
|
|
.await
|
||
|
|
.unwrap()
|
||
|
|
.size,
|
||
|
|
Some(70 * 1024)
|
||
|
|
);
|
||
|
|
|
||
|
|
std::fs::write(&local_a, b"replacement").unwrap();
|
||
|
|
let overwrite = remote_plan(
|
||
|
|
2,
|
||
|
|
OperationKind::Copy,
|
||
|
|
vec![Location::local(&local_a)],
|
||
|
|
incoming.clone(),
|
||
|
|
CollisionPolicy::Overwrite,
|
||
|
|
epoch,
|
||
|
|
);
|
||
|
|
execute_checked(&overwrite, &local, &remote, &sandbox_path, false).await;
|
||
|
|
assert_eq!(
|
||
|
|
remote
|
||
|
|
.transfer
|
||
|
|
.metadata(&incoming.join("a.txt"), false)
|
||
|
|
.await
|
||
|
|
.unwrap()
|
||
|
|
.size,
|
||
|
|
Some(11)
|
||
|
|
);
|
||
|
|
|
||
|
|
let same_host_move = remote_plan(
|
||
|
|
3,
|
||
|
|
OperationKind::Move,
|
||
|
|
vec![incoming.join("a.txt")],
|
||
|
|
moved.clone(),
|
||
|
|
CollisionPolicy::Ask,
|
||
|
|
epoch,
|
||
|
|
);
|
||
|
|
execute_checked(&same_host_move, &remote, &remote, &sandbox_path, true).await;
|
||
|
|
assert!(matches!(
|
||
|
|
remote
|
||
|
|
.transfer
|
||
|
|
.metadata(&incoming.join("a.txt"), false)
|
||
|
|
.await,
|
||
|
|
Err(FsError::NotFound)
|
||
|
|
));
|
||
|
|
|
||
|
|
let cross_endpoint_move = remote_plan(
|
||
|
|
4,
|
||
|
|
OperationKind::Move,
|
||
|
|
vec![incoming.join("b.txt")],
|
||
|
|
Location::local(&local_output),
|
||
|
|
CollisionPolicy::Ask,
|
||
|
|
epoch,
|
||
|
|
);
|
||
|
|
execute_checked(&cross_endpoint_move, &remote, &local, &sandbox_path, true).await;
|
||
|
|
assert_eq!(
|
||
|
|
std::fs::read(local_output.join("b.txt")).unwrap(),
|
||
|
|
b"second"
|
||
|
|
);
|
||
|
|
assert!(matches!(
|
||
|
|
remote
|
||
|
|
.transfer
|
||
|
|
.metadata(&incoming.join("b.txt"), false)
|
||
|
|
.await,
|
||
|
|
Err(FsError::NotFound)
|
||
|
|
));
|
||
|
|
|
||
|
|
// Abandon a known partial file before commit, verify that it is not a final file, then remove
|
||
|
|
// only this operation-owned residual through the explicit discard API.
|
||
|
|
let partial_destination = incoming.join("partial.txt");
|
||
|
|
let mut writer = remote
|
||
|
|
.transfer
|
||
|
|
.open_part(&partial_destination)
|
||
|
|
.await
|
||
|
|
.unwrap();
|
||
|
|
let partial = writer.part().clone();
|
||
|
|
assert!(partial.location().path().starts_with(&sandbox_path));
|
||
|
|
remote
|
||
|
|
.transfer
|
||
|
|
.write_part(&mut writer, b"partial")
|
||
|
|
.await
|
||
|
|
.unwrap();
|
||
|
|
let partial = remote.transfer.flush_and_close(writer).await.unwrap();
|
||
|
|
assert!(matches!(
|
||
|
|
remote.transfer.metadata(&partial_destination, false).await,
|
||
|
|
Err(FsError::NotFound)
|
||
|
|
));
|
||
|
|
assert_eq!(
|
||
|
|
remote
|
||
|
|
.transfer
|
||
|
|
.metadata(partial.location(), false)
|
||
|
|
.await
|
||
|
|
.unwrap()
|
||
|
|
.kind,
|
||
|
|
EntryKind::File
|
||
|
|
);
|
||
|
|
remote.transfer.discard_part(partial).await.unwrap();
|
||
|
|
|
||
|
|
// Success-path cleanup uses only explicitly created fixtures, never recursive deletion.
|
||
|
|
remote
|
||
|
|
.transfer
|
||
|
|
.remove_file(&moved.join("a.txt"))
|
||
|
|
.await
|
||
|
|
.unwrap();
|
||
|
|
remote.transfer.remove_dir(&moved).await.unwrap();
|
||
|
|
remote.transfer.remove_dir(&incoming).await.unwrap();
|
||
|
|
remote.transfer.remove_dir(&sandbox).await.unwrap();
|
||
|
|
assert!(matches!(
|
||
|
|
remote.transfer.metadata(&sandbox, false).await,
|
||
|
|
Err(FsError::NotFound)
|
||
|
|
));
|
||
|
|
}
|