From eb54c1c1c013936e425c1f291af07a67f4c1c845 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Tue, 3 Sep 2024 09:59:29 -0700 Subject: [PATCH] Add extra logging around cockroach-admin tests (#6507) Hoping to shed light on the test flake in #6506 --- cockroach-admin/src/cockroach_cli.rs | 42 ++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/cockroach-admin/src/cockroach_cli.rs b/cockroach-admin/src/cockroach_cli.rs index b812cf9749..0aa0532a23 100644 --- a/cockroach-admin/src/cockroach_cli.rs +++ b/cockroach-admin/src/cockroach_cli.rs @@ -135,14 +135,44 @@ impl CockroachCli { #[cfg(test)] mod tests { - use std::net::SocketAddr; - use super::*; use cockroach_admin_types::NodeMembership; use nexus_test_utils::db::test_setup_database; use omicron_test_utils::dev; + use std::net::SocketAddr; use url::Url; + // Helper function to execute `command`, log its stdout/stderr/status, and + // return its stdout. + // + // This is to help debug test flakes like + // https://github.com/oxidecomputer/omicron/issues/6506. + async fn exec_command_logging_output(command: &mut Command) -> String { + let command_str = command + .as_std() + .get_args() + .map(|s| s.to_string_lossy()) + .collect::>() + .join(" "); + let output = match command.output().await { + Ok(output) => output, + Err(err) => panic!("failed executing [{command_str}]: {err}"), + }; + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + eprintln!("Executed [{command_str}]"); + eprintln!(" Status: {}", output.status); + eprintln!(" Stdout:"); + eprintln!("----------------"); + eprintln!("{stdout}"); + eprintln!("----------------"); + eprintln!(" Stderr:"); + eprintln!("----------------"); + eprintln!("{stderr}"); + eprintln!("----------------"); + stdout.to_string() + } + // Ensure that if `cockroach node status` changes in a future CRDB version // bump, we have a test that will fail to force us to check whether our // current parsing is still valid. @@ -165,10 +195,8 @@ mod tests { .arg(&db_url) .arg("--format") .arg("csv"); - let output = - command.output().await.expect("ran `cockroach node status`"); + let stdout = exec_command_logging_output(&mut command).await; - let stdout = String::from_utf8_lossy(&output.stdout); let mut lines = stdout.lines(); let headers = lines.next().expect("header line"); assert_eq!( @@ -228,10 +256,8 @@ mod tests { .arg(&db_url) .arg("--format") .arg("csv"); - let output = - command.output().await.expect("ran `cockroach node decommission`"); + let stdout = exec_command_logging_output(&mut command).await; - let stdout = String::from_utf8_lossy(&output.stdout); let mut lines = stdout.lines(); let headers = lines.next().expect("header line"); assert_eq!(