From: Martin Schwenke Date: Mon, 31 Jan 2022 03:36:23 +0000 (+1100) Subject: ctdb-tests: Add unit tests for tunables code X-Git-Tag: tevent-0.12.0~164 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b14f2a205d25548062fbe23651bdbe1886b253fa;p=thirdparty%2Fsamba.git ctdb-tests: Add unit tests for tunables code This aims to test ctdb_tunable_load_file() but also exercises ctdb_tunable_names() and ctdb_tunable_get_value(). ctdb_tunable_set_value() is indirectly exercised via ctdb_tunable_load_file(). Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- diff --git a/ctdb/tests/UNIT/cunit/tunable_test_001.sh b/ctdb/tests/UNIT/cunit/tunable_test_001.sh new file mode 100755 index 00000000000..2ae87f6f1fd --- /dev/null +++ b/ctdb/tests/UNIT/cunit/tunable_test_001.sh @@ -0,0 +1,306 @@ +#!/bin/sh + +. "${TEST_SCRIPTS_DIR}/unit.sh" + +tfile="${CTDB_TEST_TMP_DIR}/tunable.$$" + +remove_files () +{ + rm -f "$tfile" +} +test_cleanup remove_files + +defaults="\ +SeqnumInterval=1000 +ControlTimeout=60 +TraverseTimeout=20 +KeepaliveInterval=5 +KeepaliveLimit=5 +RecoverTimeout=30 +RecoverInterval=1 +ElectionTimeout=3 +TakeoverTimeout=9 +MonitorInterval=15 +TickleUpdateInterval=20 +EventScriptTimeout=30 +MonitorTimeoutCount=20 +RecoveryGracePeriod=120 +RecoveryBanPeriod=300 +DatabaseHashSize=100001 +DatabaseMaxDead=5 +RerecoveryTimeout=10 +EnableBans=1 +NoIPFailback=0 +VerboseMemoryNames=0 +RecdPingTimeout=60 +RecdFailCount=10 +LogLatencyMs=0 +RecLockLatencyMs=1000 +RecoveryDropAllIPs=120 +VacuumInterval=10 +VacuumMaxRunTime=120 +RepackLimit=10000 +VacuumFastPathCount=60 +MaxQueueDropMsg=1000000 +AllowUnhealthyDBRead=0 +StatHistoryInterval=1 +DeferredAttachTO=120 +AllowClientDBAttach=1 +FetchCollapse=1 +HopcountMakeSticky=50 +StickyDuration=600 +StickyPindown=200 +NoIPTakeover=0 +DBRecordCountWarn=100000 +DBRecordSizeWarn=10000000 +DBSizeWarn=100000000 +PullDBPreallocation=10485760 +LockProcessesPerDB=200 +RecBufferSizeLimit=1000000 +QueueBufferSize=1024 +IPAllocAlgorithm=2 +AllowMixedVersions=0 +" + +ok_tunable_defaults () +{ + ok "$defaults" +} + +# Set required output to a version of $defaults where values for +# tunables specified in $tfile replace the default values +ok_tunable () +{ + # Construct a version of $defaults prepended with a lowercase + # version of the tunable variable, to allow case-insensitive + # matching. This would be easier with the GNU sed + # case-insensitivity flag, but that is less portable. The $0 + # condition in awk causes empty lines to be skipped, in case + # there are trailing empty lines in $defaults. + _map=$(echo "$defaults" | + awk -F= '$0 { printf "%s:%s=%s\n", tolower($1), $1, $2 }') + + # Replace values for tunables set in $tfile + while IFS='= ' read -r _var _val ; do + case "$_var" in + \#* | "") continue ;; + esac + _decval=$((_val)) + _vl=$(echo "$_var" | tr '[:upper:]' '[:lower:]') + _map=$(echo "$_map" | + sed -e "s|^\\(${_vl}:.*=\\).*\$|\\1${_decval}|") + done <"$tfile" + + # Set result, stripping off lowercase tunable prefix + ok "$(echo "$_map" | awk -F: '{ print $2 }')" +} + +test_case "Unreadable file" +: >"$tfile" +chmod a-r "$tfile" +required_error EINVAL <"$tfile" +required_error EINVAL <"$tfile" +required_error EINVAL <"$tfile" +required_error EINVAL <"$tfile" +required_error EINVAL <"$tfile" +required_error EINVAL <"$tfile" +required_error EINVAL <"$tfile" +required_error EINVAL <"$tfile" <"$tfile" <"$tfile" +ok_tunable_defaults +unit_test tunable_test "$tfile" + +test_case "OK, comments and blanks only" +cat >"$tfile" <"$tfile" <"$tfile" <"$tfile" <"$tfile" <"$tfile" <"$tfile" <"$tfile" <. +*/ + +#include "replace.h" +#include "system/filesys.h" + +#include +#include + +#include "common/tunable.c" + +int main(int argc, const char **argv) +{ + TALLOC_CTX *mem_ctx; + struct ctdb_tunable_list tun_list; + struct ctdb_var_list *list; + bool status; + int ret = 0; + int i; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + mem_ctx = talloc_new(NULL); + assert(mem_ctx != NULL); + + status = ctdb_tunable_load_file(mem_ctx, &tun_list, argv[1]); + if (!status) { + ret = EINVAL; + goto done; + } + + list = ctdb_tunable_names(mem_ctx); + assert(list != NULL); + + for (i = 0; i < list->count; i++) { + const char *var = list->var[i]; + uint32_t val; + + status = ctdb_tunable_get_value(&tun_list, var, &val); + if (!status) { + ret = EIO; + goto done; + } + + printf("%s=%"PRIu32"\n", var, val); + fflush(stdout); + } + +done: + talloc_free(mem_ctx); + return ret; +} diff --git a/ctdb/wscript b/ctdb/wscript index 4a28dfd3cf0..2be9d28e26b 100644 --- a/ctdb/wscript +++ b/ctdb/wscript @@ -920,6 +920,7 @@ def build(bld): 'conf_test', 'line_test', 'event_script_test', + 'tunable_test', ] for target in ctdb_unit_tests: