-
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
Merged
sunshowers
merged 3 commits into
main
from
sunshowers/spr/nexus-test-utils-replace-buildrs-with-a-new-setup-script
Sep 27, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
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()) | ||
} | ||
|
||
enum SeedDirectoryStatus { | ||
Created, | ||
Existing, | ||
} | ||
|
||
async fn ensure_seed_directory_exists( | ||
log: &Logger, | ||
) -> (Utf8PathBuf, SeedDirectoryStatus) { | ||
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 desired_seed_dir.exists() { | ||
return (desired_seed_dir, SeedDirectoryStatus::Existing); | ||
} | ||
|
||
// The directory didn't exist when we started, so try to create it. | ||
// | ||
// Nextest will execute it just once, but it is possible for a user to start | ||
// up multiple nextest processes to be running at the same time. So we | ||
// should consider it possible for another caller to create this seed | ||
// directory before we finish setting it up ourselves. | ||
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, there was either no | ||
// contention or we won a creation race. | ||
// | ||
// 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, SeedDirectoryStatus::Created) | ||
} | ||
|
||
#[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, status) = ensure_seed_directory_exists(&logctx.log).await; | ||
match status { | ||
SeedDirectoryStatus::Created => { | ||
slog::info!(logctx.log, "Created seed directory: `{dir}`"); | ||
} | ||
SeedDirectoryStatus::Existing => { | ||
slog::info!(logctx.log, "Using existing seed directory: `{dir}`"); | ||
} | ||
} | ||
if let Ok(env_path) = std::env::var("NEXTEST_ENV") { | ||
let mut file = std::fs::File::create(&env_path) | ||
.expect("failed to open NEXTEST_ENV file"); | ||
writeln!(file, "CRDB_SEED_DIR={dir}") | ||
.expect("failed to write to NEXTEST_ENV file"); | ||
} else { | ||
slog::warn!( | ||
logctx.log, | ||
"NEXTEST_ENV not set (is this script running under nextest?)" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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 comment
The 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 comment
The 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.