Fix CommandLineTest port conflicts on CI (#5908)

* Fix port conflicts on CI.
This commit is contained in:
Jimmy Chen
2024-06-18 01:05:15 +10:00
committed by GitHub
parent 1eb8694a86
commit bc044ed275
3 changed files with 71 additions and 37 deletions

View File

@@ -688,9 +688,15 @@ fn run<E: EthSpec>(
let executor = context.executor.clone(); let executor = context.executor.clone();
let mut config = beacon_node::get_config::<E>(matches, &context)?; let mut config = beacon_node::get_config::<E>(matches, &context)?;
config.logger_config = logger_config; config.logger_config = logger_config;
let shutdown_flag = matches.get_flag("immediate-shutdown");
// Dump configs if `dump-config` or `dump-chain-config` flags are set // Dump configs if `dump-config` or `dump-chain-config` flags are set
clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?; clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?;
let shutdown_flag = matches.get_flag("immediate-shutdown");
if shutdown_flag {
info!(log, "Beacon node immediate shutdown triggered.");
return Ok(());
}
executor.clone().spawn( executor.clone().spawn(
async move { async move {
if let Err(e) = ProductionBeaconNode::new(context.clone(), config).await { if let Err(e) = ProductionBeaconNode::new(context.clone(), config).await {
@@ -700,10 +706,6 @@ fn run<E: EthSpec>(
let _ = executor let _ = executor
.shutdown_sender() .shutdown_sender()
.try_send(ShutdownReason::Failure("Failed to start beacon node")); .try_send(ShutdownReason::Failure("Failed to start beacon node"));
} else if shutdown_flag {
let _ = executor.shutdown_sender().try_send(ShutdownReason::Success(
"Beacon node immediate shutdown triggered.",
));
} }
}, },
"beacon_node", "beacon_node",
@@ -715,31 +717,31 @@ fn run<E: EthSpec>(
let executor = context.executor.clone(); let executor = context.executor.clone();
let config = validator_client::Config::from_cli(matches, context.log()) let config = validator_client::Config::from_cli(matches, context.log())
.map_err(|e| format!("Unable to initialize validator config: {}", e))?; .map_err(|e| format!("Unable to initialize validator config: {}", e))?;
let shutdown_flag = matches.get_flag("immediate-shutdown");
// Dump configs if `dump-config` or `dump-chain-config` flags are set // Dump configs if `dump-config` or `dump-chain-config` flags are set
clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?; clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?;
if !shutdown_flag {
executor.clone().spawn( let shutdown_flag = matches.get_flag("immediate-shutdown");
async move { if shutdown_flag {
if let Err(e) = ProductionValidatorClient::new(context, config) info!(log, "Validator client immediate shutdown triggered.");
.and_then(|mut vc| async move { vc.start_service().await }) return Ok(());
.await
{
crit!(log, "Failed to start validator client"; "reason" => e);
// Ignore the error since it always occurs during normal operation when
// shutting down.
let _ = executor.shutdown_sender().try_send(ShutdownReason::Failure(
"Failed to start validator client",
));
}
},
"validator_client",
);
} else {
let _ = executor.shutdown_sender().try_send(ShutdownReason::Success(
"Validator client immediate shutdown triggered.",
));
} }
executor.clone().spawn(
async move {
if let Err(e) = ProductionValidatorClient::new(context, config)
.and_then(|mut vc| async move { vc.start_service().await })
.await
{
crit!(log, "Failed to start validator client"; "reason" => e);
// Ignore the error since it always occurs during normal operation when
// shutting down.
let _ = executor
.shutdown_sender()
.try_send(ShutdownReason::Failure("Failed to start validator client"));
}
},
"validator_client",
);
} }
_ => { _ => {
crit!(log, "No subcommand supplied. See --help ."); crit!(log, "No subcommand supplied. See --help .");

View File

@@ -58,13 +58,22 @@ impl CommandLineTest {
fn run_with_zero_port(&mut self) -> CompletedTest<Config> { fn run_with_zero_port(&mut self) -> CompletedTest<Config> {
// Required since Deneb was enabled on mainnet. // Required since Deneb was enabled on mainnet.
self.cmd.arg("--allow-insecure-genesis-sync"); self.set_allow_insecure_genesis_sync()
self.run_with_zero_port_and_no_genesis_sync() .run_with_zero_port_and_no_genesis_sync()
} }
fn run_with_zero_port_and_no_genesis_sync(&mut self) -> CompletedTest<Config> { fn run_with_zero_port_and_no_genesis_sync(&mut self) -> CompletedTest<Config> {
self.set_zero_port().run()
}
fn set_allow_insecure_genesis_sync(&mut self) -> &mut Self {
self.cmd.arg("--allow-insecure-genesis-sync");
self
}
fn set_zero_port(&mut self) -> &mut Self {
self.cmd.arg("-z"); self.cmd.arg("-z");
self.run() self
} }
} }
@@ -101,9 +110,20 @@ fn staking_flag() {
} }
#[test] #[test]
#[should_panic]
fn allow_insecure_genesis_sync_default() { fn allow_insecure_genesis_sync_default() {
CommandLineTest::new().run_with_zero_port_and_no_genesis_sync(); CommandLineTest::new()
.run_with_zero_port_and_no_genesis_sync()
.with_config(|config| {
assert_eq!(config.allow_insecure_genesis_sync, false);
});
}
#[test]
#[should_panic]
fn insecure_genesis_sync_should_panic() {
CommandLineTest::new()
.set_zero_port()
.run_with_immediate_shutdown(false);
} }
#[test] #[test]
@@ -2252,7 +2272,9 @@ fn ensure_panic_on_failed_launch() {
CommandLineTest::new() CommandLineTest::new()
.flag("slasher", None) .flag("slasher", None)
.flag("slasher-chunk-size", Some("10")) .flag("slasher-chunk-size", Some("10"))
.run_with_zero_port() .set_allow_insecure_genesis_sync()
.set_zero_port()
.run_with_immediate_shutdown(false)
.with_config(|config| { .with_config(|config| {
let slasher_config = config let slasher_config = config
.slasher .slasher

View File

@@ -21,12 +21,19 @@ pub trait CommandLineTestExec {
self self
} }
fn run(&mut self) -> CompletedTest<Self::Config> {
self.run_with_immediate_shutdown(true)
}
/// Executes the `Command` returned by `Self::cmd_mut` with temporary data directory, dumps /// Executes the `Command` returned by `Self::cmd_mut` with temporary data directory, dumps
/// the configuration and shuts down immediately. /// the configuration and shuts down immediately if `immediate_shutdown` is set to true.
/// ///
/// Options `--datadir`, `--dump-config`, `--dump-chain-config` and `--immediate-shutdown` must /// Options `--datadir`, `--dump-config`, `--dump-chain-config` and `--immediate-shutdown` must
/// not be set on the command. /// not be set on the command.
fn run(&mut self) -> CompletedTest<Self::Config> { fn run_with_immediate_shutdown(
&mut self,
immediate_shutdown: bool,
) -> CompletedTest<Self::Config> {
// Setup temp directory. // Setup temp directory.
let tmp_dir = TempDir::new().expect("Unable to create temporary directory"); let tmp_dir = TempDir::new().expect("Unable to create temporary directory");
let tmp_config_path: PathBuf = tmp_dir.path().join("config.json"); let tmp_config_path: PathBuf = tmp_dir.path().join("config.json");
@@ -39,8 +46,11 @@ pub trait CommandLineTestExec {
.arg(format!("--{}", "dump-config")) .arg(format!("--{}", "dump-config"))
.arg(tmp_config_path.as_os_str()) .arg(tmp_config_path.as_os_str())
.arg(format!("--{}", "dump-chain-config")) .arg(format!("--{}", "dump-chain-config"))
.arg(tmp_chain_config_path.as_os_str()) .arg(tmp_chain_config_path.as_os_str());
.arg("--immediate-shutdown");
if immediate_shutdown {
cmd.arg("--immediate-shutdown");
}
// Run the command. // Run the command.
let output = output_result(cmd); let output = output_result(cmd);