Skip to content

Fully remove automock, USE_MOCKS - #7908

Merged
smklein merged 12 commits into
mainfrom
stop-using-mocks
Apr 5, 2025
Merged

smklein merged 12 commits into
mainfrom
stop-using-mocks

Conversation

@smklein

@smklein smklein commented Apr 3, 2025

Copy link
Copy Markdown
Collaborator

Fixes #4481

@@ -0,0 +1,53 @@
:showtitle:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend starting with this file, for review


/// A fake implementation of [crate::dladm::Dladm].
///
/// This struct implements the [crate::dladm::Api] interface but avoids

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This comment also applies to zone.rs) this could be more real, but this was the "minimal API required" to replace most mock-based tests.

Comment thread illumos-utils/src/dladm.rs Resolved
Comment thread illumos-utils/src/zone.rs
}
}

async fn wait_for_service(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, this isn't a "zone" command, but it's tightly coupled with the zone boot process

@gjcolombo gjcolombo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exciting stuff! With apologies for the unsolicited review, I had a couple of questions about the high-level testing arrangement you're aiming for here. Overall, though, this looks really nice (and there are some concepts here I think I'd like to borrow in Propolis...).

Comment thread illumos-utils/src/fakes/README.adoc Resolved
Comment thread illumos-utils/src/dladm.rs Resolved
Comment on lines +29 to +31
* They jump between abstraction layers. If you're calling a mocked API which
accesses the host OS several callstacks down, your test **must** have an
"expect" call for that API, or it will fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This question mildly risks touching the third rail and setting off a long discussion about the philosophy of test doubles, but I want to ask anyway to check my understanding of your approach here, particularly because I've been equally unsatisfied with my attempts to use mocks for propolis-server testing. Would it be fair to restate the idea like this?

  • Today there's a chain of objects that goes Foo -> Bar -> Baz -> illumos-utils
  • The code uses mocks as follows:
    • When the testing feature is enabled, illumos-utils exports mock versions of its various structs
    • There's also a static USE_MOCKS flag that determines whether that lib's public free functions are mocked
  • The problem is that if you're testing Foo code, you have to obtain and correctly set expectations on any illumos-utils mocks the layers under Foo happen to create, or the tests will fail
  • The solutions are:
    • Use explicit dependency injection via trait objects (e.g. the SystemApi trait and the assorted Api traits in illumos-utils) instead of using cfg_if to redirect callers to mocks
    • Define the trait objects so they don't have expectations that callers have to manage

Assuming all this is right: in principle, the trait objects could still refer to a mock instead of a fake, correct? That is, the test double could still be of the sort that asserts/panics if the end of a test is reached and it has unmet expectations. We're just (justifiably!) choosing not to do that here because it creates the various test maintenance burdens described on line 32ff.

Fair summary?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how useful this is, but it's in the same area so I thought I'd note it - in the large sled-agent reconciler branch I'm working on, I'm using the "trait that can be implemented by a real thing or a test" technique to entirely swap out "do we even call into sled-storage or illumos_utils at all" for simpler unit testing. None of this is in PR yet, but by way of example, links from a subset of the reconciler that adopts disks:

  1. The public API is written exclusively in terms of real things (e.g., adopting a disk requires a StorageKeyRequester to interact with LRTQ)
  2. That public API is a tiny wrapper around a private method that does all the real work and accepts a thing that implements a trait to do disk adotption
  3. The public API uses an impl of that trait that calls into the real disk adoption implementation from sled-storage (and ultimately illumos_utils)
  4. Tests can provide whatever implementation they want by calling the private method directly. (In this case the test implementor records what the request was and always returns ok; other tests might have the test adapter return errors or whatever.)

