Skip to content

Commit

Permalink
update to zone 0.3 for list_blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
lif committed Aug 11, 2023
1 parent 39aa3ff commit 97f4c85
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 107 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ wicket-common = { path = "wicket-common" }
wicketd-client = { path = "wicketd-client" }
zeroize = { version = "1.6.0", features = ["zeroize_derive", "std"] }
zip = { version = "0.6.6", default-features = false, features = ["deflate","bzip2"] }
zone = { version = "0.2", default-features = false, features = ["async"] }
zone = { version = "0.3", default-features = false, features = ["async", "sync"] }

[profile.dev]
panic = "abort"
Expand Down
193 changes: 92 additions & 101 deletions sled-agent/src/storage/dump_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,28 +217,6 @@ pub(self) enum ZfsGetError {
Parse(#[from] std::num::ParseIntError),
}

#[cfg(test)]
impl Clone for ZfsGetError {
fn clone(&self) -> Self {
match self {
ZfsGetError::IoError(_err) => unimplemented!(),
ZfsGetError::Utf8(err) => ZfsGetError::Utf8(err.clone()),
ZfsGetError::Parse(err) => ZfsGetError::Parse(err.clone()),
}
}
}

#[cfg(test)]
#[derive(Default)]
struct Fake {
pub zpool_props: HashMap<
&'static str,
HashMap<&'static str, Result<String, ZfsGetError>>,
>,
pub zones: Vec<Zone>,
}
struct Real {}

trait Invoker {
fn coreadm(&self, core_dir: &Utf8PathBuf) -> Result<(), ExecutionError>;
fn dumpadm(
Expand Down Expand Up @@ -283,71 +261,7 @@ trait Invoker {
fn get_zones(&self) -> Result<Vec<Zone>, ArchiveLogsError>;
}

#[cfg(test)]
impl Invoker for Fake {
fn coreadm(&self, _core_dir: &Utf8PathBuf) -> Result<(), ExecutionError> {
Ok(())
}

fn dumpadm(
&self,
_dump_slice: &Utf8PathBuf,
_savecore_dir: Option<&Utf8PathBuf>,
) -> Result<Option<OsString>, ExecutionError> {
Ok(None)
}

fn zfs_get_prop(
&self,
mountpoint_or_name: impl AsRef<str> + Sized,
property: &str,
) -> Result<String, ZfsGetError> {
self.zpool_props
.get(mountpoint_or_name.as_ref())
.unwrap_or_else(|| {
panic!(
"Test did not provide fake zpool {}",
mountpoint_or_name.as_ref()
)
})
.get(property)
.unwrap_or_else(|| {
panic!(
"Test did not provide property {property} for fake zpool {}",
mountpoint_or_name.as_ref()
)
})
.clone()
}

fn mountpoint(
&self,
zpool: &ZpoolName,
mountpoint: &'static str,
) -> Utf8PathBuf {
Utf8PathBuf::from(
self.zpool_props
.get(zpool.to_string().as_str())
.unwrap_or_else(|| {
panic!("Test did not provide fake zpool {}", zpool)
})
.get("mountpoint")
.unwrap_or_else(|| {
panic!(
"Test did not provide mountpoint for fake zpool {}",
zpool
)
})
.clone()
.unwrap(),
)
.join(mountpoint)
}

fn get_zones(&self) -> Result<Vec<Zone>, ArchiveLogsError> {
Ok(self.zones.clone())
}
}
struct Real {}

impl Invoker for Real {
fn coreadm(&self, core_dir: &Utf8PathBuf) -> Result<(), ExecutionError> {
Expand Down Expand Up @@ -451,18 +365,10 @@ impl Invoker for Real {
}

fn get_zones(&self) -> Result<Vec<Zone>, ArchiveLogsError> {
// zone crate's 'deprecated' functions collide if you try to enable
// its 'sync' and 'async' features simultaneously :(
let rt =
tokio::runtime::Runtime::new().map_err(ArchiveLogsError::Tokio)?;

rt.block_on(async {
Ok(zone::Adm::list()
.await?
.into_iter()
.filter(|z| z.global() || z.name().starts_with(ZONE_PREFIX))
.collect::<Vec<_>>())
})
Ok(zone::Adm::list_blocking()?
.into_iter()
.filter(|z| z.global() || z.name().starts_with(ZONE_PREFIX))
.collect::<Vec<_>>())
}
}

Expand Down Expand Up @@ -988,8 +894,6 @@ impl<I: Invoker> DumpSetupWorker<I> {
#[cfg_attr(test, allow(dead_code))] // mock doesn't construct Tokio variant
#[derive(thiserror::Error, Debug)]
pub enum ArchiveLogsError {
#[error("Couldn't make an async runtime to get zone info: {0}")]
Tokio(std::io::Error),
#[error("I/O error: {0}")]
IoError(#[from] std::io::Error),
#[error("Error calling zoneadm: {0}")]
Expand Down Expand Up @@ -1036,6 +940,93 @@ mod tests {
use std::str::FromStr;
use tempfile::TempDir;

impl Clone for ZfsGetError {
fn clone(&self) -> Self {
match self {
ZfsGetError::IoError(_err) => unimplemented!(),
ZfsGetError::Utf8(err) => ZfsGetError::Utf8(err.clone()),
ZfsGetError::Parse(err) => ZfsGetError::Parse(err.clone()),
}
}
}

#[derive(Default)]
struct Fake {
pub zpool_props: HashMap<
&'static str,
HashMap<&'static str, Result<String, ZfsGetError>>,
>,
pub zones: Vec<Zone>,
}

impl Invoker for Fake {
fn coreadm(
&self,
_core_dir: &Utf8PathBuf,
) -> Result<(), ExecutionError> {
Ok(())
}

fn dumpadm(
&self,
_dump_slice: &Utf8PathBuf,
_savecore_dir: Option<&Utf8PathBuf>,
) -> Result<Option<OsString>, ExecutionError> {
Ok(None)
}

fn zfs_get_prop(
&self,
mountpoint_or_name: impl AsRef<str> + Sized,
property: &str,
) -> Result<String, ZfsGetError> {
self.zpool_props
.get(mountpoint_or_name.as_ref())
.unwrap_or_else(|| {
panic!(
"Test did not provide fake zpool {}",
mountpoint_or_name.as_ref()
)
})
.get(property)
.unwrap_or_else(|| {
panic!(
"Test did not provide property {property} for fake zpool {}",
mountpoint_or_name.as_ref()
)
})
.clone()
}

fn mountpoint(
&self,
zpool: &ZpoolName,
mountpoint: &'static str,
) -> Utf8PathBuf {
Utf8PathBuf::from(
self.zpool_props
.get(zpool.to_string().as_str())
.unwrap_or_else(|| {
panic!("Test did not provide fake zpool {}", zpool)
})
.get("mountpoint")
.unwrap_or_else(|| {
panic!(
"Test did not provide mountpoint for fake zpool {}",
zpool
)
})
.clone()
.unwrap(),
)
.join(mountpoint)
}

fn get_zones(&self) -> Result<Vec<Zone>, ArchiveLogsError> {
Ok(self.zones.clone())
}
}

#[test]
fn test_does_not_configure_coreadm_when_no_crash_dataset_mounted() {
let logctx = omicron_test_utils::dev::test_setup_log(
Expand Down

0 comments on commit 97f4c85

Please sign in to comment.