Compare commits

..

No commits in common. "c55ebec16c8e37bec15b9d78c2297f00e8b83529" and "ae0c47df353debee64f0dfd44ace682fd8f60348" have entirely different histories.

9 changed files with 27 additions and 167 deletions

View File

@ -79,7 +79,7 @@ async fn main() {
})); }));
for i in handler { for i in handler {
let _ = i.await; i.await.unwrap();
} }
return; return;
} }

View File

@ -7,7 +7,7 @@ use std::sync::Arc;
use std::{env, fs}; use std::{env, fs};
use tokio::time::Duration; use tokio::time::Duration;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Write; use std::io::{self, Write};
const CONFIG_PATH: &str = "settings.json"; const CONFIG_PATH: &str = "settings.json";
@ -316,11 +316,10 @@ fn parse_extern_config(json_string: &str) -> Option<Processes> {
assert_eq!(config_comparing(&a, &b), ConfigActuality::Remote); assert_eq!(config_comparing(&a, &b), ConfigActuality::Remote);
} }
// TODO : strange output #[test]
// #[test] fn get_actual_config_mechanism() {
// fn get_actual_config_mechanism() { assert!(get_actual_config().is_some())
// assert!(get_actual_config().is_some()) }
// }
#[test] #[test]
fn save_config() { fn save_config() {
let a = Processes { let a = Processes {

View File

@ -44,13 +44,3 @@ pub fn setup_logger() -> Result<(), crate::options::structs::CustomError> {
Ok(()) Ok(())
} }
#[cfg(test)]
mod logger_tests {
use super::*;
#[test]
fn setting_up_logger() {
assert!(setup_logger().is_ok());
}
}

View File

@ -75,13 +75,3 @@ impl SigPostProcessing for Sig {
Ok(()) Ok(())
} }
} }
#[cfg(test)]
mod signals_unittest {
use super::*;
#[tokio::test]
async fn get_signal_check() {
assert!(Signals::Sigint.get_signal().is_ok())
}
}

View File

@ -31,13 +31,7 @@ pub async fn run_daemons(
// creating watchers + ---buffers--- // creating watchers + ---buffers---
let mut watchers: Vec<Inotify> = vec![]; let mut watchers: Vec<Inotify> = vec![];
for file in proc.dependencies.files.clone().into_iter() { for file in proc.dependencies.files.clone().into_iter() {
if let Ok(watcher) = create_watcher(&file.filename, &file.src).await { watchers.push(create_watcher(&file.filename, &file.src).await.unwrap());
watchers.push(watcher);
}
else {
let _ = tx.send(121).await;
}
// watchers.push(create_watcher(&file.filename, &file.src).await.unwrap());
} }
let watchers_clone: Arc<tokio::sync::Mutex<Vec<Inotify>>> = let watchers_clone: Arc<tokio::sync::Mutex<Vec<Inotify>>> =
Arc::new(tokio::sync::Mutex::new(watchers)); Arc::new(tokio::sync::Mutex::new(watchers));
@ -136,13 +130,6 @@ pub async fn run_daemons(
} }
break; break;
}, },
//
// 121 - Cannot create valid watcher for file dependency
121 => {
error!("Cannot create valid watcher for {}'s file dependency. Terminating...", proc.name);
let _ = terminate_process("runner-rs").await;
break;
},
// 111 - global thread termination with killing current child in a face // 111 - global thread termination with killing current child in a face
// of a current process // of a current process
111 => { 111 => {
@ -211,17 +198,8 @@ pub fn get_container_id() -> Option<String> {
Some(String::from_utf8_lossy(&output.stdout).to_string()) Some(String::from_utf8_lossy(&output.stdout).to_string())
} }
Err(er) => { Err(er) => {
println!("failed( : {}", er);
None None
} }
} }
} }
#[cfg(test)]
mod utils_unittests {
use super::get_container_id;
#[test]
fn check_if_container_id_can_be_grabed() {
assert!(get_container_id().is_some());
}
}

View File

