-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[nexus-test-utils] replace build.rs with a new setup script #4150
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "crdb-seed" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MPL-2.0" | ||
|
||
[dependencies] | ||
camino.workspace = true | ||
camino-tempfile.workspace = true | ||
dropshot.workspace = true | ||
hex.workspace = true | ||
omicron-test-utils.workspace = true | ||
ring.workspace = true | ||
slog.workspace = true | ||
tokio.workspace = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use camino::Utf8PathBuf; | ||
use dropshot::{test_util::LogContext, ConfigLogging, ConfigLoggingLevel}; | ||
use omicron_test_utils::dev; | ||
use slog::Logger; | ||
use std::io::Write; | ||
|
||
// Creates a string identifier for the current DB schema and version. | ||
// | ||
// The goal here is to allow to create different "seed" directories | ||
// for each revision of the DB. | ||
fn digest_unique_to_schema() -> String { | ||
let schema = include_str!("../../schema/crdb/dbinit.sql"); | ||
let crdb_version = include_str!("../../tools/cockroachdb_version"); | ||
let mut ctx = ring::digest::Context::new(&ring::digest::SHA256); | ||
ctx.update(&schema.as_bytes()); | ||
ctx.update(&crdb_version.as_bytes()); | ||
let digest = ctx.finish(); | ||
hex::encode(digest.as_ref()) | ||
} | ||
|
||
async fn ensure_seed_directory_exists(log: &Logger) -> Utf8PathBuf { | ||
let base_seed_dir = Utf8PathBuf::from_path_buf(std::env::temp_dir()) | ||
.expect("Not a UTF-8 path") | ||
.join("crdb-base"); | ||
std::fs::create_dir_all(&base_seed_dir).unwrap(); | ||
let desired_seed_dir = base_seed_dir.join(digest_unique_to_schema()); | ||
|
||
// If the directory didn't exist when we started, try to create it. | ||
// | ||
// Note that this may be executed concurrently by many tests, so | ||
// we should consider it possible for another caller to create this | ||
// seed directory before we finish setting it up ourselves. | ||
if !desired_seed_dir.exists() { | ||
let tmp_seed_dir = | ||
camino_tempfile::Utf8TempDir::new_in(base_seed_dir).unwrap(); | ||
dev::test_setup_database_seed(log, tmp_seed_dir.path()).await; | ||
|
||
// If we can successfully perform the rename, we made the seed directory | ||
// faster than other tests. | ||
// | ||
// If we couldn't perform the rename, the directory might already exist. | ||
// Check that this is the error we encountered -- otherwise, we're | ||
// struggling. | ||
if let Err(err) = | ||
std::fs::rename(tmp_seed_dir.path(), &desired_seed_dir) | ||
{ | ||
if !desired_seed_dir.exists() { | ||
panic!("Cannot rename seed directory for CockroachDB: {err}"); | ||
} | ||
} | ||
} | ||
|
||
desired_seed_dir | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// TODO: dropshot is v heavyweight for this, we should be able to pull in a | ||
// smaller binary | ||
let logctx = LogContext::new( | ||
"crdb_seeding", | ||
&ConfigLogging::StderrTerminal { level: ConfigLoggingLevel::Info }, | ||
); | ||
let dir = ensure_seed_directory_exists(&logctx.log).await; | ||
if let Ok(env_path) = std::env::var("NEXTEST_ENV") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, what sets this environment variable? Also, if this environment variable cannot be found, should we throw an error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nextest does -- it's the way setup scripts can communicate with nextest.
I figured folks might want to run the script by hand. Maybe we should warn in that case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. So someone could run this binary manually, and it would create the seed dir, but not pass the necessary info to nextest. That makes sense -- I'm okay with this structure as-is, I just wanted to make sure it would be clear "where things went wrong" if the environment variable from nextest was not provided. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added some more logging and a warning message to make this clearer. |
||
let mut file = std::fs::File::create(&env_path) | ||
.expect("failed to open NEXTEST_ENV file"); | ||
write!(file, "CRDB_SEED_DIR={}", dir.as_str()) | ||
.expect("failed to write to NEXTEST_ENV file"); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,9 @@ | |
|
||
//! Database testing facilities. | ||
|
||
use camino::Utf8PathBuf; | ||
use omicron_test_utils::dev; | ||
use slog::Logger; | ||
use std::path::PathBuf; | ||
|
||
/// Path to the "seed" CockroachDB directory. | ||
/// | ||
|
@@ -16,8 +16,13 @@ use std::path::PathBuf; | |
/// By creating a "seed" version of the database, we can cut down | ||
/// on the time spent performing this operation. Instead, we opt | ||
/// to copy the database from this seed location. | ||
fn seed_dir() -> PathBuf { | ||
PathBuf::from(concat!(env!("OUT_DIR"), "/crdb-base")) | ||
fn seed_dir() -> Utf8PathBuf { | ||
// The setup script should set this environment variable. | ||
let seed_dir = std::env::var("CRDB_SEED_DIR") | ||
.expect("CRDB_SEED_DIR missing -- are you running this test with `cargo nextest run`?"); | ||
// TODO: replace with temp dir written out by nextest | ||
// Use "out/" for now. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this happening already? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, lol. Will fix. |
||
seed_dir.into() | ||
} | ||
|
||
/// Wrapper around [`dev::test_setup_database`] which uses a a | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still true, or will nextest prevent the concurrent execution of this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nextest will execute it once, but it's possible that the user spins up two instances of nextest at the same time. I'll update the comment.