Skip to content

Commit

Permalink
tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjstone committed Oct 25, 2023
1 parent b44defa commit d60994c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
27 changes: 26 additions & 1 deletion illumos-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

//! Wrappers around illumos-specific commands.
use std::sync::atomic::{AtomicBool, Ordering};

use cfg_if::cfg_if;

pub mod addrobj;
Expand Down Expand Up @@ -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<std::process::Output, ExecutionError> {
let output = command.output().map_err(|err| {
Expand All @@ -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<std::process::Output, ExecutionError> {
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::*;
Expand Down
3 changes: 2 additions & 1 deletion sled-agent/src/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3037,6 +3037,7 @@ mod test {

// Returns the expectations for a new service to be created.
fn expect_new_service() -> Vec<Box<dyn std::any::Any>> {
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(
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 8 additions & 0 deletions sled-storage/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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",
);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit d60994c

Please sign in to comment.