2018-10-04 15:29:19 +01:00
|
|
|
extern crate bakare;
|
|
|
|
extern crate dir_diff;
|
2018-10-04 15:33:01 +01:00
|
|
|
extern crate tempfile;
|
2018-10-04 15:29:19 +01:00
|
|
|
|
2018-10-04 15:33:01 +01:00
|
|
|
use bakare::backup;
|
|
|
|
use bakare::restore;
|
|
|
|
use bakare::restore::WhatToRestore::SpecificPath;
|
2018-10-04 15:29:19 +01:00
|
|
|
use dir_diff::is_different;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Error;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
|
|
|
use tempfile::tempdir;
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn restore_backed_up_files() -> Result<(), Error> {
|
|
|
|
let source = Source::new()?;
|
|
|
|
let repository = tempdir()?;
|
|
|
|
|
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
|
|
|
|
|
|
|
assert_same_after_restore(source.path(), repository.path())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn restore_older_version_of_file() -> Result<(), Error> {
|
|
|
|
let source = Source::new()?;
|
|
|
|
let repository = tempdir()?;
|
2018-10-04 15:33:01 +01:00
|
|
|
let backup_engine = backup::Engine::new(source.path(), repository.path());
|
2018-10-04 15:29:19 +01:00
|
|
|
let path = "some path";
|
|
|
|
let new_contents = "totally new contents";
|
|
|
|
let restore_target = tempdir()?;
|
2018-10-04 15:33:01 +01:00
|
|
|
let restore_engine = restore::Engine::new(repository.path(), &restore_target.path());
|
2018-10-04 15:29:19 +01:00
|
|
|
let old_contents = "some old contents";
|
|
|
|
|
|
|
|
source.write_text_to_file(path, old_contents)?;
|
|
|
|
backup_engine.backup()?;
|
|
|
|
let old_version = backup_engine.file_version(path.as_ref());
|
|
|
|
|
|
|
|
source.write_text_to_file(path, new_contents)?;
|
|
|
|
backup_engine.backup()?;
|
|
|
|
|
|
|
|
restore_engine.restore_as_of_version(SpecificPath(path.into()), old_version)?;
|
|
|
|
|
|
|
|
assert_target_file_contents(restore_target.path(), path, old_contents)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: restore latest version by default
|
|
|
|
// TODO: deduplicate data
|
|
|
|
|
|
|
|
fn assert_target_file_contents(target: &Path, filename: &str, expected_contents: &str) -> Result<(), Error> {
|
|
|
|
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, repository_path: &Path) -> Result<(), Error> {
|
2018-10-04 15:33:01 +01:00
|
|
|
let backup_engine = backup::Engine::new(source_path, repository_path);
|
2018-10-04 15:29:19 +01:00
|
|
|
backup_engine.backup()?;
|
|
|
|
|
|
|
|
let restore_target = tempdir()?;
|
2018-10-04 15:33:01 +01:00
|
|
|
let restore_engine = restore::Engine::new(repository_path, &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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Source {
|
|
|
|
directory: TempDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Source {
|
|
|
|
fn new() -> Result<Self, Error> {
|
|
|
|
Ok(Self { directory: tempdir()? })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_text_to_file(&self, filename: &str, text: &str) -> Result<(), Error> {
|
|
|
|
Ok(File::create(self.directory.path().join(filename))?.write_all(text.as_bytes())?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> &Path {
|
|
|
|
self.directory.path()
|
|
|
|
}
|
|
|
|
}
|