From 06d3049349c77fc88f0f724a75c75e3de3d3dd90 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 f922acba94..3d873bf673 100644 --- a/src/agent/Cargo.lock +++ b/src/agent/Cargo.lock @@ -545,6 +545,7 @@ dependencies = [ "scan_fmt", "scopeguard", "serde_json", + "serial_test", "slog", "slog-scope", "slog-stdlog", diff --git a/src/agent/Cargo.toml b/src/agent/Cargo.toml index 10cf2082c5..d1a4d57492 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 93a4fa0feb..03a2162197 100644 --- a/src/agent/src/sandbox.rs +++ b/src/agent/src/sandbox.rs @@ -465,7 +465,10 @@ mod tests { baremount.mount() } + 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(); @@ -500,6 +503,7 @@ mod tests { } #[tokio::test] + #[serial] async fn remove_sandbox_storage() { skip_if_not_root!(); @@ -556,6 +560,7 @@ mod tests { } #[tokio::test] + #[serial] async fn unset_and_remove_sandbox_storage() { skip_if_not_root!(); @@ -607,6 +612,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(); @@ -690,6 +696,7 @@ mod tests { } #[tokio::test] + #[serial] async fn get_container_entry_exist() { skip_if_not_root!(); let logger = slog::Logger::root(slog::Discard, o!()); @@ -703,6 +710,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(); @@ -712,6 +720,7 @@ mod tests { } #[tokio::test] + #[serial] async fn add_and_get_container() { skip_if_not_root!(); let logger = slog::Logger::root(slog::Discard, o!()); @@ -723,6 +732,7 @@ mod tests { } #[tokio::test] + #[serial] async fn update_shared_pidns() { skip_if_not_root!(); let logger = slog::Logger::root(slog::Discard, o!()); @@ -741,6 +751,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(); @@ -764,6 +775,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 ef5030763b..d35d591a44 100644 --- a/src/agent/src/watcher.rs +++ b/src/agent/src/watcher.rs @@ -982,7 +982,10 @@ mod tests { ); } + use serial_test::serial; + #[tokio::test] + #[serial] async fn create_tmpfs() { skip_if_not_root!(); @@ -997,6 +1000,7 @@ mod tests { } #[tokio::test] + #[serial] async fn spawn_thread() { skip_if_not_root!(); @@ -1026,6 +1030,7 @@ mod tests { } #[tokio::test] + #[serial] async fn verify_container_cleanup_watching() { skip_if_not_root!();