Skip to content

Commit

Permalink
chore(tests-integration): return temp dir config paths
Browse files Browse the repository at this point in the history
commit-id:fb18a3b8
  • Loading branch information
Itay-Tsabary-Starkware committed Nov 5, 2024
1 parent 10beddd commit c05a6ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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.
Expand Down
32 changes: 20 additions & 12 deletions crates/tests-integration/src/integration_test_config_utils.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit c05a6ea

Please sign in to comment.