From 056f2fba8866d3c4856ac103656d50b45b44573c Mon Sep 17 00:00:00 2001 From: Enzo Cioppettini Date: Mon, 7 Jun 2021 01:37:29 -0300 Subject: [PATCH] wip: fix explorer sanity test 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. --- .../jormungandr-integration-tests/build.rs | 2 + .../src/common/configuration.rs | 26 +++--- .../src/common/jormungandr/process.rs | 2 +- .../src/jormungandr/explorer/mod.rs | 87 ++++++++++++++++--- .../src/testing/node/explorer/client.rs | 2 +- 5 files changed, 90 insertions(+), 29 deletions(-) diff --git a/testing/jormungandr-integration-tests/build.rs b/testing/jormungandr-integration-tests/build.rs index 608fa48a1a..7bacb1e536 100644 --- a/testing/jormungandr-integration-tests/build.rs +++ b/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"); } diff --git a/testing/jormungandr-integration-tests/src/common/configuration.rs b/testing/jormungandr-integration-tests/src/common/configuration.rs index a75c7f2602..3c815a5936 100644 --- a/testing/jormungandr-integration-tests/src/common/configuration.rs +++ b/testing/jormungandr-integration-tests/src/common/configuration.rs @@ -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"); } diff --git a/testing/jormungandr-integration-tests/src/common/jormungandr/process.rs b/testing/jormungandr-integration-tests/src/common/jormungandr/process.rs index 673c9ccaa7..9d025754df 100644 --- a/testing/jormungandr-integration-tests/src/common/jormungandr/process.rs +++ b/testing/jormungandr-integration-tests/src/common/jormungandr/process.rs @@ -42,7 +42,7 @@ pub enum Status { pub struct JormungandrProcess { pub child: Child, pub logger: JormungandrLogger, - temp_dir: Option, + pub temp_dir: Option, alias: String, p2p_public_address: Multiaddr, rest_socket_addr: SocketAddr, diff --git a/testing/jormungandr-integration-tests/src/jormungandr/explorer/mod.rs b/testing/jormungandr-integration-tests/src/jormungandr/explorer/mod.rs index 4377a35fa2..b1e1d1c0b0 100644 --- a/testing/jormungandr-integration-tests/src/jormungandr/explorer/mod.rs +++ b/testing/jormungandr-integration-tests/src/jormungandr/explorer/mod.rs @@ -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: @@ -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() @@ -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(), @@ -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); @@ -177,3 +205,34 @@ fn epoch(explorer: &Explorer) { assert_eq!(epoch.data.unwrap().epoch.id, "1", "can't find epoch"); } + +struct ExplorerProcess { + handler: Option, + logs_dir: Option, +} + +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)); + } + } + } +} diff --git a/testing/jormungandr-testing-utils/src/testing/node/explorer/client.rs b/testing/jormungandr-testing-utils/src/testing/node/explorer/client.rs index 4e0addfa4b..c51821506e 100644 --- a/testing/jormungandr-testing-utils/src/testing/node/explorer/client.rs +++ b/testing/jormungandr-testing-utils/src/testing/node/explorer/client.rs @@ -17,7 +17,7 @@ pub enum GraphQlClientError { impl GraphQlClient { pub fn new>(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,