From 39da62fb06c688506c29d7f6667871a0408ea6ed Mon Sep 17 00:00:00 2001 From: Tess Eisenberger Date: Tue, 23 Nov 2021 16:17:13 -0800 Subject: [PATCH 1/2] dev: Make db-run check CockroachDB version This should help catch when developers forget to upgrade their version to match the one in use by the repo. Example failure: $ cargo run --bin omicron-dev db-run Compiling omicron-test-utils v0.1.0 Finished dev [unoptimized + debuginfo] target(s) in 5.94s Running `target/debug/omicron-dev db-run` omicron-dev: using temporary directory for database store (cleaned up on clean exit) omicron-dev: will run this to start CockroachDB: cockroach start-single-node --insecure --http-addr=:0 [...] omicron-dev: temporary directory: /tmp/.tmpWJTKQn omicron-dev: wrong version of CockroachDB installed. expected 'v21.1.11', found: 'Ok("v21.1.10") --- test-utils/src/dev/db.rs | 52 +++++++++++++++++++++++++++++++++-- tools/ci_download_cockroachdb | 2 +- 2 files changed, 51 insertions(+), 3 deletions(-) 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" From e15ae712a188f3b25000a96a0260b7ccfebe0a62 Mon Sep 17 00:00:00 2001 From: Tess Eisenberger Date: Tue, 23 Nov 2021 16:23:05 -0800 Subject: [PATCH 2/2] Include the new version file --- tools/cockroachdb_version | 1 + 1 file changed, 1 insertion(+) create mode 100644 tools/cockroachdb_version 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