Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
doziestar committed Jun 24, 2024
1 parent 89cd273 commit 9167761
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 237 deletions.
14 changes: 7 additions & 7 deletions src/containerization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn deploy_containers(
Ok(())
}

fn install_docker() -> Result<(), Box<dyn Error>> {
pub fn install_docker() -> Result<(), Box<dyn Error>> {
let package_manager = get_package_manager()?;

match package_manager {
Expand Down Expand Up @@ -142,7 +142,7 @@ fn install_docker() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn configure_docker() -> Result<(), Box<dyn Error>> {
pub fn configure_docker() -> Result<(), Box<dyn Error>> {
// Create docker group if it doesn't exist
run_command("groupadd", &["docker"])?;

Expand Down Expand Up @@ -174,7 +174,7 @@ fn configure_docker() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn install_kubernetes() -> Result<(), Box<dyn Error>> {
pub fn install_kubernetes() -> Result<(), Box<dyn Error>> {
let package_manager = get_package_manager()?;

// Install kubectl
Expand Down Expand Up @@ -204,7 +204,7 @@ fn install_kubernetes() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn configure_kubernetes() -> Result<(), Box<dyn Error>> {
pub fn configure_kubernetes() -> Result<(), Box<dyn Error>> {
// Start minikube
run_command("minikube", &["start"])?;

Expand All @@ -221,7 +221,7 @@ fn configure_kubernetes() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn deploy_container(app: &str, use_kubernetes: bool) -> Result<(), Box<dyn Error>> {
pub fn deploy_container(app: &str, use_kubernetes: bool) -> Result<(), Box<dyn Error>> {
if use_kubernetes {
deploy_to_kubernetes(app)?;
} else {
Expand All @@ -230,7 +230,7 @@ fn deploy_container(app: &str, use_kubernetes: bool) -> Result<(), Box<dyn Error
Ok(())
}

fn deploy_to_kubernetes(app: &str) -> Result<(), Box<dyn Error>> {
pub fn deploy_to_kubernetes(app: &str) -> Result<(), Box<dyn Error>> {
// Create a basic deployment YAML
let deployment_yaml = format!(
r#"
Expand Down Expand Up @@ -281,7 +281,7 @@ spec:
Ok(())
}

fn deploy_to_docker(app: &str) -> Result<(), Box<dyn Error>> {
pub fn deploy_to_docker(app: &str) -> Result<(), Box<dyn Error>> {
// Pull the latest image
run_command("docker", &["pull", app])?;

Expand Down
4 changes: 2 additions & 2 deletions src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub fn deploy_python() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn setup_web_server_config(app: &str) -> Result<(), Box<dyn Error>> {
pub fn setup_web_server_config(app: &str) -> Result<(), Box<dyn Error>> {
match app {
"nginx" => setup_nginx_config()?,
"apache" => setup_apache_config()?,
Expand Down Expand Up @@ -235,7 +235,7 @@ fn setup_apache_config() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn setup_database(db: &str) -> Result<(), Box<dyn Error>> {
pub fn setup_database(db: &str) -> Result<(), Box<dyn Error>> {
match db {
"mysql" => setup_mysql()?,
"postgresql" => setup_postgresql()?,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pub mod rollback;
pub mod security;
pub mod setup;
pub mod updates;
pub mod utils;
pub mod utils;
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ mod updates;
mod utils;

mod distro;
mod tests;

use rollback::RollbackManager;
use utils::{generate_report, get_user_input, save_config, setup_logging};
Expand Down
4 changes: 2 additions & 2 deletions src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub fn setup_node_exporter() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn install_prometheus_from_source() -> Result<(), Box<dyn Error>> {
pub fn install_prometheus_from_source() -> Result<(), Box<dyn Error>> {
run_command("wget", &["https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz"])?;
run_command("tar", &["xvfz", "prometheus-2.30.3.linux-amd64.tar.gz"])?;
run_command("mv", &["prometheus-2.30.3.linux-amd64", "prometheus"])?;
Expand Down Expand Up @@ -218,7 +218,7 @@ WantedBy=multi-user.target
Ok(())
}

fn install_node_exporter_from_source() -> Result<(), Box<dyn Error>> {
pub fn install_node_exporter_from_source() -> Result<(), Box<dyn Error>> {
run_command("wget", &["https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz"])?;
run_command("tar", &["xvfz", "node_exporter-1.2.2.linux-amd64.tar.gz"])?;

Expand Down
2 changes: 1 addition & 1 deletion src/rollback.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::distro::{get_package_manager, uninstall_package};
use log::{info};
use log::info;
use std::cell::RefCell;
use std::error::Error;
use std::fs;
Expand Down
2 changes: 1 addition & 1 deletion src/updates.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::Config;
use crate::distro::{get_package_manager};
use crate::distro::get_package_manager;
use crate::rollback::RollbackManager;
use crate::utils::run_command;
use log::info;
Expand Down
46 changes: 22 additions & 24 deletions tests/backup_tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::ptr::eq;
use crate::common::{CommandRunner, MockConfig, MockRollbackManager};
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 server_forge::distro::PackageManager;
use crate::common;

use std::ptr::eq;

#[test]
fn test_setup_backup_system() {
Expand All @@ -19,7 +17,7 @@ fn test_setup_backup_system() {

mock.expect_run().times(4).returning(|_, _| Ok(()));

assert!(setup_backup_system(&config, &rollback, &mock).is_ok());
assert!(setup_backup_system(&config, &rollback).is_ok());
}

#[test]
Expand All @@ -31,25 +29,25 @@ fn test_install_backup_tools() {
.times(1)
.returning(|_, _| Ok(()));

assert!(install_backup_tools(&PackageManager::Apt, &mock).is_ok());
assert!(install_backup_tools().is_ok());
}

#[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());
}
// #[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());
// }

#[test]
fn test_setup_backup_locations() {
Expand All @@ -61,5 +59,5 @@ fn test_setup_backup_locations() {

mock.expect_run().times(2).returning(|_, _| Ok(()));

assert!(setup_backup_locations(&config, &mock).is_ok());
assert!(setup_backup_locations(&config).is_ok());
}
15 changes: 10 additions & 5 deletions tests/common/mod.rs → tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use mockall::mock;
use mockall::automock;
use std::error::Error;

mock! {
pub CommandRunner {}
impl CommandRunner {
pub fn run(&self, command: &str, args: &[&str]) -> Result<(), Box<dyn 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(())
}
}

Expand Down
7 changes: 5 additions & 2 deletions tests/config_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ fn test_config_serialization() {
assert_eq!(config.monitoring, deserialized.monitoring);
assert_eq!(config.backup_frequency, deserialized.backup_frequency);
assert_eq!(config.deployed_apps, deserialized.deployed_apps);
assert_eq!(config.custom_firewall_rules, deserialized.custom_firewall_rules);
assert_eq!(
config.custom_firewall_rules,
deserialized.custom_firewall_rules
);
assert_eq!(config.update_schedule, deserialized.update_schedule);
assert_eq!(config.use_containers, deserialized.use_containers);
assert_eq!(config.use_kubernetes, deserialized.use_kubernetes);
}
}
36 changes: 19 additions & 17 deletions tests/containerization_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use common::{CommandRunner, MockConfig, MockRollbackManager};
use server_forge::containerization::{deploy_containers, setup_docker, setup_kubernetes};
use crate::common;

use crate::common::{MockConfig, MockRollbackManager};
use server_forge::containerization::{
configure_docker, configure_kubernetes, deploy_container, deploy_containers, deploy_to_docker,
deploy_to_kubernetes, install_docker, install_kubernetes, setup_docker, setup_kubernetes,
};

#[test]
fn test_setup_docker() {
Expand All @@ -10,7 +12,7 @@ fn test_setup_docker() {

mock.expect_run().times(7).returning(|_, _| Ok(()));

assert!(setup_docker(&rollback, &mock).is_ok());
assert!(setup_docker(&rollback).is_ok());
}

#[test]
Expand All @@ -20,7 +22,7 @@ fn test_setup_kubernetes() {

mock.expect_run().times(6).returning(|_, _| Ok(()));

assert!(setup_kubernetes(&rollback, &mock).is_ok());
assert!(setup_kubernetes(&rollback).is_ok());
}

#[test]
Expand All @@ -35,7 +37,7 @@ fn test_deploy_containers() {

mock.expect_run().times(6).returning(|_, _| Ok(()));

assert!(deploy_containers(&config, &rollback, &mock).is_ok());
assert!(deploy_containers(&config, &rollback).is_ok());
}

#[test]
Expand All @@ -44,7 +46,7 @@ fn test_install_docker() {

mock.expect_run().times(7).returning(|_, _| Ok(()));

assert!(install_docker(&PackageManager::Apt, &mock).is_ok());
assert!(install_docker().is_ok());
}

#[test]
Expand All @@ -53,7 +55,7 @@ fn test_configure_docker() {

mock.expect_run().times(4).returning(|_, _| Ok(()));

assert!(configure_docker(&mock).is_ok());
assert!(configure_docker().is_ok());
}

#[test]
Expand All @@ -62,7 +64,7 @@ fn test_install_kubernetes() {

mock.expect_run().times(5).returning(|_, _| Ok(()));

assert!(install_kubernetes(&PackageManager::Apt, &mock).is_ok());
assert!(install_kubernetes().is_ok());
}

#[test]
Expand All @@ -71,7 +73,7 @@ fn test_configure_kubernetes() {

mock.expect_run().times(3).returning(|_, _| Ok(()));

assert!(configure_kubernetes(&mock).is_ok());
assert!(configure_kubernetes().is_ok());
}

#[test]
Expand All @@ -80,11 +82,11 @@ fn test_deploy_container() {

mock.expect_run().times(3).returning(|_, _| Ok(()));

assert!(deploy_container("test-app", true, &mock).is_ok());
assert!(deploy_container("test-app", true).is_ok());

mock.expect_run().times(4).returning(|_, _| Ok(()));

assert!(deploy_container("test-app", false, &mock).is_ok());
assert!(deploy_container("test-app", false).is_ok());
}

#[test]
Expand All @@ -93,7 +95,7 @@ fn test_deploy_to_kubernetes() {

mock.expect_run().times(3).returning(|_, _| Ok(()));

assert!(deploy_to_kubernetes("test-app", &mock).is_ok());
assert!(deploy_to_kubernetes("test-app").is_ok());
}

#[test]
Expand All @@ -102,7 +104,7 @@ fn test_deploy_to_docker() {

mock.expect_run().times(4).returning(|_, _| Ok(()));

assert!(deploy_to_docker("test-app", &mock).is_ok());
assert!(deploy_to_docker("test-app").is_ok());
}

#[test]
Expand All @@ -119,7 +121,7 @@ fn test_containerization_error_handling() {
mock.expect_run()
.returning(|_, _| Err("Command failed".into()));

assert!(setup_docker(&rollback, &mock).is_err());
assert!(setup_kubernetes(&rollback, &mock).is_err());
assert!(deploy_containers(&config, &rollback, &mock).is_err());
assert!(setup_docker(&rollback).is_err());
assert!(setup_kubernetes(&rollback).is_err());
assert!(deploy_containers(&config, &rollback).is_err());
}
Loading

0 comments on commit 9167761

Please sign in to comment.