]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-tests: Add logging support to the tunables unit test
authorMartin Schwenke <mschwenke@ddn.com>
Sat, 28 Jun 2025 02:01:41 +0000 (12:01 +1000)
committerMartin Schwenke <martins@samba.org>
Tue, 22 Jul 2025 23:02:34 +0000 (23:02 +0000)
Make tunable_test respect CTDB_DEBUGLEVEL.  Using test_options.[ch]
would be overkill here.  This means including logging.c - we can't
link to the subsystem containing logging.c because the file being
tested (tunable.c) is part of that subsystem.

Support logging in the test script.  tunable_ok() builds the logging
output for the good path.  Set the debug level to NOTICE and update
expected results for individual failure path tests.

Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/tests/UNIT/cunit/tunable_test_001.sh
ctdb/tests/src/tunable_test.c

index 0fc669d84a24c28484406924e18afc8cc33135ca..d180b8ea5ff6f8c000c4846ce4c15009b114e7d6 100755 (executable)
@@ -67,6 +67,25 @@ ok_tunable_defaults()
        ok "$defaults"
 }
 
+tunable_log()
+{
+       _level="$1"
+       _msg="$2"
+
+       _all=":DEBUG:INFO:NOTICE:WARNING:ERR:"
+       # Keep the debug levels log at.  This strips off the levels up
+       # to and including the current $CTDB_DEBUGLEVEL, but then puts
+       # back $CTDB_DEBUGLEVEL.  Cheaper than a loop...
+       _want=":${CTDB_DEBUGLEVEL}:${_all#*":${CTDB_DEBUGLEVEL}:"}"
+
+       case "$_want" in
+       *":${_level}:"*)
+               log="${log}${_msg}
+" # Intentional newline
+               ;;
+       esac
+}
+
 # Update $_map with tunable settings from 1 file
 # values
 ok_tunable_1()
@@ -77,6 +96,8 @@ ok_tunable_1()
                return
        fi
 
+       tunable_log "NOTICE" "Loading tunables from ${_file}"
+
        while IFS='=    ' read -r _var _val; do
                case "$_var" in
                \#* | "") continue ;;
@@ -101,12 +122,16 @@ ok_tunable()
        _map=$(echo "$defaults" |
                awk -F= '$0 { printf "%s:%s=%s\n", tolower($1), $1, $2 }')
 
+       log=""
+
        ok_tunable_1 "$tfile"
 
        # Set result, stripping off lowercase tunable prefix
-       ok "$(echo "$_map" | awk -F: '{ print $2 }')"
+       ok "${log}$(echo "$_map" | awk -F: '{ print $2 }')"
 }
 
+export CTDB_DEBUGLEVEL="NOTICE"
+
 test_case "Unreadable file"
 : >"$tfile"
 chmod a-r "$tfile"
@@ -125,6 +150,7 @@ rm -f "$tfile"
 test_case "Invalid file, contains 1 word"
 echo "Hello" >"$tfile"
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Invalid line containing "Hello"
 EOF
 unit_test tunable_test "$tfile"
@@ -132,6 +158,7 @@ unit_test tunable_test "$tfile"
 test_case "Invalid file, contains multiple words"
 echo "Hello world!" >"$tfile"
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Invalid line containing "Hello world!"
 EOF
 unit_test tunable_test "$tfile"
@@ -139,6 +166,7 @@ unit_test tunable_test "$tfile"
 test_case "Invalid file, missing value"
 echo "EnableBans=" >"$tfile"
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Invalid line containing "EnableBans"
 EOF
 unit_test tunable_test "$tfile"
@@ -146,6 +174,7 @@ unit_test tunable_test "$tfile"
 test_case "Invalid file, invalid value (not a number)"
 echo "EnableBans=value" >"$tfile"
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Invalid value "value" for tunable "EnableBans"
 EOF
 unit_test tunable_test "$tfile"
@@ -153,6 +182,7 @@ unit_test tunable_test "$tfile"
 test_case "Invalid file, missing key"
 echo "=123" >"$tfile"
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Syntax error
 EOF
 unit_test tunable_test "$tfile"
@@ -162,6 +192,7 @@ cat >"$tfile" <<EOF
  =0
 EOF
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Syntax error
 EOF
 unit_test tunable_test "$tfile"
@@ -169,6 +200,7 @@ unit_test tunable_test "$tfile"
 test_case "Invalid file, unknown tunable"
 echo "HelloWorld=123" >"$tfile"
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Unknown tunable "HelloWorld"
 EOF
 unit_test tunable_test "$tfile"
@@ -176,6 +208,7 @@ unit_test tunable_test "$tfile"
 test_case "Invalid file, obsolete tunable"
 echo "MaxRedirectCount=123" >"$tfile"
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Obsolete tunable "MaxRedirectCount"
 EOF
 unit_test tunable_test "$tfile"
@@ -183,6 +216,7 @@ unit_test tunable_test "$tfile"
 test_case "Invalid file, trailing non-whitespace garbage"
 echo "EnableBans=0xgg" >"$tfile"
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Invalid value "0xgg" for tunable "EnableBans"
 EOF
 unit_test tunable_test "$tfile"
@@ -196,6 +230,7 @@ HelloWorld=123
 MaxRedirectCount =123
 EOF
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Invalid line containing "EnableBans"
 ctdb_tunable_load_file: Invalid value "value" for tunable "EnableBans"
 ctdb_tunable_load_file: Syntax error
@@ -209,6 +244,7 @@ EnableBans=value
 EnableBans=0
 EOF
 required_error EINVAL <<EOF
+Loading tunables from ${tfile}
 ctdb_tunable_load_file: Unknown tunable "HelloWorld"
 ctdb_tunable_load_file: Invalid value "value" for tunable "EnableBans"
 EOF
@@ -216,12 +252,12 @@ unit_test tunable_test "$tfile"
 
 test_case "OK, missing file"
 rm -f "$tfile"
-ok_tunable_defaults
+ok_tunable
 unit_test tunable_test "$tfile"
 
 test_case "OK, empty file"
 : >"$tfile"
-ok_tunable_defaults
+ok_tunable
 unit_test tunable_test "$tfile"
 
 test_case "OK, comments and blanks only"
@@ -232,7 +268,7 @@ cat >"$tfile" <<EOF
 
 
 EOF
-ok_tunable_defaults
+ok_tunable
 unit_test tunable_test "$tfile"
 
 test_case "OK, 1 tunable"
index c2ff48b9b69d3331449b584803c56508c4b72f42..d98900e4e10216b7110d877f0d4d4dd49e51da0e 100644 (file)
@@ -23,6 +23,8 @@
 #include <talloc.h>
 #include <assert.h>
 
+#include "common/logging.c"
+
 #include "common/tunable.c"
 
 int main(int argc, const char **argv)
@@ -42,6 +44,8 @@ int main(int argc, const char **argv)
        mem_ctx = talloc_new(NULL);
        assert(mem_ctx != NULL);
 
+       logging_init(mem_ctx, "file:", NULL, "tunable_test");
+
        ctdb_tunable_set_defaults(&tun_list);
 
        status = ctdb_tunable_load_file(mem_ctx, &tun_list, argv[1]);