-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests): introducing tests framework
Signed-off-by: Vincenzo Palazzo <[email protected]>
- Loading branch information
1 parent
48dd734
commit 89b28f1
Showing
17 changed files
with
1,153 additions
and
58 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ members = [ | |
"coffee_cmd", | ||
"coffee_httpd", | ||
"coffee_plugin", | ||
"coffee_testing", | ||
] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "coffee_testing" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
//! Bitcoin Testing framework. | ||
use bitcoincore_rpc::{Auth, Client, RpcApi}; | ||
use port::Port; | ||
use port_selector as port; | ||
use tempfile::TempDir; | ||
|
||
pub mod macros { | ||
#[macro_export] | ||
macro_rules! bitcoind { | ||
($dir:expr, $port:expr, $opt_args:expr) => { | ||
async { | ||
use std::process::Stdio; | ||
|
||
use log; | ||
use tokio::process::Command; | ||
|
||
let opt_args = format!($opt_args); | ||
let args = opt_args.trim(); | ||
let args_tok: Vec<&str> = args.split(" ").collect(); | ||
log::debug!("additional args: {:?}", args_tok); | ||
let mut command = Command::new("bitcoind"); | ||
command | ||
.args(&args_tok) | ||
.arg(format!("-port={}", $port + 1)) | ||
.arg(format!("-rpcport={}", $port)) | ||
.arg(format!("-datadir={}", $dir.path().to_str().unwrap())) | ||
.stdout(Stdio::null()) | ||
.spawn() | ||
} | ||
.await | ||
}; | ||
($dir:expr, $port:expr) => { | ||
$crate::bitcoind!($dir, $port, "") | ||
}; | ||
} | ||
|
||
pub use bitcoind; | ||
} | ||
|
||
pub struct BtcNode { | ||
inner: Client, | ||
pub user: String, | ||
pub pass: String, | ||
pub port: Port, | ||
root_path: TempDir, | ||
process: Vec<tokio::process::Child>, | ||
} | ||
|
||
impl Drop for BtcNode { | ||
fn drop(&mut self) { | ||
for process in self.process.iter() { | ||
let child = process.id().unwrap(); | ||
let mut kill = std::process::Command::new("kill") | ||
.args(["-s", "SIGKILL", &child.to_string()]) | ||
.spawn() | ||
.unwrap(); | ||
kill.wait().unwrap(); | ||
} | ||
std::fs::remove_dir_all(self.root_path.path()).unwrap(); | ||
} | ||
} | ||
|
||
impl BtcNode { | ||
pub async fn tmp() -> anyhow::Result<Self> { | ||
let dir = tempfile::tempdir()?; | ||
let user = "crab".to_owned(); | ||
let pass = "crab".to_owned(); | ||
let port = port::random_free_port().unwrap(); | ||
let process = macros::bitcoind!( | ||
dir, | ||
port, | ||
"-server -regtest -rpcuser={user} -rpcpassword={pass}" | ||
)?; | ||
let rpc = Client::new( | ||
&format!("http://localhost:{port}"), | ||
Auth::UserPass(user.clone(), pass.clone()), | ||
)?; | ||
let bg_process = vec![process]; | ||
Ok(Self { | ||
inner: rpc, | ||
root_path: dir, | ||
user, | ||
pass, | ||
port, | ||
process: bg_process, | ||
}) | ||
} | ||
|
||
pub fn rpc(&self) -> &Client { | ||
&self.inner | ||
} | ||
|
||
pub async fn stop(&mut self) -> anyhow::Result<()> { | ||
log::info!("stop bitcoin node"); | ||
self.inner.stop()?; | ||
for process in self.process.iter_mut() { | ||
process.kill().await?; | ||
let _ = process.wait().await?; | ||
log::debug!("process killed"); | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//! Integration testing library for core lightning | ||
use clightningrpc::LightningRPC; | ||
use port_selector as port; | ||
use tempfile::TempDir; | ||
|
||
use crate::btc::BtcNode; | ||
use crate::prelude::*; | ||
|
||
pub mod macros { | ||
#[macro_export] | ||
macro_rules! lightningd { | ||
($dir:expr, $port:expr, $($opt_args:tt)*) => { | ||
async { | ||
use std::process::Stdio; | ||
|
||
use tokio::process::Command; | ||
|
||
let opt_args = format!($($opt_args)*); | ||
let args = opt_args.trim(); | ||
let args_tok: Vec<&str> = args.split(" ").collect(); | ||
|
||
let mut command = Command::new("lightningd"); | ||
command | ||
.args(&args_tok) | ||
.arg(format!("--addr=127.0.0.1:{}", $port)) | ||
.arg(format!("--bind-addr=127.0.0.1:{}", $port)) | ||
.arg(format!("--lightning-dir={}", $dir.path().to_str().unwrap())) | ||
.arg("--dev-fast-gossip") | ||
.arg("--funding-confirms=1") | ||
.stdout(Stdio::null()) | ||
.spawn() | ||
}.await | ||
}; | ||
($dir:expr, $port:expr) => { | ||
$crate::lightningd!($dir, $port, "") | ||
}; | ||
} | ||
|
||
pub use lightningd; | ||
} | ||
|
||
pub struct Node { | ||
inner: LightningRPC, | ||
root_path: TempDir, | ||
bitcoin: BtcNode, | ||
process: Vec<tokio::process::Child>, | ||
} | ||
|
||
impl Drop for Node { | ||
fn drop(&mut self) { | ||
for process in self.process.iter() { | ||
let child = process.id().unwrap(); | ||
let mut kill = std::process::Command::new("kill") | ||
.args(["-s", "SIGKILL", &child.to_string()]) | ||
.spawn() | ||
.unwrap(); | ||
kill.wait().unwrap(); | ||
} | ||
|
||
std::fs::remove_dir_all(self.root_path.path()).unwrap(); | ||
} | ||
} | ||
|
||
impl Node { | ||
pub async fn tmp() -> anyhow::Result<Self> { | ||
let btc = BtcNode::tmp().await?; | ||
|
||
let dir = tempfile::tempdir()?; | ||
let process = macros::lightningd!( | ||
dir, | ||
port::random_free_port().unwrap(), | ||
"--network=regtest --bitcoin-rpcuser={} --bitcoin-rpcpassword={} --bitcoin-rpcport={}", | ||
btc.user, | ||
btc.pass, | ||
btc.port, | ||
)?; | ||
|
||
let rpc = LightningRPC::new(dir.path().join("regtest").join("lightning-rpc")); | ||
|
||
wait_for!(async { rpc.getinfo() }); | ||
|
||
Ok(Self { | ||
inner: rpc, | ||
root_path: dir, | ||
bitcoin: btc, | ||
process: vec![process], | ||
}) | ||
} | ||
|
||
pub fn rpc(&self) -> &LightningRPC { | ||
&self.inner | ||
} | ||
|
||
pub async fn stop(&mut self) -> anyhow::Result<()> { | ||
log::info!("stop lightning node"); | ||
self.inner.stop()?; | ||
for process in self.process.iter_mut() { | ||
process.kill().await?; | ||
let _ = process.wait().await?; | ||
log::debug!("killing process"); | ||
} | ||
self.bitcoin.stop().await?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.