Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Review feedback
  • Loading branch information
bkchr committed Mar 15, 2023
commit fe2da6b76860674074af81bbd1a2d22f6e1ee3c6
27 changes: 18 additions & 9 deletions bin/node/cli/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ use nix::{
unistd::Pid,
};
use node_primitives::{Hash, Header};
use regex::Regex;
use std::{
io::{BufRead, BufReader, Read},
ops::{Deref, DerefMut},
path::Path,
path::{Path, PathBuf},
process::{self, Child, Command},
time::Duration,
};
Expand Down Expand Up @@ -72,7 +73,7 @@ pub async fn run_node_for_a_while(base_path: &Path, args: &[&str]) {

let mut child = KillChildOnDrop(cmd);

let (ws_url, _) = find_ws_url_from_output(stderr);
let ws_url = extract_info_from_output(stderr).0.ws_url;

// Let it produce some blocks.
wait_n_finalized_blocks(3, &ws_url).await;
Expand Down Expand Up @@ -127,18 +128,22 @@ impl DerefMut for KillChildOnDrop {
}
}

/// Read the WS address from the output.
/// Information about extracted from a running node.
pub struct NodeInfo {
pub ws_url: String,
pub db_path: PathBuf,
}

/// Extract [`NodeInfo`] from a running node by parsing its output.
///
/// This is hack to get the actual binded sockaddr because
/// substrate assigns a random port if the specified port was already binded.
pub fn find_ws_url_from_output(read: impl Read + Send) -> (String, String) {
/// Returns the [`NodeInfo`] and all the read data.
pub fn extract_info_from_output(read: impl Read + Send) -> (NodeInfo, String) {
let mut data = String::new();

let ws_url = BufReader::new(read)
.lines()
.find_map(|line| {
let line =
line.expect("failed to obtain next line from stdout for WS address discovery");
let line = line.expect("failed to obtain next line while extracting node info");
data.push_str(&line);
data.push_str("\n");

Expand All @@ -155,5 +160,9 @@ pub fn find_ws_url_from_output(read: impl Read + Send) -> (String, String) {
panic!("We should get a WebSocket address")
});

(ws_url, data)
// Database path is printed before the ws url!
let re = Regex::new(r"Database: .+ at (\S+)").unwrap();
let db_path = PathBuf::from(re.captures(data.as_str()).unwrap().get(1).unwrap().as_str());

(NodeInfo { ws_url, db_path }, data)
}
4 changes: 2 additions & 2 deletions bin/node/cli/tests/running_the_node_and_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn running_the_node_works_and_can_be_interrupted() {

let stderr = cmd.stderr.take().unwrap();

let (ws_url, _) = common::find_ws_url_from_output(stderr);
let ws_url = common::extract_info_from_output(stderr).0.ws_url;

common::wait_n_finalized_blocks(3, &ws_url).await;

Expand Down Expand Up @@ -84,7 +84,7 @@ async fn running_two_nodes_with_the_same_ws_port_should_work() {
let mut second_node = common::KillChildOnDrop(start_node());

let stderr = first_node.stderr.take().unwrap();
let (ws_url, _) = common::find_ws_url_from_output(stderr);
let ws_url = common::extract_info_from_output(stderr).0.ws_url;

common::wait_n_finalized_blocks(3, &ws_url).await;

Expand Down
19 changes: 7 additions & 12 deletions bin/node/cli/tests/temp_base_path_works.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
#![cfg(unix)]

use assert_cmd::cargo::cargo_bin;
use regex::Regex;
use std::{
io::Read,
path::PathBuf,
process::{Command, Stdio},
time::Duration,
};
Expand All @@ -42,23 +39,21 @@ async fn temp_base_path_works() {
);

let mut stderr = child.stderr.take().unwrap();
let (ws_url, mut data) = common::find_ws_url_from_output(&mut stderr);
let node_info = common::extract_info_from_output(&mut stderr).0;

// Let it produce some blocks.
common::wait_n_finalized_blocks(3, &ws_url).await;
common::wait_n_finalized_blocks(3, &node_info.ws_url).await;

// Ensure the db path exists while the node is running
assert!(node_info.db_path.exists());

child.assert_still_running();

// Stop the process
child.stop();

// Ensure the database has been deleted
stderr.read_to_string(&mut data).unwrap();
let re = Regex::new(r"Database: .+ at (\S+)").unwrap();
let db_path = PathBuf::from(re.captures(data.as_str()).unwrap().get(1).unwrap().as_str());

if db_path.exists() {
panic!("Database path `{}` wasn't deleted!", db_path.display());
if node_info.db_path.exists() {
panic!("Database path `{}` wasn't deleted!", node_info.db_path.display());
}
})
.await;
Expand Down