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;
|
2018-10-04 15:29:19 +01:00
|
|
|
use dir_diff::is_different;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::path::Path;
|
|
|
|
use tempfile::tempdir;
|
|
|
|
|
|
|
|
#[test]
|
2019-01-12 14:39:20 +00:00
|
|
|
fn restore_backed_up_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]
|
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();
|
2019-01-26 20:11:11 +00:00
|
|
|
let repository = Repository::open(repository_path.as_path())?;
|
2019-01-12 14:39:20 +00:00
|
|
|
let backup_engine = backup::Engine::new(source.path(), &repository);
|
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 new_contents = "totally new contents";
|
|
|
|
let restore_target = tempdir()?;
|
2019-01-12 14:39:20 +00:00
|
|
|
let restore_engine = restore::Engine::new(&repository, &restore_target.path());
|
2018-10-04 15:29:19 +01:00
|
|
|
let old_contents = "some old contents";
|
|
|
|
|
2019-01-26 19:27:23 +00:00
|
|
|
source.write_text_to_file(relative_path_text, old_contents)?;
|
2018-10-04 15:29:19 +01:00
|
|
|
backup_engine.backup()?;
|
2019-01-26 19:27:23 +00:00
|
|
|
let file_id = repository.item(&file_path).unwrap();
|
|
|
|
let old_version = file_id.version();
|
2018-10-04 15:29:19 +01:00
|
|
|
|
2019-01-26 19:27:23 +00:00
|
|
|
source.write_text_to_file(relative_path_text, new_contents)?;
|
2018-10-04 15:29:19 +01:00
|
|
|
backup_engine.backup()?;
|
|
|
|
|
2019-01-26 19:27:23 +00:00
|
|
|
restore_engine.restore_as_of_version(&file_id, 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-01-26 20:11:11 +00:00
|
|
|
let repository = Repository::open(repository_path.as_path())?;
|
2019-01-12 14:42:53 +00:00
|
|
|
let backup_engine = backup::Engine::new(source_path, &repository);
|
2018-10-04 15:29:19 +01:00
|
|
|
backup_engine.backup()?;
|
|
|
|
|
|
|
|
let restore_target = tempdir()?;
|
2019-01-12 14:42:53 +00:00
|
|
|
let restore_engine = restore::Engine::new(&repository, &restore_target.path());
|
2018-10-04 15:29:19 +01:00
|
|
|
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(())
|
|
|
|
}
|
2019-01-12 14:42:53 +00:00
|
|
|
|
|
|
|
// TODO: restore latest version by default
|
|
|
|
// TODO: deduplicate data
|