Skip to content

Commit

Permalink
wip: fix explorer sanity test
Browse files Browse the repository at this point in the history
having integration tests with the explorer and jormungandr will always
be good anyway, so having a way of launching an explorer in integration
tests for the purposes of testing the explorer is something that we
probably want. This is a first approximation towards that.
  • Loading branch information
ecioppettini committed Jun 8, 2021
1 parent 5de823e commit 056f2fb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 29 deletions.
2 changes: 2 additions & 0 deletions testing/jormungandr-integration-tests/build.rs
@@ -1,7 +1,9 @@
fn main() {
let jor_cli_name = option_env!("JOR_CLI_NAME").unwrap_or("jcli");
let jormungandr_name = option_env!("JORMUNGANDR_NAME").unwrap_or("jormungandr");
let jor_explorer_name = option_env!("JOR_EXPLORER_NAME").unwrap_or("explorer");
println!("cargo:rustc-env=JOR_CLI_NAME={}", jor_cli_name);
println!("cargo:rustc-env=JORMUNGANDR_NAME={}", jormungandr_name);
println!("cargo:rustc-env=JOR_EXPLORER_NAME={}", jor_explorer_name);
println!("cargo:rustc-env=RUST_BACKTRACE=full");
}
26 changes: 13 additions & 13 deletions testing/jormungandr-integration-tests/src/common/configuration.rs
Expand Up @@ -9,25 +9,25 @@ pub use jormungandr_testing_utils::testing::node::configuration::{
/// Get jormungandr executable from current environment
pub fn get_jormungandr_app() -> PathBuf {
const JORMUNGANDR_NAME: &str = env!("JORMUNGANDR_NAME");
let mut path = get_working_directory();
path.push(JORMUNGANDR_NAME);
if cfg!(windows) {
path.set_extension("exe");
}
assert!(
path.is_file(),
"File does not exist: {:?}, pwd: {:?}",
path,
env::current_dir()
);
path
get_app_from_current_dir(JORMUNGANDR_NAME)
}

/// Get jcli executable from current environment
pub fn get_jcli_app() -> PathBuf {
const JOR_CLI_NAME: &str = env!("JOR_CLI_NAME");
get_app_from_current_dir(JOR_CLI_NAME)
}

/// Get explorer executable from current environment
pub fn get_explorer_app() -> PathBuf {
const JOR_EXPLORER_NAME: &str = env!("JOR_EXPLORER_NAME");
get_app_from_current_dir(JOR_EXPLORER_NAME)
}

/// Get executable from current environment
pub fn get_app_from_current_dir(app_name: &str) -> PathBuf {
let mut path = get_working_directory();
path.push(JOR_CLI_NAME);
path.push(app_name);
if cfg!(windows) {
path.set_extension("exe");
}
Expand Down
Expand Up @@ -42,7 +42,7 @@ pub enum Status {
pub struct JormungandrProcess {
pub child: Child,
pub logger: JormungandrLogger,
temp_dir: Option<TempDir>,
pub temp_dir: Option<TempDir>,
alias: String,
p2p_public_address: Multiaddr,
rest_socket_addr: SocketAddr,
Expand Down
Expand Up @@ -4,11 +4,13 @@ use crate::common::{
use chain_impl_mockchain::fragment::FragmentId;
use chain_impl_mockchain::key::Hash;
use jormungandr_lib::interfaces::ActiveSlotCoefficient;
use jormungandr_testing_utils::stake_pool::StakePool;
use jormungandr_testing_utils::testing::node::Explorer;
use jormungandr_testing_utils::{
stake_pool::StakePool, testing::node::configuration::get_available_port,
};
use jortestkit::process::Wait;
use std::str::FromStr;
use std::time::Duration;
use std::{process::Command, str::FromStr};
use std::{process::Stdio, time::Duration};

/// test checks if there is upto date schema
/// prereq:
Expand Down Expand Up @@ -36,15 +38,12 @@ pub fn explorer_schema_diff_test() {
std::process::Command::new(
"../jormungandr-testing-utils/resources/explorer/graphql/generate_schema.sh",
)
.args(&[
jormungandr.explorer().uri(),
actual_schema_path
.path()
.as_os_str()
.to_str()
.unwrap()
.to_string(),
])
.args(&[actual_schema_path
.path()
.as_os_str()
.to_str()
.unwrap()
.to_string()])
.spawn()
.unwrap()
.wait()
Expand All @@ -65,6 +64,37 @@ pub fn explorer_sanity_test() {
let (jormungandr, initial_stake_pools) =
startup::start_stake_pool(&[faucet.clone()], &[], &mut config).unwrap();

let path = crate::common::configuration::get_explorer_app();

let block0_hash = jormungandr.rest().settings().unwrap().block0_hash;

let explorer_port = get_available_port();
let explorer_listen_address = format!("127.0.0.1:{}", explorer_port);

// TODO: encapsulate this
let _process = ExplorerProcess {
handler: Some(
Command::new(path)
.args(&[
block0_hash.as_ref(),
"--node",
format!("http://{}", jormungandr.address().to_string()).as_ref(),
"--binding-address",
explorer_listen_address.as_ref(),
])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("failed to execute explorer process"),
),
logs_dir: jormungandr
.temp_dir
.as_ref()
.map(|tmp_dir| tmp_dir.path().into()),
};

let explorer: Explorer = Explorer::new(explorer_listen_address);

let transaction = faucet
.transaction_to(
&jormungandr.genesis_block_hash(),
Expand All @@ -81,8 +111,6 @@ pub fn explorer_sanity_test() {
.send(&transaction)
.assert_in_block_with_wait(&wait);

let explorer = jormungandr.explorer();

transaction_by_id(&explorer, fragment_id);
blocks(&explorer, jormungandr.logger.get_created_blocks_hashes());
stake_pools(&explorer, &initial_stake_pools);
Expand Down Expand Up @@ -177,3 +205,34 @@ fn epoch(explorer: &Explorer) {

assert_eq!(epoch.data.unwrap().epoch.id, "1", "can't find epoch");
}

struct ExplorerProcess {
handler: Option<std::process::Child>,
logs_dir: Option<std::path::PathBuf>,
}

impl Drop for ExplorerProcess {
fn drop(&mut self) {
let output = if let Some(mut handler) = self.handler.take() {
let _ = handler.kill();
handler.wait_with_output().unwrap()
} else {
return;
};

if std::thread::panicking() {
if let Some(logs_dir) = &self.logs_dir {
println!(
"persisting explorer logs after panic: {}",
logs_dir.display()
);

std::fs::write(logs_dir.join("explorer.log"), output.stdout)
.unwrap_or_else(|e| eprint!("Could not write node logs to disk: {}", e));

std::fs::write(logs_dir.join("explorer.err"), output.stderr)
.unwrap_or_else(|e| eprint!("Could not write node logs to disk: {}", e));
}
}
}
}
Expand Up @@ -17,7 +17,7 @@ pub enum GraphQlClientError {

impl GraphQlClient {
pub fn new<S: Into<String>>(base_address: S) -> GraphQlClient {
let base_url = format!("http://{}/explorer/graphql", base_address.into());
let base_url = format!("http://{}/graphql", base_address.into());
GraphQlClient {
base_url,
print_out: true,
Expand Down

0 comments on commit 056f2fb

Please sign in to comment.