When a test case requires specific modules for testing, it loads and uses the modules. To maintain a clean environment, it is desired that the test case unloads after execution. The modules should be unloaded only when the module was not loaded before the test case run. However, writing logic to determine the module load status and unload accordingly can be cumbersome. To simplify and standardize such module unload control, introduce the helper function _load_module(). It attempts to load the specified module and records it to the global array MODULES_TO_UNLOAD only when the module was not already loaded. Modules listed in MODULES_TO_UNLOAD are unloaded by _unload_modules() after each test case run. Signed-off-by: Shin'ichiro Kawasaki --- common/rc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/common/rc b/common/rc index abf66a4..0a8caca 100644 --- a/common/rc +++ b/common/rc @@ -69,6 +69,18 @@ _have_driver() return 0 } +# Load the specified module. Record the module to MODULES_TO_UNLOAD so that they +# will be unloaded by _unload_modules() after each test case run. +_load_module() { + local modname="${1/-/_}" + + if ! modprobe --quiet --first-time "${modname}"; then + return 1 + fi + + MODULES_TO_UNLOAD+=("${modname}") +} + # Check that the specified crypto algorithm is present, regardless of whether # it's built-in or as module. _have_crypto_algorithm() -- 2.53.0