From 77ecfed24e8989dd34dfe7cd20fdd22fd9bcef4a Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Wed, 9 Sep 2020 10:46:32 -0500 Subject: [PATCH] agent/rustjail/mount: don't use unwrap Don't use unwrap in `init_rootfs` instead return an Error, this way we can write unit tests that don't panic. Signed-off-by: Julio Montes --- src/agent/rustjail/src/mount.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/agent/rustjail/src/mount.rs b/src/agent/rustjail/src/mount.rs index c63cccb8cf..dba475bc33 100644 --- a/src/agent/rustjail/src/mount.rs +++ b/src/agent/rustjail/src/mount.rs @@ -138,16 +138,28 @@ pub fn init_rootfs( lazy_static::initialize(&PROPAGATION); lazy_static::initialize(&LINUXDEVICETYPE); - let linux = spec.linux.as_ref().unwrap(); + let linux = &spec + .linux + .as_ref() + .ok_or::(anyhow!("Could not get linux configuration from spec"))?; + let mut flags = MsFlags::MS_REC; match PROPAGATION.get(&linux.rootfs_propagation.as_str()) { Some(fl) => flags |= *fl, None => flags |= MsFlags::MS_SLAVE, } - let rootfs = spec.root.as_ref().unwrap().path.as_str(); - let root = fs::canonicalize(rootfs)?; - let rootfs = root.to_str().unwrap(); + let root = spec + .root + .as_ref() + .ok_or(anyhow!("Could not get rootfs path from spec")) + .and_then(|r| { + fs::canonicalize(r.path.as_str()).context("Could not canonicalize rootfs path") + })?; + + let rootfs = (*root) + .to_str() + .ok_or(anyhow!("Could not convert rootfs path to string"))?; mount(None::<&str>, "/", None::<&str>, flags, None::<&str>)?;