From d60994cb76daebfbe18e74994c0a9085b7be1e6f Mon Sep 17 00:00:00 2001 From: "Andrew J. Stone" Date: Wed, 25 Oct 2023 19:50:06 +0000 Subject: [PATCH] tests pass --- illumos-utils/src/lib.rs | 27 ++++++++++++++++++++++++++- sled-agent/src/services.rs | 3 ++- sled-storage/src/manager.rs | 8 ++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/illumos-utils/src/lib.rs b/illumos-utils/src/lib.rs index 345f097ae2..e50b35849a 100644 --- a/illumos-utils/src/lib.rs +++ b/illumos-utils/src/lib.rs @@ -4,6 +4,8 @@ //! Wrappers around illumos-specific commands. +use std::sync::atomic::{AtomicBool, Ordering}; + use cfg_if::cfg_if; pub mod addrobj; @@ -93,7 +95,7 @@ mod inner { // Helper function for starting the process and checking the // exit code result. - pub fn execute( + pub fn execute_helper( command: &mut std::process::Command, ) -> Result { let output = command.output().map_err(|err| { @@ -108,6 +110,29 @@ mod inner { } } +// Due to feature unification, the `testing` feature is enabled when some tests +// don't actually want to use it. We allow them to opt out of the use of the +// free function here. We also explicitly opt-in where mocks are used. +// +// We can remove all this when we get rid of the mocks. +pub static USE_MOCKS: AtomicBool = AtomicBool::new(false); + +pub fn execute( + command: &mut std::process::Command, +) -> Result { + cfg_if! { + if #[cfg(any(test, feature = "testing"))] { + if USE_MOCKS.load(Ordering::SeqCst) { + mock_inner::execute_helper(command) + } else { + inner::execute_helper(command) + } + } else { + inner::execute_helper(command) + } + } +} + cfg_if! { if #[cfg(any(test, feature = "testing"))] { pub use mock_inner::*; diff --git a/sled-agent/src/services.rs b/sled-agent/src/services.rs index b9a8cdada0..c692cca309 100644 --- a/sled-agent/src/services.rs +++ b/sled-agent/src/services.rs @@ -3037,6 +3037,7 @@ mod test { // Returns the expectations for a new service to be created. fn expect_new_service() -> Vec> { + illumos_utils::USE_MOCKS.store(true, Ordering::SeqCst); // Create a VNIC let create_vnic_ctx = MockDladm::create_vnic_context(); create_vnic_ctx.expect().return_once( @@ -3079,7 +3080,7 @@ mod test { let wait_ctx = svc::wait_for_service_context(); wait_ctx.expect().return_once(|_, _| Ok(())); - let execute_ctx = illumos_utils::execute_context(); + let execute_ctx = illumos_utils::execute_helper_context(); execute_ctx.expect().times(..).returning(|_| { Ok(std::process::Output { status: std::process::ExitStatus::from_raw(0), diff --git a/sled-storage/src/manager.rs b/sled-storage/src/manager.rs index fd0e607348..46fb34a3bf 100644 --- a/sled-storage/src/manager.rs +++ b/sled-storage/src/manager.rs @@ -606,6 +606,7 @@ mod tests { #[tokio::test] async fn add_u2_disk_while_not_in_normal_stage_and_ensure_it_gets_queued() { + illumos_utils::USE_MOCKS.store(false, Ordering::SeqCst); let logctx = test_setup_log( "add_u2_disk_while_not_in_normal_stage_and_ensure_it_gets_queued", ); @@ -630,6 +631,7 @@ mod tests { #[tokio::test] async fn ensure_u2_gets_added_to_resources() { + illumos_utils::USE_MOCKS.store(false, Ordering::SeqCst); let logctx = test_setup_log("ensure_u2_gets_added_to_resources"); let (mut key_manager, key_requester) = KeyManager::new(&logctx.log, HardcodedSecretRetriever::default()); @@ -651,6 +653,7 @@ mod tests { #[tokio::test] async fn wait_for_bootdisk() { + illumos_utils::USE_MOCKS.store(false, Ordering::SeqCst); let logctx = test_setup_log("wait_for_bootdisk"); let (mut key_manager, key_requester) = KeyManager::new(&logctx.log, HardcodedSecretRetriever::default()); @@ -677,6 +680,7 @@ mod tests { #[tokio::test] async fn queued_disks_get_added_as_resources() { + illumos_utils::USE_MOCKS.store(false, Ordering::SeqCst); let logctx = test_setup_log("queued_disks_get_added_as_resources"); let (mut key_manager, key_requester) = KeyManager::new(&logctx.log, HardcodedSecretRetriever::default()); @@ -714,6 +718,7 @@ mod tests { /// This allows us to control timing precisely. #[tokio::test] async fn queued_disks_get_requeued_on_secret_retriever_error() { + illumos_utils::USE_MOCKS.store(false, Ordering::SeqCst); let logctx = test_setup_log( "queued_disks_get_requeued_on_secret_retriever_error", ); @@ -766,6 +771,7 @@ mod tests { #[tokio::test] async fn delete_disk_triggers_notification() { + illumos_utils::USE_MOCKS.store(false, Ordering::SeqCst); let logctx = test_setup_log("delete_disk_triggers_notification"); let (mut key_manager, key_requester) = KeyManager::new(&logctx.log, HardcodedSecretRetriever::default()); @@ -806,6 +812,7 @@ mod tests { #[tokio::test] async fn ensure_using_exactly_these_disks() { + illumos_utils::USE_MOCKS.store(false, Ordering::SeqCst); let logctx = test_setup_log("ensure_using_exactly_these_disks"); let (mut key_manager, key_requester) = KeyManager::new(&logctx.log, HardcodedSecretRetriever::default()); @@ -922,6 +929,7 @@ mod tests { #[tokio::test] async fn upsert_filesystem() { + illumos_utils::USE_MOCKS.store(false, Ordering::SeqCst); let logctx = test_setup_log("upsert_filesystem"); let (mut key_manager, key_requester) = KeyManager::new(&logctx.log, HardcodedSecretRetriever::default());