From c05a6eac5cb64b94e30f0ec0a7582fa2328fc79c Mon Sep 17 00:00:00 2001 From: Itay Tsabary Date: Mon, 4 Nov 2024 19:30:21 +0200 Subject: [PATCH] chore(tests-integration): return temp dir config paths commit-id:fb18a3b8 --- .../src/bin/run_test_rpc_state_reader.rs | 4 ++- .../src/integration_test_config_utils.rs | 32 ++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/crates/tests-integration/src/bin/run_test_rpc_state_reader.rs b/crates/tests-integration/src/bin/run_test_rpc_state_reader.rs index 2a78f4b51b..b4c18eb17b 100644 --- a/crates/tests-integration/src/bin/run_test_rpc_state_reader.rs +++ b/crates/tests-integration/src/bin/run_test_rpc_state_reader.rs @@ -8,6 +8,7 @@ use starknet_integration_tests::integration_test_utils::{ }; use starknet_integration_tests::state_reader::{spawn_test_rpc_state_reader, StorageTestSetup}; use starknet_sequencer_infra::trace_util::configure_tracing; +use tempfile::tempdir; use tracing::info; #[tokio::main] @@ -29,7 +30,8 @@ async fn main() -> anyhow::Result<()> { // Note: the batcher storage file handle is passed as a reference to maintain its ownership in // this scope, such that the handle is not dropped and the storage is maintained. - dump_config_file_changes(config, required_params); + let temp_dir = tempdir().unwrap(); + dump_config_file_changes(&config, required_params, &temp_dir); // Keep the program running so the rpc state reader server, its storage, and the batcher // storage, are all maintained. diff --git a/crates/tests-integration/src/integration_test_config_utils.rs b/crates/tests-integration/src/integration_test_config_utils.rs index d59a48b2c0..48a3734c48 100644 --- a/crates/tests-integration/src/integration_test_config_utils.rs +++ b/crates/tests-integration/src/integration_test_config_utils.rs @@ -1,14 +1,13 @@ use std::fs::File; use std::io::Write; +use std::path::PathBuf; use serde_json::{json, Value}; use starknet_sequencer_node::config::test_utils::RequiredParams; use starknet_sequencer_node::config::SequencerNodeConfig; -use tempfile::tempdir; -use tokio::io::Result; +use tempfile::TempDir; use tracing::info; // TODO(Tsabary): Move here all config-related functions from "integration_test_utils.rs". -// TODO(Tsabary): Wrap dumped config files in a temp dir. const NODE_CONFIG_CHANGES_FILE_PATH: &str = "node_integration_test_config_changes.json"; const TX_GEN_CONFIG_CHANGES_FILE_PATH: &str = "tx_gen_integration_test_config_changes.json"; @@ -37,13 +36,18 @@ macro_rules! config_fields_to_json { }; } +// TODO(Tsabary): fix comment after removing run_test_tx_generator. /// Returns config files to be supplied for the sequencer node and the transaction generator. Then /// /// Sequencer node: /// cargo run --bin starknet_sequencer_node -- --config_file NODE_CONFIG_CHANGES_FILE_PATH /// Transaction generator: /// cargo run --bin run_test_tx_generator -- --config_file TX_GEN_CONFIG_CHANGES_FILE_PATH -pub fn dump_config_file_changes(config: SequencerNodeConfig, required_params: RequiredParams) { +pub fn dump_config_file_changes( + config: &SequencerNodeConfig, + required_params: RequiredParams, + dir: &TempDir, +) -> (PathBuf, PathBuf) { // Dump config changes file for the sequencer node. let json_data = config_fields_to_json!( required_params.chain_id, @@ -56,8 +60,10 @@ pub fn dump_config_file_changes(config: SequencerNodeConfig, required_params: Re config.http_server_config.port, config.consensus_manager_config.consensus_config.start_height, ); - dump_json_data(json_data, NODE_CONFIG_CHANGES_FILE_PATH).unwrap(); + let node_config_path = dump_json_data(json_data, NODE_CONFIG_CHANGES_FILE_PATH, dir); + assert!(node_config_path.exists(), "File does not exist: {:?}", node_config_path); + // TODO(Tsabary): should be deprecated. // Dump config changes file for the transaction generator. let json_data = config_fields_to_json!( required_params.chain_id, @@ -66,22 +72,24 @@ pub fn dump_config_file_changes(config: SequencerNodeConfig, required_params: Re config.http_server_config.ip, config.http_server_config.port, ); - dump_json_data(json_data, TX_GEN_CONFIG_CHANGES_FILE_PATH).unwrap(); + let tx_gen_config_path = dump_json_data(json_data, TX_GEN_CONFIG_CHANGES_FILE_PATH, dir); + assert!(tx_gen_config_path.exists(), "File does not exist: {:?}", tx_gen_config_path); + + (node_config_path, tx_gen_config_path) } /// Dumps the input JSON data to a file at the specified path. -fn dump_json_data(json_data: Value, path: &str) -> Result<()> { - let dir = tempdir()?; +fn dump_json_data(json_data: Value, path: &str, dir: &TempDir) -> PathBuf { let temp_dir_path = dir.path().join(path); // Serialize the JSON data to a pretty-printed string let json_string = serde_json::to_string_pretty(&json_data).unwrap(); // Write the JSON string to a file - let mut file = File::create(&temp_dir_path)?; - file.write_all(json_string.as_bytes())?; + let mut file = File::create(&temp_dir_path).unwrap(); + file.write_all(json_string.as_bytes()).unwrap(); - info!("Writing required config changes to: {:?}", temp_dir_path); - Ok(()) + info!("Writing required config changes to: {:?}", &temp_dir_path); + temp_dir_path } /// Strips the "config." and "required_params." prefixes from the input string.