From d3764448aa817408735724f3a08e3088fadf0cfe Mon Sep 17 00:00:00 2001 From: prplV Date: Mon, 2 Dec 2024 14:04:09 +0300 Subject: [PATCH] hagent unittests fixed --- src/utils/hagent.rs | 49 +++++++++++++------------- tests/examples/hagent/hagent_test.sock | 0 2 files changed, 25 insertions(+), 24 deletions(-) create mode 100755 tests/examples/hagent/hagent_test.sock diff --git a/src/utils/hagent.rs b/src/utils/hagent.rs index c794538..06cb173 100644 --- a/src/utils/hagent.rs +++ b/src/utils/hagent.rs @@ -1,5 +1,7 @@ // module needed to check host-agent health condition and to communicate with it use tokio::{io::Interest, net::UnixStream}; +use anyhow::{Ok, Result, Error}; +use tokio::net::UnixListener; /// # Fn `open_unix_socket` /// ## opening unix-socket for host-agent communication @@ -14,9 +16,9 @@ use tokio::{io::Interest, net::UnixStream}; /// /// *depends on* : - /// -async fn open_unix_socket() -> Result { - let socket = UnixStream::connect("/var/run/enode/hostagent.sock").await?; - Ok(socket) +async fn open_unix_socket(sock_path: &str) -> Result { + // "/var/run/enode/hostagent.sock" + UnixStream::connect(sock_path).await } /// # Fn `ha_healthcheck` @@ -32,15 +34,10 @@ async fn open_unix_socket() -> Result { /// /// *depends on* : - /// -async fn ha_healthcheck(socket: &UnixStream) -> Result<(), std::io::Error >{ +async fn ha_healthcheck(socket: &UnixStream) -> Result<(), Error> { socket.ready(Interest::WRITABLE).await?; - if socket.writable().await.is_ok() { - if let Err(er) = socket.try_write(b"Hello HAgent") { - return Err(er); - } - } else { - return Err(std::io::ErrorKind::WouldBlock.into()); - } + socket.writable().await?; + socket.try_write(b"Hello HAgent")?; Ok(()) } @@ -57,28 +54,30 @@ async fn ha_healthcheck(socket: &UnixStream) -> Result<(), std::io::Error >{ /// /// *depends on* : - /// -async fn ha_send_data(socket: &UnixStream, data: &str) -> Result<(), std::io::Error > { +async fn ha_send_data(socket: &UnixStream, data: &str) -> Result<(), Error > { socket.ready(Interest::WRITABLE).await?; - if socket.writable().await.is_ok() { - if let Err(er) = socket.try_write(data.as_bytes()) { - return Err(er); - } - } else { - return Err(std::io::ErrorKind::WouldBlock.into()); - } + socket.writable().await?; + socket.try_write(data.as_bytes())?; Ok(()) } #[cfg(test)] mod hagent_unittets { use super::*; + const TEST_SOCKET: &str = "./tests/examples/hagent/hagent_test.sock"; + + async fn init_listener() -> UnixListener { + 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 sock = open_unix_socket().await; + let mut list = init_listener().await; + let sock = open_unix_socket(TEST_SOCKET).await; assert!(sock.is_ok()); - let sock = sock.unwrap(); + let mut sock = sock.unwrap(); assert!(ha_healthcheck(&sock).await.is_ok()); } #[tokio::test] @@ -92,15 +91,17 @@ mod hagent_unittets { let metrics = Metrics::new(contm, vec![procm]); let metrics = &serde_json::to_string_pretty(&metrics).unwrap(); - let sock = open_unix_socket().await; + let mut list = init_listener().await; + let sock = open_unix_socket(TEST_SOCKET).await; assert!(sock.is_ok()); - let sock = sock.unwrap(); + let mut 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() { - assert!(open_unix_socket().await.is_ok()); + let mut list = init_listener().await; + assert!(open_unix_socket(TEST_SOCKET).await.is_ok()); } } diff --git a/tests/examples/hagent/hagent_test.sock b/tests/examples/hagent/hagent_test.sock new file mode 100755 index 0000000..e69de29