diff --git a/test-utils/src/dev/db.rs b/test-utils/src/dev/db.rs index df49a45234e..b51da4ddedf 100644 --- a/test-utils/src/dev/db.rs +++ b/test-utils/src/dev/db.rs @@ -5,9 +5,10 @@ use anyhow::anyhow; use anyhow::bail; use anyhow::Context; use omicron_common::config::PostgresConfigWithUrl; -use std::ffi::OsStr; +use std::ffi::{OsStr, OsString}; use std::fmt; use std::ops::Deref; +use std::os::unix::ffi::OsStringExt; use std::path::Path; use std::path::PathBuf; use std::process::Stdio; @@ -42,6 +43,13 @@ const COCKROACHDB_DATABASE: &'static str = "omicron"; */ const COCKROACHDB_USER: &'static str = "root"; +/// Path to the CockroachDB binary +const COCKROACHDB_BIN: &str = "cockroach"; + +/// The expected CockroachDB version +const COCKROACHDB_VERSION: &str = + include_str!("../../../tools/cockroachdb_version"); + /** * Builder for [`CockroachStarter`] that supports setting some command-line * arguments for the `cockroach start-single-node` command @@ -74,7 +82,7 @@ pub struct CockroachStarterBuilder { impl CockroachStarterBuilder { pub fn new() -> CockroachStarterBuilder { - CockroachStarterBuilder::new_with_cmd("cockroach") + CockroachStarterBuilder::new_with_cmd(COCKROACHDB_BIN) } fn new_with_cmd(cmd: &str) -> CockroachStarterBuilder { @@ -281,6 +289,8 @@ impl CockroachStarter { pub async fn start( mut self, ) -> Result { + check_db_version().await?; + let mut child_process = self.cmd_builder.spawn().map_err(|source| { CockroachStartError::BadCmd { cmd: self.args[0].clone(), source } })?; @@ -378,6 +388,9 @@ pub enum CockroachStartError { source: std::io::Error, }, + #[error("wrong version of CockroachDB installed. expected '{expected:}', found: '{found:?}")] + BadVersion { expected: String, found: Result }, + #[error("cockroach failed to start (see error output above)")] Exited, @@ -548,6 +561,41 @@ impl Drop for CockroachInstance { } } +/// Verify that CockroachDB has the correct version +pub async fn check_db_version() -> Result<(), CockroachStartError> { + let mut cmd = tokio::process::Command::new(COCKROACHDB_BIN); + cmd.args(&["version", "--build-tag"]); + let output = cmd.output().await.map_err(|source| { + CockroachStartError::BadCmd { cmd: COCKROACHDB_BIN.to_string(), source } + })?; + if !output.status.success() { + return Err(CockroachStartError::BadVersion { + expected: COCKROACHDB_VERSION.trim().to_string(), + found: Err(anyhow!( + "error {:?} when checking CockroachDB version", + output.status.code() + )), + }); + } + let version_str = + OsString::from_vec(output.stdout).into_string().map_err(|_| { + CockroachStartError::BadVersion { + expected: COCKROACHDB_VERSION.trim().to_string(), + found: Err(anyhow!("Error parsing CockroachDB version output")), + } + })?; + let version_str = version_str.trim(); + + if version_str != COCKROACHDB_VERSION.trim() { + return Err(CockroachStartError::BadVersion { + found: Ok(version_str.to_string()), + expected: COCKROACHDB_VERSION.trim().to_string(), + }); + } + + Ok(()) +} + /** * Wrapper around tokio::process::Child::try_wait() so that we can unwrap() the * result in one place with this explanatory comment. diff --git a/tools/ci_download_cockroachdb b/tools/ci_download_cockroachdb index 24e31825a18..c46198c4c77 100755 --- a/tools/ci_download_cockroachdb +++ b/tools/ci_download_cockroachdb @@ -14,7 +14,7 @@ SOURCE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" ARG0="$(basename ${BASH_SOURCE[0]})" # If you change this, you must also update the md5sums below -CIDL_VERSION="v21.1.10" +CIDL_VERSION="$(cat "$SOURCE_DIR/cockroachdb_version")" source "$SOURCE_DIR/cockroachdb_checksums" CIDL_ASSEMBLE_DIR="./cockroachdb" diff --git a/tools/cockroachdb_version b/tools/cockroachdb_version new file mode 100644 index 00000000000..162a680ad02 --- /dev/null +++ b/tools/cockroachdb_version @@ -0,0 +1 @@ +v21.1.10