Skip to content

Commit

Permalink
feat(tests): add a new httpd test
Browse files Browse the repository at this point in the history
This commit:
1- adds an httpd integration test to further stress test the server. The new test mimics a test case where all endpoints are called.
2- implements Debug for CoffeeHTTPDTesting

Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser authored and vincenzopalazzo committed Jul 24, 2023
1 parent 334e8db commit 5224e63
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 0 deletions.
1 change: 1 addition & 0 deletions coffee_testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl CoffeeTesting {
}

/// Coffee HTTPD testing manager.
#[derive(Debug)]
pub struct CoffeeHTTPDTesting {
root_path: Arc<TempDir>,
httpd_pid: tokio::process::Child,
Expand Down
240 changes: 240 additions & 0 deletions tests/src/coffee_httpd_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,243 @@ pub async fn httpd_init_add_remote() {

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

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

let mut cln = Node::tmp("regtest").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;
assert!(manager.is_ok(), "{:?}", manager);
let manager = manager.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 response = client
.post(format!("{}/remote/add", url))
.json(&remote_add_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.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 /show endpoint
let show_request = Show {
plugin: "helpme".to_string(),
};

let response = client
.get(format!("{}/show", url))
.json(&show_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

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

// Parse the response body
let response_json = serde_json::from_str(&body);
assert!(response_json.is_ok(), "{:?}", response_json);
let response_json: serde_json::Value = response_json.unwrap();

// Extract the `readme` field from the response JSON
let readme = response_json["readme"].as_str();
assert!(readme.is_some(), "{:?}", readme);
let readme = readme.unwrap();

// Assert that the `readme` starts with the expected content
assert!(readme.starts_with("# Helpme plugin"), "{:?}", readme);

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

let response = client
.post(format!("{}/install", url))
.json(&install_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

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

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

let response = client
.post(format!("{}/install", url))
.json(&install_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

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

let body = reqwest::get(format!("{}/remote/list", url)).await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap().json::<serde_json::Value>().await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap();

// Log the response body
log::info!("/remote/list response: {}", body);

// Assert that the "lightningd" remote repository exists in the response
let remotes = body["remotes"].as_array();
assert!(remotes.is_some(), "{:?}", remotes);
let remotes = remotes.unwrap();
assert!(
remotes
.iter()
.any(|repo| repo["local_name"] == "lightningd"),
"lightningd remote repository not found in the response"
);

let body = reqwest::get(format!("{}/list", url)).await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap().json::<serde_json::Value>().await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap();

// Log the response body
log::info!("/list response: {}", body);

// Assert that the "helpme" and "summary" plugin exist in the response
let plugins = body["plugins"].as_array();
assert!(plugins.is_some(), "{:?}", plugins);
let plugins = plugins.unwrap();
assert!(
plugins.iter().any(|plugin| plugin["name"] == "helpme"),
"helpme plugin not found in the response"
);
assert!(
plugins.iter().any(|plugin| plugin["name"] == "summary"),
"summary plugin not found in the response"
);

// Define the request body to be sent
let plugin_remove_request = Remove {
plugin: "summary".to_string(),
};

let response = client
.post(format!("{}/remove", url))
.json(&plugin_remove_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

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

let body = reqwest::get(format!("{}/list", url)).await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap().json::<serde_json::Value>().await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap();

// Log the response body
log::info!("/list response: {}", body);

// Assert that the "summary" plugin was removed
let plugins = body["plugins"].as_array();
assert!(plugins.is_some(), "{:?}", plugins);
let plugins = plugins.unwrap();
assert!(
!(plugins.iter().any(|plugin| plugin["name"] == "summary")),
"summary plugin is found in the list response while it should have been removed"
);

// Define the request body to be sent
let remote_rm_request = RemoteRm {
repository_name: "lightningd".to_string(),
};

// This should also remove the helpme plugin
let response = client
.post(format!("{}/remote/rm", url))
.json(&remote_rm_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

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

let body = reqwest::get(format!("{}/list", url)).await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap().json::<serde_json::Value>().await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap();

// Log the response body
log::info!("/list response: {}", body);

// Assert that the "helpme" plugin was removed
let plugins = body["plugins"].as_array();
assert!(plugins.is_some(), "{:?}", plugins);
let plugins = plugins.unwrap();
assert!(
!(plugins.iter().any(|plugin| plugin["name"] == "helpme")),
"helpme plugin is found in the list response while it should have been removed"
);

let body = reqwest::get(format!("{}/remote/list", url)).await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap().json::<serde_json::Value>().await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap();

// Log the response body
log::info!("/remote/list response: {}", body);

// Assert that the "lightningd" remote repository doesn't exist in the response
let remotes = body["remotes"].as_array();
assert!(remotes.is_some(), "{:?}", remotes);
let remotes = remotes.unwrap();
assert!(
!(remotes
.iter()
.any(|repo| repo["local_name"] == "lightningd")),
"lightningd remote repository is found in the response while it should have been removed"
);

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

0 comments on commit 5224e63

Please sign in to comment.