update acceptance tests

This commit is contained in:
Jane Lusby 2020-10-29 18:00:02 -07:00 committed by Deirdre Connolly
parent d093b4e528
commit 4bfe747f34
2 changed files with 78 additions and 57 deletions

View File

@ -2,16 +2,16 @@ use color_eyre::{
eyre::{eyre, Context, Report, Result}, eyre::{eyre, Context, Report, Result},
Help, SectionExt, Help, SectionExt,
}; };
use tempdir::TempDir;
use tracing::instrument; use tracing::instrument;
use std::convert::Infallible as NoDir; use std::{convert::Infallible as NoDir, io::BufRead};
use std::fmt::Write as _; use std::{
#[cfg(unix)] fmt::Write as _,
use std::os::unix::process::ExitStatusExt; io::{BufReader, Lines},
};
#[cfg(unix)]
use std::{io::Read, os::unix::process::ExitStatusExt};
use std::{ use std::{
borrow::Borrow,
io::{BufRead, BufReader, Lines, Read},
path::Path, path::Path,
process::{Child, ChildStdout, Command, ExitStatus, Output, Stdio}, process::{Child, ChildStdout, Command, ExitStatus, Output, Stdio},
time::{Duration, Instant}, time::{Duration, Instant},
@ -98,7 +98,7 @@ impl CommandExt for Command {
/// `zebra_test::command` without running `zebrad`. /// `zebra_test::command` without running `zebrad`.
pub trait TestDirExt pub trait TestDirExt
where where
Self: Borrow<TempDir> + Sized, Self: AsRef<Path> + Sized,
{ {
/// Spawn `cmd` with `args` as a child process in this test directory, /// Spawn `cmd` with `args` as a child process in this test directory,
/// potentially taking ownership of the tempdir for the duration of the /// potentially taking ownership of the tempdir for the duration of the
@ -108,11 +108,10 @@ where
impl<T> TestDirExt for T impl<T> TestDirExt for T
where where
Self: Borrow<TempDir> + Sized, Self: AsRef<Path> + Sized,
{ {
fn spawn_child_with_command(self, cmd: &str, args: &[&str]) -> Result<TestChild<Self>> { fn spawn_child_with_command(self, cmd: &str, args: &[&str]) -> Result<TestChild<Self>> {
let tempdir = self.borrow(); let mut cmd = test_cmd(cmd, self.as_ref())?;
let mut cmd = test_cmd(cmd, tempdir.path())?;
Ok(cmd Ok(cmd
.args(args) .args(args)

View File

@ -18,10 +18,10 @@
#![allow(clippy::field_reassign_with_default)] #![allow(clippy::field_reassign_with_default)]
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use eyre::{eyre, WrapErr}; use eyre::WrapErr;
use tempdir::TempDir; use tempdir::TempDir;
use std::{borrow::Borrow, env, fs, io::Write, path::Path, time::Duration}; use std::{env, fs, io::Write, path::Path, path::PathBuf, time::Duration};
use zebra_chain::{ use zebra_chain::{
block::Height, block::Height,
@ -55,7 +55,7 @@ fn testdir() -> Result<TempDir> {
/// directory for `zebrad`. /// directory for `zebrad`.
trait ZebradTestDirExt trait ZebradTestDirExt
where where
Self: Borrow<TempDir> + Sized, Self: AsRef<Path> + Sized,
{ {
/// Spawn `zebrad` with `args` as a child process in this test directory, /// Spawn `zebrad` with `args` as a child process in this test directory,
/// potentially taking ownership of the tempdir for the duration of the /// potentially taking ownership of the tempdir for the duration of the
@ -73,11 +73,11 @@ where
impl<T> ZebradTestDirExt for T impl<T> ZebradTestDirExt for T
where where
Self: TestDirExt + Borrow<TempDir> + Sized, Self: TestDirExt + AsRef<Path> + Sized,
{ {
fn spawn_child(self, args: &[&str]) -> Result<TestChild<Self>> { fn spawn_child(self, args: &[&str]) -> Result<TestChild<Self>> {
let tempdir = self.borrow(); let path = self.as_ref();
let default_config_path = tempdir.path().join("zebrad.toml"); let default_config_path = path.join("zebrad.toml");
if default_config_path.exists() { if default_config_path.exists() {
let mut extra_args: Vec<_> = Vec::new(); let mut extra_args: Vec<_> = Vec::new();
@ -96,7 +96,7 @@ where
} }
fn with_config(self, mut config: ZebradConfig) -> Result<Self> { fn with_config(self, mut config: ZebradConfig) -> Result<Self> {
let dir = self.borrow().path(); let dir = self.as_ref();
if !config.state.ephemeral { if !config.state.ephemeral {
let cache_dir = dir.join("state"); let cache_dir = dir.join("state");
@ -111,7 +111,7 @@ where
} }
fn replace_config(self, mut config: ZebradConfig) -> Result<Self> { fn replace_config(self, mut config: ZebradConfig) -> Result<Self> {
let dir = self.borrow().path(); let dir = self.as_ref();
if !config.state.ephemeral { if !config.state.ephemeral {
let cache_dir = dir.join("state"); let cache_dir = dir.join("state");
@ -648,56 +648,78 @@ fn sync_until(
Ok(child.dir) Ok(child.dir)
} }
#[test] fn cached_sapling_test_config() -> Result<ZebradConfig> {
#[ignore] let mut config = persistent_test_config()?;
fn sync_to_sapling() -> Result<()> { config.consensus.checkpoint_sync = true;
let mut testdir = testdir()?; config.state.cache_dir = "/zebrad-cache".into();
let backup_dir = dirs::cache_dir() config.state.memory_cache_bytes = 52428800;
.ok_or_else(|| eyre!("no cache dir found"))? config.tracing.endpoint_addr = Some("0.0.0.0:3000".parse().unwrap());
.join("sapling_backup"); Ok(config)
}
if !backup_dir.exists() { fn create_cached_database_height(network: Network, height: Height) -> Result<()> {
testdir = create_sapling_backup(testdir, &backup_dir)?; println!("Creating cached database");
} // 8 hours
let timeout = Duration::from_secs(60 * 60 * 8);
for entry in std::fs::read_dir(backup_dir)? { // Use a persistent state, so we can handle large syncs
let entry = entry?; let mut config = cached_sapling_test_config()?;
std::process::Command::new("cp") // TODO: add convenience methods?
.arg("-r") config.network.network = network;
.arg(entry.path()) config.state.debug_stop_at_height = Some(height.0);
.arg(testdir.path()) let dir = PathBuf::from("/");
.status2()? fs::File::create(dir.join("zebrad.toml"))?.write_all(toml::to_string(&config)?.as_bytes())?;
.assert_success()?;
}
let height = LARGE_CHECKPOINT_TEST_HEIGHT; let mut child = dir.spawn_child(&["start"])?.with_timeout(timeout);
let network = Mainnet;
let timeout = Duration::from_secs(60);
println!("Running real sync"); // TODO: is there a way to check for testnet or mainnet here?
sync_until(height, network, STOP_AT_HEIGHT_REGEX, timeout, testdir)?; // For example: "network=Mainnet" or "network=Testnet"
child.expect_stdout("network: Mainnet,")?;
child.expect_stdout(STOP_AT_HEIGHT_REGEX)?;
child.kill()?;
Ok(()) Ok(())
} }
fn create_sapling_backup(testdir: TempDir, dest: &Path) -> Result<TempDir> { fn create_cached_database(network: Network) -> Result<()> {
println!("Creating backup database");
// 1 hour
let timeout = Duration::from_secs(60 * 60);
let network = Mainnet;
let height = NetworkUpgrade::Sapling.activation_height(network).unwrap(); let height = NetworkUpgrade::Sapling.activation_height(network).unwrap();
create_cached_database_height(network, height)
}
let tempdir = sync_until(height, network, STOP_AT_HEIGHT_REGEX, timeout, testdir)?; fn sync_past_sapling(network: Network) -> Result<()> {
let path = tempdir.path(); let height = NetworkUpgrade::Sapling.activation_height(network).unwrap() + 1000;
create_cached_database_height(network, height.unwrap())
}
std::process::Command::new("cp") #[test]
.arg("-r") #[ignore]
.arg(path) fn create_mainnet_cache() {
.arg(dest) zebra_test::init();
.status2()? let network = Mainnet;
.assert_success()?; create_cached_database(network).unwrap();
}
#[test]
#[ignore]
fn create_testnet_cache() {
zebra_test::init();
let network = Testnet;
create_cached_database(network).unwrap();
}
Ok(tempdir) #[test]
#[ignore]
fn sync_past_sapling_mainnet() {
zebra_test::init();
let network = Mainnet;
sync_past_sapling(network).unwrap();
}
#[test]
#[ignore]
fn sync_past_sapling_testnet() {
zebra_test::init();
let network = Testnet;
sync_past_sapling(network).unwrap();
} }
#[tokio::test] #[tokio::test]