The biggest thing I like about this is that it's all local; I don't have to worry about how my code interacts with test support provided by some other crate (or even some other module). It does mean these tests don't exercise any of the external dependencies at all, but that seems fine for unit tests, I think? This is consistent with the README's note that mocks are kinda painful to use due to having to set up expectations ahead of time, but I'm less sure how to feel about things like .real_api(). That's pretty similar to my RealDiskAdopter, except that the latter is entirely confined within the module.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gjcolombo , re: your comments:

  • I agree, the trait objects are certainly allowed to implement whatever they'd like to - so theoretically, they could be using mocks, but it seems a little silly to me to use both "mockall mocks" and "fakes" together. That being said, the implementation of the fake can be "whatever you want" - so if you want to have an API to "ensure that certain functions must be called, or the test fails", that's 100% valid!

@jgallagher , re your comments:

This approach seems very similar to mine - they're fake-based, with an abstraction that allows tests to control "what the underlying layers are doing". I think the main distinction in our approaches is:

  • What layer do we want to do the faking? In this PR, I happened to slice at illumos-utils, but if you have multiple dependencies (I'm being handwavey here), like "sled-agent" -> "sled-storage" -> "sled-hardware" -> "illumos-utils", presumably, you'd have fakes for the "sled-storage", "sled-hardware" and "illumos-utils" layers, and they would be private, not appearing in the API, right?

My main goal with this PR was "get rid of the mocks used in sled-agent/src/services.rs" - which I considered particularly difficult to reason about - which required calls to illumos-utils through multiple layers, some contained within the services module, and others invoked indirectly through e.g. illumos-utils/src/running_zone.rs, or illumos-utils/src/link.rs

However, I do think we could create a "fake running zone" and "fake link" layer instead of dropping all the way down to dladm and zones. I just wondered if that would be "as flexible", because the representation of the host OS itself seems like a more concrete abstraction.

Either way, I like the idea of minimizing the calls to real_api between abstraction layers. In some spots within the codebase, I think it's fine (it's labeling where future PRs need to do cleanup), but I do think it's a good goal to "use concrete, real types as inputs, and use shims to make internal testing viable".

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I moved in this direction in 61e4176 - many of the "real_api" calls still exist, but especially for larger modules, I'm moving them towards "being part of fn new(), where fn new_inner() is an alternative used by tests".

vnic_allocator: VnicAllocator::new(
VNIC_ALLOCATOR_SCOPE,
etherstub,
Arc::new(illumos_utils::dladm::Dladm::real_api()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the change (and similar changes elsewhere) is what led me to ask the long-winded question on the README. I was sort of expecting to see that ProbeManager would get an Arc<dyn Dladm> here instead of unconditionally calling real_api, so that whatever code path is instantiating this can specify whether or not to use a fake. Is the idea that for now the fakes will only be used by code paths that are exercised by existing tests?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to test the ProbeManager, and abstract out the dladm API, I think it should have an API (or at least a new_internal API) that takes a dyn-based argument.

I admit, this PR is not a big improvement in the probe_manager, but rather, a lateral move: it's basically labeling that "a real API is being used here", which would need to be pulled out to be unit tested properly.

(this is part of why I used the real_api name, to make it clear where we're doing this -- injection of real_api is basically a sign that this code, as written, is not currently unit-testable with fakes. But we can find / fix those spots)

@smklein
smklein requested review from gjcolombo and jgallagher April 4, 2025 21:10

@gjcolombo gjcolombo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I've sat with it a bit more, I think I get what you're going for here. The newest changes LGTM. Thanks for working on this (and for discussing it in the comments!).

Comment thread illumos-utils/src/dladm.rs Resolved
}

// InstanceManager::ensure_state calls Instance::put_state(Running),
// which calls Instance::propolis_ensure,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on a branch that renames a bunch of these routines and I'm grateful not to have to update any of the names here anymore ;)

@smklein
smklein enabled auto-merge (squash) April 5, 2025 18:08
@smklein
smklein merged commit bad5ff2 into main Apr 5, 2025
@smklein
smklein deleted the stop-using-mocks branch April 5, 2025 19:24
Nieuwejaar pushed a commit that referenced this pull request Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

static USE_MOCKS is gross and should be removed

3 participants