Skip to content

Commit

Permalink
fix(tests): use ZEBRA_CACHED_STATE_DIR for tests instead of hard-co…
Browse files Browse the repository at this point in the history
…ded `/zebrad-cache` dir
  • Loading branch information
gustavovalverde committed Sep 16, 2024
1 parent dea386c commit 95f9ace
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
11 changes: 4 additions & 7 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,20 @@ case "$1" in
# Run a Zebra full sync test on mainnet.
run_cargo_test "${ENTRYPOINT_FEATURES}" "full_sync_mainnet"
# List directory generated by test
# TODO: replace with ${ZEBRA_CACHED_STATE_DIR} in Rust and workflows
check_directory_files "/zebrad-cache"
check_directory_files "${ZEBRA_CACHED_STATE_DIR}"

elif [[ -n "${FULL_SYNC_TESTNET_TIMEOUT_MINUTES}" ]]; then
# Run a Zebra full sync test on testnet.
run_cargo_test "${ENTRYPOINT_FEATURES}" "full_sync_testnet"
# List directory generated by test
# TODO: replace with ${ZEBRA_CACHED_STATE_DIR} in Rust and workflows
check_directory_files "/zebrad-cache"
check_directory_files "${ZEBRA_CACHED_STATE_DIR}"

elif [[ "${TEST_DISK_REBUILD}" -eq "1" ]]; then
# Run a Zebra sync up to the mandatory checkpoint.
#
# TODO: use environmental variables instead of Rust features (part of #2995)
run_cargo_test "test_sync_to_mandatory_checkpoint_${NETWORK,,},${ENTRYPOINT_FEATURES}" "sync_to_mandatory_checkpoint_${NETWORK,,}"
# TODO: replace with ${ZEBRA_CACHED_STATE_DIR} in Rust and workflows
check_directory_files "/zebrad-cache"
check_directory_files "${ZEBRA_CACHED_STATE_DIR}"

elif [[ "${TEST_UPDATE_SYNC}" -eq "1" ]]; then
# Run a Zebra sync starting at the cached tip, and syncing to the latest tip.
Expand Down Expand Up @@ -367,4 +364,4 @@ case "$1" in
exec "$@"
fi
;;
esac
esac
24 changes: 14 additions & 10 deletions zebrad/tests/acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
//! - `FULL_SYNC_MAINNET_TIMEOUT_MINUTES` env variable: The total number of minutes we
//! will allow this test to run or give up. Value for the Mainnet full sync tests.
//! - `FULL_SYNC_TESTNET_TIMEOUT_MINUTES` env variable: The total number of minutes we
//! will allow this test to run or give up. Value for the Testnet ful sync tests.
//! - `/zebrad-cache` directory: For some sync tests, this needs to be created in
//! the file system, the created directory should have write permissions.
//! will allow this test to run or give up. Value for the Testnet full sync tests.
//! - `ZEBRA_CACHED_STATE_DIR` env variable: The path to a Zebra cached state directory.
//! If not set, it defaults to `/zebrad-cache`. For some sync tests, this directory needs to be
//! created in the file system with write permissions.
//!
//! Here are some examples on how to run each of the tests:
//!
Expand All @@ -40,13 +41,15 @@
//!
//! $ cargo test sync_large_checkpoints_mempool_mainnet -- --ignored --nocapture
//!
//! $ sudo mkdir /zebrad-cache
//! $ sudo chmod 777 /zebrad-cache
//! $ export ZEBRA_CACHED_STATE_DIR="/zebrad-cache"
//! $ sudo mkdir -p "$ZEBRA_CACHED_STATE_DIR"
//! $ sudo chmod 777 "$ZEBRA_CACHED_STATE_DIR"
//! $ export FULL_SYNC_MAINNET_TIMEOUT_MINUTES=600
//! $ cargo test full_sync_mainnet -- --ignored --nocapture
//!
//! $ sudo mkdir /zebrad-cache
//! $ sudo chmod 777 /zebrad-cache
//! $ export ZEBRA_CACHED_STATE_DIR="/zebrad-cache"
//! $ sudo mkdir -p "$ZEBRA_CACHED_STATE_DIR"
//! $ sudo chmod 777 "$ZEBRA_CACHED_STATE_DIR"
//! $ export FULL_SYNC_TESTNET_TIMEOUT_MINUTES=600
//! $ cargo test full_sync_testnet -- --ignored --nocapture
//! ```
Expand All @@ -67,9 +70,10 @@
//! at least the `ZEBRA_TEST_LIGHTWALLETD` environment variable is present:
//!
//! - `ZEBRA_TEST_LIGHTWALLETD` env variable: Needs to be present to run any of the lightwalletd tests.
//! - `ZEBRA_CACHED_STATE_DIR` env var: The path to a zebra blockchain database.
//! - `LIGHTWALLETD_DATA_DIR` env variable. The path to a lightwalletd database.
//! - `--features lightwalletd-grpc-tests` cargo flag. The flag given to cargo to build the source code of the running test.
//! - `ZEBRA_CACHED_STATE_DIR` env variable: The path to a Zebra cached state directory.
//! If not set, it defaults to `/zebrad-cache`.
//! - `LIGHTWALLETD_DATA_DIR` env variable: The path to a lightwalletd database.
//! - `--features lightwalletd-grpc-tests` cargo flag: The flag given to cargo to build the source code of the running test.
//!
//! Here are some examples of running each test:
//!
Expand Down
16 changes: 13 additions & 3 deletions zebrad/tests/common/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! Test functions in this file will not be run.
//! This file is only for test library code.

use std::{path::PathBuf, time::Duration};
use std::{path::PathBuf, time::Duration, env};

use tempfile::TempDir;

Expand Down Expand Up @@ -326,10 +326,20 @@ pub fn check_sync_logs_until(
Ok(zebrad)
}

/// Returns the cache directory for Zebra's state.
///
/// It checks the `ZEBRA_CACHED_STATE_DIR` environment variable and returns its value if set.
/// Otherwise, it defaults to `"/zebrad-cache"`.
fn get_zebra_cached_state_dir() -> PathBuf {
env::var("ZEBRA_CACHED_STATE_DIR")
.unwrap_or_else(|_| "/zebrad-cache".to_string())
.into()
}

/// Returns a test config for caching Zebra's state up to the mandatory checkpoint.
pub fn cached_mandatory_checkpoint_test_config(network: &Network) -> Result<ZebradConfig> {
let mut config = persistent_test_config(network)?;
config.state.cache_dir = "/zebrad-cache".into();
config.state.cache_dir = get_zebra_cached_state_dir();

// To get to the mandatory checkpoint, we need to sync lots of blocks.
// (Most tests use a smaller limit to minimise redundant block downloads.)
Expand Down Expand Up @@ -377,7 +387,7 @@ pub fn create_cached_database_height(
config.state.debug_stop_at_height = Some(height.0);
config.consensus.checkpoint_sync = checkpoint_sync;

let dir = PathBuf::from("/zebrad-cache");
let dir = get_zebra_cached_state_dir();
let mut child = dir
.with_exact_config(&config)?
.spawn_child(args!["start"])?
Expand Down

0 comments on commit 95f9ace

Please sign in to comment.