Skip to content

Commit

Permalink
fix(tests): disable the metrics port of all cli tests (#2154)
Browse files Browse the repository at this point in the history
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

<!-- A summary of what this pull request achieves and a rough list of
changes. -->

## Notes & open questions

<!-- Any notes, remarks or open questions you have to make about the PR.
-->

## Change checklist

- [ ] Self-review.
- [ ] Documentation updates if relevant.
- [x] Tests if relevant.
  • Loading branch information
rklaehn authored Apr 8, 2024
1 parent bfb7560 commit 1d51caa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
31 changes: 25 additions & 6 deletions iroh-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -43,7 +44,26 @@ pub(crate) struct Cli {

/// Port to serve metrics on. -1 to disable.
#[clap(long)]
pub(crate) metrics_port: Option<i16>,
pub(crate) metrics_port: Option<MetricsPort>,
}

#[derive(Debug, Clone)]
pub(crate) enum MetricsPort {
Disabled,
Port(u16),
}

impl FromStr for MetricsPort {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.to_ascii_lowercase() == "disabled" {
Ok(MetricsPort::Disabled)
} else {
let port = s.parse()?;
Ok(MetricsPort::Port(port))
}
}
}

#[derive(Parser, Debug, Clone)]
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 33 additions & 9 deletions iroh-cli/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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<ReaderHandle> {
let mut args = vec!["start"];
let mut args = vec!["--metrics-port", "disabled", "start"];
if wrap {
args.push("--wrap");
}
Expand Down Expand Up @@ -678,7 +685,16 @@ fn make_get_cmd(iroh_data_dir: &Path, ticket: &str, out: Option<PathBuf>) -> 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: {}",
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1d51caa

Please sign in to comment.