Repository navigation
Fully remove automock, USE_MOCKS - #7908
Conversation
| @@ -0,0 +1,53 @@ | |||
| :showtitle: | |||
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
(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.
| } | ||
| } | ||
|
|
||
| async fn wait_for_service( |
There was a problem hiding this comment.
Strictly speaking, this isn't a "zone" command, but it's tightly coupled with the zone boot process
gjcolombo
left a comment
There was a problem hiding this comment.
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...).
| * 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. |
There was a problem hiding this comment.
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
testingfeature is enabled,illumos-utilsexports mock versions of its various structs - There's also a
static USE_MOCKSflag that determines whether that lib's public free functions are mocked
- When the
- The problem is that if you're testing Foo code, you have to obtain and correctly set expectations on any
illumos-utilsmocks 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
SystemApitrait and the assortedApitraits inillumos-utils) instead of usingcfg_ifto redirect callers to mocks - Define the trait objects so they don't have expectations that callers have to manage
- Use explicit dependency injection via trait objects (e.g. the
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?
There was a problem hiding this comment.
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:
- The public API is written exclusively in terms of real things (e.g., adopting a disk requires a
StorageKeyRequesterto interact with LRTQ) - 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
- The public API uses an impl of that trait that calls into the real disk adoption implementation from sled-storage (and ultimately illumos_utils)
- 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.
There was a problem hiding this comment.
@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".
There was a problem hiding this comment.
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()), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
gjcolombo
left a comment
There was a problem hiding this comment.
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!).
| } | ||
|
|
||
| // InstanceManager::ensure_state calls Instance::put_state(Running), | ||
| // which calls Instance::propolis_ensure, |
There was a problem hiding this comment.
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 ;)
Fixes #4481