add wrapping modules in integration tests

This commit is contained in:
Cyryl Płotnicki 2020-11-28 14:29:23 +00:00
parent 5728645add
commit 4266f48b03
3 changed files with 218 additions and 211 deletions

View file

@ -1,16 +1,17 @@
use std::fs; #[cfg(test)]
use std::path::Path; mod must {
use std::fs;
use std::path::Path;
use anyhow::Result; use anyhow::Result;
use bakare::repository::Repository; use bakare::repository::Repository;
use bakare::test::{assertions::*, source::TestSource}; use bakare::test::{assertions::*, source::TestSource};
use bakare::{backup, restore}; use bakare::{backup, restore};
use nix::sys::wait::{waitpid, WaitStatus}; use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::{fork, ForkResult}; use nix::unistd::{fork, ForkResult};
use tempfile::tempdir; use tempfile::tempdir;
#[test]
#[test] fn handle_concurrent_backups() -> Result<()> {
fn handle_concurrent_backups() -> Result<()> {
setup_logger(); setup_logger();
let repository_path = &tempdir().unwrap().into_path(); let repository_path = &tempdir().unwrap().into_path();
Repository::init(repository_path)?; Repository::init(repository_path)?;
@ -34,16 +35,16 @@ fn handle_concurrent_backups() -> Result<()> {
} }
} }
Ok(()) Ok(())
} }
fn backup_in_parallel<T>( fn backup_in_parallel<T>(
repository_path: T, repository_path: T,
parallel_backups_number: usize, parallel_backups_number: usize,
files_per_backup_number: usize, files_per_backup_number: usize,
) -> Result<Vec<usize>> ) -> Result<Vec<usize>>
where where
T: AsRef<Path> + Sync, T: AsRef<Path> + Sync,
{ {
let task_numbers = (0..parallel_backups_number).collect::<Vec<_>>(); let task_numbers = (0..parallel_backups_number).collect::<Vec<_>>();
let mut child_pids = vec![]; let mut child_pids = vec![];
for task_number in &task_numbers { for task_number in &task_numbers {
@ -70,12 +71,12 @@ where
} }
} }
Ok(task_numbers) Ok(task_numbers)
} }
fn backup_process<T>(task_number: usize, repository_path: T, files_per_backup_number: usize) -> Result<()> fn backup_process<T>(task_number: usize, repository_path: T, files_per_backup_number: usize) -> Result<()>
where where
T: AsRef<Path> + Sync, T: AsRef<Path> + Sync,
{ {
let mut repository = Repository::open(repository_path.as_ref())?; let mut repository = Repository::open(repository_path.as_ref())?;
let source = TestSource::new().unwrap(); let source = TestSource::new().unwrap();
let mut backup_engine = backup::Engine::new(source.path(), &mut repository)?; let mut backup_engine = backup::Engine::new(source.path(), &mut repository)?;
@ -85,20 +86,21 @@ where
} }
backup_engine.backup()?; backup_engine.backup()?;
Ok(()) Ok(())
} }
fn restore_all<T: AsRef<Path>>(repository_path: T) -> Result<Vec<Box<Path>>> { fn restore_all<T: AsRef<Path>>(repository_path: T) -> Result<Vec<Box<Path>>> {
let restore_target = tempdir().unwrap().into_path(); let restore_target = tempdir().unwrap().into_path();
let mut restore_repository = Repository::open(repository_path.as_ref())?; let mut restore_repository = Repository::open(repository_path.as_ref())?;
let mut restore_engine = restore::Engine::new(&mut restore_repository, restore_target.as_ref())?; let mut restore_engine = restore::Engine::new(&mut restore_repository, restore_target.as_ref())?;
restore_engine.restore_all()?; restore_engine.restore_all()?;
get_sorted_files_recursively(&restore_target) get_sorted_files_recursively(&restore_target)
} }
fn setup_logger() { fn setup_logger() {
femme::with_level(log::LevelFilter::Info); femme::with_level(log::LevelFilter::Info);
} }
fn file_id(i: usize, j: usize) -> String { fn file_id(i: usize, j: usize) -> String {
format!("{}-{}", i, j) format!("{}-{}", i, j)
}
} }

View file

