bakare/tests/system_tests.rs
2019-01-26 20:11:11 +00:00

75 lines
2.6 KiB
Rust

use bakare::backup;
use bakare::restore;
use bakare::source::TempSource;
use bakare::error::BakareError;
use bakare::repository::Repository;
use dir_diff::is_different;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use tempfile::tempdir;
#[test]
fn restore_backed_up_files() -> Result<(), BakareError> {
let source = TempSource::new()?;
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")?;
assert_same_after_restore(source.path())
}
#[test]
fn restore_older_version_of_file() -> Result<(), BakareError> {
let source = TempSource::new()?;
let repository_path = tempdir()?.into_path();
let repository = Repository::open(repository_path.as_path())?;
let backup_engine = backup::Engine::new(source.path(), &repository);
let relative_path_text = "some path";
let file_path = source.file_path(relative_path_text);
let new_contents = "totally new contents";
let restore_target = tempdir()?;
let restore_engine = restore::Engine::new(&repository, &restore_target.path());
let old_contents = "some old contents";
source.write_text_to_file(relative_path_text, old_contents)?;
backup_engine.backup()?;
let file_id = repository.item(&file_path).unwrap();
let old_version = file_id.version();
source.write_text_to_file(relative_path_text, new_contents)?;
backup_engine.backup()?;
restore_engine.restore_as_of_version(&file_id, old_version)?;
assert_target_file_contents(restore_target.path(), relative_path_text, old_contents)
}
fn assert_target_file_contents(target: &Path, filename: &str, expected_contents: &str) -> Result<(), BakareError> {
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(())
}
fn assert_same_after_restore(source_path: &Path) -> Result<(), BakareError> {
let repository_path = tempdir()?.into_path();
let repository = Repository::open(repository_path.as_path())?;
let backup_engine = backup::Engine::new(source_path, &repository);
backup_engine.backup()?;
let restore_target = tempdir()?;
let restore_engine = restore::Engine::new(&repository, &restore_target.path());
restore_engine.restore_all()?;
let are_source_and_target_different = is_different(source_path, &restore_target.path()).unwrap();
assert!(!are_source_and_target_different);
Ok(())
}
// TODO: restore latest version by default
// TODO: deduplicate data