Skip to content

Commit 537fc5b

Browse files
authored
Revive network-test logs files in CI (#7459)
#7187 This PR adds a writer that implements `tracing_subscriber::fmt::MakeWriter`, which writes logs to separate files for each test.
1 parent 817f14c commit 537fc5b

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

.github/workflows/test-suite.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ jobs:
196196
- name: Run network tests for all known forks
197197
run: make test-network
198198
env:
199-
TEST_FEATURES: portable,ci_logger
199+
TEST_FEATURES: portable
200200
CI_LOGGER_DIR: ${{ runner.temp }}/network_test_logs
201201
- name: Upload logs
202202
uses: actions/upload-artifact@v4

beacon_node/beacon_chain/src/test_utils.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ use types::{typenum::U4294967296, *};
6969
pub const HARNESS_GENESIS_TIME: u64 = 1_567_552_690;
7070
// Environment variable to read if `fork_from_env` feature is enabled.
7171
pub const FORK_NAME_ENV_VAR: &str = "FORK_NAME";
72-
// Environment variable to read if `ci_logger` feature is enabled.
73-
pub const CI_LOGGER_DIR_ENV_VAR: &str = "CI_LOGGER_DIR";
7472

7573
// Pre-computed data column sidecar using a single static blob from:
7674
// `beacon_node/execution_layer/src/test_utils/fixtures/mainnet/test_blobs_bundle.ssz`

beacon_node/network/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,3 @@ disable-backfill = []
5757
fork_from_env = ["beacon_chain/fork_from_env"]
5858
portable = ["beacon_chain/portable"]
5959
test_logger = []
60-
ci_logger = []

beacon_node/network/src/sync/tests/lookups.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ impl TestRig {
107107
// deterministic seed
108108
let rng = ChaCha20Rng::from_seed([0u8; 32]);
109109

110+
init_tracing();
111+
110112
TestRig {
111113
beacon_processor_rx,
112114
beacon_processor_rx_queue: vec![],

beacon_node/network/src/sync/tests/mod.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ use beacon_processor::WorkEvent;
99
use lighthouse_network::NetworkGlobals;
1010
use rand_chacha::ChaCha20Rng;
1111
use slot_clock::ManualSlotClock;
12-
use std::sync::Arc;
12+
use std::fs::OpenOptions;
13+
use std::io::Write;
14+
use std::sync::{Arc, Once};
1315
use store::MemoryStore;
1416
use tokio::sync::mpsc;
17+
use tracing_subscriber::fmt::MakeWriter;
18+
use tracing_subscriber::layer::SubscriberExt;
19+
use tracing_subscriber::util::SubscriberInitExt;
1520
use types::{ChainSpec, ForkName, MinimalEthSpec as E};
1621

1722
mod lookups;
@@ -65,3 +70,55 @@ struct TestRig {
6570
fork_name: ForkName,
6671
spec: Arc<ChainSpec>,
6772
}
73+
74+
// Environment variable to read if `fork_from_env` feature is enabled.
75+
pub const FORK_NAME_ENV_VAR: &str = "FORK_NAME";
76+
// Environment variable specifying the log output directory in CI.
77+
pub const CI_LOGGER_DIR_ENV_VAR: &str = "CI_LOGGER_DIR";
78+
79+
static INIT_TRACING: Once = Once::new();
80+
81+
pub fn init_tracing() {
82+
INIT_TRACING.call_once(|| {
83+
if std::env::var(CI_LOGGER_DIR_ENV_VAR).is_ok() {
84+
// Enable logging to log files for each test and each fork.
85+
tracing_subscriber::registry()
86+
.with(
87+
tracing_subscriber::fmt::layer()
88+
.with_ansi(false)
89+
.with_writer(CILogWriter),
90+
)
91+
.init();
92+
}
93+
});
94+
}
95+
96+
// CILogWriter writes logs to separate files for each test and each fork.
97+
struct CILogWriter;
98+
99+
impl<'a> MakeWriter<'a> for CILogWriter {
100+
type Writer = Box<dyn Write + Send>;
101+
102+
// fmt::Layer calls this method each time an event is recorded.
103+
fn make_writer(&'a self) -> Self::Writer {
104+
let log_dir = std::env::var(CI_LOGGER_DIR_ENV_VAR).unwrap();
105+
let fork_name = std::env::var(FORK_NAME_ENV_VAR)
106+
.map(|s| format!("{s}_"))
107+
.unwrap_or_default();
108+
109+
// The current test name can be got via the thread name.
110+
let test_name = std::thread::current()
111+
.name()
112+
.unwrap_or("unnamed")
113+
.replace(|c: char| !c.is_alphanumeric(), "_");
114+
115+
let file_path = format!("{log_dir}/{fork_name}{test_name}.log");
116+
let file = OpenOptions::new()
117+
.append(true)
118+
.create(true)
119+
.open(&file_path)
120+
.expect("failed to open a log file");
121+
122+
Box::new(file)
123+
}
124+
}

0 commit comments

Comments
 (0)