Add tests for snapshot directory structure

next
Sayan Nandan 3 years ago
parent e5909eb1f9
commit 076282df2b
No known key found for this signature in database
GPG Key ID: 8BC07A0A4D41DD52

@ -36,6 +36,7 @@ All changes in this project will be noted in this file.
- Fixed bug where the preload wasn't flushed if a snapshot already flushed it before the
save on termination routine
- Fixed bug that prevented tree cleanup from working
- Disallow `PRELOAD` and `PARTMAP` as entity names
## Version 0.7.4

@ -265,7 +265,7 @@ mod bytemark_set_tests {
mod bytemark_actual_table_restore {
use crate::corestore::{
memstore::ObjectID,
table::{DescribeTable, KVEList, Table, KVEBlob},
table::{DescribeTable, KVEBlob, KVEList, Table},
Data,
};
use crate::kvengine::LockedVec;
@ -695,3 +695,91 @@ mod corruption_tests {
assert!(super::de::deserialize_list_map(&v).is_none());
}
}
mod storage_target_directory_structure {
use crate::{
corestore::{
memstore::{Memstore, ObjectID},
table::{SystemTable, Table},
},
storage::v1::flush::{self, LocalSnapshot, RemoteSnapshot},
util::{
os::{self, EntryKind},
Wrapper,
},
};
enum FileKind {
Dir(&'static str),
File(&'static str),
}
impl FileKind {
fn into_entrykind_path(self, closure: impl Fn(&'static str) -> String + Copy) -> EntryKind {
match self {
Self::Dir(dir) => EntryKind::Directory(closure(dir)),
Self::File(file) => EntryKind::File(closure(file)),
}
}
}
fn get_memstore_and_file_list() -> (Memstore, Vec<FileKind>) {
let paths = vec![
// the default keyspace
FileKind::Dir("default"),
FileKind::File("default/default"),
FileKind::File("default/PARTMAP"),
// the superks keyspace
FileKind::Dir("superks"),
FileKind::File("superks/PARTMAP"),
FileKind::File("superks/blueshark"),
// the system keyspace
FileKind::Dir("system"),
FileKind::File("system/PARTMAP"),
FileKind::File("system/superauthy"),
// the preload file
FileKind::File("PRELOAD"),
];
(get_memstore(), paths)
}
fn get_memstore() -> Memstore {
let store = Memstore::new_default();
assert!(store.create_keyspace(ObjectID::try_from_slice("superks").unwrap()));
assert!(store
.get_keyspace_atomic_ref("superks".as_bytes())
.unwrap()
.create_table(
ObjectID::try_from_slice("blueshark").unwrap(),
Table::new_default_kve()
));
assert!(store.system.tables.true_if_insert(
ObjectID::try_from_slice("superauthy").unwrap(),
Wrapper::new(SystemTable::new_auth(Default::default()))
));
store
}
use std::fs;
#[test]
fn local_snapshot_dir() {
let (store, paths) = get_memstore_and_file_list();
let paths = paths
.into_iter()
.map(|v| v.into_entrykind_path(|file| format!("data/snaps/localsnap/{file}")))
.collect::<Vec<EntryKind>>();
let target = LocalSnapshot::new("localsnap".to_owned());
flush::flush_full(target, &store).unwrap();
let files = os::rlistdir("data/snaps/localsnap").unwrap();
assert_veceq!(files, paths);
fs::remove_dir_all("data/snaps/localsnap").unwrap();
}
#[test]
fn remote_snapshot_dir() {
let (store, paths) = get_memstore_and_file_list();
let paths = paths
.into_iter()
.map(|v| v.into_entrykind_path(|file| format!("data/rsnap/wisnap/{file}")))
.collect::<Vec<EntryKind>>();
let target = RemoteSnapshot::new("wisnap");
flush::flush_full(target, &store).unwrap();
let files = os::rlistdir("data/rsnap/wisnap").unwrap();
assert_veceq!(files, paths);
fs::remove_dir_all("data/rsnap/wisnap").unwrap();
}
}

@ -209,3 +209,42 @@ fn rcopy_okay() {
fs::remove_dir_all("testdata").unwrap();
fs::remove_dir_all("my-backups").unwrap();
}
#[derive(Debug, PartialEq)]
pub enum EntryKind {
Directory(String),
File(String),
}
impl EntryKind {
pub fn get_inner(&self) -> &str {
match self {
Self::Directory(rf) | Self::File(rf) => rf,
}
}
}
/// Returns a vector with a complete list of entries (both directories and files)
/// in the given path (recursive extraction)
pub fn rlistdir(path: impl AsRef<Path>) -> crate::IoResult<Vec<EntryKind>> {
let mut ret = Vec::new();
rlistdir_inner(path.as_ref(), &mut ret)?;
Ok(ret)
}
fn rlistdir_inner(path: &Path, paths: &mut Vec<EntryKind>) -> crate::IoResult<()> {
let dir = fs::read_dir(path)?;
for entry in dir {
let entry = entry?;
let path = entry.path();
let path_str = path.to_string_lossy().to_string();
// we want both directory names and file names
if path.is_dir() {
paths.push(EntryKind::Directory(path_str));
rlistdir_inner(&path, paths)?;
} else {
paths.push(EntryKind::File(path_str));
}
}
Ok(())
}

Loading…
Cancel
Save