From 1d51caa920e37ee89c8a9d4e248246b382a848b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Klaehn?= Date: Mon, 8 Apr 2024 13:26:37 +0300 Subject: [PATCH] fix(tests): disable the metrics port of all cli tests (#2154) fix(tests): disable the metrics port of all cli tests otherwise they won't run due to "Address already in use (os error 48)". Somehow one is missing... This still does not work: ``` > cargo test -- --ignored cli running 11 tests test cli_provide_addresses ... ok test cli_bao_store_migration ... ok test cli_provide_one_file_single_stdout ... ok test cli_provide_one_file_basic ... FAILED test cli_provide_folder ... FAILED test cli_provide_persistence ... ok test cli_provide_one_file_single_path ... ok test cli_provide_from_stdin_to_stdout ... FAILED test cli_provide_tree ... FAILED test cli_rpc_lock_restart ... ok ``` ## Description ## Notes & open questions ## Change checklist - [ ] Self-review. - [ ] Documentation updates if relevant. - [x] Tests if relevant. --- iroh-cli/src/commands.rs | 31 +++++++++++++++++++++++------ iroh-cli/tests/cli.rs | 42 +++++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/iroh-cli/src/commands.rs b/iroh-cli/src/commands.rs index f1310d7082..4f1ebe08aa 100644 --- a/iroh-cli/src/commands.rs +++ b/iroh-cli/src/commands.rs @@ -2,6 +2,7 @@ use std::path::{Path, PathBuf}; use anyhow::{ensure, Context, Result}; use clap::Parser; +use derive_more::FromStr; use iroh::client::quic::Iroh as IrohRpc; use crate::config::{ConsoleEnv, NodeConfig}; @@ -43,7 +44,26 @@ pub(crate) struct Cli { /// Port to serve metrics on. -1 to disable. #[clap(long)] - pub(crate) metrics_port: Option, + pub(crate) metrics_port: Option, +} + +#[derive(Debug, Clone)] +pub(crate) enum MetricsPort { + Disabled, + Port(u16), +} + +impl FromStr for MetricsPort { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + if s.to_ascii_lowercase() == "disabled" { + Ok(MetricsPort::Disabled) + } else { + let port = s.parse()?; + Ok(MetricsPort::Port(port)) + } + } } #[derive(Parser, Debug, Clone)] @@ -142,11 +162,10 @@ impl Cli { } let mut config = NodeConfig::from_env(self.config.as_deref())?; if let Some(metrics_port) = self.metrics_port { - if metrics_port < 0 { - config.metrics_addr = None; - } else { - config.metrics_addr = Some(([127, 0, 0, 1], metrics_port as u16).into()) - } + config.metrics_addr = match metrics_port { + MetricsPort::Disabled => None, + MetricsPort::Port(port) => Some(([127, 0, 0, 1], port).into()), + }; } let add_command = add.map(|source| blob::BlobCommands::Add { diff --git a/iroh-cli/tests/cli.rs b/iroh-cli/tests/cli.rs index 0d189d3ccb..0663459a3e 100644 --- a/iroh-cli/tests/cli.rs +++ b/iroh-cli/tests/cli.rs @@ -356,7 +356,7 @@ fn cli_bao_store_migration() -> anyhow::Result<()> { let dir = testdir!(); let iroh_data_dir = dir.join("iroh_data_dir"); init_v0_blob_store(&iroh_data_dir)?; - let mut reader_handle = cmd(iroh_bin(), ["start"]) + let mut reader_handle = cmd(iroh_bin(), ["--metrics-port", "disabled", "start"]) .env_remove("RUST_LOG") .env("IROH_DATA_DIR", &iroh_data_dir) .stderr_to_stdout() @@ -409,7 +409,14 @@ async fn cli_provide_persistence() -> anyhow::Result<()> { let iroh_provide = |path: &PathBuf| { cmd( iroh_bin(), - ["start", "--add", path.to_str().unwrap(), "--wrap"], + [ + "--metrics-port", + "disabled", + "start", + "--add", + path.to_str().unwrap(), + "--wrap", + ], ) .env("IROH_DATA_DIR", &iroh_data_dir) .env_remove("RUST_LOG") @@ -467,7 +474,7 @@ fn cli_provide_addresses() -> Result<()> { let _ticket = match_provide_output(&mut provider, 1, BlobOrCollection::Collection)?; // test output - let get_output = cmd(iroh_bin(), ["node", "status"]) + let get_output = cmd(iroh_bin(), ["--metrics-port", "disabled", "node", "status"]) .env_remove("RUST_LOG") .env("IROH_DATA_DIR", iroh_data_dir) // .stderr_file(std::io::stderr().as_raw_fd()) // for debug output @@ -500,7 +507,7 @@ fn cli_rpc_lock_restart() -> Result<()> { let iroh_data_dir = dir.join("data-dir"); println!("start"); - let mut reader_handle = cmd(iroh_bin(), ["start"]) + let mut reader_handle = cmd(iroh_bin(), ["--metrics-port", "disabled", "start"]) .env_remove("RUST_LOG") .env("IROH_DATA_DIR", &iroh_data_dir) .stderr_to_stdout() @@ -529,7 +536,7 @@ fn cli_rpc_lock_restart() -> Result<()> { // Restart should work fine println!("restart"); - let mut reader_handle = cmd(iroh_bin(), ["start"]) + let mut reader_handle = cmd(iroh_bin(), ["--metrics-port", "disabled", "start"]) .env_remove("RUST_LOG") .env("IROH_DATA_DIR", &iroh_data_dir) .stderr_to_stdout() @@ -541,7 +548,7 @@ fn cli_rpc_lock_restart() -> Result<()> { ); println!("double start"); - let output = cmd(iroh_bin(), ["start"]) + let output = cmd(iroh_bin(), ["--metrics-port", "disabled", "start"]) .env_remove("RUST_LOG") .env("IROH_DATA_DIR", &iroh_data_dir) .stderr_capture() @@ -617,7 +624,7 @@ fn iroh_bin() -> &'static str { /// Makes a provider process with it's home directory in `iroh_data_dir`. fn make_provider_in(iroh_data_dir: &Path, input: Input, wrap: bool) -> Result { - let mut args = vec!["start"]; + let mut args = vec!["--metrics-port", "disabled", "start"]; if wrap { args.push("--wrap"); } @@ -678,7 +685,16 @@ fn make_get_cmd(iroh_data_dir: &Path, ticket: &str, out: Option) -> duc let out = out .map(|ref o| o.to_str().unwrap().to_string()) .unwrap_or("STDOUT".into()); - let args = vec!["--start", "blob", "get", ticket, "--out", &out]; + let args = vec![ + "--metrics-port", + "disabled", + "--start", + "blob", + "get", + ticket, + "--out", + &out, + ]; println!( "running iroh {:?} in dir: {}", @@ -792,7 +808,15 @@ fn test_provide_get_loop_single(input: Input, output: Output, hash: Hash) -> Res .to_string(); // create a `get-ticket` cmd & optionally provide out path - let mut args = vec!["--start", "blob", "get", "--node", &node]; + let mut args = vec![ + "--metrics-port", + "disabled", + "--start", + "blob", + "get", + "--node", + &node, + ]; for addr in &addrs { args.push("--address"); args.push(addr);