181 lines
5.5 KiB
Rust
181 lines
5.5 KiB
Rust
use std::collections::BTreeMap;
|
|
|
|
use rcom::domain::{HostAlias, Location, SourceId};
|
|
|
|
use rcom::operations::{
|
|
CollisionPolicy, CommitOutcome, ItemId, ItemOutcome, ItemPhase, OperationKind, OperationPlan,
|
|
PlanGeneration, PlanId, PlanValidationError, SourceCleanupOutcome, SourceItem, TaskResult,
|
|
};
|
|
|
|
fn source(id: u64, path: &str) -> SourceItem {
|
|
SourceItem::new(ItemId::new(id), Location::local(path))
|
|
}
|
|
|
|
fn plan_with_totals(expected_items: Option<u64>, expected_bytes: Option<u64>) -> OperationPlan {
|
|
OperationPlan::new(
|
|
PlanId::new(7),
|
|
PlanGeneration::new(3),
|
|
OperationKind::Copy,
|
|
vec![source(1, "/tmp/source")],
|
|
Location::local("/tmp/destination"),
|
|
CollisionPolicy::Ask,
|
|
expected_items,
|
|
expected_bytes,
|
|
BTreeMap::new(),
|
|
)
|
|
.expect("one source should form a valid plan")
|
|
}
|
|
|
|
fn succeeded(id: u64, source_cleanup: SourceCleanupOutcome) -> ItemOutcome {
|
|
ItemOutcome::new(
|
|
ItemId::new(id),
|
|
ItemPhase::Succeeded,
|
|
CommitOutcome::Committed,
|
|
source_cleanup,
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn operation_plan_rejects_empty_sources() {
|
|
let result = OperationPlan::new(
|
|
PlanId::new(1),
|
|
PlanGeneration::new(0),
|
|
OperationKind::Move,
|
|
Vec::new(),
|
|
Location::local("/tmp/destination"),
|
|
CollisionPolicy::Overwrite,
|
|
Some(0),
|
|
Some(0),
|
|
BTreeMap::new(),
|
|
);
|
|
|
|
assert_eq!(result.unwrap_err(), PlanValidationError::EmptySources);
|
|
}
|
|
|
|
#[test]
|
|
fn operation_plan_rejects_duplicate_item_ids() {
|
|
let result = OperationPlan::new(
|
|
PlanId::new(2),
|
|
PlanGeneration::new(0),
|
|
OperationKind::Copy,
|
|
vec![source(1, "/tmp/a"), source(1, "/tmp/b")],
|
|
Location::local("/tmp/destination"),
|
|
CollisionPolicy::Ask,
|
|
Some(2),
|
|
None,
|
|
BTreeMap::new(),
|
|
);
|
|
|
|
assert_eq!(result.unwrap_err(), PlanValidationError::DuplicateItemId);
|
|
}
|
|
|
|
#[test]
|
|
fn operation_plan_preserves_unknown_totals() {
|
|
let plan = plan_with_totals(None, None);
|
|
|
|
assert_eq!(plan.expected_items(), None);
|
|
assert_eq!(plan.expected_bytes(), None);
|
|
}
|
|
|
|
#[test]
|
|
fn operation_plan_is_an_immutable_snapshot() {
|
|
let source_endpoint = SourceId::Ssh(HostAlias::new("source"));
|
|
let sources = vec![SourceItem::new(
|
|
ItemId::new(1),
|
|
Location::ssh(HostAlias::new("source"), "/tmp/source"),
|
|
)];
|
|
let destination = Location::ssh(HostAlias::new("destination"), "/tmp/destination");
|
|
let mut epochs = BTreeMap::from([
|
|
(source_endpoint.clone(), 11),
|
|
(destination.source.clone(), 12),
|
|
]);
|
|
let expected_epochs = epochs.clone();
|
|
|
|
let plan = OperationPlan::new(
|
|
PlanId::new(9),
|
|
PlanGeneration::new(4),
|
|
OperationKind::Move,
|
|
sources.clone(),
|
|
destination.clone(),
|
|
CollisionPolicy::Skip,
|
|
Some(1),
|
|
Some(128),
|
|
epochs.clone(),
|
|
)
|
|
.expect("one source should form a valid plan");
|
|
epochs.insert(SourceId::Ssh(HostAlias::new("later")), 99);
|
|
|
|
assert_eq!(plan.id(), PlanId::new(9));
|
|
assert_eq!(plan.id().get(), 9);
|
|
assert_eq!(plan.generation(), PlanGeneration::new(4));
|
|
assert_eq!(plan.generation().get(), 4);
|
|
assert_eq!(plan.kind(), OperationKind::Move);
|
|
assert_eq!(plan.sources(), sources);
|
|
assert_eq!(plan.sources()[0].id().get(), 1);
|
|
assert_eq!(plan.sources()[0].location(), sources[0].location());
|
|
assert_eq!(plan.destination(), &destination);
|
|
assert_eq!(plan.policy(), CollisionPolicy::Skip);
|
|
assert_eq!(plan.expected_items(), Some(1));
|
|
assert_eq!(plan.expected_bytes(), Some(128));
|
|
assert_eq!(plan.endpoint_epochs(), &expected_epochs);
|
|
assert_eq!(plan.endpoint_epoch(&source_endpoint), Some(11));
|
|
assert_eq!(plan.endpoint_epoch(destination.source()), Some(12));
|
|
assert_eq!(
|
|
plan.endpoint_epoch(&SourceId::Ssh(HostAlias::new("later"))),
|
|
None
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn task_result_is_partial_when_only_some_items_succeed() {
|
|
let outcomes = [
|
|
succeeded(1, SourceCleanupOutcome::NotRequired),
|
|
ItemOutcome::new(
|
|
ItemId::new(2),
|
|
ItemPhase::Failed,
|
|
CommitOutcome::NotCommitted,
|
|
SourceCleanupOutcome::NotRequired,
|
|
),
|
|
];
|
|
|
|
assert_eq!(TaskResult::from_outcomes(&outcomes), TaskResult::Partial);
|
|
}
|
|
|
|
#[test]
|
|
fn committed_move_with_failed_source_cleanup_is_not_full_success() {
|
|
let outcomes = [succeeded(1, SourceCleanupOutcome::Failed)];
|
|
|
|
assert_eq!(outcomes[0].commit(), CommitOutcome::Committed);
|
|
assert_eq!(outcomes[0].source_cleanup(), SourceCleanupOutcome::Failed);
|
|
assert_eq!(TaskResult::from_outcomes(&outcomes), TaskResult::Partial);
|
|
}
|
|
|
|
#[test]
|
|
fn skipped_empty_and_unknown_results_are_not_full_success() {
|
|
let skipped = [ItemOutcome::new(
|
|
ItemId::new(1),
|
|
ItemPhase::Skipped,
|
|
CommitOutcome::NotAttempted,
|
|
SourceCleanupOutcome::NotAttempted,
|
|
)];
|
|
let unknown = [ItemOutcome::new(
|
|
ItemId::new(1),
|
|
ItemPhase::Failed,
|
|
CommitOutcome::Unknown,
|
|
SourceCleanupOutcome::NotAttempted,
|
|
)];
|
|
|
|
assert_eq!(TaskResult::from_outcomes(&[]), TaskResult::Failed);
|
|
assert_eq!(TaskResult::from_outcomes(&skipped), TaskResult::Failed);
|
|
assert_eq!(TaskResult::from_outcomes(&unknown), TaskResult::Failed);
|
|
}
|
|
|
|
#[test]
|
|
fn successful_copy_and_move_cleanup_both_count_as_success() {
|
|
let outcomes = [
|
|
succeeded(1, SourceCleanupOutcome::NotRequired),
|
|
succeeded(2, SourceCleanupOutcome::Removed),
|
|
];
|
|
|
|
assert_eq!(TaskResult::from_outcomes(&outcomes), TaskResult::Succeeded);
|
|
}
|