-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,164 additions
and
921 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Use an Ubuntu base image | ||
FROM ubuntu:20.04 | ||
|
||
# Avoid prompts from apt | ||
ENV DEBIAN_FRONTEND=noninteractive | ||
|
||
# Install necessary packages | ||
RUN apt-get update && apt-get install -y \ | ||
curl \ | ||
build-essential \ | ||
pkg-config \ | ||
libssl-dev \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
# Install Rust | ||
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y | ||
ENV PATH="/root/.cargo/bin:${PATH}" | ||
|
||
WORKDIR /app | ||
|
||
COPY . /app | ||
|
||
CMD ["cargo", "test"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2024-06-24T21:54:58.246935+02:00 - INFO - Test log message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2024-06-24T22:08:38.539979+02:00 - INFO - Test log message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,76 @@ | ||
use crate::common; | ||
use crate::common::{MockConfig, MockRollbackManager}; | ||
use server_forge::backup::{ | ||
configure_backup_schedule, install_backup_tools, setup_backup_locations, setup_backup_system, | ||
}; | ||
use std::ptr::eq; | ||
use server_forge::backup; | ||
use server_forge::config::Config; | ||
use server_forge::rollback::RollbackManager; | ||
use std::fs; | ||
use std::os::unix::fs::PermissionsExt; | ||
|
||
#[test] | ||
fn test_setup_backup_system() { | ||
let mut mock = common::CommandRunner::new(); | ||
let config = MockConfig { | ||
backup_frequency: "daily".to_string(), | ||
server_role: "web".to_string(), | ||
fn test_install_backup_tools() { | ||
assert!(backup::install_backup_tools().is_ok()); | ||
|
||
// Verify restic installation | ||
let restic_status = std::process::Command::new("restic") | ||
.arg("version") | ||
.status() | ||
.unwrap(); | ||
assert!(restic_status.success()); | ||
} | ||
|
||
#[test] | ||
fn test_configure_backup_schedule() { | ||
let config = Config { | ||
backup_frequency: String::from("daily"), | ||
..Default::default() | ||
}; | ||
let rollback = MockRollbackManager::new(); | ||
|
||
mock.expect_run().times(4).returning(|_, _| Ok(())); | ||
assert!(backup::configure_backup_schedule(&config).is_ok()); | ||
|
||
assert!(setup_backup_system(&config, &rollback).is_ok()); | ||
// Verify cron job creation | ||
let cron_content = fs::read_to_string("/etc/cron.d/restic-backup").unwrap(); | ||
assert!(cron_content.contains("0 2 * * * root /usr/bin/restic backup")); | ||
} | ||
|
||
#[test] | ||
fn test_install_backup_tools() { | ||
let mut mock = common::CommandRunner::new(); | ||
fn test_setup_backup_locations() { | ||
let config = Config { | ||
server_role: String::from("web"), | ||
..Default::default() | ||
}; | ||
|
||
mock.expect_run() | ||
.with(eq("apt", ()), eq(&["install", "-y", "restic"], ())) | ||
.times(1) | ||
.returning(|_, _| Ok(())); | ||
assert!(backup::setup_backup_locations(&config).is_ok()); | ||
|
||
assert!(install_backup_tools().is_ok()); | ||
} | ||
// Verify backup script creation | ||
let script_content = fs::read_to_string("/usr/local/bin/run-backup.sh").unwrap(); | ||
assert!(script_content.contains("restic backup")); | ||
assert!(script_content.contains("/var/www")); | ||
assert!(script_content.contains("/etc/nginx")); | ||
|
||
// #[test] | ||
// fn test_configure_backup_schedule() { | ||
// let config = MockConfig { | ||
// backup_frequency: "daily".to_string(), | ||
// ..Default::default() | ||
// }; | ||
// | ||
// assert!(configure_backup_schedule(&config).is_ok()); | ||
// | ||
// // Test invalid frequency | ||
// let invalid_config = MockConfig { | ||
// backup_frequency: "invalid".to_string(), | ||
// ..Default::default() | ||
// }; | ||
// assert!(configure_backup_schedule(&invalid_config).is_err()); | ||
// } | ||
// Verify script permissions | ||
let script_metadata = fs::metadata("/usr/local/bin/run-backup.sh").unwrap(); | ||
assert!(script_metadata.permissions().mode() & 0o111 != 0); | ||
} | ||
|
||
#[test] | ||
fn test_setup_backup_locations() { | ||
let mut mock = common::CommandRunner::new(); | ||
let config = MockConfig { | ||
server_role: "web".to_string(), | ||
fn test_setup_backup_system() { | ||
let config = Config { | ||
backup_frequency: String::from("daily"), | ||
server_role: String::from("web"), | ||
..Default::default() | ||
}; | ||
let rollback_manager = RollbackManager::new(); | ||
|
||
assert!(backup::setup_backup_system(&config, &rollback_manager).is_ok()); | ||
|
||
// Verify restic installation | ||
assert!(std::process::Command::new("restic") | ||
.arg("version") | ||
.status() | ||
.unwrap() | ||
.success()); | ||
|
||
mock.expect_run().times(2).returning(|_, _| Ok(())); | ||
// Verify cron job creation | ||
assert!(fs::read_to_string("/etc/cron.d/restic-backup").is_ok()); | ||
|
||
assert!(setup_backup_locations(&config).is_ok()); | ||
// Verify backup script creation | ||
assert!(fs::read_to_string("/usr/local/bin/run-backup.sh").is_ok()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1 @@ | ||
use mockall::automock; | ||
use std::error::Error; | ||
|
||
pub trait CommandRunner { | ||
fn run(&self, command: &str, args: &[&str]) -> Result<(), Box<dyn Error>>; | ||
} | ||
|
||
pub struct MockCommandRunner; | ||
#[automock] | ||
impl CommandRunner for MockCommandRunner { | ||
fn run(&self, command: &str, args: &[&str]) -> Result<(), Box<dyn Error>> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Clone, Default)] | ||
pub struct MockConfig { | ||
pub linux_distro: String, | ||
pub server_role: String, | ||
pub security_level: String, | ||
pub monitoring: bool, | ||
pub backup_frequency: String, | ||
pub deployed_apps: Vec<String>, | ||
pub custom_firewall_rules: Vec<String>, | ||
pub update_schedule: String, | ||
pub use_containers: bool, | ||
pub use_kubernetes: bool, | ||
} | ||
|
||
pub struct MockRollbackManager; | ||
|
||
impl MockRollbackManager { | ||
pub fn new() -> Self { | ||
MockRollbackManager | ||
} | ||
|
||
pub fn create_snapshot(&self) -> Result<usize, Box<dyn Error>> { | ||
Ok(0) | ||
} | ||
|
||
pub fn commit_snapshot(&self, _snapshot_id: usize) -> Result<(), Box<dyn Error>> { | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.