use crate::disk::error::DiskError;
use crate::disk::error_reduce::{OBJECT_OP_IGNORED_ERRS, reduce_errs};
use crate::error::StorageError;
use rustfs_protos::proto_gen::node_service::Error as WireError;
fn heal_matched_disk_variants() -> Vec<DiskError> {
vec![
DiskError::FileNotFound,
DiskError::FileVersionNotFound,
DiskError::ErasureReadQuorum,
DiskError::ErasureWriteQuorum,
]
}
#[test]
fn disk_to_storage_to_disk_preserves_heal_matched_variants() {
for disk_err in heal_matched_disk_variants() {
let storage: StorageError = disk_err.clone().into();
let back: DiskError = storage.narrow_to_disk().expect("heal-matched variants must narrow");
assert_eq!(back, disk_err, "DiskError → StorageError → DiskError must be identity for {disk_err:?}");
}
}
#[test]
fn storage_to_disk_to_storage_preserves_heal_matched_variants() {
let variants = vec![
StorageError::FileNotFound,
StorageError::FileVersionNotFound,
StorageError::ErasureReadQuorum,
StorageError::ErasureWriteQuorum,
];
for storage_err in variants {
let disk: DiskError = storage_err
.clone()
.narrow_to_disk()
.expect("heal-matched variants must narrow");
let back: StorageError = disk.into();
assert_eq!(
back, storage_err,
"StorageError → DiskError → StorageError must be identity for {storage_err:?}"
);
}
}
#[test]
fn slowdown_collapses_to_too_many_open_files_across_disk_boundary() {
let disk: DiskError = StorageError::SlowDown.narrow_to_disk().expect("SlowDown narrows, lossily");
assert_eq!(disk, DiskError::TooManyOpenFiles);
let back: StorageError = disk.into();
assert_eq!(
back,
StorageError::TooManyOpenFiles,
"SlowDown identity is lost after one disk-boundary loop"
);
}
#[test]
fn storage_full_collapses_to_disk_full_across_disk_boundary() {
let disk: DiskError = StorageError::StorageFull
.narrow_to_disk()
.expect("StorageFull narrows, lossily");
assert_eq!(disk, DiskError::DiskFull);
let back: StorageError = disk.into();
assert_eq!(back, StorageError::DiskFull);
}
#[test]
fn wire_roundtrip_preserves_typed_variants() {
let mut variants = heal_matched_disk_variants();
variants.push(DiskError::TooManyOpenFiles);
for disk_err in variants {
let wire: WireError = disk_err.clone().into();
let back: DiskError = wire.into();
assert_eq!(back, disk_err, "DiskError → wire → DiskError must be identity for {disk_err:?}");
}
}
#[test]
fn wire_roundtrip_rewraps_io_payload_and_drops_kind() {
let original = DiskError::Io(std::io::Error::new(std::io::ErrorKind::NotFound, "open failed"));
let wire: WireError = original.into();
assert_eq!(wire.code, 0x24);
assert_eq!(wire.error_info, "io error open failed");
let after_one_hop: DiskError = wire.into();
let DiskError::Io(io_err) = &after_one_hop else {
panic!("wire Io code must decode to DiskError::Io, got {after_one_hop:?}");
};
assert_eq!(io_err.kind(), std::io::ErrorKind::Other);
assert_eq!(io_err.to_string(), "io error open failed");
let wire_again: WireError = after_one_hop.into();
assert_eq!(
wire_again.error_info, "io error io error open failed",
"each wire hop re-wraps the rendered message"
);
}
#[test]
fn io_error_bridge_recovers_disk_error_identity() {
for disk_err in heal_matched_disk_variants() {
let io_err: std::io::Error = disk_err.clone().into();
let back: DiskError = io_err.into();
assert_eq!(back, disk_err, "DiskError → io::Error → DiskError must recover identity for {disk_err:?}");
}
}
#[test]
fn io_error_bridge_recovers_storage_error_identity() {
let variants = vec![
StorageError::FileNotFound,
StorageError::FileVersionNotFound,
StorageError::ErasureReadQuorum,
StorageError::ErasureWriteQuorum,
StorageError::SlowDown,
];
for storage_err in variants {
let io_err: std::io::Error = storage_err.clone().into();
let back: StorageError = io_err.into();
assert_eq!(
back, storage_err,
"StorageError → io::Error → StorageError must recover identity for {storage_err:?}"
);
}
}
#[test]
fn io_error_bridge_recovers_identity_across_layers() {
let io_err: std::io::Error = StorageError::FileVersionNotFound.into();
let disk: DiskError = io_err.into();
assert_eq!(disk, DiskError::FileVersionNotFound);
let io_err: std::io::Error = DiskError::ErasureReadQuorum.into();
let storage: StorageError = io_err.into();
assert_eq!(storage, StorageError::ErasureReadQuorum);
}
#[test]
fn storage_to_filemeta_to_storage_preserves_matched_variants() {
let variants = vec![
StorageError::FileNotFound,
StorageError::FileVersionNotFound,
StorageError::FileCorrupt,
StorageError::MethodNotAllowed,
StorageError::VolumeNotFound,
StorageError::DoneForNow,
StorageError::Unexpected,
];
for storage_err in variants {
let filemeta: rustfs_filemeta::Error = storage_err
.clone()
.narrow_to_filemeta()
.expect("matched variants must narrow");
let back: StorageError = filemeta.into();
assert_eq!(
back, storage_err,
"StorageError → filemeta::Error → StorageError must be identity for {storage_err:?}"
);
}
}
#[test]
fn storage_to_filemeta_other_recovers_identity_via_io_bridge() {
let refused = StorageError::SlowDown
.narrow_to_filemeta()
.expect_err("SlowDown has no filemeta identity");
let filemeta = rustfs_filemeta::Error::other(refused);
assert!(
matches!(filemeta, rustfs_filemeta::Error::Io(_)),
"unmatched variants fold into the io-backed other()"
);
let back: StorageError = filemeta.into();
assert_eq!(back, StorageError::SlowDown, "the io bridge must recover the boxed StorageError identity");
}
#[test]
fn reduce_errs_splits_other_messages_with_per_disk_detail() {
let typed: Vec<Option<DiskError>> = (0..3).map(|_| Some(DiskError::FaultyDisk)).collect();
let (count, err) = reduce_errs(&typed, &[]);
assert_eq!(count, 3);
assert_eq!(err, Some(DiskError::FaultyDisk));
let fragmented: Vec<Option<DiskError>> = (0..3)
.map(|peer| {
let message = format!("can not get client, err: connection refused to peer {peer}");
Some(DiskError::other(message))
})
.collect();
let (count, _) = reduce_errs(&fragmented, OBJECT_OP_IGNORED_ERRS);
assert_eq!(
count, 1,
"per-disk detail in other() messages fragments quorum buckets: 3 same-cause failures count as 1+1+1"
);
}
#[test]
fn reduce_errs_buckets_identical_other_messages_together() {
let same: Vec<Option<DiskError>> = (0..3).map(|_| Some(DiskError::other("can not get client"))).collect();
let (count, err) = reduce_errs(&same, OBJECT_OP_IGNORED_ERRS);
assert_eq!(count, 3);
assert_eq!(err, Some(DiskError::other("can not get client")));
}
#[test]
fn stable_io_context_buckets_by_cause_and_preserves_diagnostic_source() {
let first = StorageError::other_with_context("tier mutation intent changed", "mutation-a");
let second = StorageError::other_with_context("tier mutation intent changed", "mutation-b");
assert_eq!(first, second, "diagnostic identity must not split quorum buckets");
let StorageError::Io(io_error) = first else {
panic!("stable context must remain an io error");
};
assert_eq!(io_error.to_string(), "tier mutation intent changed");
let context = io_error.get_ref().expect("stable context must remain downcastable");
assert_eq!(context.source().expect("diagnostic source must be retained").to_string(), "mutation-a");
}