Skip to content

Commit

Permalink
Test that Zebra warns the user when it cannot parse zebrad.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
upbqdn committed Jun 26, 2022
1 parent 34e487b commit c93bd89
Showing 1 changed file with 74 additions and 6 deletions.
80 changes: 74 additions & 6 deletions zebrad/tests/acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@
//!
//! Please refer to the documentation of each test for more information.
use std::{collections::HashSet, env, path::PathBuf};
use std::{collections::HashSet, env, fs, path::PathBuf, time::Duration};

use color_eyre::{
eyre::{Result, WrapErr},
eyre::{eyre, Result, WrapErr},
Help,
};

Expand Down Expand Up @@ -355,7 +355,6 @@ fn misconfigured_ephemeral_missing_directory() -> Result<()> {
}

fn ephemeral(cache_dir_config: EphemeralConfig, cache_dir_check: EphemeralCheck) -> Result<()> {
use std::fs;
use std::io::ErrorKind;

zebra_test::init();
Expand Down Expand Up @@ -503,12 +502,14 @@ fn version_args() -> Result<()> {
}

#[test]
fn valid_generated_config_test() -> Result<()> {
fn config_test() -> Result<()> {
// Unlike the other tests, these tests can not be run in parallel, because
// they use the generated config. So parallel execution can cause port and
// cache conflicts.
valid_generated_config("start", "Starting zebrad")?;

invalid_generated_config()?;

Ok(())
}

Expand Down Expand Up @@ -561,6 +562,75 @@ fn valid_generated_config(command: &str, expect_stdout_line_contains: &str) -> R
Ok(())
}

/// Checks that Zebra prints an informative message when it cannot parse the
/// config file.
fn invalid_generated_config() -> Result<()> {
zebra_test::init();

let testdir = &testdir()?;

// Add a config file name to tempdir path.
let config_path = testdir.path().join("zebrad.toml");

// Generate a valid config file in the temp dir.
let child = testdir.spawn_child(args!["generate", "-o": config_path.to_str().unwrap()])?;

let output = child.wait_with_output()?;
let output = output.assert_success()?;

assert_with_context!(
config_path.exists(),
&output,
"generated config file not found"
);

// Load the valid config file that Zebra generated.
let mut config_file = fs::read_to_string(config_path.to_str().unwrap()).unwrap();

// Let's now alter the config file so that it contains a deprecated format
// of `mempool.eviction_memory_time`.

config_file = config_file
.lines()
// Remove the valid `eviction_memory_time` key/value pair from the
// config.
.filter(|line| !line.contains("eviction_memory_time"))
.map(|line| line.to_owned() + "\n")
.collect();

// Append the `eviction_memory_time` key/value pair in a deprecated format.
config_file += r"
[mempool.eviction_memory_time]
nanos = 0
secs = 3600
";

// Write the altered config file so that Zebra can pick it up.
fs::write(config_path.to_str().unwrap(), config_file.as_bytes())
.expect("Could not write the altered config file.");

// Run Zebra in a temp dir so that it loads the config.
let mut child = testdir.spawn_child(args!["start"])?;

// Return an error if Zebra is running for more than two seconds.
//
// Since the config is invalid, Zebra should terminate instantly after its
// start. Two seconds should be sufficient for Zebra to read the config file
// and terminate.
std::thread::sleep(Duration::from_secs(2));
if child.is_running() {
child.kill()?;
return Err(eyre!("Zebra should not be running anymore."));
}

let output = child.wait_with_output()?;

// Check that Zebra produced an informative message.
output.stderr_contains("Zebra could not parse the provided config file. This might mean you are using a deprecated format of the file.")?;

Ok(())
}
/// Test if `zebrad` can sync the first checkpoint on mainnet.
///
/// The first checkpoint contains a single genesis block.
Expand Down Expand Up @@ -1558,8 +1628,6 @@ where
// See #1781.
#[cfg(target_os = "linux")]
if node2.is_running() {
use color_eyre::eyre::eyre;

return node2
.kill_on_error::<(), _>(Err(eyre!(
"conflicted node2 was still running, but the test expected a panic"
Expand Down

0 comments on commit c93bd89

Please sign in to comment.