diff --git a/src/agent/rustjail/src/mount.rs b/src/agent/rustjail/src/mount.rs index d71db32b85..8bb3b47256 100644 --- a/src/agent/rustjail/src/mount.rs +++ b/src/agent/rustjail/src/mount.rs @@ -35,17 +35,9 @@ use crate::log_child; // struct is populated from the content in the /proc//mountinfo file. #[derive(std::fmt::Debug)] pub struct Info { - id: i32, - parent: i32, - major: i32, - minor: i32, - root: String, mount_point: String, - opts: String, optional: String, fstype: String, - source: String, - vfs_opts: String, } const MOUNTINFOFORMAT: &str = "{d} {d} {d}:{d} {} {} {} {}"; @@ -112,6 +104,7 @@ lazy_static! { } #[inline(always)] +#[cfg(not(test))] pub fn mount< P1: ?Sized + NixPath, P2: ?Sized + NixPath, @@ -124,21 +117,42 @@ pub fn mount< flags: MsFlags, data: Option<&P4>, ) -> std::result::Result<(), nix::Error> { - #[cfg(not(test))] - return mount::mount(source, target, fstype, flags, data); - #[cfg(test)] - return Ok(()); + mount::mount(source, target, fstype, flags, data) } #[inline(always)] +#[cfg(test)] +pub fn mount< + P1: ?Sized + NixPath, + P2: ?Sized + NixPath, + P3: ?Sized + NixPath, + P4: ?Sized + NixPath, +>( + _source: Option<&P1>, + _target: &P2, + _fstype: Option<&P3>, + _flags: MsFlags, + _data: Option<&P4>, +) -> std::result::Result<(), nix::Error> { + Ok(()) +} + +#[inline(always)] +#[cfg(not(test))] pub fn umount2( target: &P, flags: MntFlags, ) -> std::result::Result<(), nix::Error> { - #[cfg(not(test))] - return mount::umount2(target, flags); - #[cfg(test)] - return Ok(()); + mount::umount2(target, flags) +} + +#[inline(always)] +#[cfg(test)] +pub fn umount2( + _target: &P, + _flags: MntFlags, +) -> std::result::Result<(), nix::Error> { + Ok(()) } pub fn init_rootfs( @@ -450,14 +464,20 @@ fn mount_cgroups( Ok(()) } +#[cfg(not(test))] fn pivot_root( new_root: &P1, put_old: &P2, ) -> anyhow::Result<(), nix::Error> { - #[cfg(not(test))] - return unistd::pivot_root(new_root, put_old); - #[cfg(test)] - return Ok(()); + unistd::pivot_root(new_root, put_old) +} + +#[cfg(test)] +fn pivot_root( + _new_root: &P1, + _put_old: &P2, +) -> anyhow::Result<(), nix::Error> { + Ok(()) } pub fn pivot_rootfs(path: &P) -> Result<()> { @@ -535,7 +555,20 @@ fn parse_mount_table() -> Result> { for (_index, line) in reader.lines().enumerate() { let line = line?; - let (id, parent, major, minor, root, mount_point, opts, optional) = scan_fmt!( + //Example mountinfo format: + // id + // | / parent + // | | / major:minor + // | | | / root + // | | | | / mount_point + // | | | | | / opts + // | | | | | | / optional + // | | | | | | | / fstype + // | | | | | | | | / source + // | | | | | | | | | / vfs_opts + // 22 96 0:21 / /sys rw,nosuid,nodev,noexec,relatime shared:2 - sysfs sysfs rw,seclabel + + let (_id, _parent, _major, _minor, _root, mount_point, _opts, optional) = scan_fmt!( &line, MOUNTINFOFORMAT, i32, @@ -550,7 +583,7 @@ fn parse_mount_table() -> Result> { let fields: Vec<&str> = line.split(" - ").collect(); if fields.len() == 2 { - let (fstype, source, vfs_opts) = + let (fstype, _source, _vfs_opts) = scan_fmt!(fields[1], "{} {} {}", String, String, String)?; let mut optional_new = String::new(); @@ -559,17 +592,9 @@ fn parse_mount_table() -> Result> { } let info = Info { - id, - parent, - major, - minor, - root, mount_point, - opts, optional: optional_new, fstype, - source, - vfs_opts, }; infos.push(info); @@ -582,11 +607,15 @@ fn parse_mount_table() -> Result> { } #[inline(always)] +#[cfg(not(test))] fn chroot(path: &P) -> Result<(), nix::Error> { - #[cfg(not(test))] - return unistd::chroot(path); - #[cfg(test)] - return Ok(()); + unistd::chroot(path) +} + +#[inline(always)] +#[cfg(test)] +fn chroot(_path: &P) -> Result<(), nix::Error> { + Ok(()) } pub fn ms_move_root(rootfs: &str) -> Result { @@ -1382,7 +1411,7 @@ mod tests { for (i, t) in tests.iter().enumerate() { // Create a string containing details of the test - let msg = format!("test[{}]: {:?}", i, t); + let msg = format!("test[{}]: {:?}", i, t.name); // if is_symlink, then should be prepare the softlink environment if t.symlink_path != "" { diff --git a/src/agent/src/network.rs b/src/agent/src/network.rs index 4b72d4093e..1152fce917 100644 --- a/src/agent/src/network.rs +++ b/src/agent/src/network.rs @@ -5,30 +5,22 @@ use anyhow::{anyhow, Result}; use nix::mount::{self, MsFlags}; -use protocols::types::{Interface, Route}; use slog::Logger; -use std::collections::HashMap; use std::fs; const KATA_GUEST_SANDBOX_DNS_FILE: &str = "/run/kata-containers/sandbox/resolv.conf"; const GUEST_DNS_FILE: &str = "/etc/resolv.conf"; -// Network fully describes a sandbox network with its interfaces, routes and dns +// Network describes a sandbox network, includings its dns // related information. #[derive(Debug, Default)] pub struct Network { - ifaces: HashMap, - routes: Vec, dns: Vec, } impl Network { pub fn new() -> Network { - Network { - ifaces: HashMap::new(), - routes: Vec::new(), - dns: Vec::new(), - } + Network { dns: Vec::new() } } pub fn set_dns(&mut self, dns: String) { diff --git a/src/agent/src/uevent.rs b/src/agent/src/uevent.rs index 93dabcafcb..1f9f7f08eb 100644 --- a/src/agent/src/uevent.rs +++ b/src/agent/src/uevent.rs @@ -240,7 +240,6 @@ pub(crate) fn spawn_test_watcher(sandbox: Arc>, uev: Uevent) { if matcher.is_match(&uev) { let (_, sender) = watch.take().unwrap(); let _ = sender.send(uev.clone()); - return; } } }); diff --git a/src/agent/src/util.rs b/src/agent/src/util.rs index 0e262e7ee3..8761f45885 100644 --- a/src/agent/src/util.rs +++ b/src/agent/src/util.rs @@ -86,7 +86,6 @@ mod tests { #[derive(Debug, Default, Clone)] struct BufWriter { data: Arc>>, - slow_write: bool, write_delay: Duration, } @@ -94,7 +93,6 @@ mod tests { fn new() -> Self { BufWriter { data: Arc::new(Mutex::new(Vec::::new())), - slow_write: false, write_delay: Duration::new(0, 0), } } @@ -173,45 +171,35 @@ mod tests { #[derive(Debug)] struct TestData { reader_value: String, - result: io::Result, } let tests = &[ TestData { reader_value: "".into(), - result: Ok(0), }, TestData { reader_value: "a".into(), - result: Ok(1), }, TestData { reader_value: "foo".into(), - result: Ok(3), }, TestData { reader_value: "b".repeat(BUF_SIZE - 1), - result: Ok((BUF_SIZE - 1) as u64), }, TestData { reader_value: "c".repeat(BUF_SIZE), - result: Ok((BUF_SIZE) as u64), }, TestData { reader_value: "d".repeat(BUF_SIZE + 1), - result: Ok((BUF_SIZE + 1) as u64), }, TestData { reader_value: "e".repeat((2 * BUF_SIZE) - 1), - result: Ok(((2 * BUF_SIZE) - 1) as u64), }, TestData { reader_value: "f".repeat(2 * BUF_SIZE), - result: Ok((2 * BUF_SIZE) as u64), }, TestData { reader_value: "g".repeat((2 * BUF_SIZE) + 1), - result: Ok(((2 * BUF_SIZE) + 1) as u64), }, ];