From 95f9ace8e6554d682ef85d06f2a65def8701b90d Mon Sep 17 00:00:00 2001 From: Gustavo Valverde Date: Mon, 16 Sep 2024 10:22:17 +0100 Subject: [PATCH] fix(tests): use `ZEBRA_CACHED_STATE_DIR` for tests instead of hard-coded `/zebrad-cache` dir --- docker/entrypoint.sh | 11 ++++------- zebrad/tests/acceptance.rs | 24 ++++++++++++++---------- zebrad/tests/common/sync.rs | 16 +++++++++++++--- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 0af0f6c8c61..b6613e97157 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -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. @@ -367,4 +364,4 @@ case "$1" in exec "$@" fi ;; -esac \ No newline at end of file +esac diff --git a/zebrad/tests/acceptance.rs b/zebrad/tests/acceptance.rs index c21b0a0e3e3..cd3572ce3f2 100644 --- a/zebrad/tests/acceptance.rs +++ b/zebrad/tests/acceptance.rs @@ -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: //! @@ -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 //! ``` @@ -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: //! diff --git a/zebrad/tests/common/sync.rs b/zebrad/tests/common/sync.rs index ff5234c2b1e..f1a6a46caa2 100644 --- a/zebrad/tests/common/sync.rs +++ b/zebrad/tests/common/sync.rs @@ -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; @@ -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 { 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.) @@ -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"])?