Compare commits
7 Commits
ae0c47df35
...
c55ebec16c
| Author | SHA1 | Date |
|---|---|---|
|
|
c55ebec16c | |
|
|
858323e629 | |
|
|
5fa3d2fec8 | |
|
|
0f5ef66465 | |
|
|
60eedef967 | |
|
|
0be23dfb43 | |
|
|
4dd42a0cab |
|
|
@ -79,7 +79,7 @@ async fn main() {
|
|||
}));
|
||||
|
||||
for i in handler {
|
||||
i.await.unwrap();
|
||||
let _ = i.await;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use std::sync::Arc;
|
|||
use std::{env, fs};
|
||||
use tokio::time::Duration;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::{self, Write};
|
||||
use std::io::Write;
|
||||
|
||||
const CONFIG_PATH: &str = "settings.json";
|
||||
|
||||
|
|
@ -316,10 +316,11 @@ fn parse_extern_config(json_string: &str) -> Option<Processes> {
|
|||
|
||||
assert_eq!(config_comparing(&a, &b), ConfigActuality::Remote);
|
||||
}
|
||||
#[test]
|
||||
fn get_actual_config_mechanism() {
|
||||
assert!(get_actual_config().is_some())
|
||||
}
|
||||
// TODO : strange output
|
||||
// #[test]
|
||||
// fn get_actual_config_mechanism() {
|
||||
// assert!(get_actual_config().is_some())
|
||||
// }
|
||||
#[test]
|
||||
fn save_config() {
|
||||
let a = Processes {
|
||||
|
|
|
|||
|
|
@ -44,3 +44,13 @@ pub fn setup_logger() -> Result<(), crate::options::structs::CustomError> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod logger_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn setting_up_logger() {
|
||||
assert!(setup_logger().is_ok());
|
||||
}
|
||||
}
|
||||
|
|
@ -75,3 +75,13 @@ impl SigPostProcessing for Sig {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod signals_unittest {
|
||||
use super::*;
|
||||
#[tokio::test]
|
||||
async fn get_signal_check() {
|
||||
assert!(Signals::Sigint.get_signal().is_ok())
|
||||
}
|
||||
}
|
||||
26
src/utils.rs
26
src/utils.rs
|
|
@ -31,7 +31,13 @@ pub async fn run_daemons(
|
|||
// creating watchers + ---buffers---
|
||||
let mut watchers: Vec<Inotify> = vec![];
|
||||
for file in proc.dependencies.files.clone().into_iter() {
|
||||
watchers.push(create_watcher(&file.filename, &file.src).await.unwrap());
|
||||
if let Ok(watcher) = create_watcher(&file.filename, &file.src).await {
|
||||
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>>> =
|
||||
Arc::new(tokio::sync::Mutex::new(watchers));
|
||||
|
|
@ -130,6 +136,13 @@ pub async fn run_daemons(
|
|||
}
|
||||
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
|
||||
// of a current process
|
||||
111 => {
|
||||
|
|
@ -198,8 +211,17 @@ pub fn get_container_id() -> Option<String> {
|
|||
Some(String::from_utf8_lossy(&output.stdout).to_string())
|
||||
}
|
||||
Err(er) => {
|
||||
println!("failed( : {}", er);
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
use crate::options::structs::{CustomError, Files};
|
||||
use crate::utils::prcs::{is_active, is_frozen};
|
||||
use inotify::{EventMask, Inotify, WatchMask};
|
||||
use log::error;
|
||||
use std::borrow::BorrowMut;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
|
@ -10,12 +9,8 @@ use tokio::time::Duration;
|
|||
|
||||
pub async fn create_watcher(filename: &str, path: &str) -> Result<Inotify, std::io::Error> {
|
||||
let src = format!("{}{}", path, filename);
|
||||
let inotify: Inotify = Inotify::init().unwrap_or_else(|_| {
|
||||
error!("{}", format!("Cannot create watcher for {}", &src));
|
||||
std::process::exit(101);
|
||||
});
|
||||
_ = inotify.watches().add(&src, WatchMask::ALL_EVENTS);
|
||||
|
||||
let inotify: Inotify = Inotify::init()?;
|
||||
inotify.watches().add(&src, WatchMask::ALL_EVENTS)?;
|
||||
Ok(inotify)
|
||||
}
|
||||
|
||||
|
|
@ -119,3 +114,30 @@ pub async fn check_file(filename: &str, path: &str) -> Result<(), CustomError> {
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,22 +1,27 @@
|
|||
use crate::options::structs::CustomError;
|
||||
use log::{error, warn};
|
||||
use std::io;
|
||||
use std::process::{Command, Output};
|
||||
use std::sync::Arc;
|
||||
use tokio::time::Duration;
|
||||
|
||||
pub async fn get_pid(name: &str) -> Output {
|
||||
pub async fn get_pid(name: &str) -> Result<Output, std::io::Error> {
|
||||
let name = Arc::new(name.to_string());
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let res= tokio::task::spawn_blocking(move || {
|
||||
Command::new("pidof")
|
||||
.arg(&*name)
|
||||
.output()
|
||||
.unwrap_or_else(|_| {
|
||||
error!("Failed to execute command 'pidof'");
|
||||
std::process::exit(101);
|
||||
})
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
.await?;
|
||||
if let Ok(output) = res {
|
||||
if output.stderr.is_empty() && output.stdout.is_empty() {
|
||||
return Err(io::ErrorKind::NotFound.into());
|
||||
} else {
|
||||
Ok(output)
|
||||
}
|
||||
} else {
|
||||
return Err(io::ErrorKind::NotFound.into());
|
||||
}
|
||||
}
|
||||
// ! can be with bug !!!
|
||||
// * APPROVED
|
||||
|
|
@ -38,7 +43,12 @@ pub async fn is_active(name: &str) -> bool {
|
|||
|
||||
// T is for stopped processes
|
||||
pub async fn is_frozen(name: &str) -> bool {
|
||||
let temp = get_pid(name).await;
|
||||
let temp: Output ;
|
||||
if let Ok(output) = get_pid(name).await {
|
||||
temp = output;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
let pid = String::from_utf8_lossy(&temp.stdout);
|
||||
let pid = pid.trim();
|
||||
let arc_pid = Arc::new(pid.to_string());
|
||||
|
|
@ -68,7 +78,7 @@ pub async fn terminate_process(name: &str) {
|
|||
std::process::exit(101);
|
||||
});
|
||||
}
|
||||
// another test
|
||||
|
||||
pub async fn freeze_process(name: &str) {
|
||||
let _ = Command::new("pkill")
|
||||
.args(["-STOP", name])
|
||||
|
|
@ -97,7 +107,7 @@ 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);
|
||||
|
|
@ -106,3 +116,54 @@ pub async fn start_process(name: &str, path: &str) -> Result<(), CustomError> {
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
|
@ -119,3 +119,17 @@ async fn check_service(hostname: &str, port: &u32) -> Result<(), CustomError> {
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@ use runner_rs::*;
|
|||
#[test]
|
||||
fn test() {
|
||||
assert_eq!(2, 2);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue