processes unittests
parent
60eedef967
commit
0f5ef66465
|
|
@ -4,19 +4,18 @@ 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) -> Output {
|
pub async fn get_pid(name: &str) -> Result<Output, std::io::Error> {
|
||||||
let name = Arc::new(name.to_string());
|
let name = Arc::new(name.to_string());
|
||||||
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(|_| {
|
// .unwrap_or_else(|_| {
|
||||||
error!("Failed to execute command 'pidof'");
|
// error!("Failed to execute command 'pidof'");
|
||||||
std::process::exit(101);
|
// std::process::exit(101);
|
||||||
})
|
// })
|
||||||
})
|
})
|
||||||
.await
|
.await?
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
// ! can be with bug !!!
|
// ! can be with bug !!!
|
||||||
// * APPROVED
|
// * APPROVED
|
||||||
|
|
@ -38,7 +37,12 @@ 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 = 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 = 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());
|
||||||
|
|
@ -68,7 +72,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])
|
||||||
|
|
@ -106,3 +110,52 @@ 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());
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn pidof_disabled_process() {
|
||||||
|
assert!(get_pid("invalid-process-name").await.is_ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue