use revocatio::error::RevocatioError; use revocatio::{run, BackupResult, Config}; use crate::get_test_path; #[test] fn missing_dump_files() -> Result<(), RevocatioError> { let config = format!( "backups: - name: missing-files options: kind: pg_dump path: \"{}\" file_format: pg_dump_\\d+.sql ok_days: 3 ", get_test_path(&vec!["pg-backups", "missing"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("missing-files", result.name); match &result.result { BackupResult::Success => assert!(false), BackupResult::Failure(msg) => assert_eq!("no files matched format", msg), } Ok(()) } #[test] fn corrupted_dumps() -> Result<(), RevocatioError> { let config = format!( "backups: - name: corrupted options: kind: pg_dump path: \"{}\" file_format: pg_dump.dump verify: compression: none ok_days: 3 ", get_test_path(&vec!["pg-backups", "corrupted"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("corrupted", result.name); match &result.result { BackupResult::Success => assert!(false), BackupResult::Failure(msg) => assert_eq!("no valid SQL dump found", msg), } Ok(()) } #[test] fn only_new_enough_are_corrupted() -> Result<(), RevocatioError> { let config = format!( "backups: - name: new-corrupted options: kind: pg_dump path: \"{}\" file_format: pg_dump_\\d+.dump verify: compression: none ok_days: 3 ", get_test_path(&vec!["pg-backups", "new-corrupted"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("new-corrupted", result.name); match &result.result { BackupResult::Success => assert!(false), BackupResult::Failure(msg) => assert_eq!("no valid SQL dump found", msg), } Ok(()) } #[test] fn dumps_too_small() -> Result<(), RevocatioError> { let config = format!( "backups: - name: too-small options: kind: pg_dump path: \"{}\" file_format: pg_dump_\\d+.sql min_size: 1024 ok_days: 3 ", get_test_path(&vec!["pg-backups", "too-small"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("too-small", result.name); match &result.result { BackupResult::Success => assert!(false), BackupResult::Failure(msg) => assert_eq!("all matching files are too small", msg), } Ok(()) } #[test] fn dumps_are_valid_but_too_old() -> Result<(), RevocatioError> { let config = format!( "backups: - name: valid-but-too-old options: kind: pg_dump path: \"{}\" file_format: pg_dump_\\d+.sql min_size: 1024 ok_days: 3 ", get_test_path(&vec!["pg-backups", "valid-but-too-old"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("valid-but-too-old", result.name); match &result.result { BackupResult::Success => assert!(false), BackupResult::Failure(msg) => assert_eq!("all matching files are too old", msg), } Ok(()) } #[test] fn valid_compressed() -> Result<(), RevocatioError> { let config = format!( "backups: - name: valid-compressed options: kind: pg_dump path: \"{}\" file_format: pg_dump_\\d+.dump.gz verify: compression: gzip ok_days: 3 ", get_test_path(&vec!["pg-backups", "valid-compressed"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("valid-compressed", result.name); match &result.result { BackupResult::Success => {} BackupResult::Failure(msg) => { println!("{}", msg); assert!(false); } } Ok(()) } #[test] fn valid_custom() -> Result<(), RevocatioError> { let config = format!( "backups: - name: valid-custom options: kind: pg_dump path: \"{}\" file_format: pg_dump_\\d+.dump verify: compression: none ok_days: 3 ", get_test_path(&vec!["pg-backups", "valid-custom"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("valid-custom", result.name); match &result.result { BackupResult::Success => {} BackupResult::Failure(_) => assert!(false), } Ok(()) } #[test] fn valid_sql() -> Result<(), RevocatioError> { let config = format!( "backups: - name: valid-sql options: kind: pg_dump path: \"{}\" file_format: pg_dump_\\d+.sql ok_days: 3 ", get_test_path(&vec!["pg-backups", "valid-sql"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("valid-sql", result.name); match &result.result { BackupResult::Success => {} BackupResult::Failure(_) => assert!(false), } Ok(()) } #[test] fn valid_tar() -> Result<(), RevocatioError> { let config = format!( "backups: - name: valid-tar options: kind: pg_dump path: \"{}\" file_format: pg_dump_\\d+.tar ok_days: 3 ", get_test_path(&vec!["pg-backups", "valid-tar"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("valid-tar", result.name); match &result.result { BackupResult::Success => {} BackupResult::Failure(_) => assert!(false), } Ok(()) } #[test] fn compression_of_corrupted_dump() -> Result<(), RevocatioError> { let config = format!( "backups: - name: corrupted-compression options: kind: pg_dump path: \"{}\" file_format: pg_dump_\\d+.dump.gz verify: compression: gzip ok_days: 3 ", get_test_path(&vec!["pg-backups", "corrupted-compression"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("corrupted-compression", result.name); match &result.result { BackupResult::Success => assert!(false), BackupResult::Failure(msg) => assert_eq!("no valid SQL dump found", msg), } Ok(()) } // This test currently fails, see https://todo.sr.ht/~reesmichael1/revocatio/23 // It's not worth fixing that crash before finishing the test suite, // so we'll keep it around for now. #[test] fn dump_compressed_then_corrupted() -> Result<(), RevocatioError> { let config = format!( "backups: - name: compression-corrupted options: kind: pg_dump path: \"{}\" file_format: pg_dump_\\d+.dump.gz verify: compression: gzip ok_days: 3 ", get_test_path(&vec!["pg-backups", "compression-corrupted"]) .to_str() .unwrap() ); let config: Config = serde_yaml::from_str(&config).unwrap(); let result = run(config)?; assert_eq!(1, result.len()); let result = result.get(0).unwrap(); assert_eq!("compression-corrupted", result.name); match &result.result { BackupResult::Success => assert!(false), BackupResult::Failure(msg) => assert_eq!("no valid SQL dump found", msg), } Ok(()) }