On Thu, Aug 22, 2024 at 03:49:40PM +0200, Petr Machata wrote:
In commit 8510801a9dbd ("selftests: drv-net: add ability to schedule cleanup with defer()"), a defer helper was added to Python selftests. The idea is to keep cleanup commands close to their dirtying counterparts, thereby making it more transparent what is cleaning up what, making it harder to miss a cleanup, and make the whole cleanup business exception safe. All these benefits are applicable to bash as well, exception safety can be interpreted in terms of safety vs. a SIGINT.
This patch therefore introduces a framework of several helpers that serve to schedule cleanups in bash selftests:
- defer_scope_push(), defer_scope_pop(): Deferred statements can be batched together in scopes. When a scope is popped, the deferred commands schoduled in that scope are executed in the order opposite to order of
s/schoduled/scheduled/
their scheduling.
defer(): Schedules a defer to the most recently pushed scope (or the default scope if none was pushed.)
defer_scopes_cleanup(): Pops any unpopped scopes, including the default one. The selftests that use defer should run this in their cleanup function. This is important to get cleanups of interrupted scripts.
Consistent use of defers however obviates the need for a separate cleanup function -- everything is just taken care of in defers. So this patch actually introduces a cleanup() helper in the forwarding lib.sh, which calls just pre_cleanup() and defer_scopes_cleanup(). Selftests are obviously still free to override the function.
defer_scoped_fn(): Sometimes a function would like to introduce a new defer scope, then run whatever it is that it wants to run, and then pop the scope to run the deferred cleanups. The helper defer_scoped_fn() can be used to derive from one function its wrapper that pushes a defer scope before the function is called, and pops it after it returns.
The following patches will convert several selftests to this new framework.
The intention is to make sure new tests are using these helpers?
Signed-off-by: Petr Machata petrm@nvidia.com
tools/testing/selftests/net/forwarding/lib.sh | 83 +++++++++++++++++++
Does it make sense to place these helpers in net/lib.sh?