mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-18 10:43:15 +00:00
add disk test
This commit is contained in:
+347
-592
@@ -637,598 +637,6 @@ pub struct WalkDirOptions {
|
||||
pub disk_id: String,
|
||||
}
|
||||
|
||||
// #[derive(Clone, Debug, Default)]
|
||||
// pub struct MetadataResolutionParams {
|
||||
// pub dir_quorum: usize,
|
||||
// pub obj_quorum: usize,
|
||||
// pub requested_versions: usize,
|
||||
// pub bucket: String,
|
||||
// pub strict: bool,
|
||||
// pub candidates: Vec<Vec<FileMetaShallowVersion>>,
|
||||
// }
|
||||
|
||||
// #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
|
||||
// pub struct MetaCacheEntry {
|
||||
// // name is the full name of the object including prefixes
|
||||
// pub name: String,
|
||||
// // Metadata. If none is present it is not an object but only a prefix.
|
||||
// // Entries without metadata will only be present in non-recursive scans.
|
||||
// pub metadata: Vec<u8>,
|
||||
|
||||
// // cached contains the metadata if decoded.
|
||||
// pub cached: Option<FileMeta>,
|
||||
|
||||
// // Indicates the entry can be reused and only one reference to metadata is expected.
|
||||
// pub reusable: bool,
|
||||
// }
|
||||
|
||||
// impl MetaCacheEntry {
|
||||
// pub fn marshal_msg(&self) -> Result<Vec<u8>> {
|
||||
// let mut wr = Vec::new();
|
||||
// rmp::encode::write_bool(&mut wr, true)?;
|
||||
|
||||
// rmp::encode::write_str(&mut wr, &self.name)?;
|
||||
|
||||
// rmp::encode::write_bin(&mut wr, &self.metadata)?;
|
||||
|
||||
// Ok(wr)
|
||||
// }
|
||||
|
||||
// pub fn is_dir(&self) -> bool {
|
||||
// self.metadata.is_empty() && self.name.ends_with('/')
|
||||
// }
|
||||
// pub fn is_in_dir(&self, dir: &str, separator: &str) -> bool {
|
||||
// if dir.is_empty() {
|
||||
// let idx = self.name.find(separator);
|
||||
// return idx.is_none() || idx.unwrap() == self.name.len() - separator.len();
|
||||
// }
|
||||
|
||||
// let ext = self.name.trim_start_matches(dir);
|
||||
|
||||
// if ext.len() != self.name.len() {
|
||||
// let idx = ext.find(separator);
|
||||
// return idx.is_none() || idx.unwrap() == ext.len() - separator.len();
|
||||
// }
|
||||
|
||||
// false
|
||||
// }
|
||||
// pub fn is_object(&self) -> bool {
|
||||
// !self.metadata.is_empty()
|
||||
// }
|
||||
|
||||
// pub fn is_object_dir(&self) -> bool {
|
||||
// !self.metadata.is_empty() && self.name.ends_with(SLASH_SEPARATOR)
|
||||
// }
|
||||
|
||||
// pub fn is_latest_delete_marker(&mut self) -> bool {
|
||||
// if let Some(cached) = &self.cached {
|
||||
// if cached.versions.is_empty() {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// return cached.versions[0].header.version_type == VersionType::Delete;
|
||||
// }
|
||||
|
||||
// if !FileMeta::is_xl2_v1_format(&self.metadata) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// match FileMeta::check_xl2_v1(&self.metadata) {
|
||||
// Ok((meta, _, _)) => {
|
||||
// if !meta.is_empty() {
|
||||
// return FileMeta::is_latest_delete_marker(meta);
|
||||
// }
|
||||
// }
|
||||
// Err(_) => return true,
|
||||
// }
|
||||
|
||||
// match self.xl_meta() {
|
||||
// Ok(res) => {
|
||||
// if res.versions.is_empty() {
|
||||
// return true;
|
||||
// }
|
||||
// res.versions[0].header.version_type == VersionType::Delete
|
||||
// }
|
||||
// Err(_) => true,
|
||||
// }
|
||||
// }
|
||||
|
||||
// #[tracing::instrument(level = "debug", skip(self))]
|
||||
// pub fn to_fileinfo(&self, bucket: &str) -> Result<FileInfo> {
|
||||
// if self.is_dir() {
|
||||
// return Ok(FileInfo {
|
||||
// volume: bucket.to_owned(),
|
||||
// name: self.name.clone(),
|
||||
// ..Default::default()
|
||||
// });
|
||||
// }
|
||||
|
||||
// if self.cached.is_some() {
|
||||
// let fm = self.cached.as_ref().unwrap();
|
||||
// if fm.versions.is_empty() {
|
||||
// return Ok(FileInfo {
|
||||
// volume: bucket.to_owned(),
|
||||
// name: self.name.clone(),
|
||||
// deleted: true,
|
||||
// is_latest: true,
|
||||
// mod_time: Some(OffsetDateTime::UNIX_EPOCH),
|
||||
// ..Default::default()
|
||||
// });
|
||||
// }
|
||||
|
||||
// let fi = fm.into_fileinfo(bucket, self.name.as_str(), "", false, false)?;
|
||||
|
||||
// return Ok(fi);
|
||||
// }
|
||||
|
||||
// let mut fm = FileMeta::new();
|
||||
// fm.unmarshal_msg(&self.metadata)?;
|
||||
|
||||
// let fi = fm.into_fileinfo(bucket, self.name.as_str(), "", false, false)?;
|
||||
|
||||
// Ok(fi)
|
||||
// }
|
||||
|
||||
// pub fn file_info_versions(&self, bucket: &str) -> Result<FileInfoVersions> {
|
||||
// if self.is_dir() {
|
||||
// return Ok(FileInfoVersions {
|
||||
// volume: bucket.to_string(),
|
||||
// name: self.name.clone(),
|
||||
// versions: vec![FileInfo {
|
||||
// volume: bucket.to_string(),
|
||||
// name: self.name.clone(),
|
||||
// ..Default::default()
|
||||
// }],
|
||||
// ..Default::default()
|
||||
// });
|
||||
// }
|
||||
|
||||
// let mut fm = FileMeta::new();
|
||||
// fm.unmarshal_msg(&self.metadata)?;
|
||||
|
||||
// fm.into_file_info_versions(bucket, self.name.as_str(), false)
|
||||
// }
|
||||
|
||||
// pub fn matches(&self, other: Option<&MetaCacheEntry>, strict: bool) -> (Option<MetaCacheEntry>, bool) {
|
||||
// if other.is_none() {
|
||||
// return (None, false);
|
||||
// }
|
||||
|
||||
// let other = other.unwrap();
|
||||
|
||||
// let mut prefer = None;
|
||||
// if self.name != other.name {
|
||||
// if self.name < other.name {
|
||||
// return (Some(self.clone()), false);
|
||||
// }
|
||||
// return (Some(other.clone()), false);
|
||||
// }
|
||||
|
||||
// if other.is_dir() || self.is_dir() {
|
||||
// if self.is_dir() {
|
||||
// return (Some(self.clone()), other.is_dir() == self.is_dir());
|
||||
// }
|
||||
|
||||
// return (Some(other.clone()), other.is_dir() == self.is_dir());
|
||||
// }
|
||||
// let self_vers = match &self.cached {
|
||||
// Some(file_meta) => file_meta.clone(),
|
||||
// None => match FileMeta::load(&self.metadata) {
|
||||
// Ok(meta) => meta,
|
||||
// Err(_) => {
|
||||
// return (None, false);
|
||||
// }
|
||||
// },
|
||||
// };
|
||||
// let other_vers = match &other.cached {
|
||||
// Some(file_meta) => file_meta.clone(),
|
||||
// None => match FileMeta::load(&other.metadata) {
|
||||
// Ok(meta) => meta,
|
||||
// Err(_) => {
|
||||
// return (None, false);
|
||||
// }
|
||||
// },
|
||||
// };
|
||||
|
||||
// if self_vers.versions.len() != other_vers.versions.len() {
|
||||
// match self_vers.lastest_mod_time().cmp(&other_vers.lastest_mod_time()) {
|
||||
// Ordering::Greater => {
|
||||
// return (Some(self.clone()), false);
|
||||
// }
|
||||
// Ordering::Less => {
|
||||
// return (Some(other.clone()), false);
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
|
||||
// if self_vers.versions.len() > other_vers.versions.len() {
|
||||
// return (Some(self.clone()), false);
|
||||
// }
|
||||
// return (Some(other.clone()), false);
|
||||
// }
|
||||
|
||||
// for (s_version, o_version) in self_vers.versions.iter().zip(other_vers.versions.iter()) {
|
||||
// if s_version.header != o_version.header {
|
||||
// if s_version.header.has_ec() != o_version.header.has_ec() {
|
||||
// // One version has EC and the other doesn't - may have been written later.
|
||||
// // Compare without considering EC.
|
||||
// let (mut a, mut b) = (s_version.header.clone(), o_version.header.clone());
|
||||
// (a.ec_n, a.ec_m, b.ec_n, b.ec_m) = (0, 0, 0, 0);
|
||||
// if a == b {
|
||||
// continue;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if !strict && s_version.header.matches_not_strict(&o_version.header) {
|
||||
// if prefer.is_none() {
|
||||
// if s_version.header.sorts_before(&o_version.header) {
|
||||
// prefer = Some(self.clone());
|
||||
// } else {
|
||||
// prefer = Some(other.clone());
|
||||
// }
|
||||
// }
|
||||
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// if prefer.is_some() {
|
||||
// return (prefer, false);
|
||||
// }
|
||||
|
||||
// if s_version.header.sorts_before(&o_version.header) {
|
||||
// return (Some(self.clone()), false);
|
||||
// }
|
||||
|
||||
// return (Some(other.clone()), false);
|
||||
// }
|
||||
// }
|
||||
|
||||
// if prefer.is_none() {
|
||||
// prefer = Some(self.clone());
|
||||
// }
|
||||
|
||||
// (prefer, true)
|
||||
// }
|
||||
|
||||
// pub fn xl_meta(&mut self) -> Result<FileMeta> {
|
||||
// if self.is_dir() {
|
||||
// return Err(DiskError::FileNotFound);
|
||||
// }
|
||||
|
||||
// if let Some(meta) = &self.cached {
|
||||
// Ok(meta.clone())
|
||||
// } else {
|
||||
// if self.metadata.is_empty() {
|
||||
// return Err(DiskError::FileNotFound);
|
||||
// }
|
||||
|
||||
// let meta = FileMeta::load(&self.metadata)?;
|
||||
|
||||
// self.cached = Some(meta.clone());
|
||||
|
||||
// Ok(meta)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// #[derive(Debug, Default)]
|
||||
// pub struct MetaCacheEntries(pub Vec<Option<MetaCacheEntry>>);
|
||||
|
||||
// impl MetaCacheEntries {
|
||||
// #[allow(clippy::should_implement_trait)]
|
||||
// pub fn as_ref(&self) -> &[Option<MetaCacheEntry>] {
|
||||
// &self.0
|
||||
// }
|
||||
// pub fn resolve(&self, mut params: MetadataResolutionParams) -> Option<MetaCacheEntry> {
|
||||
// if self.0.is_empty() {
|
||||
// warn!("decommission_pool: entries resolve empty");
|
||||
// return None;
|
||||
// }
|
||||
|
||||
// let mut dir_exists = 0;
|
||||
// let mut selected = None;
|
||||
|
||||
// params.candidates.clear();
|
||||
// let mut objs_agree = 0;
|
||||
// let mut objs_valid = 0;
|
||||
|
||||
// for entry in self.0.iter().flatten() {
|
||||
// let mut entry = entry.clone();
|
||||
|
||||
// warn!("decommission_pool: entries resolve entry {:?}", entry.name);
|
||||
// if entry.name.is_empty() {
|
||||
// continue;
|
||||
// }
|
||||
// if entry.is_dir() {
|
||||
// dir_exists += 1;
|
||||
// selected = Some(entry.clone());
|
||||
// warn!("decommission_pool: entries resolve entry dir {:?}", entry.name);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// let xl = match entry.xl_meta() {
|
||||
// Ok(xl) => xl,
|
||||
// Err(e) => {
|
||||
// warn!("decommission_pool: entries resolve entry xl_meta {:?}", e);
|
||||
// continue;
|
||||
// }
|
||||
// };
|
||||
|
||||
// objs_valid += 1;
|
||||
|
||||
// params.candidates.push(xl.versions.clone());
|
||||
|
||||
// if selected.is_none() {
|
||||
// selected = Some(entry.clone());
|
||||
// objs_agree = 1;
|
||||
// warn!("decommission_pool: entries resolve entry selected {:?}", entry.name);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// if let (prefer, true) = entry.matches(selected.as_ref(), params.strict) {
|
||||
// selected = prefer;
|
||||
// objs_agree += 1;
|
||||
// warn!("decommission_pool: entries resolve entry prefer {:?}", entry.name);
|
||||
// continue;
|
||||
// }
|
||||
// }
|
||||
|
||||
// let Some(selected) = selected else {
|
||||
// warn!("decommission_pool: entries resolve entry no selected");
|
||||
// return None;
|
||||
// };
|
||||
|
||||
// if selected.is_dir() && dir_exists >= params.dir_quorum {
|
||||
// warn!("decommission_pool: entries resolve entry dir selected {:?}", selected.name);
|
||||
// return Some(selected);
|
||||
// }
|
||||
|
||||
// // If we would never be able to reach read quorum.
|
||||
// if objs_valid < params.obj_quorum {
|
||||
// warn!(
|
||||
// "decommission_pool: entries resolve entry not enough objects {} < {}",
|
||||
// objs_valid, params.obj_quorum
|
||||
// );
|
||||
// return None;
|
||||
// }
|
||||
|
||||
// if objs_agree == objs_valid {
|
||||
// warn!("decommission_pool: entries resolve entry all agree {} == {}", objs_agree, objs_valid);
|
||||
// return Some(selected);
|
||||
// }
|
||||
|
||||
// let Some(cached) = selected.cached else {
|
||||
// warn!("decommission_pool: entries resolve entry no cached");
|
||||
// return None;
|
||||
// };
|
||||
|
||||
// let versions = merge_file_meta_versions(params.obj_quorum, params.strict, params.requested_versions, ¶ms.candidates);
|
||||
// if versions.is_empty() {
|
||||
// warn!("decommission_pool: entries resolve entry no versions");
|
||||
// return None;
|
||||
// }
|
||||
|
||||
// let metadata = match cached.marshal_msg() {
|
||||
// Ok(meta) => meta,
|
||||
// Err(e) => {
|
||||
// warn!("decommission_pool: entries resolve entry marshal_msg {:?}", e);
|
||||
// return None;
|
||||
// }
|
||||
// };
|
||||
|
||||
// // Merge if we have disagreement.
|
||||
// // Create a new merged result.
|
||||
// let new_selected = MetaCacheEntry {
|
||||
// name: selected.name.clone(),
|
||||
// cached: Some(FileMeta {
|
||||
// meta_ver: cached.meta_ver,
|
||||
// versions,
|
||||
// ..Default::default()
|
||||
// }),
|
||||
// reusable: true,
|
||||
// metadata,
|
||||
// };
|
||||
|
||||
// warn!("decommission_pool: entries resolve entry selected {:?}", new_selected.name);
|
||||
// Some(new_selected)
|
||||
// }
|
||||
|
||||
// pub fn first_found(&self) -> (Option<MetaCacheEntry>, usize) {
|
||||
// (self.0.iter().find(|x| x.is_some()).cloned().unwrap_or_default(), self.0.len())
|
||||
// }
|
||||
// }
|
||||
|
||||
// #[derive(Debug, Default)]
|
||||
// pub struct MetaCacheEntriesSortedResult {
|
||||
// pub entries: Option<MetaCacheEntriesSorted>,
|
||||
// pub err: Option<Error>,
|
||||
// }
|
||||
|
||||
// // impl MetaCacheEntriesSortedResult {
|
||||
// // pub fn entriy_list(&self) -> Vec<&MetaCacheEntry> {
|
||||
// // if let Some(entries) = &self.entries {
|
||||
// // entries.entries()
|
||||
// // } else {
|
||||
// // Vec::new()
|
||||
// // }
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// #[derive(Debug, Default)]
|
||||
// pub struct MetaCacheEntriesSorted {
|
||||
// pub o: MetaCacheEntries,
|
||||
// pub list_id: Option<String>,
|
||||
// pub reuse: bool,
|
||||
// pub last_skipped_entry: Option<String>,
|
||||
// }
|
||||
|
||||
// impl MetaCacheEntriesSorted {
|
||||
// pub fn entries(&self) -> Vec<&MetaCacheEntry> {
|
||||
// let entries: Vec<&MetaCacheEntry> = self.o.0.iter().flatten().collect();
|
||||
// entries
|
||||
// }
|
||||
// pub fn forward_past(&mut self, marker: Option<String>) {
|
||||
// if let Some(val) = marker {
|
||||
// // TODO: reuse
|
||||
// if let Some(idx) = self.o.0.iter().flatten().position(|v| v.name > val) {
|
||||
// self.o.0 = self.o.0.split_off(idx);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// pub async fn file_infos(&self, bucket: &str, prefix: &str, delimiter: Option<String>) -> Vec<ObjectInfo> {
|
||||
// let vcfg = get_versioning_config(bucket).await.ok();
|
||||
// let mut objects = Vec::with_capacity(self.o.as_ref().len());
|
||||
// let mut prev_prefix = "";
|
||||
// for entry in self.o.as_ref().iter().flatten() {
|
||||
// if entry.is_object() {
|
||||
// if let Some(delimiter) = &delimiter {
|
||||
// if let Some(idx) = entry.name.trim_start_matches(prefix).find(delimiter) {
|
||||
// let idx = prefix.len() + idx + delimiter.len();
|
||||
// if let Some(curr_prefix) = entry.name.get(0..idx) {
|
||||
// if curr_prefix == prev_prefix {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// prev_prefix = curr_prefix;
|
||||
|
||||
// objects.push(ObjectInfo {
|
||||
// is_dir: true,
|
||||
// bucket: bucket.to_owned(),
|
||||
// name: curr_prefix.to_owned(),
|
||||
// ..Default::default()
|
||||
// });
|
||||
// }
|
||||
// continue;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if let Ok(fi) = entry.to_fileinfo(bucket) {
|
||||
// // TODO:VersionPurgeStatus
|
||||
// let versioned = vcfg.clone().map(|v| v.0.versioned(&entry.name)).unwrap_or_default();
|
||||
// objects.push(fi.to_object_info(bucket, &entry.name, versioned));
|
||||
// }
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// if entry.is_dir() {
|
||||
// if let Some(delimiter) = &delimiter {
|
||||
// if let Some(idx) = entry.name.trim_start_matches(prefix).find(delimiter) {
|
||||
// let idx = prefix.len() + idx + delimiter.len();
|
||||
// if let Some(curr_prefix) = entry.name.get(0..idx) {
|
||||
// if curr_prefix == prev_prefix {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// prev_prefix = curr_prefix;
|
||||
|
||||
// objects.push(ObjectInfo {
|
||||
// is_dir: true,
|
||||
// bucket: bucket.to_owned(),
|
||||
// name: curr_prefix.to_owned(),
|
||||
// ..Default::default()
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// objects
|
||||
// }
|
||||
|
||||
// pub async fn file_info_versions(
|
||||
// &self,
|
||||
// bucket: &str,
|
||||
// prefix: &str,
|
||||
// delimiter: Option<String>,
|
||||
// after_v: Option<String>,
|
||||
// ) -> Vec<ObjectInfo> {
|
||||
// let vcfg = get_versioning_config(bucket).await.ok();
|
||||
// let mut objects = Vec::with_capacity(self.o.as_ref().len());
|
||||
// let mut prev_prefix = "";
|
||||
// let mut after_v = after_v;
|
||||
// for entry in self.o.as_ref().iter().flatten() {
|
||||
// if entry.is_object() {
|
||||
// if let Some(delimiter) = &delimiter {
|
||||
// if let Some(idx) = entry.name.trim_start_matches(prefix).find(delimiter) {
|
||||
// let idx = prefix.len() + idx + delimiter.len();
|
||||
// if let Some(curr_prefix) = entry.name.get(0..idx) {
|
||||
// if curr_prefix == prev_prefix {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// prev_prefix = curr_prefix;
|
||||
|
||||
// objects.push(ObjectInfo {
|
||||
// is_dir: true,
|
||||
// bucket: bucket.to_owned(),
|
||||
// name: curr_prefix.to_owned(),
|
||||
// ..Default::default()
|
||||
// });
|
||||
// }
|
||||
// continue;
|
||||
// }
|
||||
// }
|
||||
|
||||
// let mut fiv = match entry.file_info_versions(bucket) {
|
||||
// Ok(res) => res,
|
||||
// Err(_err) => {
|
||||
// //
|
||||
// continue;
|
||||
// }
|
||||
// };
|
||||
|
||||
// let fi_versions = 'c: {
|
||||
// if let Some(after_val) = &after_v {
|
||||
// if let Some(idx) = fiv.find_version_index(after_val) {
|
||||
// after_v = None;
|
||||
// break 'c fiv.versions.split_off(idx + 1);
|
||||
// }
|
||||
|
||||
// after_v = None;
|
||||
// break 'c fiv.versions;
|
||||
// } else {
|
||||
// break 'c fiv.versions;
|
||||
// }
|
||||
// };
|
||||
|
||||
// for fi in fi_versions.into_iter() {
|
||||
// // VersionPurgeStatus
|
||||
|
||||
// let versioned = vcfg.clone().map(|v| v.0.versioned(&entry.name)).unwrap_or_default();
|
||||
// objects.push(fi.to_object_info(bucket, &entry.name, versioned));
|
||||
// }
|
||||
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// if entry.is_dir() {
|
||||
// if let Some(delimiter) = &delimiter {
|
||||
// if let Some(idx) = entry.name.trim_start_matches(prefix).find(delimiter) {
|
||||
// let idx = prefix.len() + idx + delimiter.len();
|
||||
// if let Some(curr_prefix) = entry.name.get(0..idx) {
|
||||
// if curr_prefix == prev_prefix {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// prev_prefix = curr_prefix;
|
||||
|
||||
// objects.push(ObjectInfo {
|
||||
// is_dir: true,
|
||||
// bucket: bucket.to_owned(),
|
||||
// name: curr_prefix.to_owned(),
|
||||
// ..Default::default()
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// objects
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct DiskOption {
|
||||
pub cleanup: bool,
|
||||
@@ -1307,3 +715,350 @@ pub fn conv_part_err_to_int(err: &Option<Error>) -> usize {
|
||||
pub fn has_part_err(part_errs: &[usize]) -> bool {
|
||||
part_errs.iter().any(|err| *err != CHECK_PART_SUCCESS)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use endpoint::Endpoint;
|
||||
use local::LocalDisk;
|
||||
use std::path::PathBuf;
|
||||
use tokio::fs;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Test DiskLocation validation
|
||||
#[test]
|
||||
fn test_disk_location_valid() {
|
||||
let valid_location = DiskLocation {
|
||||
pool_idx: Some(0),
|
||||
set_idx: Some(1),
|
||||
disk_idx: Some(2),
|
||||
};
|
||||
assert!(valid_location.valid());
|
||||
|
||||
let invalid_location = DiskLocation {
|
||||
pool_idx: None,
|
||||
set_idx: None,
|
||||
disk_idx: None,
|
||||
};
|
||||
assert!(!invalid_location.valid());
|
||||
|
||||
let partial_valid_location = DiskLocation {
|
||||
pool_idx: Some(0),
|
||||
set_idx: None,
|
||||
disk_idx: Some(2),
|
||||
};
|
||||
assert!(!partial_valid_location.valid());
|
||||
}
|
||||
|
||||
/// Test FileInfoVersions find_version_index
|
||||
#[test]
|
||||
fn test_file_info_versions_find_version_index() {
|
||||
let mut versions = Vec::new();
|
||||
let v1_uuid = Uuid::new_v4();
|
||||
let v2_uuid = Uuid::new_v4();
|
||||
let fi1 = FileInfo {
|
||||
version_id: Some(v1_uuid),
|
||||
..Default::default()
|
||||
};
|
||||
let fi2 = FileInfo {
|
||||
version_id: Some(v2_uuid),
|
||||
..Default::default()
|
||||
};
|
||||
versions.push(fi1);
|
||||
versions.push(fi2);
|
||||
|
||||
let fiv = FileInfoVersions {
|
||||
volume: "test-bucket".to_string(),
|
||||
name: "test-object".to_string(),
|
||||
latest_mod_time: None,
|
||||
versions,
|
||||
free_versions: Vec::new(),
|
||||
};
|
||||
|
||||
assert_eq!(fiv.find_version_index(&v1_uuid.to_string()), Some(0));
|
||||
assert_eq!(fiv.find_version_index(&v2_uuid.to_string()), Some(1));
|
||||
assert_eq!(fiv.find_version_index("non-existent"), None);
|
||||
assert_eq!(fiv.find_version_index(""), None);
|
||||
}
|
||||
|
||||
/// Test part error conversion functions
|
||||
#[test]
|
||||
fn test_conv_part_err_to_int() {
|
||||
assert_eq!(conv_part_err_to_int(&None), CHECK_PART_SUCCESS);
|
||||
assert_eq!(
|
||||
conv_part_err_to_int(&Some(Error::from(DiskError::DiskNotFound))),
|
||||
CHECK_PART_DISK_NOT_FOUND
|
||||
);
|
||||
assert_eq!(
|
||||
conv_part_err_to_int(&Some(Error::from(DiskError::VolumeNotFound))),
|
||||
CHECK_PART_VOLUME_NOT_FOUND
|
||||
);
|
||||
assert_eq!(
|
||||
conv_part_err_to_int(&Some(Error::from(DiskError::FileNotFound))),
|
||||
CHECK_PART_FILE_NOT_FOUND
|
||||
);
|
||||
assert_eq!(conv_part_err_to_int(&Some(Error::from(DiskError::FileCorrupt))), CHECK_PART_FILE_CORRUPT);
|
||||
assert_eq!(conv_part_err_to_int(&Some(Error::from(DiskError::Unexpected))), CHECK_PART_UNKNOWN);
|
||||
}
|
||||
|
||||
/// Test has_part_err function
|
||||
#[test]
|
||||
fn test_has_part_err() {
|
||||
assert!(!has_part_err(&[]));
|
||||
assert!(!has_part_err(&[CHECK_PART_SUCCESS]));
|
||||
assert!(!has_part_err(&[CHECK_PART_SUCCESS, CHECK_PART_SUCCESS]));
|
||||
|
||||
assert!(has_part_err(&[CHECK_PART_FILE_NOT_FOUND]));
|
||||
assert!(has_part_err(&[CHECK_PART_SUCCESS, CHECK_PART_FILE_CORRUPT]));
|
||||
assert!(has_part_err(&[CHECK_PART_DISK_NOT_FOUND, CHECK_PART_VOLUME_NOT_FOUND]));
|
||||
}
|
||||
|
||||
/// Test WalkDirOptions structure
|
||||
#[test]
|
||||
fn test_walk_dir_options() {
|
||||
let opts = WalkDirOptions {
|
||||
bucket: "test-bucket".to_string(),
|
||||
base_dir: "/path/to/dir".to_string(),
|
||||
recursive: true,
|
||||
report_notfound: false,
|
||||
filter_prefix: Some("prefix_".to_string()),
|
||||
forward_to: Some("object/path".to_string()),
|
||||
limit: 100,
|
||||
disk_id: "disk-123".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(opts.bucket, "test-bucket");
|
||||
assert_eq!(opts.base_dir, "/path/to/dir");
|
||||
assert!(opts.recursive);
|
||||
assert!(!opts.report_notfound);
|
||||
assert_eq!(opts.filter_prefix, Some("prefix_".to_string()));
|
||||
assert_eq!(opts.forward_to, Some("object/path".to_string()));
|
||||
assert_eq!(opts.limit, 100);
|
||||
assert_eq!(opts.disk_id, "disk-123");
|
||||
}
|
||||
|
||||
/// Test DeleteOptions structure
|
||||
#[test]
|
||||
fn test_delete_options() {
|
||||
let opts = DeleteOptions {
|
||||
recursive: true,
|
||||
immediate: false,
|
||||
undo_write: true,
|
||||
old_data_dir: Some(Uuid::new_v4()),
|
||||
};
|
||||
|
||||
assert!(opts.recursive);
|
||||
assert!(!opts.immediate);
|
||||
assert!(opts.undo_write);
|
||||
assert!(opts.old_data_dir.is_some());
|
||||
}
|
||||
|
||||
/// Test ReadOptions structure
|
||||
#[test]
|
||||
fn test_read_options() {
|
||||
let opts = ReadOptions {
|
||||
incl_free_versions: true,
|
||||
read_data: false,
|
||||
healing: true,
|
||||
};
|
||||
|
||||
assert!(opts.incl_free_versions);
|
||||
assert!(!opts.read_data);
|
||||
assert!(opts.healing);
|
||||
}
|
||||
|
||||
/// Test UpdateMetadataOpts structure
|
||||
#[test]
|
||||
fn test_update_metadata_opts() {
|
||||
let opts = UpdateMetadataOpts { no_persistence: true };
|
||||
|
||||
assert!(opts.no_persistence);
|
||||
}
|
||||
|
||||
/// Test DiskOption structure
|
||||
#[test]
|
||||
fn test_disk_option() {
|
||||
let opt = DiskOption {
|
||||
cleanup: true,
|
||||
health_check: false,
|
||||
};
|
||||
|
||||
assert!(opt.cleanup);
|
||||
assert!(!opt.health_check);
|
||||
}
|
||||
|
||||
/// Test DiskInfoOptions structure
|
||||
#[test]
|
||||
fn test_disk_info_options() {
|
||||
let opts = DiskInfoOptions {
|
||||
disk_id: "test-disk-id".to_string(),
|
||||
metrics: true,
|
||||
noop: false,
|
||||
};
|
||||
|
||||
assert_eq!(opts.disk_id, "test-disk-id");
|
||||
assert!(opts.metrics);
|
||||
assert!(!opts.noop);
|
||||
}
|
||||
|
||||
/// Test ReadMultipleReq structure
|
||||
#[test]
|
||||
fn test_read_multiple_req() {
|
||||
let req = ReadMultipleReq {
|
||||
bucket: "test-bucket".to_string(),
|
||||
prefix: "prefix/".to_string(),
|
||||
files: vec!["file1.txt".to_string(), "file2.txt".to_string()],
|
||||
max_size: 1024,
|
||||
metadata_only: false,
|
||||
abort404: true,
|
||||
max_results: 10,
|
||||
};
|
||||
|
||||
assert_eq!(req.bucket, "test-bucket");
|
||||
assert_eq!(req.prefix, "prefix/");
|
||||
assert_eq!(req.files.len(), 2);
|
||||
assert_eq!(req.max_size, 1024);
|
||||
assert!(!req.metadata_only);
|
||||
assert!(req.abort404);
|
||||
assert_eq!(req.max_results, 10);
|
||||
}
|
||||
|
||||
/// Test ReadMultipleResp structure
|
||||
#[test]
|
||||
fn test_read_multiple_resp() {
|
||||
let resp = ReadMultipleResp {
|
||||
bucket: "test-bucket".to_string(),
|
||||
prefix: "prefix/".to_string(),
|
||||
file: "test-file.txt".to_string(),
|
||||
exists: true,
|
||||
error: "".to_string(),
|
||||
data: vec![1, 2, 3, 4],
|
||||
mod_time: Some(time::OffsetDateTime::now_utc()),
|
||||
};
|
||||
|
||||
assert_eq!(resp.bucket, "test-bucket");
|
||||
assert_eq!(resp.prefix, "prefix/");
|
||||
assert_eq!(resp.file, "test-file.txt");
|
||||
assert!(resp.exists);
|
||||
assert!(resp.error.is_empty());
|
||||
assert_eq!(resp.data, vec![1, 2, 3, 4]);
|
||||
assert!(resp.mod_time.is_some());
|
||||
}
|
||||
|
||||
/// Test VolumeInfo structure
|
||||
#[test]
|
||||
fn test_volume_info() {
|
||||
let now = time::OffsetDateTime::now_utc();
|
||||
let vol_info = VolumeInfo {
|
||||
name: "test-volume".to_string(),
|
||||
created: Some(now),
|
||||
};
|
||||
|
||||
assert_eq!(vol_info.name, "test-volume");
|
||||
assert_eq!(vol_info.created, Some(now));
|
||||
}
|
||||
|
||||
/// Test CheckPartsResp structure
|
||||
#[test]
|
||||
fn test_check_parts_resp() {
|
||||
let resp = CheckPartsResp {
|
||||
results: vec![CHECK_PART_SUCCESS, CHECK_PART_FILE_NOT_FOUND, CHECK_PART_FILE_CORRUPT],
|
||||
};
|
||||
|
||||
assert_eq!(resp.results.len(), 3);
|
||||
assert_eq!(resp.results[0], CHECK_PART_SUCCESS);
|
||||
assert_eq!(resp.results[1], CHECK_PART_FILE_NOT_FOUND);
|
||||
assert_eq!(resp.results[2], CHECK_PART_FILE_CORRUPT);
|
||||
}
|
||||
|
||||
/// Test RenameDataResp structure
|
||||
#[test]
|
||||
fn test_rename_data_resp() {
|
||||
let uuid = Uuid::new_v4();
|
||||
let signature = vec![0x01, 0x02, 0x03];
|
||||
|
||||
let resp = RenameDataResp {
|
||||
old_data_dir: Some(uuid),
|
||||
sign: Some(signature.clone()),
|
||||
};
|
||||
|
||||
assert_eq!(resp.old_data_dir, Some(uuid));
|
||||
assert_eq!(resp.sign, Some(signature));
|
||||
}
|
||||
|
||||
/// Test constants
|
||||
#[test]
|
||||
fn test_constants() {
|
||||
assert_eq!(RUSTFS_META_BUCKET, ".rustfs.sys");
|
||||
assert_eq!(RUSTFS_META_MULTIPART_BUCKET, ".rustfs.sys/multipart");
|
||||
assert_eq!(RUSTFS_META_TMP_BUCKET, ".rustfs.sys/tmp");
|
||||
assert_eq!(RUSTFS_META_TMP_DELETED_BUCKET, ".rustfs.sys/tmp/.trash");
|
||||
assert_eq!(BUCKET_META_PREFIX, "buckets");
|
||||
assert_eq!(FORMAT_CONFIG_FILE, "format.json");
|
||||
assert_eq!(STORAGE_FORMAT_FILE, "xl.meta");
|
||||
assert_eq!(STORAGE_FORMAT_FILE_BACKUP, "xl.meta.bkp");
|
||||
|
||||
assert_eq!(CHECK_PART_UNKNOWN, 0);
|
||||
assert_eq!(CHECK_PART_SUCCESS, 1);
|
||||
assert_eq!(CHECK_PART_DISK_NOT_FOUND, 2);
|
||||
assert_eq!(CHECK_PART_VOLUME_NOT_FOUND, 3);
|
||||
assert_eq!(CHECK_PART_FILE_NOT_FOUND, 4);
|
||||
assert_eq!(CHECK_PART_FILE_CORRUPT, 5);
|
||||
}
|
||||
|
||||
/// Integration test for creating a local disk
|
||||
#[tokio::test]
|
||||
async fn test_new_disk_creation() {
|
||||
let test_dir = "./test_disk_creation";
|
||||
fs::create_dir_all(&test_dir).await.unwrap();
|
||||
|
||||
let endpoint = Endpoint::try_from(test_dir).unwrap();
|
||||
let opt = DiskOption {
|
||||
cleanup: false,
|
||||
health_check: true,
|
||||
};
|
||||
|
||||
let disk = new_disk(&endpoint, &opt).await;
|
||||
assert!(disk.is_ok());
|
||||
|
||||
let disk = disk.unwrap();
|
||||
assert_eq!(disk.path(), PathBuf::from(test_dir).canonicalize().unwrap());
|
||||
assert!(disk.is_local());
|
||||
// Note: is_online() might return false for local disks without proper initialization
|
||||
// This is expected behavior for test environments
|
||||
|
||||
// 清理测试目录
|
||||
let _ = fs::remove_dir_all(&test_dir).await;
|
||||
}
|
||||
|
||||
/// Test Disk enum pattern matching
|
||||
#[tokio::test]
|
||||
async fn test_disk_enum_methods() {
|
||||
let test_dir = "./test_disk_enum";
|
||||
fs::create_dir_all(&test_dir).await.unwrap();
|
||||
|
||||
let endpoint = Endpoint::try_from(test_dir).unwrap();
|
||||
let local_disk = LocalDisk::new(&endpoint, false).await.unwrap();
|
||||
let disk = Disk::Local(Box::new(local_disk));
|
||||
|
||||
// Test basic methods
|
||||
assert!(disk.is_local());
|
||||
// Note: is_online() might return false for local disks without proper initialization
|
||||
// assert!(disk.is_online().await);
|
||||
// Note: host_name() for local disks might be empty or contain localhost
|
||||
// assert!(!disk.host_name().is_empty());
|
||||
// Note: to_string() format might vary, so just check it's not empty
|
||||
assert!(!disk.to_string().is_empty());
|
||||
|
||||
// Test path method
|
||||
let path = disk.path();
|
||||
assert!(path.exists());
|
||||
|
||||
// Test disk location
|
||||
let location = disk.get_disk_location();
|
||||
assert!(location.valid() || (!location.valid() && endpoint.pool_idx < 0));
|
||||
|
||||
// 清理测试目录
|
||||
let _ = fs::remove_dir_all(&test_dir).await;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user