diff --git a/src/options/config.rs b/src/options/config.rs index e0867c7..3cc8e64 100644 --- a/src/options/config.rs +++ b/src/options/config.rs @@ -1,13 +1,13 @@ use crate::options::structs::*; use log::{error, info, warn}; use redis::{Client, Commands, Connection, RedisResult}; +use std::fs::OpenOptions; +use std::io::Write; use std::os::unix::process::CommandExt; use std::process::Command; use std::sync::Arc; use std::{env, fs}; use tokio::time::Duration; -use std::fs::OpenOptions; -use std::io::Write; const CONFIG_PATH: &str = "settings.json"; @@ -258,21 +258,21 @@ fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomEr Ok(st) => { let file = OpenOptions::new() .read(true) - .write(true) - .create(true) - .truncate(false) - .open(config_file); + .write(true) + .create(true) + .truncate(false) + .open(config_file); match file { Ok(fs) => { let mut writer = fs; match writeln!(writer, "{}", st) { Ok(_) => Ok(()), - Err(_) => Err(CustomError::Fatal), + Err(_) => Err(CustomError::Fatal), } - }, - Err(_) => return Err(CustomError::Fatal) + } + Err(_) => return Err(CustomError::Fatal), } - }, + } Err(_) => Err(CustomError::Fatal), } } @@ -284,12 +284,9 @@ fn parse_extern_config(json_string: &str) -> Option { None } - - - // unit tests #[cfg(test)] - mod config_unittests { +mod config_unittests { use super::*; #[test] fn parsing_valid_conf() { @@ -316,7 +313,7 @@ fn parse_extern_config(json_string: &str) -> Option { assert_eq!(config_comparing(&a, &b), ConfigActuality::Remote); } - // TODO : strange output + // TODO : strange output // #[test] // fn get_actual_config_mechanism() { // assert!(get_actual_config().is_some()) @@ -340,4 +337,4 @@ fn parse_extern_config(json_string: &str) -> Option { }; assert!(save_new_config(&a, "tests/examples/none.json").is_ok()); } - } \ No newline at end of file +} diff --git a/src/options/logger.rs b/src/options/logger.rs index 8ed0ca3..ce44420 100644 --- a/src/options/logger.rs +++ b/src/options/logger.rs @@ -45,7 +45,6 @@ pub fn setup_logger() -> Result<(), crate::options::structs::CustomError> { Ok(()) } - #[cfg(test)] mod logger_tests { use super::*; @@ -53,4 +52,4 @@ mod logger_tests { fn setting_up_logger() { assert!(setup_logger().is_ok()); } -} \ No newline at end of file +} diff --git a/src/options/signals.rs b/src/options/signals.rs index 1e6d5df..7cdc614 100644 --- a/src/options/signals.rs +++ b/src/options/signals.rs @@ -76,7 +76,6 @@ impl SigPostProcessing for Sig { } } - #[cfg(test)] mod signals_unittest { use super::*; @@ -84,4 +83,4 @@ mod signals_unittest { async fn get_signal_check() { assert!(Signals::Sigint.get_signal().is_ok()) } -} \ No newline at end of file +} diff --git a/src/options/structs.rs b/src/options/structs.rs index 7e56077..5687443 100644 --- a/src/options/structs.rs +++ b/src/options/structs.rs @@ -3,7 +3,6 @@ use serde::{Deserialize, Serialize}; /// # an Error enum (next will be deleted and replaced) pub enum CustomError { Fatal, - } #[derive(Debug, PartialEq)] pub enum ConfigActuality { diff --git a/src/utils.rs b/src/utils.rs index 556854e..9c7e92a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,8 +1,8 @@ pub mod files; -pub mod prcs; -pub mod services; pub mod hagent; pub mod metrics; +pub mod prcs; +pub mod services; use crate::options::structs::TrackingProcess; use files::create_watcher; @@ -34,9 +34,8 @@ pub async fn run_daemons( let mut watchers: Vec = vec![]; for file in proc.dependencies.files.clone().into_iter() { if let Ok(watcher) = create_watcher(&file.filename, &file.src).await { - watchers.push(watcher); - } - else { + watchers.push(watcher); + } else { let _ = tx.send(121).await; } // watchers.push(create_watcher(&file.filename, &file.src).await.unwrap()); @@ -141,7 +140,7 @@ pub async fn run_daemons( // // 121 - Cannot create valid watcher for file dependency 121 => { - error!("Cannot create valid watcher for {}'s file dependency. Terminating...", proc.name); + error!("Cannot create valid watcher for {}'s file dependency. Terminating thread...", proc.name); let _ = terminate_process("runner-rs").await; break; }, @@ -212,13 +211,10 @@ pub fn get_container_id() -> Option { } Some(String::from_utf8_lossy(&output.stdout).to_string()) } - Err(_) => { - None - } + Err(_) => None, } } - #[cfg(test)] mod utils_unittests { use super::get_container_id; diff --git a/src/utils/files.rs b/src/utils/files.rs index 0f1b33c..4221da9 100644 --- a/src/utils/files.rs +++ b/src/utils/files.rs @@ -115,8 +115,6 @@ pub async fn check_file(filename: &str, path: &str) -> Result<(), CustomError> { }) } - - #[cfg(test)] mod files_unittests { use super::*; @@ -140,4 +138,4 @@ mod files_unittests { let res = check_file("invalid-file", "/path/to/the/hell").await; assert!(res.is_err()); } -} \ No newline at end of file +} diff --git a/src/utils/hagent.rs b/src/utils/hagent.rs index c4138e5..9ae224b 100644 --- a/src/utils/hagent.rs +++ b/src/utils/hagent.rs @@ -1,26 +1,25 @@ -// module needed to check host-agent health condition and to communicate with it -use tokio::net::UnixStream; +// module needed to check host-agent health condition and to communicate with it use crate::options::structs::CustomError; -// +use tokio::net::UnixStream; +// // code will be here -// +// async fn open_unix_socket() -> Result { let socket = UnixStream::connect("/var/run/runner-rs.sock").await?; Ok(socket) } - #[cfg(test)] mod hagent_unittets { use super::*; #[tokio::test] - // maybe bool : true -> alive, false -> dead - // simple request on api + // maybe bool : true -> alive, false -> dead + // simple request on api async fn hagent_healthcheck() { assert!(true); } #[tokio::test] - // Result + // Result // one-shot func async fn send_metrics_to_hagent() { assert!(true); @@ -29,4 +28,4 @@ mod hagent_unittets { async fn open_unixsocket_test() { assert!(open_unix_socket().await.is_ok()); } -} \ No newline at end of file +} diff --git a/src/utils/metrics.rs b/src/utils/metrics.rs index 6d0d1e9..9805273 100644 --- a/src/utils/metrics.rs +++ b/src/utils/metrics.rs @@ -1,10 +1,9 @@ // module needed to get metrics such as // cpu load, memmory load and net activity -// +// // code will be here -// - +// #[cfg(test)] mod metrics_unittets { @@ -13,23 +12,23 @@ mod metrics_unittets { // echo func async fn get_cpu_load() { assert!(true); - } + } #[tokio::test] // Option output // echo func async fn get_ram_load() { assert!(true); - } + } #[tokio::test] // Option output // echo func async fn get_mem_load() { assert!(true); - } + } #[tokio::test] // can't be tested this way because of 0-0 loop of checking buffer - // async func with loop inside - async fn get_net_stat(){ + // async func with loop inside + async fn get_net_stat() { assert!(true); } #[tokio::test] @@ -42,4 +41,4 @@ mod metrics_unittets { async fn get_info_about_subsystems() { assert!(true); } -} \ No newline at end of file +} diff --git a/src/utils/prcs.rs b/src/utils/prcs.rs index 523d00b..87e8cbc 100644 --- a/src/utils/prcs.rs +++ b/src/utils/prcs.rs @@ -7,15 +7,11 @@ use tokio::time::Duration; pub async fn get_pid(name: &str) -> Result { let name = Arc::new(name.to_string()); - let res= tokio::task::spawn_blocking(move || { - Command::new("pidof") - .arg(&*name) - .output() - }) - .await?; + let res = + tokio::task::spawn_blocking(move || Command::new("pidof").arg(&*name).output()).await?; if let Ok(output) = res { if output.stderr.is_empty() && output.stdout.is_empty() { - return Err(io::ErrorKind::NotFound.into()); + return Err(io::ErrorKind::NotFound.into()); } else { Ok(output) } @@ -43,7 +39,7 @@ pub async fn is_active(name: &str) -> bool { // T is for stopped processes pub async fn is_frozen(name: &str) -> bool { - let temp: Output ; + let temp: Output; if let Ok(output) = get_pid(name).await { temp = output; } else { @@ -107,39 +103,42 @@ pub async fn start_process(name: &str, path: &str) -> Result<(), CustomError> { // let runsh = format!("{} {}", "exec", path); let mut command = Command::new(path); // command.arg(path); - + match command.spawn() { Ok(_) => { warn!("Process {} is running now!", name); Ok(()) } - Err(er) => {println!("{:?}", er); Err(CustomError::Fatal)}, + Err(er) => { + println!("{:?}", er); + Err(CustomError::Fatal) + } } } - #[cfg(test)] mod process_unittests { use super::*; // 1 full cycle - start -> restart -> stop // 2 full cycle - start -> freeze -> unfreze -> stop - // 2x is active + // 2x is active // 2x is frozen - // 2x pidof + // 2x pidof - // rewrite, its a pipe + // rewrite, its a pipe #[tokio::test] async fn full_cycle_with_restart() { assert!(true); let res1 = start_process("temp-process", "/home/user/monitor/runner-rs/temp-process").await; assert!(res1.is_ok()); - let res2 = restart_process("temp-process", "/home/user/monitor/runner-rs/temp-process").await; + let res2 = + restart_process("temp-process", "/home/user/monitor/runner-rs/temp-process").await; assert!(res2.is_ok()); let _ = terminate_process("temp-process").await; let res3 = is_active("temp-process").await; assert!(res3); } - // rewrite, its a pipe + // rewrite, its a pipe #[tokio::test] async fn full_cycle_with_freeze() { assert!(true); @@ -166,4 +165,4 @@ mod process_unittests { async fn pidof_disabled_process() { assert!(get_pid("invalid-process-name").await.is_err()); } -} \ No newline at end of file +} diff --git a/src/utils/services.rs b/src/utils/services.rs index 0ea3f63..234be50 100644 --- a/src/utils/services.rs +++ b/src/utils/services.rs @@ -120,7 +120,6 @@ async fn check_service(hostname: &str, port: &u32) -> Result<(), CustomError> { } } - #[cfg(test)] mod service_unittests { use super::check_service; @@ -132,4 +131,4 @@ mod service_unittests { async fn check_unavailable_service() { assert!(check_service("unavailable.service", &1111).await.is_err()); } -} \ No newline at end of file +} diff --git a/tests/start_stop.rs b/tests/start_stop.rs index 56629d5..7f44a02 100644 --- a/tests/start_stop.rs +++ b/tests/start_stop.rs @@ -3,4 +3,4 @@ #[test] fn test() { assert_eq!(2, 2); -} \ No newline at end of file +}