Skip to content

Commit

Permalink
dev: Make db-run check CockroachDB version (#427)
Browse files Browse the repository at this point in the history
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")
  • Loading branch information
teisenbe authored Nov 24, 2021
1 parent dee1336 commit 9a830b4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
52 changes: 50 additions & 2 deletions test-utils/src/dev/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -281,6 +289,8 @@ impl CockroachStarter {
pub async fn start(
mut self,
) -> Result<CockroachInstance, CockroachStartError> {
check_db_version().await?;

let mut child_process = self.cmd_builder.spawn().map_err(|source| {
CockroachStartError::BadCmd { cmd: self.args[0].clone(), source }
})?;
Expand Down Expand Up @@ -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<String, anyhow::Error> },

#[error("cockroach failed to start (see error output above)")]
Exited,

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tools/ci_download_cockroachdb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions tools/cockroachdb_version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v21.1.10

0 comments on commit 9a830b4

Please sign in to comment.