3
0
mirror of https://github.com/rvalitov/zabbix-php-fpm.git synced 2023-11-05 03:30:27 +01:00

[fix] correct output for arguments specified, correct time elapsed asserts

This commit is contained in:
Ramil Valitov 2020-09-21 14:27:04 +03:00
parent 37d980cff0
commit d4b2ff46a6
No known key found for this signature in database
GPG Key ID: 2AB186DABDFE1914
2 changed files with 45 additions and 12 deletions

View File

@ -148,13 +148,17 @@ function StartTimer() {
function printElapsedTime() {
local END_TIME
local ELAPSED_TIME
ELAPSED_TIME
END_TIME=$(date +%s%N)
ELAPSED_TIME=$(echo "($END_TIME - $START_TIME)/1000000" | bc)
printYellow "Elapsed time $ELAPSED_TIME ms"
}
function assertExecutionTime() {
assertTrue "The script worked for too long" "[ $ELAPSED_TIME -lt $MAX_TIME ]"
}
function getPHPVersion() {
TEST_STRING=$1
PHP_VERSION=$(echo "$TEST_STRING" | grep -oP "(\d\.\d)")
@ -607,6 +611,7 @@ testStatusScriptSocket() {
IS_OK=$(echo "$DATA" | grep -F '{"pool":"')
printElapsedTime
assertNotNull "Failed to get status from pool $TEST_SOCKET: $DATA" "$IS_OK"
assertExecutionTime
printGreen "Success test of $TEST_SOCKET"
printSuccess "${FUNCNAME[0]}"
}
@ -620,6 +625,7 @@ testStatusScriptPort() {
IS_OK=$(echo "$DATA" | grep -F '{"pool":"')
printElapsedTime
assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK"
assertExecutionTime
printGreen "Success test of $PHP_POOL"
printSuccess "${FUNCNAME[0]}"
}
@ -629,6 +635,7 @@ testZabbixStatusSocket() {
IS_OK=$(echo "$DATA" | grep -F '{"pool":"')
printElapsedTime
assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK"
assertExecutionTime
printGreen "Success test of $PHP_POOL"
printSuccess "${FUNCNAME[0]}"
}
@ -641,6 +648,7 @@ testZabbixStatusPort() {
IS_OK=$(echo "$DATA" | grep -F '{"pool":"')
printElapsedTime
assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK"
assertExecutionTime
printGreen "Success test of $PHP_POOL"
printSuccess "${FUNCNAME[0]}"
}
@ -650,18 +658,39 @@ testDiscoverScriptReturnsData() {
IS_OK=$(echo "$DATA" | grep -F '{"data":[{"{#POOLNAME}"')
printElapsedTime
assertNotNull "Discover script failed: $DATA" "$IS_OK"
assertExecutionTime
printSuccess "${FUNCNAME[0]}"
}
testDiscoverScriptDebug() {
local DATA
local NUMBER_OF_ERRORS
DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "nosleep" "/php-fpm-status")
NUMBER_OF_ERRORS=$(echo "$DATA" | grep -o -F 'Error:' | wc -l)
if [[ $NUMBER_OF_ERRORS -gt 0 ]]; then
ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:')
printYellow "Errors list:"
printYellow "$ERRORS_LIST"
travis_fold_start "testDiscoverScriptDebug_full" "ⓘ Full output"
echo "$DATA"
travis_fold_end
fi
printElapsedTime
assertEquals "Discover script errors mismatch" "0" "$NUMBER_OF_ERRORS"
assertExecutionTime
printSuccess "${FUNCNAME[0]}"
}
testDiscoverScriptTimeout() {
DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "nosleep" "max_tasks" "1" "/php-fpm-status")
NUMBER_OF_ERRORS=$(echo "$DATA" | grep -o -F 'Error:' | wc -l)
PHP_COUNT=$(getNumberOfPHPVersions)
if [[ $PHP_COUNT != "$NUMBER_OF_ERRORS" ]]; then
ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:')
printYellow "Errors list:"
printYellow "$ERRORS_LIST"
travis_fold_start "testDiscoverScriptDebug_full" "ⓘ Full output"
travis_fold_start "testDiscoverScriptTimeout_full" "ⓘ Full output"
echo "$DATA"
travis_fold_end
fi
@ -675,6 +704,7 @@ testZabbixDiscoverReturnsData() {
IS_OK=$(echo "$DATA" | grep -F '{"data":[{"{#POOLNAME}"')
printElapsedTime
assertNotNull "Discover script failed: $DATA" "$IS_OK"
assertExecutionTime
printSuccess "${FUNCNAME[0]}"
}
@ -694,6 +724,7 @@ testDiscoverScriptSleep() {
fi
assertTrue "No success time checks detected" "[ $CHECK_OK_COUNT -gt 0 ] || [ $STOP_OK_COUNT -eq 1 ]"
assertTrue "No success stop checks detected" "[ $STOP_OK_COUNT -gt 0 ]"
assertExecutionTime
printSuccess "${FUNCNAME[0]}"
}
@ -715,7 +746,7 @@ testDiscoverScriptRunDuration() {
printYellow "Success time checks: $CHECK_OK_COUNT"
printYellow "Stop time checks: $STOP_OK_COUNT"
assertTrue "The script worked for too long" "[ $ELAPSED_TIME -lt $MAX_TIME ]"
assertExecutionTime
printSuccess "${FUNCNAME[0]}"
}
@ -727,8 +758,7 @@ testZabbixDiscoverRunDuration() {
MAX_TIME=$(echo "$ZABBIX_TIMEOUT * 1000" | bc)
printElapsedTime
assertTrue "The script worked for too long" "[ $ELAPSED_TIME -lt $MAX_TIME ]"
assertExecutionTime
printSuccess "${FUNCNAME[0]}"
}

View File

@ -691,10 +691,13 @@ while [ $ARG_ID -le $ARGS_COUNT ]; do
ARG=$1
if [[ $ARG == "debug" ]]; then
DEBUG_MODE="1"
echo "Debug mode enabled"
PrintDebug "Debug mode enabled"
if [[ $ARG_ID -ne 1 ]]; then
PrintDebug "Error: argument 'debug' if used must be the first argument"
fi
elif [[ $ARG == "sleep" ]]; then
USE_SLEEP_TIMEOUT="1"
echo "Debug: Sleep timeout enabled"
PrintDebug "Debug: Sleep timeout enabled"
elif [[ $ARG == "max_tasks" ]]; then
ARG_ID=$((ARG_ID + 1))
shift 1
@ -702,7 +705,7 @@ while [ $ARG_ID -le $ARGS_COUNT ]; do
if [[ $ARG -ge 1 ]]; then
MAX_PARALLEL_TASKS=$1
fi
echo "Debug: argument 'max_tasks' = '$ARG' detected, the resulting parameter is set to $MAX_PARALLEL_TASKS"
PrintDebug "Debug: argument 'max_tasks' = '$ARG' detected, the resulting parameter is set to $MAX_PARALLEL_TASKS"
elif [[ $ARG == "max_time" ]]; then
ARG_ID=$((ARG_ID + 1))
shift 1
@ -710,16 +713,16 @@ while [ $ARG_ID -le $ARGS_COUNT ]; do
if [[ $ARG -ge 1 ]]; then
MAX_EXECUTION_TIME=$1
fi
echo "Debug: argument 'max_time' = '$ARG' detected, the resulting parameter is set to $MAX_EXECUTION_TIME"
PrintDebug "Debug: argument 'max_time' = '$ARG' detected, the resulting parameter is set to $MAX_EXECUTION_TIME"
elif [[ $ARG == "nosleep" ]]; then
MAX_EXECUTION_TIME="10000000"
echo "Debug: Timeout checks disabled"
PrintDebug "Debug: Timeout checks disabled"
elif [[ $ARG == "?" ]] || [[ $ARG == "help" ]] || [[ $ARG == "/?" ]]; then
$S_CAT <<EOF
NAME: discovery script for zabbix-php-fpm
USAGE: $0 [ARGUMENTS...]
USAGE: $0 [debug] [ARGUMENTS...]
ARGUMENTS:
debug - to display verbose output on screen
debug - to display verbose output on screen, if you use this argument, then put it first
nosleep - to disable timeout checks, used for testing only
sleep - to enable forced timeouts between operations, used for testing only
max_tasks <VALUE> - sets maximum number of allowed parallel tasks to VALUE (must be >=1)