From 63c5a8aa532ec8ee2b08f132ca5ac2bfd65d4cc2 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Fri, 26 Nov 2021 12:23:03 +0000 Subject: [PATCH 1/5] uevent: Fix clippy issue in test code Remove a bare `return` from a test function. This looks wrong but isn't because the callers are all tests that just wait for a state change caused by this test function. Signed-off-by: James O. D. Hunt --- src/agent/src/uevent.rs | 1 - 1 file changed, 1 deletion(-) 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; } } }); From fc012a2baba4debe456b8609625c720ab2ccdf05 Mon Sep 17 00:00:00 2001 From: bin Date: Mon, 22 Nov 2021 18:16:32 +0800 Subject: [PATCH 2/5] agent: clear cargo test warnings Function parameters in test config is not used. This commit will add under score before variable name in test config. Fixes: #3091 Signed-off-by: bin --- src/agent/rustjail/src/mount.rs | 64 ++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/src/agent/rustjail/src/mount.rs b/src/agent/rustjail/src/mount.rs index d71db32b85..37370778a3 100644 --- a/src/agent/rustjail/src/mount.rs +++ b/src/agent/rustjail/src/mount.rs @@ -112,6 +112,7 @@ lazy_static! { } #[inline(always)] +#[cfg(not(test))] pub fn mount< P1: ?Sized + NixPath, P2: ?Sized + NixPath, @@ -124,21 +125,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 +472,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<()> { @@ -582,11 +610,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 { From 87f9a69035fa14ca65798e94279304de8d6939ce Mon Sep 17 00:00:00 2001 From: Eric Ernst Date: Thu, 2 Dec 2021 16:22:51 -0800 Subject: [PATCH 3/5] agent: drop unused fields from network We don't utilize routes or inteface vectors. Let's drop them. Signed-off-by: Eric Ernst --- src/agent/src/network.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/agent/src/network.rs b/src/agent/src/network.rs index 4b72d4093e..80ff00d6d8 100644 --- a/src/agent/src/network.rs +++ b/src/agent/src/network.rs @@ -5,28 +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(), } } From a829867674fc734f2b2e539dd1700e9b389f0119 Mon Sep 17 00:00:00 2001 From: Eric Ernst Date: Thu, 2 Dec 2021 16:22:51 -0800 Subject: [PATCH 4/5] agent: remove unused field in mount handling In our parsing of mountinfo, majority of the fields are unused. Let's stop saving these. Fixes: #3180 Signed-off-by: Eric Ernst --- src/agent/rustjail/src/mount.rs | 33 +++++++++++++++------------------ src/agent/src/network.rs | 4 +--- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/agent/rustjail/src/mount.rs b/src/agent/rustjail/src/mount.rs index 37370778a3..15bc039c93 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} {} {} {} {}"; @@ -563,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, @@ -578,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(); @@ -587,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); diff --git a/src/agent/src/network.rs b/src/agent/src/network.rs index 80ff00d6d8..1152fce917 100644 --- a/src/agent/src/network.rs +++ b/src/agent/src/network.rs @@ -20,9 +20,7 @@ pub struct Network { impl Network { pub fn new() -> Network { - Network { - dns: Vec::new(), - } + Network { dns: Vec::new() } } pub fn set_dns(&mut self, dns: String) { From 78afa10ab9e0f350f180aaac742ddfce3083f205 Mon Sep 17 00:00:00 2001 From: Eric Ernst Date: Sun, 16 Jan 2022 14:04:30 -0800 Subject: [PATCH 5/5] agent: resolve unused variables in tests A few tests have unused or unread variables. Let's clean these up... Fixes: #3530 Signed-off-by: Eric Ernst --- src/agent/rustjail/src/mount.rs | 2 +- src/agent/src/util.rs | 12 ------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/agent/rustjail/src/mount.rs b/src/agent/rustjail/src/mount.rs index 15bc039c93..8bb3b47256 100644 --- a/src/agent/rustjail/src/mount.rs +++ b/src/agent/rustjail/src/mount.rs @@ -1411,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/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), }, ];