From fd838ac4dba07120d53a16166f2dab4695af3bd0 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Thu, 15 May 2025 07:41:36 +0900 Subject: [PATCH 1/2] Revive network-test logs files in CI --- .github/workflows/test-suite.yml | 2 +- beacon_node/beacon_chain/src/test_utils.rs | 2 - beacon_node/network/Cargo.toml | 1 - beacon_node/network/src/sync/tests/lookups.rs | 2 + beacon_node/network/src/sync/tests/mod.rs | 58 ++++++++++++++++++- 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test-suite.yml b/.github/workflows/test-suite.yml index a94a19900c1..d86abd0721f 100644 --- a/.github/workflows/test-suite.yml +++ b/.github/workflows/test-suite.yml @@ -196,7 +196,7 @@ jobs: - name: Run network tests for all known forks run: make test-network env: - TEST_FEATURES: portable,ci_logger + TEST_FEATURES: portable CI_LOGGER_DIR: ${{ runner.temp }}/network_test_logs - name: Upload logs uses: actions/upload-artifact@v4 diff --git a/beacon_node/beacon_chain/src/test_utils.rs b/beacon_node/beacon_chain/src/test_utils.rs index ca083f05721..c5d87689198 100644 --- a/beacon_node/beacon_chain/src/test_utils.rs +++ b/beacon_node/beacon_chain/src/test_utils.rs @@ -69,8 +69,6 @@ use types::{typenum::U4294967296, *}; pub const HARNESS_GENESIS_TIME: u64 = 1_567_552_690; // Environment variable to read if `fork_from_env` feature is enabled. pub const FORK_NAME_ENV_VAR: &str = "FORK_NAME"; -// Environment variable to read if `ci_logger` feature is enabled. -pub const CI_LOGGER_DIR_ENV_VAR: &str = "CI_LOGGER_DIR"; // Pre-computed data column sidecar using a single static blob from: // `beacon_node/execution_layer/src/test_utils/fixtures/mainnet/test_blobs_bundle.ssz` diff --git a/beacon_node/network/Cargo.toml b/beacon_node/network/Cargo.toml index 4e369538803..e9bd850089b 100644 --- a/beacon_node/network/Cargo.toml +++ b/beacon_node/network/Cargo.toml @@ -57,4 +57,3 @@ disable-backfill = [] fork_from_env = ["beacon_chain/fork_from_env"] portable = ["beacon_chain/portable"] test_logger = [] -ci_logger = [] diff --git a/beacon_node/network/src/sync/tests/lookups.rs b/beacon_node/network/src/sync/tests/lookups.rs index 5863091cf0e..38095ec434d 100644 --- a/beacon_node/network/src/sync/tests/lookups.rs +++ b/beacon_node/network/src/sync/tests/lookups.rs @@ -107,6 +107,8 @@ impl TestRig { // deterministic seed let rng = ChaCha20Rng::from_seed([0u8; 32]); + init_tracing(); + TestRig { beacon_processor_rx, beacon_processor_rx_queue: vec![], diff --git a/beacon_node/network/src/sync/tests/mod.rs b/beacon_node/network/src/sync/tests/mod.rs index ec24ddb036a..37d9fa00eb4 100644 --- a/beacon_node/network/src/sync/tests/mod.rs +++ b/beacon_node/network/src/sync/tests/mod.rs @@ -9,9 +9,14 @@ use beacon_processor::WorkEvent; use lighthouse_network::NetworkGlobals; use rand_chacha::ChaCha20Rng; use slot_clock::ManualSlotClock; -use std::sync::Arc; +use std::fs::OpenOptions; +use std::io::Write; +use std::sync::{Arc, Once}; use store::MemoryStore; use tokio::sync::mpsc; +use tracing_subscriber::fmt::MakeWriter; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt; use types::{ChainSpec, ForkName, MinimalEthSpec as E}; mod lookups; @@ -65,3 +70,54 @@ struct TestRig { fork_name: ForkName, spec: Arc, } + +// Environment variable to read if `fork_from_env` feature is enabled. +pub const FORK_NAME_ENV_VAR: &str = "FORK_NAME"; +// Environment variable specifying the log output directory in CI. +pub const CI_LOGGER_DIR_ENV_VAR: &str = "CI_LOGGER_DIR"; + +static INIT_TRACING: Once = Once::new(); + +pub fn init_tracing() { + INIT_TRACING.call_once(|| { + if std::env::var(CI_LOGGER_DIR_ENV_VAR).is_ok() { + // Enable logging to log files for each test and each fork. + tracing_subscriber::registry() + .with( + tracing_subscriber::fmt::layer() + .with_ansi(false) + .with_writer(CILogWriter), + ) + .init(); + } + }); +} + +struct CILogWriter; + +impl<'a> MakeWriter<'a> for CILogWriter { + type Writer = Box; + + // fmt::Layer calls this method each time an event is recorded. + fn make_writer(&'a self) -> Self::Writer { + let log_dir = std::env::var(CI_LOGGER_DIR_ENV_VAR).unwrap(); + let fork_name = std::env::var(FORK_NAME_ENV_VAR) + .map(|s| format!("{s}_")) + .unwrap_or_default(); + + // The current test name can be got via the thread name. + let test_name = std::thread::current() + .name() + .unwrap_or("unnamed") + .replace(|c: char| !c.is_alphanumeric(), "_"); + + let file_path = format!("{log_dir}/{fork_name}{test_name}.log"); + let file = OpenOptions::new() + .append(true) + .create(true) + .open(&file_path) + .expect("failed to open a log file"); + + Box::new(file) + } +} From 7666b72e78b02fdab35254633ad6126f7dd171da Mon Sep 17 00:00:00 2001 From: ackintosh Date: Sat, 17 May 2025 06:35:33 +0900 Subject: [PATCH 2/2] add comment --- beacon_node/network/src/sync/tests/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon_node/network/src/sync/tests/mod.rs b/beacon_node/network/src/sync/tests/mod.rs index 37d9fa00eb4..3dca4571086 100644 --- a/beacon_node/network/src/sync/tests/mod.rs +++ b/beacon_node/network/src/sync/tests/mod.rs @@ -93,6 +93,7 @@ pub fn init_tracing() { }); } +// CILogWriter writes logs to separate files for each test and each fork. struct CILogWriter; impl<'a> MakeWriter<'a> for CILogWriter {