Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kairos server test hook #49

Merged
merged 8 commits into from Apr 18, 2024
Merged
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Expand Up @@ -41,7 +41,7 @@
];
perSystem = { config, self', inputs', system, pkgs, lib, ... }:
let
rustToolchain = inputs'.fenix.packages.stable.toolchain;
koxu1996 marked this conversation as resolved.
Show resolved Hide resolved
rustToolchain = inputs'.fenix.packages.complete.toolchain;
jonas089 marked this conversation as resolved.
Show resolved Hide resolved
craneLib = inputs.crane.lib.${system}.overrideToolchain rustToolchain;

kairosNodeAttrs = {
Expand Down
1 change: 1 addition & 0 deletions kairos-test-utils/Cargo.toml
Expand Up @@ -16,4 +16,5 @@ nom = "7"
tokio = { version = "1", features = [ "full", "tracing", "macros" ] }
tempfile = "3"
tracing = "0.1"
reqwest = { version = "*", features = ["json"] }
jonas089 marked this conversation as resolved.
Show resolved Hide resolved

3 changes: 1 addition & 2 deletions kairos-test-utils/src/cctl.rs
@@ -1,6 +1,6 @@
pub mod parsers;
use anyhow::anyhow;
use backoff::{self, backoff::Constant, future::retry};
use backoff::{backoff::Constant, future::retry};
use casper_client::{get_node_status, rpcs::results::ReactorState, Error, JsonRpcId, Verbosity};
use std::io::{self, Write};
use std::path::PathBuf;
Expand Down Expand Up @@ -133,7 +133,6 @@ impl Drop for CCTLNetwork {
#[cfg(test)]
mod tests {
use super::*;
use casper_client::{get_node_status, rpcs::results::ReactorState, JsonRpcId, Verbosity};
#[tokio::test]
async fn test_cctl_network_starts_and_terminates() {
let network = CCTLNetwork::run().await.unwrap();
Expand Down
68 changes: 68 additions & 0 deletions kairos-test-utils/src/kairos.rs
@@ -0,0 +1,68 @@
use backoff::future::retry;
use backoff::ExponentialBackoff;
use reqwest::Url;
use std::env;
use std::io;
use std::net::{SocketAddr, TcpListener};
use std::path::PathBuf;
use std::process::{Child, Command};
use tokio::net::TcpStream;

// A hacky way to get the cargo binary directory path
pub fn bin_dir() -> PathBuf {
let mut path = env::current_exe().unwrap();
path.pop(); // pop kairos_test_utils-hash
path.pop(); // pop deps
path
}

async fn wait_for_port(address: &SocketAddr) -> Result<(), io::Error> {
retry(ExponentialBackoff::default(), || async {
Ok(TcpStream::connect(address).await.map(|_| ())?)
})
.await
}

pub struct Kairos {
pub url: Url,
process_handle: Child,
}

impl Kairos {
pub async fn run() -> Result<Kairos, io::Error> {
let port = TcpListener::bind("127.0.0.1:0")?
.local_addr()?
.port()
.to_string();
let url = Url::parse(format!("http://127.0.0.1:{}", port).as_str()).unwrap();
let kairos = bin_dir().join("kairos-server");
let process_handle = Command::new(kairos)
.env("KAIROS_SERVER_PORT", &port)
.spawn()
.expect("Failed to start the kairos-server");

wait_for_port(url.socket_addrs(|| Option::None).unwrap().first().unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment: In the future we should introduce healtcheck mechanism - for example /health endpoint - because open port does not mean that server is ready to process requests.

.await
.unwrap();

Ok(Kairos {
url,
process_handle,
})
}
}

impl Drop for Kairos {
fn drop(&mut self) {
let _ = self.process_handle.kill();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment: This is not a graceful shutdown, but for testing it seems to be okay.

}
}

#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_kairos_starts_and_terminates() {
let _kairos = Kairos::run().await.unwrap();
}
}
2 changes: 2 additions & 0 deletions kairos-test-utils/src/lib.rs
@@ -1 +1,3 @@
#![feature(async_closure)]
koxu1996 marked this conversation as resolved.
Show resolved Hide resolved
pub mod cctl;
pub mod kairos;