diff --git a/t/sharness.d/flux-sharness.sh b/t/sharness.d/flux-sharness.sh index 0de82a43460c..3f16388cb745 100644 --- a/t/sharness.d/flux-sharness.sh +++ b/t/sharness.d/flux-sharness.sh @@ -24,6 +24,37 @@ test_size_large() { echo ${size} } + +# +# Tests using test_under_flux() and which load their own modules should +# ensure those modules are unloaded at the end of the test for proper +# cleanup and test coverage (also as a general principle, module unload +# should be something explicitly tested). +# +# The functions below ensure that every module loaded at "test_done" time +# when test_under_flux() was used was also loaded before test_under_flux +# was called. +# +flux_module_list() { + flux module list | awk '!/^Module/{print $1}' | sort +} + +check_module_list() { + flux_module_list > module-list.final + while read module; do + grep "^$module$" module-list.initial >/dev/null 2>&1 \ + || bad="${bad}${bad:+ }$module" + done < module-list.final + if test -n "$bad"; then + test -n "$logfile" \ + && say_color error >&3 \ + "Error: manually loaded module(s) not unloaded: $bad" + # This function is run under test_eval_ so redirect + # error message to &5 (saved stdout) so text doesn't disappear: + error >&5 2>&1 "manually loaded module(s) not unloaded: $bad" + fi +} + # # Reinvoke a test file under a flux comms instance # @@ -35,6 +66,8 @@ test_under_flux() { log_file="$TEST_NAME.broker.log" if test -n "$TEST_UNDER_FLUX_ACTIVE" ; then cleanup rm "${SHARNESS_TEST_DIRECTORY:-..}/$log_file" + flux_module_list > module-list.initial + cleanup check_module_list return fi quiet="-o -q,-Slog-filename=${log_file},-Slog-forward-level=7" diff --git a/t/t0001-basic.t b/t/t0001-basic.t index 9e52f948c9e4..c9ef42d74695 100755 --- a/t/t0001-basic.t +++ b/t/t0001-basic.t @@ -120,6 +120,35 @@ test_expect_success 'test_under_flux works' ' ) && grep "size=2" test-under-flux/out ' + +test_expect_success 'test_under_flux fails if loaded modules are not unloaded' ' + mkdir -p test-under-flux && ( + cd test-under-flux && + cat >.test.modcheck.t <<-EOF && + #!/bin/sh + test_description="test_under_flux with module loaded, but not unloaded" + . "\$SHARNESS_TEST_SRCDIR"/sharness.sh + test_under_flux 2 minimal + test_expect_success "flux module load kvs" " + flux module load -r 0 kvs + " + test_done + EOF + chmod +x .test.modcheck.t && + SHARNESS_TEST_DIRECTORY=`pwd` && + export SHARNESS_TEST_SRCDIR SHARNESS_TEST_DIRECTORY FLUX_BUILD_DIR debug && + test_expect_code 1 ./.test.modcheck.t 2>err.modcheck \ + | grep -v sharness: >out.modcheck + ) && + cat >expected.modcheck <<-EOF && + ok 1 - flux module load kvs + # passed all 1 test(s) + 1..1 + error: manually loaded module(s) not unloaded: kvs + EOF + test_cmp expected.modcheck test-under-flux/out.modcheck +' + test_expect_success 'flux-start -o,--setattr ATTR=VAL can set broker attributes' ' ATTR_VAL=`flux start ${ARGS} -o,--setattr=foo-test=42 flux getattr foo-test` && test $ATTR_VAL -eq 42