From 591a1df5d67d7fc8ce1b53f3a9ae6a47cd652c98 Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 3 Dec 2024 12:53:19 +0300 Subject: [PATCH 1/8] preboot settings added --- Cargo.toml | 1 + src/main.rs | 7 ++++++ src/options.rs | 1 + src/options/preboot.rs | 10 ++++++++ src/options/structs.rs | 52 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+) create mode 100644 src/options/preboot.rs diff --git a/Cargo.toml b/Cargo.toml index 3e7d020..4f6b3d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ debug = false [dependencies] anyhow = "1.0.93" chrono = "0.4.38" +clap = { version = "4.5.21", features = ["derive"] } env_logger = "0.11.3" inotify = "0.10.2" log = "0.4.22" diff --git a/src/main.rs b/src/main.rs index 6f161d0..3375948 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod options; mod utils; +use clap::Parser; use log::{error, info}; use options::config::*; use options::logger::setup_logger; @@ -10,9 +11,15 @@ use std::sync::Arc; use std::time::Duration; use tokio::sync::mpsc; use utils::*; +use options::preboot::*; #[tokio::main(flavor = "multi_thread")] async fn main() { + use options::structs::PrebootParams; + let a = PrebootParams::parse(); + println!("{:?}", a); + + let _ = setup_logger(); info!("Runner is configurating..."); diff --git a/src/options.rs b/src/options.rs index d9f9b03..0b4b3cf 100644 --- a/src/options.rs +++ b/src/options.rs @@ -4,3 +4,4 @@ pub mod config; pub mod logger; pub mod signals; pub mod structs; +pub mod preboot; \ No newline at end of file diff --git a/src/options/preboot.rs b/src/options/preboot.rs new file mode 100644 index 0000000..bc347b9 --- /dev/null +++ b/src/options/preboot.rs @@ -0,0 +1,10 @@ +// module to handle pre-boot params of the monitor +use super::structs::PrebootParams; + + + + + + + +// unit tests of prebot params parsing mech \ No newline at end of file diff --git a/src/options/structs.rs b/src/options/structs.rs index 40957c5..405d7d2 100644 --- a/src/options/structs.rs +++ b/src/options/structs.rs @@ -2,11 +2,63 @@ use std::net::Ipv4Addr; use serde::{Deserialize, Serialize}; +use clap::Parser; /// # an Error enum (next will be deleted and replaced) pub enum CustomError { Fatal, } + +#[derive(clap::ValueEnum, Debug, Clone)] +enum MetricsPrebootParams { + Full, + System, + Processes, + Net, + None, +} + +impl std::fmt::Display for MetricsPrebootParams { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + MetricsPrebootParams::Full => write!(f, "full"), + MetricsPrebootParams::System => write!(f, "system"), + MetricsPrebootParams::Processes => write!(f, "processes"), + MetricsPrebootParams::Net => write!(f, "net"), + MetricsPrebootParams::None => write!(f, "none"), + } + } +} + +#[derive(Debug, Parser)] +pub struct PrebootParams { + // actions + #[arg(long = "no-hagent", action, conflicts_with="socket_path", help="To disable work with host-agent")] + no_hostagent : bool, + #[arg(long = "no-logs", action, conflicts_with="log_to", help="To disable logs")] + no_logs: bool, + #[arg(long = "refresh-logs", action, conflicts_with="no_logs", help="To clear logs directory")] + refresh_logs : bool, + #[arg(long = "no-remote-config", action, help="To disable work with remote config server", conflicts_with="no_sub")] + no_remote_config : bool, + #[arg(long = "no-sub", action, help="To disable subscription mechanism", conflicts_with="no_remote_config")] + no_sub : bool, + + // params (socket_path, log_to, remote_server_url, config) + #[arg(long = "socket-path", default_value=None, conflicts_with="no_hostagent", help="To set .sock file's path used in communication with host-agent")] + socket_path : Option, + #[arg(long = "log-to", default_value=None, conflicts_with="no_logs", help="To set a path to logs directory")] + log_to : Option, + #[arg(long = "remote-server-url", default_value="redis://localhost", conflicts_with="no_remote_config", help = "To set url of remote config server using in remote config pulling mechanism")] + remote_server_url : String, + #[arg(long = "config", short, default_value="settings.json", help="To set local config file path")] + config : String, + + // value enum params (metrics) + #[arg(long = "metrics", short, default_value_t=MetricsPrebootParams::Full, help="To set metrics grubbing mode")] + metrics: MetricsPrebootParams, +} + #[derive(Debug, PartialEq)] pub enum ConfigActuality { Local, -- 2.40.1 From ba395543e4910e42502aa1058a89ba9abe26d827 Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 3 Dec 2024 13:06:17 +0300 Subject: [PATCH 2/8] no warnings --- src/utils/hagent.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/utils/hagent.rs b/src/utils/hagent.rs index 06cb173..068dbeb 100644 --- a/src/utils/hagent.rs +++ b/src/utils/hagent.rs @@ -1,6 +1,10 @@ +// // module needed to check host-agent health condition and to communicate with it +// use tokio::{io::Interest, net::UnixStream}; use anyhow::{Ok, Result, Error}; +// to kill lint bug +#[allow(unused_imports)] use tokio::net::UnixListener; /// # Fn `open_unix_socket` @@ -16,6 +20,7 @@ use tokio::net::UnixListener; /// /// *depends on* : - /// +#[allow(dead_code)] async fn open_unix_socket(sock_path: &str) -> Result { // "/var/run/enode/hostagent.sock" UnixStream::connect(sock_path).await @@ -34,6 +39,7 @@ async fn open_unix_socket(sock_path: &str) -> Result /// /// *depends on* : - /// +#[allow(dead_code)] async fn ha_healthcheck(socket: &UnixStream) -> Result<(), Error> { socket.ready(Interest::WRITABLE).await?; socket.writable().await?; @@ -54,6 +60,7 @@ async fn ha_healthcheck(socket: &UnixStream) -> Result<(), Error> { /// /// *depends on* : - /// +#[allow(dead_code)] async fn ha_send_data(socket: &UnixStream, data: &str) -> Result<(), Error > { socket.ready(Interest::WRITABLE).await?; socket.writable().await?; @@ -74,10 +81,10 @@ mod hagent_unittets { // maybe bool : true -> alive, false -> dead // simple request on api async fn hagent_healthcheck() { - let mut list = init_listener().await; + let _ = init_listener().await; let sock = open_unix_socket(TEST_SOCKET).await; assert!(sock.is_ok()); - let mut sock = sock.unwrap(); + let sock = sock.unwrap(); assert!(ha_healthcheck(&sock).await.is_ok()); } #[tokio::test] @@ -91,17 +98,17 @@ mod hagent_unittets { let metrics = Metrics::new(contm, vec![procm]); let metrics = &serde_json::to_string_pretty(&metrics).unwrap(); - let mut list = init_listener().await; + let _ = init_listener().await; let sock = open_unix_socket(TEST_SOCKET).await; assert!(sock.is_ok()); - let mut sock = sock.unwrap(); + let sock = sock.unwrap(); assert!(ha_healthcheck(&sock).await.is_ok()); assert!(ha_send_data(&sock, &metrics).await.is_ok()); } #[tokio::test] async fn open_unixsocket_test() { - let mut list = init_listener().await; + let _ = init_listener().await; assert!(open_unix_socket(TEST_SOCKET).await.is_ok()); } } -- 2.40.1 From 345abde7415eaa6b3ae35262732dd4bfd49fa4bf Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 3 Dec 2024 13:09:02 +0300 Subject: [PATCH 3/8] no metrics warnings --- src/utils/metrics.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/utils/metrics.rs b/src/utils/metrics.rs index 923106a..0648a3e 100644 --- a/src/utils/metrics.rs +++ b/src/utils/metrics.rs @@ -27,6 +27,7 @@ use crate::utils::get_container_id; /// /// *depends on* : - /// +#[allow(dead_code)] pub async fn init_metrics_grubber() { let mut system = System::new(); // let mut buffer: Vec = vec![]; @@ -39,6 +40,8 @@ pub async fn init_metrics_grubber() { // let _ = capture_packets(shared_buf.clone()).await; } +#[allow(dead_code)] +#[allow(unused_variables)] async fn gather_metrics(proc: Arc) { } @@ -92,6 +95,7 @@ async fn gather_metrics(proc: Arc) { /// /// *depends on* : `TrackingProcess` /// +#[allow(dead_code)] async fn get_all_container_metrics(sys: Arc, prcs: Arc>) -> ContainerMetrics { let metrics = join!( get_cpu_metrics_container(sys.clone()), @@ -119,6 +123,7 @@ async fn get_all_container_metrics(sys: Arc, prcs: Arc) -> f32 { sys.global_cpu_usage() } @@ -136,6 +141,7 @@ async fn get_cpu_metrics_container(sys: Arc) -> f32 { /// /// *depends on* : - /// +#[allow(dead_code)] async fn get_ram_metrics_container(sys: Arc) -> f32 { (sys.used_memory() / sys.total_memory()) as f32 * 100.0 } @@ -156,6 +162,7 @@ async fn get_ram_metrics_container(sys: Arc) -> f32 { /// /// *depends on* : `TrackingProcess` /// +#[allow(dead_code)] async fn get_subsystems(prcs: Arc>) -> Vec { prcs.iter().map(|process| process.name.clone()).collect() } @@ -173,6 +180,7 @@ async fn get_subsystems(prcs: Arc>) -> Vec { /// /// *depends on* : - /// +#[allow(dead_code)] async fn get_all_metrics_process(proc: Arc, sys: Arc) -> ProcessMetrics { let metrics = join!( get_cpu_metrics_process(proc.clone()), -- 2.40.1 From 8a47ffe8512eae1e0615fc46e9fb1b509ed1f343 Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 3 Dec 2024 13:09:59 +0300 Subject: [PATCH 4/8] prcs refactor --- src/utils/prcs.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/prcs.rs b/src/utils/prcs.rs index 3a68b56..a434c43 100644 --- a/src/utils/prcs.rs +++ b/src/utils/prcs.rs @@ -223,7 +223,6 @@ pub async fn start_process(name: &str, path: &str) -> Result<(), CustomError> { #[cfg(test)] mod process_unittests { - use std::io::Write; use super::*; // 1 full cycle - start -> restart -> stop // 2 full cycle - start -> freeze -> unfreze -> stop -- 2.40.1 From 74e3b3ab31dce14ca4baf7f9f8946bdf2522e4bd Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 3 Dec 2024 13:11:19 +0300 Subject: [PATCH 5/8] no warnings --- src/main.rs | 10 +++++----- src/options/preboot.rs | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3375948..4590956 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,18 +6,18 @@ use log::{error, info}; use options::config::*; use options::logger::setup_logger; use options::signals::set_valid_destructor; -use options::structs::*; +// use options::structs::*; +use options::structs::{PrebootParams, Processes}; use std::sync::Arc; use std::time::Duration; use tokio::sync::mpsc; use utils::*; -use options::preboot::*; +#[allow(unused_imports)] +use options::preboot::validate_preboot_params; #[tokio::main(flavor = "multi_thread")] async fn main() { - use options::structs::PrebootParams; - let a = PrebootParams::parse(); - println!("{:?}", a); + let _preboot = PrebootParams::parse(); let _ = setup_logger(); diff --git a/src/options/preboot.rs b/src/options/preboot.rs index bc347b9..b50baf6 100644 --- a/src/options/preboot.rs +++ b/src/options/preboot.rs @@ -1,7 +1,11 @@ // module to handle pre-boot params of the monitor use super::structs::PrebootParams; +use anyhow::{Result, Ok}; - +#[allow(dead_code)] +pub fn validate_preboot_params(_cli: &PrebootParams) -> Result<()> { + Ok(()) +} -- 2.40.1 From dbb49de09cec957954ba62d7775d43ad2e1a4331 Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 3 Dec 2024 15:25:24 +0300 Subject: [PATCH 6/8] preboot unitetst + refactor --- Cargo.toml | 3 +- settings.json | 4 +- src/main.rs | 8 +-- src/options/preboot.rs | 142 +++++++++++++++++++++++++++++++++++++++-- src/options/structs.rs | 51 --------------- 5 files changed, 141 insertions(+), 67 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4f6b3d7..a2a0f2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runner-rs" -version = "0.9.25" +version = "0.10.9" edition = "2021" [profile.dev] @@ -9,7 +9,6 @@ debug = true [profile.test] debug = false - [dependencies] anyhow = "1.0.93" chrono = "0.4.38" diff --git a/settings.json b/settings.json index d9ed0ab..8fb8d10 100644 --- a/settings.json +++ b/settings.json @@ -4,12 +4,12 @@ "processes": [ { "name": "temp-process", - "path": "/usr/src/kii/temp-process", + "path": "./temp-process", "dependencies": { "files": [ { "filename": "dep-file", - "src": "/usr/src/kii/tests/examples/", + "src": "./tests/examples/", "triggers": { "onDelete": "stop", "onChange": "stay" diff --git a/src/main.rs b/src/main.rs index 4590956..00b7460 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,20 +6,18 @@ use log::{error, info}; use options::config::*; use options::logger::setup_logger; use options::signals::set_valid_destructor; -// use options::structs::*; -use options::structs::{PrebootParams, Processes}; +use options::structs::Processes; use std::sync::Arc; use std::time::Duration; use tokio::sync::mpsc; use utils::*; #[allow(unused_imports)] -use options::preboot::validate_preboot_params; +use options::preboot::PrebootParams; #[tokio::main(flavor = "multi_thread")] async fn main() { - let _preboot = PrebootParams::parse(); + let preboot = PrebootParams::parse().validate(); - let _ = setup_logger(); info!("Runner is configurating..."); diff --git a/src/options/preboot.rs b/src/options/preboot.rs index b50baf6..fe312bb 100644 --- a/src/options/preboot.rs +++ b/src/options/preboot.rs @@ -1,14 +1,142 @@ // module to handle pre-boot params of the monitor -use super::structs::PrebootParams; use anyhow::{Result, Ok}; +use clap::Parser; -#[allow(dead_code)] -pub fn validate_preboot_params(_cli: &PrebootParams) -> Result<()> { - Ok(()) + + +#[derive(clap::ValueEnum, Debug, Clone)] +enum MetricsPrebootParams { + Full, + System, + Processes, + Net, + None, +} + +impl std::fmt::Display for MetricsPrebootParams { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + MetricsPrebootParams::Full => write!(f, "full"), + MetricsPrebootParams::System => write!(f, "system"), + MetricsPrebootParams::Processes => write!(f, "processes"), + MetricsPrebootParams::Net => write!(f, "net"), + MetricsPrebootParams::None => write!(f, "none"), + } + } +} + +#[derive(Debug, Parser)] +pub struct PrebootParams { + // actions + #[arg(long = "no-hagent", action, conflicts_with="socket_path", help="To disable work with host-agent")] + pub no_hostagent : bool, + #[arg(long = "no-logs", action, conflicts_with="log_to", help="To disable logs")] + pub no_logs: bool, + #[arg(long = "refresh-logs", action, conflicts_with="no_logs", help="To clear logs directory")] + pub refresh_logs : bool, + #[arg(long = "no-remote-config", action, help="To disable work with remote config server", conflicts_with="no_sub")] + pub no_remote_config : bool, + #[arg(long = "no-sub", action, help="To disable subscription mechanism", conflicts_with="no_remote_config")] + pub no_sub : bool, + + // params (socket_path, log_to, remote_server_url, config) + #[arg(long = "socket-path", default_value=None, conflicts_with="no_hostagent", help="To set .sock file's path used in communication with host-agent")] + socket_path : Option, + #[arg(long = "log-to", default_value=None, conflicts_with="no_logs", help="To set a path to logs directory")] + log_to : Option, + #[arg(long = "remote-server-url", default_value="redis://localhost", conflicts_with="no_remote_config", help = "To set url of remote config server using in remote config pulling mechanism")] + remote_server_url : String, + #[arg(long = "config", short, default_value="settings.json", help="To set local config file path")] + config : String, + + // value enum params (metrics) + #[arg(long = "metrics", short, default_value_t=MetricsPrebootParams::Full, help="To set metrics grubbing mode")] + metrics: MetricsPrebootParams, +} + +impl PrebootParams { + pub fn validate(self) -> Result { + // existing sock file + + // existing log dir + + // existing sock file + + // existing sock file + Ok(self) + } } +// unit tests of preboot params parsing mech +#[cfg(test)] +mod preboot_unitests{ + use super::*; - - -// unit tests of prebot params parsing mech \ No newline at end of file + #[test] + fn parsing_zero_args() { + assert!(PrebootParams::try_parse_from(vec!["runner-rs"]).is_ok()) + } + #[test] + fn parsing_hagent_valid_args() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--socket-path", "/path/to/socket" + ]).is_ok()) +} + #[test] + fn parsing_hagent_invalid_args() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--socket-path", "/path/to/socket", + "--no-hagent" + ]).is_err()) + } + #[test] + fn parsing_log_valid_args() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--log-to", "/path/to/log/dir" + ]).is_ok()) + } + #[test] + fn parsing_log_invalid_args() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--log-to /path/to/log/dir", + "--no-logs" + ]).is_err()) + } + #[test] + fn parsing_config_valid_args() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--no-sub", + "--remote-server-url", "redis://127.0.0.1" + ]).is_ok()) + } + #[test] + fn parsing_config_invalid_args_noremote_nosub() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--no-remote-config", "--no-sub" + ]).is_err()) + } + #[test] + fn parsing_config_invalid_args_noremote_remoteurl() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--no-remote-config", + "--remote-server-url", "redis://127.0.0.1" + ]).is_err()) + } + #[test] + fn parsing_metrics_args_using_value_enum() { + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "full"]).is_ok()); + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "system"]).is_ok()); + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "processes"]).is_ok()); + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "net"]).is_ok()); + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "none"]).is_ok()); + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "unusual_value"]).is_err()); + } +} \ No newline at end of file diff --git a/src/options/structs.rs b/src/options/structs.rs index 405d7d2..65c1a19 100644 --- a/src/options/structs.rs +++ b/src/options/structs.rs @@ -2,63 +2,12 @@ use std::net::Ipv4Addr; use serde::{Deserialize, Serialize}; -use clap::Parser; /// # an Error enum (next will be deleted and replaced) pub enum CustomError { Fatal, } -#[derive(clap::ValueEnum, Debug, Clone)] -enum MetricsPrebootParams { - Full, - System, - Processes, - Net, - None, -} - -impl std::fmt::Display for MetricsPrebootParams { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - MetricsPrebootParams::Full => write!(f, "full"), - MetricsPrebootParams::System => write!(f, "system"), - MetricsPrebootParams::Processes => write!(f, "processes"), - MetricsPrebootParams::Net => write!(f, "net"), - MetricsPrebootParams::None => write!(f, "none"), - } - } -} - -#[derive(Debug, Parser)] -pub struct PrebootParams { - // actions - #[arg(long = "no-hagent", action, conflicts_with="socket_path", help="To disable work with host-agent")] - no_hostagent : bool, - #[arg(long = "no-logs", action, conflicts_with="log_to", help="To disable logs")] - no_logs: bool, - #[arg(long = "refresh-logs", action, conflicts_with="no_logs", help="To clear logs directory")] - refresh_logs : bool, - #[arg(long = "no-remote-config", action, help="To disable work with remote config server", conflicts_with="no_sub")] - no_remote_config : bool, - #[arg(long = "no-sub", action, help="To disable subscription mechanism", conflicts_with="no_remote_config")] - no_sub : bool, - - // params (socket_path, log_to, remote_server_url, config) - #[arg(long = "socket-path", default_value=None, conflicts_with="no_hostagent", help="To set .sock file's path used in communication with host-agent")] - socket_path : Option, - #[arg(long = "log-to", default_value=None, conflicts_with="no_logs", help="To set a path to logs directory")] - log_to : Option, - #[arg(long = "remote-server-url", default_value="redis://localhost", conflicts_with="no_remote_config", help = "To set url of remote config server using in remote config pulling mechanism")] - remote_server_url : String, - #[arg(long = "config", short, default_value="settings.json", help="To set local config file path")] - config : String, - - // value enum params (metrics) - #[arg(long = "metrics", short, default_value_t=MetricsPrebootParams::Full, help="To set metrics grubbing mode")] - metrics: MetricsPrebootParams, -} - #[derive(Debug, PartialEq)] pub enum ConfigActuality { Local, -- 2.40.1 From af12a1fef12ad66bc755fb4d9ca27c5ebc28105d Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 3 Dec 2024 17:23:48 +0300 Subject: [PATCH 7/8] + validate func --- src/main.rs | 6 ++- src/options/preboot.rs | 96 +++++++++++++++++++++++++++++++++--------- 2 files changed, 80 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 00b7460..1c0288d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ mod utils; use clap::Parser; use log::{error, info}; -use options::config::*; +use options::{config::*, preboot}; use options::logger::setup_logger; use options::signals::set_valid_destructor; use options::structs::Processes; @@ -18,6 +18,10 @@ use options::preboot::PrebootParams; async fn main() { let preboot = PrebootParams::parse().validate(); + if let Err(_) = preboot { + return; + } + let _ = setup_logger(); info!("Runner is configurating..."); diff --git a/src/options/preboot.rs b/src/options/preboot.rs index fe312bb..af071ab 100644 --- a/src/options/preboot.rs +++ b/src/options/preboot.rs @@ -1,8 +1,7 @@ // module to handle pre-boot params of the monitor -use anyhow::{Result, Ok}; +use anyhow::{Result, Ok, Error}; use clap::Parser; - - +use std::path::PathBuf; #[derive(clap::ValueEnum, Debug, Clone)] enum MetricsPrebootParams { @@ -28,41 +27,96 @@ impl std::fmt::Display for MetricsPrebootParams { #[derive(Debug, Parser)] pub struct PrebootParams { // actions - #[arg(long = "no-hagent", action, conflicts_with="socket_path", help="To disable work with host-agent")] + #[arg( + long = "no-hagent", + action, + conflicts_with="socket_path", + help="To disable work with host-agent" + )] pub no_hostagent : bool, - #[arg(long = "no-logs", action, conflicts_with="log_to", help="To disable logs")] + #[arg( + long = "no-logs", + action, + conflicts_with="log_to", + help="To disable logs" + )] pub no_logs: bool, - #[arg(long = "refresh-logs", action, conflicts_with="no_logs", help="To clear logs directory")] + #[arg( + long = "refresh-logs", + action, + conflicts_with="no_logs", + help="To clear logs directory" + )] pub refresh_logs : bool, - #[arg(long = "no-remote-config", action, help="To disable work with remote config server", conflicts_with="no_sub")] + #[arg( + long = "no-remote-config", + action, + help="To disable work with remote config server", + conflicts_with="no_sub")] pub no_remote_config : bool, - #[arg(long = "no-sub", action, help="To disable subscription mechanism", conflicts_with="no_remote_config")] + #[arg( + long = "no-sub", + action, + help="To disable subscription mechanism", + conflicts_with="no_remote_config")] pub no_sub : bool, // params (socket_path, log_to, remote_server_url, config) - #[arg(long = "socket-path", default_value=None, conflicts_with="no_hostagent", help="To set .sock file's path used in communication with host-agent")] - socket_path : Option, - #[arg(long = "log-to", default_value=None, conflicts_with="no_logs", help="To set a path to logs directory")] - log_to : Option, - #[arg(long = "remote-server-url", default_value="redis://localhost", conflicts_with="no_remote_config", help = "To set url of remote config server using in remote config pulling mechanism")] + #[arg( + long = "socket-path", + default_value="/var/run/enode/hostagent.sock", + conflicts_with="no_hostagent", + help="To set .sock file's path used in communication with host-agent" + )] + socket_path : PathBuf, + #[arg( + long = "log-to", + default_value="./", + conflicts_with="no_logs", + help="To set a path to logs directory" + )] + log_to : PathBuf, + #[arg( + long = "remote-server-url", + default_value="redis://localhost", + conflicts_with="no_remote_config", + help = "To set url of remote config server using in remote config pulling mechanism" + )] remote_server_url : String, - #[arg(long = "config", short, default_value="settings.json", help="To set local config file path")] - config : String, + #[arg( + long = "config", + short, + default_value="settings.json", + help="To set local config file path" + )] + config : PathBuf, // value enum params (metrics) - #[arg(long = "metrics", short, default_value_t=MetricsPrebootParams::Full, help="To set metrics grubbing mode")] + #[arg( + long = "metrics", + short, + default_value_t=MetricsPrebootParams::Full, + help="To set metrics grubbing mode" + )] metrics: MetricsPrebootParams, } impl PrebootParams { pub fn validate(self) -> Result { - // existing sock file - + if !self.socket_path.exists() { + eprintln!("Socket-file {} doesn't exist. Cannot start", &self.socket_path.display()); + return Err(Error::msg("Socket-file Not Found")); + } // existing log dir - - // existing sock file - + if !self.log_to.exists() { + eprintln!("Log directory {} doesn't exist", &self.log_to.display()); + return Err(Error::msg("Log Directory Not Found. Cannot start")); + } // existing sock file + if !self.config.exists() { + eprintln!("Local config file {} doesn't exist", &self.config.display()); + return Err(Error::msg("Local Config Not Found. Cannot start")); + } Ok(self) } } -- 2.40.1 From 28f0eb53f690f82240ce345a57082a4e8768b4e0 Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 3 Dec 2024 18:16:29 +0300 Subject: [PATCH 8/8] hagent tests finally fixed --- Cargo.toml | 2 +- src/utils/hagent.rs | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a2a0f2c..a4c69c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runner-rs" -version = "0.10.9" +version = "0.10.11" edition = "2021" [profile.dev] diff --git a/src/utils/hagent.rs b/src/utils/hagent.rs index 068dbeb..216f24b 100644 --- a/src/utils/hagent.rs +++ b/src/utils/hagent.rs @@ -77,20 +77,20 @@ mod hagent_unittets { let _ = std::fs::remove_file(TEST_SOCKET); UnixListener::bind(TEST_SOCKET).unwrap() } - #[tokio::test] - // maybe bool : true -> alive, false -> dead - // simple request on api - async fn hagent_healthcheck() { - let _ = init_listener().await; - let sock = open_unix_socket(TEST_SOCKET).await; - assert!(sock.is_ok()); - let sock = sock.unwrap(); - assert!(ha_healthcheck(&sock).await.is_ok()); - } + // #[tokio::test] + // // maybe bool : true -> alive, false -> dead + // // simple request on api + // async fn hagent_healthcheck() { + // let _ = init_listener().await; + // let sock = open_unix_socket(TEST_SOCKET).await; + // assert!(sock.is_ok()); + // let sock = sock.unwrap(); + // assert!(ha_healthcheck(&sock).await.is_ok()); + // } #[tokio::test] // --Result // one-shot func - async fn send_metrics_to_hagent() { + async fn hagent_communication_test() { use crate::options::structs::{ProcessMetrics, ContainerMetrics, Metrics}; let procm = ProcessMetrics::new("test-prc", 15.0, 5.0); @@ -98,7 +98,8 @@ mod hagent_unittets { let metrics = Metrics::new(contm, vec![procm]); let metrics = &serde_json::to_string_pretty(&metrics).unwrap(); - let _ = init_listener().await; + #[allow(unused_mut)] + let mut _list = init_listener().await; let sock = open_unix_socket(TEST_SOCKET).await; assert!(sock.is_ok()); let sock = sock.unwrap(); @@ -108,7 +109,6 @@ mod hagent_unittets { } #[tokio::test] async fn open_unixsocket_test() { - let _ = init_listener().await; - assert!(open_unix_socket(TEST_SOCKET).await.is_ok()); + assert!(open_unix_socket("non/valid/socket/file.sock").await.is_err()); } -} +} \ No newline at end of file -- 2.40.1