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 httpd testing framework #170

Merged
merged 5 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
816 changes: 358 additions & 458 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ install:
$(CC) install --locked --path ./coffee_cmd

integration: default
$(CC) test -p tests $(ARGS)
$(CC) test -j 4 -p tests $(ARGS)
2 changes: 1 addition & 1 deletion coffee_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum RemoteAction {
List,
}

pub trait CoffeeArgs {
pub trait CoffeeArgs: Send + Sync {
/// return the command that coffee needs to execute
fn command(&self) -> CoffeeOperation;
/// return the conf
Expand Down
8 changes: 8 additions & 0 deletions coffee_httpd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ name = "coffee_httpd"
version = "0.1.0"
edition = "2021"

[lib]
name = "coffee_httpd"
path = "src/lib.rs"

[[bin]]
name = "coffee_httpd"
path = "src/main.rs"

[dependencies]
actix-web = "4"
clap = "4.1.11"
Expand Down
5 changes: 3 additions & 2 deletions coffee_httpd/src/httpd/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use std::collections::HashMap;
use std::net::ToSocketAddrs;
use std::sync::Arc;
use tokio::sync::Mutex;

use serde_json::Value;
use tokio::sync::Mutex;

use coffee_core::coffee::CoffeeManager;
use coffee_lib::plugin_manager::PluginManager;
Expand Down Expand Up @@ -60,7 +60,8 @@ pub async fn run_httpd<T: ToSocketAddrs>(
})
.bind(host)?
.run()
.await
.await?;
Ok(())
}

#[api_v2_operation]
Expand Down
3 changes: 3 additions & 0 deletions coffee_httpd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod httpd;

pub use httpd::*;
7 changes: 6 additions & 1 deletion coffee_httpd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use clap::Parser;
use log;

use coffee_core::coffee::CoffeeManager;

mod cmd;
Expand All @@ -13,7 +15,10 @@ async fn main() {
println!("{err}");
}
let coffee = coffee.unwrap();
if let Err(err) = httpd::run_httpd(coffee, ("127.0.0.1", 8080)).await {

let port = cmd.port.unwrap_or(8080) as u16;
log::info!("Running on port 127.0.0.1:{port}");
if let Err(err) = httpd::run_httpd(coffee, ("127.0.0.1", port)).await {
println!("{err}");
}
}
3 changes: 1 addition & 2 deletions coffee_testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ edition = "2021"
clightningrpc = "0.3.0-beta.6"
bitcoincore-rpc = "0.17.0"
log = "0.4.19"
cln-test = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/test-framework" }
cln-btc-test = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/test-framework" }
coffee_core = { path = "../coffee_core" }
tempfile = "3.6.0"
port-selector = "0.1.6"
anyhow = "1.0.71"
tokio = { version = "1.22.0", features = ["process", "time"] }
reqwest = { version = "0.11", features = ["json"] }
83 changes: 79 additions & 4 deletions coffee_testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ pub mod btc;
pub mod cln;

pub mod prelude {
pub use cln_btc_test;
pub use cln_test;

pub use crate::macros::*;
pub use port_selector as port;
pub use tempfile;
}
use std::sync::Arc;

use port_selector as port;
use tempfile::TempDir;

use coffee_core::coffee::CoffeeManager;
Expand Down Expand Up @@ -41,6 +40,33 @@ pub mod macros {
};
}

#[macro_export]
macro_rules! httpd {
($dir:expr, $port:expr, $($opt_args:tt)*) => {
async {
use tokio::process::Command;

let opt_args = format!($($opt_args)*);
let args = opt_args.trim();
let args_tok: Vec<&str> = args.split(" ").collect();

let cargo_target = concat!(env!("CARGO_MANIFEST_DIR"), "/..");
let httpd_path = std::path::Path::new(cargo_target).to_str().unwrap();
let mut command = Command::new(format!("{httpd_path}//target/debug/coffee_httpd"));
command
.args(&args_tok)
.arg("--host=127.0.0.1")
.arg(format!("--port={}", $port))
.arg(format!("--data-dir={}", $dir.path().to_str().unwrap()))
.spawn()
}.await
};
($dir:expr, $port:expr) => {
$crate::lightningd!($dir, $port, "")
};
}

pub use httpd;
pub use wait_for;
}

Expand All @@ -50,6 +76,9 @@ pub struct CoffeeTestingArgs {
pub data_dir: String,
}