@ -1,6 +1,7 @@
use crate::options::structs::{CustomError, Files}; use crate::options::structs::{CustomError, Files};
use crate::utils::prcs::{is_active, is_frozen}; use crate::utils::prcs::{is_active, is_frozen};
use inotify::{EventMask, Inotify, WatchMask}; use inotify::{EventMask, Inotify, WatchMask};
use log::error;
use std::borrow::BorrowMut; use std::borrow::BorrowMut;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
@ -9,8 +10,12 @@ use tokio::time::Duration;
pub async fn create_watcher(filename: &str, path: &str) -> Result<Inotify, std::io::Error> { pub async fn create_watcher(filename: &str, path: &str) -> Result<Inotify, std::io::Error> {
let src = format!("{}{}", path, filename); let src = format!("{}{}", path, filename);
let inotify: Inotify = Inotify::init()?; let inotify: Inotify = Inotify::init().unwrap_or_else(|_| {
inotify.watches().add(&src, WatchMask::ALL_EVENTS)?; error!("{}", format!("Cannot create watcher for {}", &src));
std::process::exit(101);
});
_ = inotify.watches().add(&src, WatchMask::ALL_EVENTS);
Ok(inotify) Ok(inotify)
} }
@ -114,30 +119,3 @@ pub async fn check_file(filename: &str, path: &str) -> Result<(), CustomError> {
panic!("Corrupted while file check process"); panic!("Corrupted while file check process");
}) })
} }
#[cfg(test)]
mod files_unittests {
use super::*;
#[tokio::test]
async fn try_to_create_watcher() {
let res = create_watcher("dep-file", "/home/user/monitor/runner-rs/tests/examples/").await;
assert!(res.is_ok());
}
#[tokio::test]
async fn try_to_create_invalid_watcher() {
let res = create_watcher("invalid-file", "/path/to/the/hell").await;
assert!(res.is_err());
}
#[tokio::test]
async fn check_existing_file() {
let res = check_file("dep-file", "/home/user/monitor/runner-rs/tests/examples/").await;
assert!(res.is_ok());
}
#[tokio::test]
async fn check_non_existing_file() {
let res = check_file("invalid-file", "/path/to/the/hell").await;
assert!(res.is_err());
}
}

View File

@ -1,27 +1,22 @@
use crate::options::structs::CustomError; use crate::options::structs::CustomError;
use log::{error, warn}; use log::{error, warn};
use std::io;
use std::process::{Command, Output}; use std::process::{Command, Output};
use std::sync::Arc; use std::sync::Arc;
use tokio::time::Duration; use tokio::time::Duration;
pub async fn get_pid(name: &str) -> Result<Output, std::io::Error> { pub async fn get_pid(name: &str) -> Output {
let name = Arc::new(name.to_string()); let name = Arc::new(name.to_string());
let res= tokio::task::spawn_blocking(move || { tokio::task::spawn_blocking(move || {
Command::new("pidof") Command::new("pidof")
.arg(&*name) .arg(&*name)
.output() .output()
.unwrap_or_else(|_| {
error!("Failed to execute command 'pidof'");
std::process::exit(101);
}) })
.await?; })
if let Ok(output) = res { .await
if output.stderr.is_empty() && output.stdout.is_empty() { .unwrap()
return Err(io::ErrorKind::NotFound.into());
} else {
Ok(output)
}
} else {
return Err(io::ErrorKind::NotFound.into());
}
} }
// ! can be with bug !!! // ! can be with bug !!!
// * APPROVED // * APPROVED
@ -43,12 +38,7 @@ pub async fn is_active(name: &str) -> bool {
// T is for stopped processes // T is for stopped processes
pub async fn is_frozen(name: &str) -> bool { pub async fn is_frozen(name: &str) -> bool {
let temp: Output ; let temp = get_pid(name).await;
if let Ok(output) = get_pid(name).await {
temp = output;
} else {
return false;
}
let pid = String::from_utf8_lossy(&temp.stdout); let pid = String::from_utf8_lossy(&temp.stdout);
let pid = pid.trim(); let pid = pid.trim();
let arc_pid = Arc::new(pid.to_string()); let arc_pid = Arc::new(pid.to_string());
@ -78,7 +68,7 @@ pub async fn terminate_process(name: &str) {
std::process::exit(101); std::process::exit(101);
}); });
} }
// another test
pub async fn freeze_process(name: &str) { pub async fn freeze_process(name: &str) {
let _ = Command::new("pkill") let _ = Command::new("pkill")
.args(["-STOP", name]) .args(["-STOP", name])
@ -116,54 +106,3 @@ pub async fn start_process(name: &str, path: &str) -> Result<(), CustomError> {
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 frozen
// 2x pidof
// 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;
assert!(res2.is_ok());
let _ = terminate_process("temp-process").await;
let res3 = is_active("temp-process").await;
assert!(res3);
}
// rewrite, its a pipe
#[tokio::test]
async fn full_cycle_with_freeze() {
assert!(true);
}
#[tokio::test]
async fn is_active_check() {
assert!(is_active("systemd").await);
}
#[tokio::test]
async fn isnt_active_check() {
assert!(!is_active("invalid-process-name").await);
}
#[tokio::test]
async fn is_frozen_check() {
assert!(!is_frozen("systemd").await);
}
#[tokio::test]
async fn pidof_active_process() {
assert!(get_pid("systemd").await.is_ok());
}
// broken mechanism need to check
#[tokio::test]
async fn pidof_disabled_process() {
assert!(get_pid("invalid-process-name").await.is_err());
}
}

View File

@ -119,17 +119,3 @@ async fn check_service(hostname: &str, port: &u32) -> Result<(), CustomError> {
Err(_) => Err(CustomError::Fatal), Err(_) => Err(CustomError::Fatal),
} }
} }
#[cfg(test)]
mod service_unittests {
use super::check_service;
#[tokio::test]
async fn check_available_service() {
assert!(check_service("ya.ru", &443).await.is_ok());
}
#[tokio::test]
async fn check_unavailable_service() {
assert!(check_service("unavailable.service", &1111).await.is_err());
}
}