@ -1,11 +1,12 @@
use tempfile::tempdir; #[cfg(test)]
mod must {
use tempfile::tempdir;
use bakare::repository::Repository; use bakare::repository::Repository;
use bakare::test::{assertions::*, source::TestSource}; use bakare::test::{assertions::*, source::TestSource};
use proptest::prelude::*; use proptest::prelude::*;
proptest! {
proptest! {
#[test] #[test]
fn store_duplicated_files_just_once(contents in any::<[u8;3]>()) { fn store_duplicated_files_just_once(contents in any::<[u8;3]>()) {
let source = TestSource::new().unwrap(); let source = TestSource::new().unwrap();
@ -24,4 +25,5 @@ proptest! {
assert_restored_file_contents(repository_path, &source.file_path("1"), &contents).unwrap(); assert_restored_file_contents(repository_path, &source.file_path("1"), &contents).unwrap();
assert_restored_file_contents(repository_path, &source.file_path("2"), &contents).unwrap(); assert_restored_file_contents(repository_path, &source.file_path("2"), &contents).unwrap();
} }
}
} }

View file

@ -1,12 +1,14 @@
use tempfile::tempdir; #[cfg(test)]
mod must {
use tempfile::tempdir;
use anyhow::Result; use anyhow::Result;
use bakare::backup; use bakare::backup;
use bakare::repository::Repository; use bakare::repository::Repository;
use bakare::test::{assertions::*, source::TestSource}; use bakare::test::{assertions::*, source::TestSource};
#[test] #[test]
fn restore_multiple_files() -> Result<()> { fn restore_multiple_files() -> Result<()> {
let source = TestSource::new().unwrap(); let source = TestSource::new().unwrap();
source.write_text_to_file("first", "some contents").unwrap(); source.write_text_to_file("first", "some contents").unwrap();
@ -14,10 +16,10 @@ fn restore_multiple_files() -> Result<()> {
source.write_text_to_file("third", "some other contents").unwrap(); source.write_text_to_file("third", "some other contents").unwrap();
assert_same_after_restore(source.path()) assert_same_after_restore(source.path())
} }
#[test] #[test]
fn restore_files_after_reopening_repository() -> Result<()> { fn restore_files_after_reopening_repository() -> Result<()> {
let source = TestSource::new().unwrap(); let source = TestSource::new().unwrap();
let repository_path = &tempdir().unwrap().into_path(); let repository_path = &tempdir().unwrap().into_path();
let restore_target = tempdir().unwrap().into_path(); let restore_target = tempdir().unwrap().into_path();
@ -32,10 +34,10 @@ fn restore_files_after_reopening_repository() -> Result<()> {
let source_file_full_path = &source.file_path(source_file_relative_path); let source_file_full_path = &source.file_path(source_file_relative_path);
assert_restored_file_contents(repository_path, source_file_full_path, original_contents.as_bytes()) assert_restored_file_contents(repository_path, source_file_full_path, original_contents.as_bytes())
} }
#[test] #[test]
fn restore_older_version_of_file() -> Result<()> { fn restore_older_version_of_file() -> Result<()> {
let source = TestSource::new().unwrap(); let source = TestSource::new().unwrap();
let repository_path = tempdir().unwrap().into_path(); let repository_path = tempdir().unwrap().into_path();
Repository::init(repository_path.as_path())?; Repository::init(repository_path.as_path())?;
@ -53,10 +55,10 @@ fn restore_older_version_of_file() -> Result<()> {
backup_file_with_text_contents(&source, &repository_path, source_file_relative_path, new_contents)?; backup_file_with_text_contents(&source, &repository_path, source_file_relative_path, new_contents)?;
assert_restored_from_version_has_contents(&repository_path, &source_file_full_path, old_contents.as_bytes(), &old_id) assert_restored_from_version_has_contents(&repository_path, &source_file_full_path, old_contents.as_bytes(), &old_id)
} }
#[test] #[test]
fn newer_version_should_be_greater_than_earlier_version() -> Result<()> { fn newer_version_should_be_greater_than_earlier_version() -> Result<()> {
let source = TestSource::new().unwrap(); let source = TestSource::new().unwrap();
let repository_path = tempdir().unwrap().into_path(); let repository_path = tempdir().unwrap().into_path();
Repository::init(repository_path.as_path())?; Repository::init(repository_path.as_path())?;
@ -77,10 +79,10 @@ fn newer_version_should_be_greater_than_earlier_version() -> Result<()> {
assert!(new_version > old_version); assert!(new_version > old_version);
Ok(()) Ok(())
} }
#[test] #[test]
fn restore_latest_version_by_default() -> Result<()> { fn restore_latest_version_by_default() -> Result<()> {
let source = TestSource::new().unwrap(); let source = TestSource::new().unwrap();
let repository_path = &tempdir().unwrap().into_path(); let repository_path = &tempdir().unwrap().into_path();
Repository::init(repository_path)?; Repository::init(repository_path)?;
@ -92,17 +94,18 @@ fn restore_latest_version_by_default() -> Result<()> {
let source_file_full_path = &source.file_path(source_file_relative_path); let source_file_full_path = &source.file_path(source_file_relative_path);
assert_restored_file_contents(repository_path, source_file_full_path, b"newest contents") assert_restored_file_contents(repository_path, source_file_full_path, b"newest contents")
} }
#[test] #[test]
fn forbid_backup_of_paths_within_repository() -> Result<()> { fn forbid_backup_of_paths_within_repository() -> Result<()> {
let repository_path = &tempdir().unwrap().into_path(); let repository_path = &tempdir().unwrap().into_path();
Repository::init(repository_path)?; Repository::init(repository_path)?;
let mut repository = Repository::open(repository_path)?; let mut repository = Repository::open(repository_path)?;
let error = backup::Engine::new(repository_path, &mut repository); let error = backup::Engine::new(repository_path, &mut repository);
assert!(error.is_err()); assert!(error.is_err());
Ok(()) Ok(())
}
// TODO: index corruption
// TODO: encryption
// TODO: resume from sleep while backup in progress
} }
// TODO: index corruption
// TODO: encryption
// TODO: resume from sleep while backup in progress