bakare/tests/system_tests.rs

163 lines
5.5 KiB
Rust
Raw Normal View History

2018-10-04 15:33:01 +01:00
use bakare::backup;
use bakare::restore;
2018-12-22 11:31:27 +00:00
2019-01-26 19:27:23 +00:00
use bakare::source::TempSource;
2018-12-22 16:09:22 +00:00
2019-01-12 14:42:53 +00:00
use bakare::error::BakareError;
use bakare::repository::Repository;
use std::fs;
2018-10-04 15:29:19 +01:00
use std::fs::File;
use std::io::Read;
use std::path::Path;
use tempfile::tempdir;
2019-09-01 17:30:45 +01:00
use walkdir::WalkDir;
2018-10-04 15:29:19 +01:00
#[test]
fn restore_multiple_files() -> Result<(), BakareError> {
2019-01-26 19:27:23 +00:00
let source = TempSource::new()?;
2018-10-04 15:29:19 +01:00
2018-10-04 15:30:18 +01:00
source.write_text_to_file("first", "some contents")?;
source.write_text_to_file("second", "some contents")?;
source.write_text_to_file("third", "some other contents")?;
2018-10-04 15:29:19 +01:00
2019-01-12 14:42:53 +00:00
assert_same_after_restore(source.path())
2018-10-04 15:29:19 +01:00
}
#[test]
fn restore_files_after_reopening_repository() -> Result<(), BakareError> {
let source = TempSource::new()?;
let repository_path = tempdir()?.into_path();
let restore_target = tempdir()?.into_path();
Repository::init(repository_path.as_path())?;
let relative_path_text = "some file path";
let original_contents = "some old contents";
{
let mut backup_repository = Repository::open(repository_path.as_path())?;
let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository);
source.write_text_to_file(relative_path_text, original_contents)?;
backup_engine.backup()?;
}
{
let restore_repository = Repository::open(repository_path.as_path())?;
let restore_engine = restore::Engine::new(&restore_repository, &restore_target)?;
restore_engine.restore_all()?;
}
let restored_file_path = restore_target.join(relative_path_text);
let contents = fs::read_to_string(restored_file_path)?;
assert_eq!(contents, original_contents);
Ok(())
}
2018-10-04 15:29:19 +01:00
#[test]
2019-01-12 14:39:20 +00:00
fn restore_older_version_of_file() -> Result<(), BakareError> {
2019-01-26 19:27:23 +00:00
let source = TempSource::new()?;
2019-01-12 14:39:20 +00:00
let repository_path = tempdir()?.into_path();
Repository::init(repository_path.as_path())?;
2019-09-01 11:05:22 +01:00
2019-01-26 19:27:23 +00:00
let relative_path_text = "some path";
let file_path = source.file_path(relative_path_text);
2018-10-04 15:29:19 +01:00
let old_contents = "some old contents";
2019-09-01 11:05:22 +01:00
{
let mut backup_repository = Repository::open(repository_path.as_path())?;
let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository);
source.write_text_to_file(relative_path_text, old_contents)?;
backup_engine.backup()?;
}
let reading_repository = Repository::open(repository_path.as_path())?;
let item = reading_repository.item_by_source_path(&file_path)?;
assert!(item.is_some());
let item = item.unwrap();
let old_version = item.version();
2018-10-04 15:29:19 +01:00
2019-09-01 11:05:22 +01:00
{
let new_contents = "totally new contents";
2019-09-01 11:05:22 +01:00
let mut backup_repository = Repository::open(repository_path.as_path())?;
let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository);
source.write_text_to_file(relative_path_text, new_contents)?;
backup_engine.backup()?;
}
2018-10-04 15:29:19 +01:00
let restore_repository = Repository::open(repository_path.as_path())?;
let restore_target = tempdir()?;
let restore_engine = restore::Engine::new(&restore_repository, &restore_target.path())?;
//restore_engine.restore_as_of_version(&item, old_version)?;
2018-10-04 15:29:19 +01:00
2019-01-26 19:27:23 +00:00
assert_target_file_contents(restore_target.path(), relative_path_text, old_contents)
2018-10-04 15:29:19 +01:00
}
2019-01-12 14:39:20 +00:00
fn assert_target_file_contents(target: &Path, filename: &str, expected_contents: &str) -> Result<(), BakareError> {
2018-10-04 15:29:19 +01:00
let restored_path = target.join(filename);
let mut actual_contents = String::new();
File::open(restored_path)?.read_to_string(&mut actual_contents)?;
assert_eq!(expected_contents, actual_contents);
Ok(())
}
2019-01-12 14:42:53 +00:00
fn assert_same_after_restore(source_path: &Path) -> Result<(), BakareError> {
let repository_path = tempdir()?.into_path();
2019-09-01 12:57:37 +01:00
let restore_target = tempdir()?.into_path();
assert_ne!(source_path, repository_path);
assert_ne!(repository_path, restore_target);
2018-10-04 15:29:19 +01:00
2019-09-01 21:19:40 +01:00
Repository::init(repository_path.as_path())?;
2019-09-01 11:05:22 +01:00
{
let mut backup_repository = Repository::open(repository_path.as_path())?;
let mut backup_engine = backup::Engine::new(source_path, &mut backup_repository);
backup_engine.backup()?;
}
{
2019-09-01 12:57:37 +01:00
let restore_repository = Repository::open(repository_path.as_path())?;
2019-09-01 17:03:27 +01:00
let restore_engine = restore::Engine::new(&restore_repository, &restore_target)?;
2019-09-01 11:05:22 +01:00
restore_engine.restore_all()?;
}
2019-09-01 17:30:45 +01:00
assert_directory_trees_have_same_contents(source_path, restore_target.as_path())?;
Ok(())
}
fn assert_directory_trees_have_same_contents(left: &Path, right: &Path) -> Result<(), BakareError> {
let left_files = get_sorted_files_recursively(left)?;
let right_files = get_sorted_files_recursively(right)?;
let pairs = left_files.iter().zip(right_files);
for (l, r) in pairs {
assert_eq!(l.file_name(), r.file_name());
let mut fl = File::open(l)?;
let mut fr = File::open(r)?;
let mut bl = vec![];
let mut br = vec![];
fl.read_to_end(&mut bl)?;
fr.read_to_end(&mut br)?;
assert_eq!(bl, bl);
}
2018-10-04 15:29:19 +01:00
Ok(())
}
2019-01-12 14:42:53 +00:00
2019-09-01 17:30:45 +01:00
fn get_sorted_files_recursively(path: &Path) -> Result<Vec<Box<Path>>, BakareError> {
let walker = WalkDir::new(path).sort_by(|a, b| a.file_name().cmp(b.file_name()));
let mut result = vec![];
for maybe_entry in walker {
let entry = maybe_entry?;
if entry.path() == path {
continue;
}
if entry.path().is_file() {
result.push(Box::from(entry.path()));
}
}
Ok(result)
}
2019-01-12 14:42:53 +00:00
// TODO: restore latest version by default
// TODO: deduplicate data