bakare/tests/system_tests.rs

132 lines
4.8 KiB
Rust
Raw Normal View History

2019-09-07 14:42:30 +01:00
use tempfile::tempdir;
2018-12-22 11:31:27 +00:00
use anyhow::Result;
2019-09-07 15:08:47 +01:00
use bakare::backup;
2019-01-12 14:42:53 +00:00
use bakare::repository::Repository;
2019-09-07 14:19:03 +01:00
use bakare::source::TempSource;
2019-09-07 14:42:30 +01:00
use bakare::test::assertions::*;
2018-10-04 15:29:19 +01:00
#[test]
fn restore_multiple_files() -> Result<()> {
let source = TempSource::new().unwrap();
2018-10-04 15:29:19 +01:00
source.write_text_to_file("first", "some contents").unwrap();
source.write_text_to_file("second", "some contents").unwrap();
source.write_text_to_file("third", "some other contents").unwrap();
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<()> {
let source = TempSource::new().unwrap();
let repository_path = &tempdir().unwrap().into_path();
let restore_target = tempdir().unwrap().into_path();
2019-09-23 12:18:18 +01:00
Repository::init(repository_path)?;
2019-09-07 10:29:27 +01:00
let source_file_relative_path = "some file path";
let original_contents = "some old contents";
2019-09-07 14:42:30 +01:00
backup_file_with_contents(&source, &repository_path, source_file_relative_path, original_contents)?;
2019-09-07 14:42:30 +01:00
restore_all_from_reloaded_repository(&repository_path, &restore_target)?;
2019-09-23 12:18:18 +01:00
let source_file_full_path = &source.file_path(source_file_relative_path);
2019-09-23 17:08:09 +01:00
assert_restored_file_contents(repository_path, source_file_full_path, original_contents)
}
2018-10-04 15:29:19 +01:00
#[test]
fn restore_older_version_of_file() -> Result<()> {
let source = TempSource::new().unwrap();
let repository_path = tempdir().unwrap().into_path();
Repository::init(repository_path.as_path())?;
2019-09-01 11:05:22 +01:00
2019-09-07 14:19:03 +01:00
let source_file_relative_path = "some path";
let source_file_full_path = source.file_path(source_file_relative_path);
2018-10-04 15:29:19 +01:00
let old_contents = "some old contents";
2019-09-07 14:42:30 +01:00
backup_file_with_contents(&source, &repository_path, source_file_relative_path, old_contents)?;
2019-09-23 16:30:40 +01:00
let old_item = newest_item(&repository_path, &source_file_full_path)?;
let old_id = old_item.id();
2019-09-07 14:42:30 +01:00
let new_contents = "totally new contents";
backup_file_with_contents(&source, &repository_path, source_file_relative_path, new_contents)?;
2019-09-23 16:30:40 +01:00
assert_restored_from_version_has_contents(&repository_path, &source_file_full_path, old_contents, &old_id)
}
#[test]
fn newer_version_should_be_greater_than_earlier_version() -> Result<()> {
let source = TempSource::new().unwrap();
let repository_path = tempdir().unwrap().into_path();
2019-09-23 16:30:40 +01:00
Repository::init(repository_path.as_path())?;
let source_file_relative_path = "some path";
let source_file_full_path = source.file_path(source_file_relative_path);
backup_file_with_contents(&source, &repository_path, source_file_relative_path, "old")?;
let old_item = newest_item(&repository_path, &source_file_full_path)?;
let old_version = old_item.version();
backup_file_with_contents(&source, &repository_path, source_file_relative_path, "new")?;
let new_item = newest_item(&repository_path, &source_file_full_path)?;
let new_version = new_item.version();
assert!(new_version > old_version);
Ok(())
2018-10-04 15:29:19 +01:00
}
2019-09-23 17:04:23 +01:00
#[test]
fn store_duplicated_files_just_once() -> Result<()> {
let source = TempSource::new().unwrap();
let repository_path = &tempdir().unwrap().into_path();
2019-09-23 17:04:23 +01:00
Repository::init(repository_path)?;
assert_eq!(data_weight(&repository_path)?, 0);
let contents = "some contents";
backup_file_with_contents(&source, &repository_path, "1", contents)?;
let first_weight = data_weight(&repository_path)?;
assert!(first_weight > 0);
backup_file_with_contents(&source, &repository_path, "2", contents)?;
let second_weight = data_weight(&repository_path)?;
assert_eq!(first_weight, second_weight);
2019-09-23 17:08:09 +01:00
assert_restored_file_contents(repository_path, &source.file_path("1"), contents)?;
assert_restored_file_contents(repository_path, &source.file_path("2"), contents)?;
2019-09-23 17:04:23 +01:00
Ok(())
}
2019-09-23 12:18:18 +01:00
#[test]
fn restore_latest_version_by_default() -> Result<()> {
let source = TempSource::new().unwrap();
let repository_path = &tempdir().unwrap().into_path();
2019-09-23 12:18:18 +01:00
Repository::init(repository_path)?;
let source_file_relative_path = "some path";
backup_file_with_contents(&source, &repository_path, source_file_relative_path, "old contents")?;
backup_file_with_contents(&source, &repository_path, source_file_relative_path, "newer contents")?;
backup_file_with_contents(&source, &repository_path, source_file_relative_path, "newest contents")?;
let source_file_full_path = &source.file_path(source_file_relative_path);
2019-09-23 17:08:09 +01:00
assert_restored_file_contents(repository_path, source_file_full_path, "newest contents")
2019-09-23 12:18:18 +01:00
}
2019-09-07 15:08:47 +01:00
#[test]
fn forbid_backup_of_paths_within_repository() -> Result<()> {
let repository_path = &tempdir().unwrap().into_path();
2019-09-07 15:08:47 +01:00
Repository::init(repository_path)?;
let mut repository = Repository::open(repository_path)?;
let error = backup::Engine::new(repository_path, &mut repository);
assert!(error.is_err());
2019-09-07 15:08:47 +01:00
Ok(())
}
2019-09-07 11:37:31 +01:00
// TODO: index corruption
2019-09-23 17:07:10 +01:00
// TODO: encryption
// TODO: resume from sleep while backup in progress