hagent unittests fixed
parent
3a51ea1418
commit
d3764448aa
|
|
@ -1,5 +1,7 @@
|
||||||
// module needed to check host-agent health condition and to communicate with it
|
// module needed to check host-agent health condition and to communicate with it
|
||||||
use tokio::{io::Interest, net::UnixStream};
|
use tokio::{io::Interest, net::UnixStream};
|
||||||
|
use anyhow::{Ok, Result, Error};
|
||||||
|
use tokio::net::UnixListener;
|
||||||
|
|
||||||
/// # Fn `open_unix_socket`
|
/// # Fn `open_unix_socket`
|
||||||
/// ## opening unix-socket for host-agent communication
|
/// ## opening unix-socket for host-agent communication
|
||||||
|
|
@ -14,9 +16,9 @@ use tokio::{io::Interest, net::UnixStream};
|
||||||
///
|
///
|
||||||
/// *depends on* : -
|
/// *depends on* : -
|
||||||
///
|
///
|
||||||
async fn open_unix_socket() -> Result<UnixStream, std::io::Error> {
|
async fn open_unix_socket(sock_path: &str) -> Result<UnixStream, std::io::Error> {
|
||||||
let socket = UnixStream::connect("/var/run/enode/hostagent.sock").await?;
|
// "/var/run/enode/hostagent.sock"
|
||||||
Ok(socket)
|
UnixStream::connect(sock_path).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Fn `ha_healthcheck`
|
/// # Fn `ha_healthcheck`
|
||||||
|
|
@ -32,15 +34,10 @@ async fn open_unix_socket() -> Result<UnixStream, std::io::Error> {
|
||||||
///
|
///
|
||||||
/// *depends on* : -
|
/// *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?;
|
socket.ready(Interest::WRITABLE).await?;
|
||||||
if socket.writable().await.is_ok() {
|
socket.writable().await?;
|
||||||
if let Err(er) = socket.try_write(b"Hello HAgent") {
|
socket.try_write(b"Hello HAgent")?;
|
||||||
return Err(er);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(std::io::ErrorKind::WouldBlock.into());
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -57,28 +54,30 @@ async fn ha_healthcheck(socket: &UnixStream) -> Result<(), std::io::Error >{
|
||||||
///
|
///
|
||||||
/// *depends on* : -
|
/// *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?;
|
socket.ready(Interest::WRITABLE).await?;
|
||||||
if socket.writable().await.is_ok() {
|
socket.writable().await?;
|
||||||
if let Err(er) = socket.try_write(data.as_bytes()) {
|
socket.try_write(data.as_bytes())?;
|
||||||
return Err(er);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(std::io::ErrorKind::WouldBlock.into());
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod hagent_unittets {
|
mod hagent_unittets {
|
||||||
use super::*;
|
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]
|
#[tokio::test]
|
||||||
// maybe bool : true -> alive, false -> dead
|
// maybe bool : true -> alive, false -> dead
|
||||||
// simple request on api
|
// simple request on api
|
||||||
async fn hagent_healthcheck() {
|
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());
|
assert!(sock.is_ok());
|
||||||
let sock = sock.unwrap();
|
let mut sock = sock.unwrap();
|
||||||
assert!(ha_healthcheck(&sock).await.is_ok());
|
assert!(ha_healthcheck(&sock).await.is_ok());
|
||||||
}
|
}
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
@ -92,15 +91,17 @@ mod hagent_unittets {
|
||||||
let metrics = Metrics::new(contm, vec![procm]);
|
let metrics = Metrics::new(contm, vec![procm]);
|
||||||
let metrics = &serde_json::to_string_pretty(&metrics).unwrap();
|
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());
|
assert!(sock.is_ok());
|
||||||
let sock = sock.unwrap();
|
let mut sock = sock.unwrap();
|
||||||
assert!(ha_healthcheck(&sock).await.is_ok());
|
assert!(ha_healthcheck(&sock).await.is_ok());
|
||||||
assert!(ha_send_data(&sock, &metrics).await.is_ok());
|
assert!(ha_send_data(&sock, &metrics).await.is_ok());
|
||||||
|
|
||||||
}
|
}
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn open_unixsocket_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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue