hagent unittests fixed

pull/15/head
prplV 2024-12-02 14:04:09 +03:00
parent 3a51ea1418
commit d3764448aa
2 changed files with 25 additions and 24 deletions

View File

@ -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<UnixStream, std::io::Error> {
let socket = UnixStream::connect("/var/run/enode/hostagent.sock").await?;
Ok(socket)
async fn open_unix_socket(sock_path: &str) -> Result<UnixStream, std::io::Error> {
// "/var/run/enode/hostagent.sock"
UnixStream::connect(sock_path).await
}
/// # Fn `ha_healthcheck`
@ -32,15 +34,10 @@ async fn open_unix_socket() -> Result<UnixStream, std::io::Error> {
///
/// *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());
}
}

View File