From 57c0f93f5407290c4f2c1116266dd05c9edf1cb6 Mon Sep 17 00:00:00 2001 From: Jianyong Wu Date: Fri, 22 Oct 2021 21:05:30 +0800 Subject: [PATCH] agent: fix race condition when test watcher create_tmpfs won't pass as the race condition in watcher umount. quote James's words here: 1. Rust runs all tests in parallel. 2. Mounts are a process-wide, not a per-thread resource. The only test that calls watcher.mount() is create_tmpfs(). However, other tests create BindWatcher objects. 3. BindWatcher's drop() implementation calls self.cleanup(), which calls unmount for the mountpoint create_tmpfs() asserts. 4. The other tests are calling unmount whenever a BindWatcher goes out of scope. To avoid that issue, let the tests using BindWatcher in watcher and sandbox.rs run sequentially. Fixes: #2809 Signed-off-by: Jianyong Wu --- src/agent/Cargo.lock | 1 + src/agent/Cargo.toml | 1 + src/agent/src/sandbox.rs | 12 ++++++++++++ src/agent/src/watcher.rs | 5 +++++ 4 files changed, 19 insertions(+) diff --git a/src/agent/Cargo.lock b/src/agent/Cargo.lock index 44a4674440..517088b1ec 100644 --- a/src/agent/Cargo.lock +++ b/src/agent/Cargo.lock @@ -546,6 +546,7 @@ dependencies = [ "scopeguard", "serde", "serde_json", + "serial_test", "slog", "slog-scope", "slog-stdlog", diff --git a/src/agent/Cargo.toml b/src/agent/Cargo.toml index 3715da42fc..c4864ef50c 100644 --- a/src/agent/Cargo.toml +++ b/src/agent/Cargo.toml @@ -20,6 +20,7 @@ scan_fmt = "0.2.3" scopeguard = "1.0.0" thiserror = "1.0.26" regex = "1" +serial_test = "0.5.1" # Async helpers async-trait = "0.1.42" diff --git a/src/agent/src/sandbox.rs b/src/agent/src/sandbox.rs index 6829eb2cf5..ddc18c5c9b 100644 --- a/src/agent/src/sandbox.rs +++ b/src/agent/src/sandbox.rs @@ -464,7 +464,10 @@ mod tests { baremount(src, dst, "bind", MsFlags::MS_BIND, "", logger) } + use serial_test::serial; + #[tokio::test] + #[serial] async fn set_sandbox_storage() { let logger = slog::Logger::root(slog::Discard, o!()); let mut s = Sandbox::new(&logger).unwrap(); @@ -499,6 +502,7 @@ mod tests { } #[tokio::test] + #[serial] async fn remove_sandbox_storage() { skip_if_not_root!(); @@ -555,6 +559,7 @@ mod tests { } #[tokio::test] + #[serial] async fn unset_and_remove_sandbox_storage() { skip_if_not_root!(); @@ -606,6 +611,7 @@ mod tests { } #[tokio::test] + #[serial] async fn unset_sandbox_storage() { let logger = slog::Logger::root(slog::Discard, o!()); let mut s = Sandbox::new(&logger).unwrap(); @@ -689,6 +695,7 @@ mod tests { } #[tokio::test] + #[serial] async fn get_container_entry_exist() { skip_if_not_root!(); let logger = slog::Logger::root(slog::Discard, o!()); @@ -702,6 +709,7 @@ mod tests { } #[tokio::test] + #[serial] async fn get_container_no_entry() { let logger = slog::Logger::root(slog::Discard, o!()); let mut s = Sandbox::new(&logger).unwrap(); @@ -711,6 +719,7 @@ mod tests { } #[tokio::test] + #[serial] async fn add_and_get_container() { skip_if_not_root!(); let logger = slog::Logger::root(slog::Discard, o!()); @@ -722,6 +731,7 @@ mod tests { } #[tokio::test] + #[serial] async fn update_shared_pidns() { skip_if_not_root!(); let logger = slog::Logger::root(slog::Discard, o!()); @@ -740,6 +750,7 @@ mod tests { } #[tokio::test] + #[serial] async fn add_guest_hooks() { let logger = slog::Logger::root(slog::Discard, o!()); let mut s = Sandbox::new(&logger).unwrap(); @@ -763,6 +774,7 @@ mod tests { } #[tokio::test] + #[serial] async fn test_sandbox_set_destroy() { let logger = slog::Logger::root(slog::Discard, o!()); let mut s = Sandbox::new(&logger).unwrap(); diff --git a/src/agent/src/watcher.rs b/src/agent/src/watcher.rs index 51e727ae45..b111aa166f 100644 --- a/src/agent/src/watcher.rs +++ b/src/agent/src/watcher.rs @@ -979,7 +979,10 @@ mod tests { ); } + use serial_test::serial; + #[tokio::test] + #[serial] async fn create_tmpfs() { skip_if_not_root!(); @@ -994,6 +997,7 @@ mod tests { } #[tokio::test] + #[serial] async fn spawn_thread() { skip_if_not_root!(); @@ -1023,6 +1027,7 @@ mod tests { } #[tokio::test] + #[serial] async fn verify_container_cleanup_watching() { skip_if_not_root!();