Skip to content

Commit

Permalink
feat(tests): add an integration test httpd_init_add_remote
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <tareknaser360@gmail.com>
  • Loading branch information
tareknaser authored and vincenzopalazzo committed Jul 11, 2023
1 parent 56dcb7e commit 049b475
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

17 changes: 15 additions & 2 deletions coffee_testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub struct CoffeeHTTPDTesting {
root_path: Arc<TempDir>,
httpd_pid: tokio::process::Child,
httpd_port: u64,
cln_path: String,
}

impl Drop for CoffeeHTTPDTesting {
Expand All @@ -172,22 +173,34 @@ impl Drop for CoffeeHTTPDTesting {

impl CoffeeHTTPDTesting {
/// init coffee httpd in a tmp directory.
pub async fn tmp() -> anyhow::Result<Self> {
pub async fn tmp(cln_path: String) -> anyhow::Result<Self> {
let dir = tempfile::tempdir()?;
let port = port::random_free_port().unwrap();
let child = httpd!(dir, port, "{}", format!("--network=regtest"))?;
let child = httpd!(
dir,
port,
"{}",
format!("--network=regtest --cln-path={cln_path}")
)?;
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(),
cln_path,
})
}

// get the root path of coffee.
pub fn root_path(&self) -> Arc<TempDir> {
self.root_path.clone()
}

// get the path of core lightning.
pub fn cln_path(&self) -> String {
self.cln_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 {
Expand Down
2 changes: 1 addition & 1 deletion docs/docs-book/src/using-coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Please note that the server runs on `localhost` with port `8080` where you can f
To start the Coffee server, run the following command:

```shell
coffee_httpd --network <network>
coffee_httpd --cln-path <core_lightning_path> --network <network>
```

Make sure the `coffee_httpd` binary is in your system PATH or in the current working directory.
54 changes: 46 additions & 8 deletions tests/src/coffee_httpd_integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,69 @@
use serde_json::json;

use coffee_lib::types::request::*;
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() {
pub async fn httpd_init_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();
let manager = CoffeeHTTPDTesting::tmp(lightning_dir.to_string())
.await
.unwrap();
log::info!("lightning path: {lightning_dir}");

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

// Define the request body to be sent to the /remote/add endpoint
let remote_add_request = RemoteAdd {
repository_name: "lightningd".to_string(),
repository_url: "https://github.com/lightningd/plugins.git".to_string(),
};

let body = reqwest::get(format!("{url}/list"))
// Send the request to add a remote repository
let response = client
.post(format!("{}/remote/add", url))
.json(&remote_add_request)
.send()
.await
.unwrap()
.text()
.unwrap();

// Check the response status code, log the body.
assert!(response.status().is_success());
let body = response.text().await.unwrap();
log::info!("/remote/add response: {}", body);

// Define the request body to be sent to the /install endpoint
let install_request = Install {
plugin: "summary".to_string(),
try_dynamic: true,
};

// Send the request to install a plugin
let response = client
.post(format!("{}/install", url))
.json(&install_request)
.send()
.await
.unwrap();

println!("Response body: {}", body);
// Check the response status code, log the body.
// assert!(response.status().is_success());
let body = response.text().await.unwrap();
log::info!("/install response: {}", body);

// Make sure the "summary" plugin is installed
cln.rpc()
.call::<serde_json::Value, serde_json::Value>("summary", json!({}))
.unwrap();

cln.stop().await.unwrap();
}

0 comments on commit 049b475

Please sign in to comment.