From 0f5ef664650e036136e635119581fca9ebb86027 Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 30 Oct 2024 16:17:06 +0300 Subject: [PATCH] processes unittests --- src/utils/prcs.rs | 71 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 9 deletions(-) diff --git a/src/utils/prcs.rs b/src/utils/prcs.rs index 37384c1..750049d 100644 --- a/src/utils/prcs.rs +++ b/src/utils/prcs.rs @@ -4,19 +4,18 @@ 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 { let name = Arc::new(name.to_string()); tokio::task::spawn_blocking(move || { Command::new("pidof") .arg(&*name) .output() - .unwrap_or_else(|_| { - error!("Failed to execute command 'pidof'"); - std::process::exit(101); - }) + // .unwrap_or_else(|_| { + // error!("Failed to execute command 'pidof'"); + // std::process::exit(101); + // }) }) - .await - .unwrap() + .await? } // ! can be with bug !!! // * APPROVED @@ -38,7 +37,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 +72,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]) @@ -106,3 +110,52 @@ 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()); + } + #[tokio::test] + async fn pidof_disabled_process() { + assert!(get_pid("invalid-process-name").await.is_ok()); + } +} \ No newline at end of file