-
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
Unit tests for DumpSetup #3788
Merged
Merged
Unit tests for DumpSetup #3788
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ac6507
Unit tests for DumpSetup behavior
8e78792
use zone 0.3's sync feature for list_blocking
6e3f93f
split out dumpsetup test fakes by discipline
dc60ba6
box dyn instead of concrete generics
3e85752
document behavior while we're at it
d36c61c
cleanup per pr comments
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,57 @@ | ||
use camino::Utf8PathBuf; | ||
use std::ffi::OsString; | ||
use std::os::unix::ffi::OsStringExt; | ||
use crate::{execute, ExecutionError}; | ||
use std::process::Command; | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum CoreAdmError { | ||
#[error("Error obtaining or modifying coreadm configuration. core_dir: {core_dir:?}")] | ||
Execution { core_dir: Utf8PathBuf }, | ||
|
||
#[error("Invalid invocation of coreadm: {0:?} {1:?}")] | ||
InvalidCommand(Vec<String>, OsString), | ||
const COREADM: &str = "/usr/bin/coreadm"; | ||
|
||
#[error("coreadm process was terminated by a signal.")] | ||
TerminatedBySignal, | ||
pub struct CoreAdm { | ||
cmd: Command, | ||
} | ||
|
||
#[error("coreadm invocation exited with unexpected return code {0}")] | ||
UnexpectedExitCode(i32), | ||
pub enum CoreFileOption { | ||
Global, | ||
GlobalSetid, | ||
Log, | ||
Process, | ||
ProcSetid, | ||
} | ||
|
||
#[error("Failed to execute dumpadm process: {0}")] | ||
Exec(std::io::Error), | ||
impl AsRef<str> for CoreFileOption { | ||
fn as_ref(&self) -> &str { | ||
match self { | ||
CoreFileOption::Global => "global", | ||
CoreFileOption::GlobalSetid => "global-setid", | ||
CoreFileOption::Log => "log", | ||
CoreFileOption::Process => "process", | ||
CoreFileOption::ProcSetid => "proc-setid", | ||
} | ||
} | ||
} | ||
|
||
const COREADM: &str = "/usr/bin/coreadm"; | ||
impl CoreAdm { | ||
pub fn new() -> Self { | ||
let mut cmd = Command::new(COREADM); | ||
cmd.env_clear(); | ||
Self { cmd } | ||
} | ||
|
||
pub fn coreadm(core_dir: &Utf8PathBuf) -> Result<(), CoreAdmError> { | ||
let mut cmd = Command::new(COREADM); | ||
cmd.env_clear(); | ||
|
||
// disable per-process core patterns | ||
cmd.arg("-d").arg("process"); | ||
cmd.arg("-d").arg("proc-setid"); | ||
|
||
// use the global core pattern | ||
cmd.arg("-e").arg("global"); | ||
cmd.arg("-e").arg("global-setid"); | ||
|
||
// set the global pattern to place all cores into core_dir, | ||
// with filenames of "core.[zone-name].[exe-filename].[pid].[time]" | ||
cmd.arg("-g").arg(core_dir.join("core.%z.%f.%p.%t")); | ||
|
||
// also collect DWARF data from the exe and its library deps | ||
cmd.arg("-G").arg("default+debug"); | ||
|
||
let out = cmd.output().map_err(CoreAdmError::Exec)?; | ||
|
||
match out.status.code() { | ||
Some(0) => Ok(()), | ||
Some(1) => Err(CoreAdmError::Execution { core_dir: core_dir.clone() }), | ||
Some(2) => { | ||
// unwrap: every arg we've provided in this function is UTF-8 | ||
let mut args = | ||
vec![cmd.get_program().to_str().unwrap().to_string()]; | ||
cmd.get_args() | ||
.for_each(|arg| args.push(arg.to_str().unwrap().to_string())); | ||
let stderr = OsString::from_vec(out.stderr); | ||
Err(CoreAdmError::InvalidCommand(args, stderr)) | ||
} | ||
Some(n) => Err(CoreAdmError::UnexpectedExitCode(n)), | ||
None => Err(CoreAdmError::TerminatedBySignal), | ||
pub fn disable(&mut self, opt: CoreFileOption) { | ||
self.cmd.arg("-d").arg(opt.as_ref()); | ||
} | ||
|
||
pub fn enable(&mut self, opt: CoreFileOption) { | ||
self.cmd.arg("-e").arg(opt.as_ref()); | ||
} | ||
|
||
pub fn global_pattern(&mut self, pat: impl AsRef<std::ffi::OsStr>) { | ||
self.cmd.arg("-g").arg(pat); | ||
} | ||
|
||
pub fn global_contents(&mut self, contents: &str) { | ||
self.cmd.arg("-G").arg(contents); | ||
} | ||
|
||
pub fn execute(mut self) -> Result<(), ExecutionError> { | ||
execute(&mut self.cmd)?; | ||
Ok(()) | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Do really need the sync feature here? If it's possible, it would be cool to keep the codebase async-only.
AFAICT, this is only used to call
Zones::list_blocking
, which seems like it could easily be replaced with:https://github.com/oxidecomputer/zone/blob/65647e678fec739d4e9a6897bf2ee48e1fb051a5/zone/src/lib.rs#L1020-L1027
If we use
async_trait
?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.
Zones::list_blocking
is called a couple layers down the stack fromDumpSetupWorker::archive_files
, which is called within a MutexGuard - I was hoping to avoid questions of cancellation-safety issues this way.