selftests: net: lib: Introduce deferred commandsIn commit 8510801a9dbd ("selftests: drv-net: add ability to schedulecleanup with defer()"), a defer helper was added to Python selftests.The idea i
selftests: net: lib: Introduce deferred commandsIn commit 8510801a9dbd ("selftests: drv-net: add ability to schedulecleanup 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 itharder to miss a cleanup, and make the whole cleanup business exceptionsafe. All these benefits are applicable to bash as well, exception safetycan be interpreted in terms of safety vs. a SIGINT.This patch therefore introduces a framework of several helpers that serveto 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 scheduled in that scope are executed in the order opposite to order of their scheduling.- defer(): Schedules a defer to the most recently pushed scope (or the default scope if none was pushed.)- defer_prio(): Schedules a defer on the priority track. The priority defer queue is run before the default defer queue when scope is popped. The issue that this is addressing is specifically the one of restoring devlink shared buffer threshold type. When setting up static thresholds, one has to first change the threshold type to static, then override the individual thresholds. When cleaning up, it would be natural to reset the threshold values first, then change the threshold type. But the values that are valid for dynamic thresholds are generally invalid for static thresholds and vice versa. Attempts to restore the values first would be bounced. Thus one has to first reset the threshold type, then adjust the thresholds. (You could argue that the shared buffer threshold type API is broken and you would be right, but here we are.) This cannot be solved by pure defers easily. I considered making it possible to disable an existing defer, so that one could then schedule a new defer and disable the original. But this forward-shifting of the defer job would have to take place after every threshold-adjusting command, which would make it very awkward to schedule these jobs.- defer_scopes_cleanup(): Pops any unpopped scopes, including the default one. The selftests that use defer should run this in their exit trap. This is important to get cleanups of interrupted scripts.- in_defer_scope(): 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 in_defer_scope() can be used to run another command within such environment, such that any scheduled defers run after the command finishes.The framework is added as a separate file lib/sh/defer.sh so that it can beused by all bash selftests, including those that do not currently uselib.sh. lib.sh however includes the file by default, because ideally alltests would use these helpers instead of hand-rolling their cleanups.Signed-off-by: Petr Machata <petrm@nvidia.com>Signed-off-by: Paolo Abeni <pabeni@redhat.com>
show more ...