unsafe impl Send for CoffeeTestingArgs {}
unsafe impl Sync for CoffeeTestingArgs {}

impl coffee_core::CoffeeArgs for CoffeeTestingArgs {
fn command(&self) -> coffee_core::CoffeeOperation {
unimplemented!()
Expand Down Expand Up @@ -77,7 +106,7 @@ pub struct CoffeeTesting {
}

impl CoffeeTesting {
// init coffee in a tmp directory.
/// init coffee in a tmp directory.
pub async fn tmp() -> anyhow::Result<Self> {
let dir = tempfile::tempdir()?;
let args = CoffeeTestingArgs {
Expand Down Expand Up @@ -119,3 +148,49 @@ impl CoffeeTesting {
self.root_path.clone()
}
}

/// Coffee HTTPD testing manager.
pub struct CoffeeHTTPDTesting {
root_path: Arc<TempDir>,
httpd_pid: tokio::process::Child,
httpd_port: u64,
}

impl Drop for CoffeeHTTPDTesting {
fn drop(&mut self) {
let Some(child) = self.httpd_pid.id() else {
return;
};
let Ok(mut kill) = std::process::Command::new("kill")
.args(["-s", "SIGKILL", &child.to_string()])
.spawn() else {
return;
};
let _ = kill.wait();
}
}

impl CoffeeHTTPDTesting {
/// init coffee httpd in a tmp directory.
pub async fn tmp() -> anyhow::Result<Self> {
let dir = tempfile::tempdir()?;
let port = port::random_free_port().unwrap();
let child = httpd!(dir, port, "{}", format!("--network=regtest"))?;
wait_for!(async { reqwest::get(format!("http://127.0.0.1:{}/list", port)).await });
Ok(Self {
root_path: Arc::new(dir),
httpd_pid: child,
httpd_port: port.into(),
})
}

pub fn root_path(&self) -> Arc<TempDir> {
self.root_path.clone()
}

/// run the httpd daemon as process and return the URL
/// this should allow to make integration testing to the httpd.
pub fn url(&self) -> String {
format!("http://127.0.0.1:{}", self.httpd_port)
}
}
1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ chrono = { version = "0.4", features = ["std"], default-features = false }
tokio = { version = "1.22.0", features = ["rt"] }
ntest = "0.9.0"
serde_json = "1"
reqwest = { version = "0.11", features = ["json"] }
31 changes: 31 additions & 0 deletions tests/src/coffee_httpd_integration_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use coffee_testing::cln::Node;
use coffee_testing::CoffeeHTTPDTesting;

use crate::init;

#[tokio::test(flavor = "multi_thread")]
#[ntest::timeout(560000)]
pub async fn init_httpd_add_remote() {
init();

let mut cln = Node::tmp().await.unwrap();
let manager = CoffeeHTTPDTesting::tmp().await.unwrap();

let lightning_dir = cln.rpc().getinfo().unwrap().ligthning_dir;
let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap();
log::info!("lightning path: {lightning_dir}");

let url = manager.url();
log::info!("base url: {url}");

let body = reqwest::get(format!("{url}/list"))
.await
.unwrap()
.text()
.await
.unwrap();

println!("Response body: {}", body);

cln.stop().await.unwrap();
}
6 changes: 3 additions & 3 deletions tests/src/coffee_plugin_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use coffee_testing::cln::Node;

use crate::init;

#[tokio::test]
#[tokio::test(flavor = "multi_thread")]
#[ntest::timeout(560000)]
pub async fn init_cln_with_coffee_plugin_test() {
init();
Expand All @@ -28,7 +28,7 @@ pub async fn init_cln_with_coffee_plugin_test() {
cln.stop().await.unwrap();
}

#[tokio::test]
#[tokio::test(flavor = "multi_thread")]
#[ntest::timeout(560000)]
pub async fn init_cln_with_coffee_add_remore_test() {
init();
Expand Down Expand Up @@ -66,7 +66,7 @@ pub async fn init_cln_with_coffee_add_remore_test() {
cln.stop().await.unwrap();
}

#[tokio::test]
#[tokio::test(flavor = "multi_thread")]
#[ntest::timeout(1000000)]
pub async fn init_cln_with_coffee_install_plugin_test() {
init();
Expand Down
2 changes: 2 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(test)]
mod coffee_httpd_integration_tests;
#[cfg(test)]
mod coffee_integration_tests;
#[cfg(test)]
mod coffee_plugin_integration_tests;
Expand Down
Loading