Skip to content

Commit

Permalink
test: Add rendezvous registration renewal test
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Oct 30, 2023
1 parent 9f0b227 commit 4d7ad72
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 7 deletions.
22 changes: 22 additions & 0 deletions homestar-runtime/tests/fixtures/test_rendezvous4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[monitoring]
process_collector_interval = 500
metrics_port = 4038
console_subscriber_port = 5588

[node]

[node.network]
rpc_port = 9828
websocket_port = 8028
listen_address = "/ip4/127.0.0.1/tcp/7003"
announce_addresses = [
"/ip4/127.0.0.1/tcp/7003/p2p/12D3KooWJWoaqZhDaoEFshF7Rh1bpY9ohihFhzcW6d69Lr2NASuq",
]
node_addresses = [
"/ip4/127.0.0.1/tcp/7000/p2p/12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN",
]
rendezvous_registration_ttl = 1
enable_mdns = false

[node.network.keypair_config]
existing = { key_type = "ed25519", path = "./fixtures/__testkey_ed25519_3.pem" }
71 changes: 70 additions & 1 deletion homestar-runtime/tests/network.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::utils::{check_lines_for, kill_homestar, retrieve_output, stop_homestar, BIN_NAME};
use crate::utils::{
check_lines_for, count_lines_where, kill_homestar, retrieve_output, stop_homestar, BIN_NAME,
};
use anyhow::Result;
use once_cell::sync::Lazy;
use serial_test::file_serial;
Expand Down Expand Up @@ -679,3 +681,70 @@ fn test_libp2p_disconnect_rendezvous_discovery_serial() -> Result<()> {

Ok(())
}

#[test]
#[file_serial]
fn test_libp2p_rendezvous_renew_registration_serial() -> Result<()> {
let _ = stop_homestar();

// Start a rendezvous server
let rendezvous_server = Command::new(BIN.as_os_str())
.env(
"RUST_LOG",
"homestar=debug,homestar_runtime=debug,libp2p=debug,libp2p_gossipsub::behaviour=debug",
)
.arg("start")
.arg("-c")
.arg("tests/fixtures/test_rendezvous1.toml")
.arg("--db")
.arg("homestar1.db")
.stdout(Stdio::piped())
.spawn()
.unwrap();

// Start a peer that will renew registrations with the rendezvous server once per second
let rendezvous_client1 = Command::new(BIN.as_os_str())
.env(
"RUST_LOG",
"homestar=debug,homestar_runtime=debug,libp2p=debug,libp2p_gossipsub::behaviour=debug",
)
.arg("start")
.arg("-c")
.arg("tests/fixtures/test_rendezvous4.toml")
.arg("--db")
.arg("homestar4.db")
.stdout(Stdio::piped())
.spawn()
.unwrap();

// Collect logs for five seconds then kill proceses.
let dead_server = kill_homestar(rendezvous_server, Some(Duration::from_secs(5)));
let dead_client = kill_homestar(rendezvous_client1, Some(Duration::from_secs(5)));

// Retrieve logs.
let stdout_server = retrieve_output(dead_server);
let stdout_client = retrieve_output(dead_client);

// Count registrations on the server
let server_registration_count = count_lines_where(
stdout_server,
vec![
"registered peer through rendezvous",
"12D3KooWJWoaqZhDaoEFshF7Rh1bpY9ohihFhzcW6d69Lr2NASuq",
],
);

// Count registrations on the client
let client_registration_count = count_lines_where(
stdout_client,
vec![
"registered self with rendezvous node",
"12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN",
],
);

assert!(server_registration_count > 1);
assert!(client_registration_count > 1);

Ok(())
}
24 changes: 18 additions & 6 deletions homestar-runtime/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,18 @@ pub(crate) fn check_lines_for(output: String, predicates: Vec<&str>) -> bool {
.fold(false, |acc, curr| acc || curr)
}

/// Check process output line for all predicates
fn line_contains(line: &str, predicates: &Vec<&str>) -> bool {
predicates
.iter()
.map(|pred| predicate::str::contains(*pred).eval(line))
.fold(true, |acc, curr| acc && curr)
pub(crate) fn count_lines_where(output: String, predicates: Vec<&str>) -> i32 {
output.split("\n").fold(0, |count, line| {
if line_contains(line, &predicates) {
count + 1
} else {
count
}
})
}

/// Extract timestamps for process output lines with matching predicates
#[allow(dead_code)]
pub(crate) fn extract_timestamps_where(
output: String,
predicates: Vec<&str>,
Expand All @@ -132,7 +135,16 @@ pub(crate) fn extract_timestamps_where(
})
}

/// Check process output line for all predicates
fn line_contains(line: &str, predicates: &Vec<&str>) -> bool {
predicates
.iter()
.map(|pred| predicate::str::contains(*pred).eval(line))
.fold(true, |acc, curr| acc && curr)
}

/// Extract label value from process output line
#[allow(dead_code)]
fn extract_label<'a>(line: &'a str, key: &str) -> Option<&'a str> {
line.split(' ')
.find(|label| predicate::str::contains(format!("{key}=")).eval(label))
Expand Down

0 comments on commit 4d7ad72

Please sign in to comment.