Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")"

This comment was marked as resolved.

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did :) I think I raced catching that and uploading the file with you seeing the patch

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