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

[add] local vars in test suite

This commit is contained in:
Ramil Valitov 2020-09-21 16:43:24 +03:00
parent 8423a7a1a1
commit 619599f692
No known key found for this signature in database
GPG Key ID: 2AB186DABDFE1914
2 changed files with 156 additions and 31 deletions

View File

@ -127,14 +127,16 @@ function getUserParameters() {
} }
function restoreUserParameters() { function restoreUserParameters() {
local PARAMS_FILE
PARAMS_FILE=$(getUserParameters) PARAMS_FILE=$(getUserParameters)
sudo rm -f "$PARAMS_FILE" sudo rm -f "$PARAMS_FILE"
sudo cp "$TRAVIS_BUILD_DIR/zabbix/userparameter_php_fpm.conf" "$(sudo find /etc/zabbix/ -name 'zabbix_agentd*.d' -type d 2>/dev/null | sort | head -n1)" sudo cp "$TRAVIS_BUILD_DIR/zabbix/userparameter_php_fpm.conf" "$(sudo find /etc/zabbix/ -name 'zabbix_agentd*.d' -type d 2>/dev/null | sort | head -n1)"
} }
function AddSleepToConfig() { function AddSleepToConfig() {
local PARAMS_FILE
PARAMS_FILE=$(getUserParameters) PARAMS_FILE=$(getUserParameters)
sudo sed -i 's#.*zabbix_php_fpm_discovery.*#UserParameter=php-fpm.discover[*],sudo /etc/zabbix/zabbix_php_fpm_discovery.sh sleep max_tasks 1 $1#' "$PARAMS_FILE" sudo sed -i 's#.*zabbix_php_fpm_discovery.*#UserParameter=php-fpm.discover[*],sudo /etc/zabbix/zabbix_php_fpm_discovery.sh sleep $1#' "$PARAMS_FILE"
travis_fold_start "AddSleepToConfig" "ⓘ New UserParameter file" travis_fold_start "AddSleepToConfig" "ⓘ New UserParameter file"
sudo cat "$PARAMS_FILE" sudo cat "$PARAMS_FILE"
travis_fold_end travis_fold_end
@ -161,7 +163,8 @@ function assertExecutionTime() {
} }
function getPHPVersion() { function getPHPVersion() {
TEST_STRING=$1 local TEST_STRING=$1
local PHP_VERSION
PHP_VERSION=$(echo "$TEST_STRING" | grep -oP "(\d\.\d)") PHP_VERSION=$(echo "$TEST_STRING" | grep -oP "(\d\.\d)")
if [[ -z "$PHP_VERSION" ]]; then if [[ -z "$PHP_VERSION" ]]; then
PHP_VERSION=$(echo "$TEST_STRING" | grep -oP "php(\d)" | grep -oP "(\d)") PHP_VERSION=$(echo "$TEST_STRING" | grep -oP "php(\d)" | grep -oP "(\d)")
@ -179,7 +182,7 @@ function getEtcPHPDirectory() {
return 0 return 0
fi fi
LIST_OF_DIRS=( local LIST_OF_DIRS=(
"/etc/php/" "/etc/php/"
"/etc/php5/" "/etc/php5/"
) )
@ -204,11 +207,12 @@ function getRunPHPDirectory() {
return 0 return 0
fi fi
LIST_OF_DIRS=( local LIST_OF_DIRS=(
"/run/" "/run/"
"/var/run/" "/var/run/"
) )
for PHP_TEST_DIR in "${LIST_OF_DIRS[@]}"; do for PHP_TEST_DIR in "${LIST_OF_DIRS[@]}"; do
local RESULT_DIR
RESULT_DIR=$(sudo find "$PHP_TEST_DIR" -name 'php*-fpm.sock' -type s -exec dirname {} \; 2>/dev/null | sort | head -n1) RESULT_DIR=$(sudo find "$PHP_TEST_DIR" -name 'php*-fpm.sock' -type s -exec dirname {} \; 2>/dev/null | sort | head -n1)
if [[ -d "$RESULT_DIR" ]]; then if [[ -d "$RESULT_DIR" ]]; then
PHP_SOCKET_DIR="$RESULT_DIR/" PHP_SOCKET_DIR="$RESULT_DIR/"
@ -218,17 +222,21 @@ function getRunPHPDirectory() {
if [[ -z "$PHP_SOCKET_DIR" ]]; then if [[ -z "$PHP_SOCKET_DIR" ]]; then
#Try to parse the location from default config #Try to parse the location from default config
local PHP_DIR
PHP_DIR=$(getEtcPHPDirectory) PHP_DIR=$(getEtcPHPDirectory)
EXIT_CODE=$? local EXIT_CODE=$?
assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE"
assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]"
local DEFAULT_CONF
DEFAULT_CONF=$(sudo find "$PHP_DIR" -name "www.conf" -type f | uniq | head -n1) DEFAULT_CONF=$(sudo find "$PHP_DIR" -name "www.conf" -type f | uniq | head -n1)
assertTrue "Failed to find default www.conf file inside '$PHP_DIR'" "[ -n $DEFAULT_CONF ]" assertTrue "Failed to find default www.conf file inside '$PHP_DIR'" "[ -n $DEFAULT_CONF ]"
local DEFAULT_SOCKET
DEFAULT_SOCKET=$(sudo grep -Po 'listen = (.+)' "$DEFAULT_CONF" | cut -d '=' -f2 | sed -e 's/^[ \t]*//') DEFAULT_SOCKET=$(sudo grep -Po 'listen = (.+)' "$DEFAULT_CONF" | cut -d '=' -f2 | sed -e 's/^[ \t]*//')
assertTrue "Failed to extract socket information from '$DEFAULT_CONF'" "[ -n $DEFAULT_SOCKET ]" assertTrue "Failed to extract socket information from '$DEFAULT_CONF'" "[ -n $DEFAULT_SOCKET ]"
local RESULT_DIR
RESULT_DIR=$(dirname "$DEFAULT_SOCKET") RESULT_DIR=$(dirname "$DEFAULT_SOCKET")
assertTrue "Directory '$RESULT_DIR' does not exist" "[ -d $RESULT_DIR ]" assertTrue "Directory '$RESULT_DIR' does not exist" "[ -d $RESULT_DIR ]"
if [[ -d "$RESULT_DIR" ]]; then if [[ -d "$RESULT_DIR" ]]; then
@ -245,15 +253,17 @@ function getRunPHPDirectory() {
} }
copyPool() { copyPool() {
ORIGINAL_FILE=$1 local ORIGINAL_FILE=$1
POOL_NAME=$2 local POOL_NAME=$2
POOL_SOCKET=$3 local POOL_SOCKET=$3
POOL_TYPE=$4 local POOL_TYPE=$4
local POOL_DIR
local PHP_VERSION
POOL_DIR=$(dirname "${ORIGINAL_FILE}") POOL_DIR=$(dirname "${ORIGINAL_FILE}")
PHP_VERSION=$(getPHPVersion "$POOL_DIR") PHP_VERSION=$(getPHPVersion "$POOL_DIR")
assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION" assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION"
NEW_POOL_FILE="$POOL_DIR/${POOL_NAME}.conf" local NEW_POOL_FILE="$POOL_DIR/${POOL_NAME}.conf"
sudo cp "$ORIGINAL_FILE" "$NEW_POOL_FILE" sudo cp "$ORIGINAL_FILE" "$NEW_POOL_FILE"
#Add status path #Add status path
@ -271,17 +281,18 @@ copyPool() {
} }
getPHPServiceName() { getPHPServiceName() {
PHP_VERSION=$1 local PHP_VERSION=$1
if [[ -z "$LIST_OF_SERVICES" ]]; then if [[ -z "$LIST_OF_SERVICES" ]]; then
LIST_OF_SERVICES=$(sudo service --status-all 2>/dev/null | sort) LIST_OF_SERVICES=$(sudo service --status-all 2>/dev/null | sort)
fi fi
LIST_OF_NAMES=( local LIST_OF_NAMES=(
"php${PHP_VERSION}-fpm" "php${PHP_VERSION}-fpm"
"php-fpm" "php-fpm"
) )
for SERVICE_NAME in "${LIST_OF_NAMES[@]}"; do for SERVICE_NAME in "${LIST_OF_NAMES[@]}"; do
local RESULT
RESULT=$(echo "$LIST_OF_SERVICES" | grep -F "$SERVICE_NAME") RESULT=$(echo "$LIST_OF_SERVICES" | grep -F "$SERVICE_NAME")
if [[ -n "$RESULT" ]]; then if [[ -n "$RESULT" ]]; then
echo "$SERVICE_NAME" echo "$SERVICE_NAME"
@ -292,16 +303,26 @@ getPHPServiceName() {
} }
setupPool() { setupPool() {
POOL_FILE=$1 local POOL_FILE=$1
local POOL_DIR
local PHP_VERSION
local POOL_NAME
local POOL_SOCKET
local PHP_SERIAL_ID
local START_PORT
local PORT_IS_BUSY
POOL_DIR=$(dirname "${POOL_FILE}") POOL_DIR=$(dirname "${POOL_FILE}")
PHP_VERSION=$(getPHPVersion "$POOL_DIR") PHP_VERSION=$(getPHPVersion "$POOL_DIR")
assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION" assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION"
local PHP_RUN_DIR
PHP_RUN_DIR=$(getRunPHPDirectory) PHP_RUN_DIR=$(getRunPHPDirectory)
EXIT_CODE=$? local EXIT_CODE=$?
assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE" assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE"
assertTrue "PHP run directory '$PHP_RUN_DIR' is not a directory" "[ -d $PHP_RUN_DIR ]" assertTrue "PHP run directory '$PHP_RUN_DIR' is not a directory" "[ -d $PHP_RUN_DIR ]"
local PHP_DIR
PHP_DIR=$(getEtcPHPDirectory) PHP_DIR=$(getEtcPHPDirectory)
EXIT_CODE=$? EXIT_CODE=$?
assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE"
@ -356,6 +377,7 @@ setupPool() {
sudo ls -l "$POOL_DIR" sudo ls -l "$POOL_DIR"
travis_fold_end travis_fold_end
local SERVICE_NAME
SERVICE_NAME=$(getPHPServiceName "$PHP_VERSION") SERVICE_NAME=$(getPHPServiceName "$PHP_VERSION")
assertNotNull "Failed to detect service name for PHP${PHP_VERSION}" "$SERVICE_NAME" assertNotNull "Failed to detect service name for PHP${PHP_VERSION}" "$SERVICE_NAME"
printAction "Restarting service $SERVICE_NAME..." printAction "Restarting service $SERVICE_NAME..."
@ -363,6 +385,7 @@ setupPool() {
sleep 3 sleep 3
travis_fold_start "running_PHP$PHP_VERSION" "ⓘ List of running PHP$PHP_VERSION pools" travis_fold_start "running_PHP$PHP_VERSION" "ⓘ List of running PHP$PHP_VERSION pools"
local E_SYSTEM_CONTROL
E_SYSTEM_CONTROL=$(type -P systemctl) E_SYSTEM_CONTROL=$(type -P systemctl)
if [[ -x "$E_SYSTEM_CONTROL" ]]; then if [[ -x "$E_SYSTEM_CONTROL" ]]; then
sudo systemctl -l status "$SERVICE_NAME.service" sudo systemctl -l status "$SERVICE_NAME.service"
@ -374,14 +397,17 @@ setupPool() {
} }
setupPools() { setupPools() {
local PHP_DIR
PHP_DIR=$(getEtcPHPDirectory) PHP_DIR=$(getEtcPHPDirectory)
EXIT_CODE=$? local EXIT_CODE=$?
assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE"
assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]"
local PHP_LIST
PHP_LIST=$(sudo find "$PHP_DIR" -name 'www.conf' -type f) PHP_LIST=$(sudo find "$PHP_DIR" -name 'www.conf' -type f)
#Call to detect and cache PHP run directory, we need to call it before we stop all PHP-FPM #Call to detect and cache PHP run directory, we need to call it before we stop all PHP-FPM
local PHP_RUN_DIR
PHP_RUN_DIR=$(getRunPHPDirectory) PHP_RUN_DIR=$(getRunPHPDirectory)
EXIT_CODE=$? EXIT_CODE=$?
assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE" assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE"
@ -390,6 +416,10 @@ setupPools() {
#First we need to stop all PHP-FPM #First we need to stop all PHP-FPM
while IFS= read -r pool; do while IFS= read -r pool; do
if [[ -n $pool ]]; then if [[ -n $pool ]]; then
local POOL_DIR
local PHP_VERSION
local SERVICE_NAME
POOL_DIR=$(dirname "$pool") POOL_DIR=$(dirname "$pool")
PHP_VERSION=$(getPHPVersion "$POOL_DIR") PHP_VERSION=$(getPHPVersion "$POOL_DIR")
assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION" assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION"
@ -409,29 +439,36 @@ setupPools() {
} }
getNumberOfPHPVersions() { getNumberOfPHPVersions() {
local PHP_DIR
PHP_DIR=$(getEtcPHPDirectory) PHP_DIR=$(getEtcPHPDirectory)
EXIT_CODE=$? local EXIT_CODE=$?
assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE"
assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]"
local PHP_COUNT
PHP_COUNT=$(sudo find "$PHP_DIR" -name 'www.conf' -type f | wc -l) PHP_COUNT=$(sudo find "$PHP_DIR" -name 'www.conf' -type f | wc -l)
echo "$PHP_COUNT" echo "$PHP_COUNT"
} }
function startOndemandPoolsCache() { function startOndemandPoolsCache() {
local PHP_DIR
PHP_DIR=$(getEtcPHPDirectory) PHP_DIR=$(getEtcPHPDirectory)
EXIT_CODE=$? local EXIT_CODE=$?
assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE"
assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]"
local PHP_RUN_DIR
PHP_RUN_DIR=$(getRunPHPDirectory) PHP_RUN_DIR=$(getRunPHPDirectory)
EXIT_CODE=$? EXIT_CODE=$?
assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE" assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE"
assertTrue "PHP run directory '$PHP_RUN_DIR' is not a directory" "[ -d $PHP_RUN_DIR ]" assertTrue "PHP run directory '$PHP_RUN_DIR' is not a directory" "[ -d $PHP_RUN_DIR ]"
# We must start all the pools # We must start all the pools
POOL_URL="/php-fpm-status" local POOL_URL="/php-fpm-status"
local PHP_LIST
local POOL_DIR
local PHP_VERSION
PHP_LIST=$(sudo find "$PHP_DIR" -name 'www.conf' -type f) PHP_LIST=$(sudo find "$PHP_DIR" -name 'www.conf' -type f)
while IFS= read -r pool; do while IFS= read -r pool; do
if [[ -n $pool ]]; then if [[ -n $pool ]]; then
@ -440,8 +477,8 @@ function startOndemandPoolsCache() {
assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION" assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION"
for ((c = 1; c <= MAX_POOLS; c++)); do for ((c = 1; c <= MAX_POOLS; c++)); do
POOL_NAME="ondemand$c" local POOL_NAME="ondemand$c"
POOL_SOCKET="${PHP_RUN_DIR}php${PHP_VERSION}-fpm-${POOL_NAME}.sock" local POOL_SOCKET="${PHP_RUN_DIR}php${PHP_VERSION}-fpm-${POOL_NAME}.sock"
PHP_STATUS=$( PHP_STATUS=$(
SCRIPT_NAME=$POOL_URL \ SCRIPT_NAME=$POOL_URL \
@ -457,27 +494,33 @@ function startOndemandPoolsCache() {
} }
getAnySocket() { getAnySocket() {
local PHP_DIR
PHP_DIR=$(getEtcPHPDirectory) PHP_DIR=$(getEtcPHPDirectory)
EXIT_CODE=$? local EXIT_CODE=$?
assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE"
assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]"
local PHP_RUN_DIR
PHP_RUN_DIR=$(getRunPHPDirectory) PHP_RUN_DIR=$(getRunPHPDirectory)
EXIT_CODE=$? EXIT_CODE=$?
assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE" assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE"
assertTrue "PHP run directory '$PHP_RUN_DIR' is not a directory" "[ -d $PHP_RUN_DIR ]" assertTrue "PHP run directory '$PHP_RUN_DIR' is not a directory" "[ -d $PHP_RUN_DIR ]"
#Get any socket of PHP-FPM: #Get any socket of PHP-FPM:
local PHP_FIRST
PHP_FIRST=$(sudo find "$PHP_DIR" -name 'www.conf' -type f | sort | head -n1) PHP_FIRST=$(sudo find "$PHP_DIR" -name 'www.conf' -type f | sort | head -n1)
assertNotNull "Failed to get PHP conf" "$PHP_FIRST" assertNotNull "Failed to get PHP conf" "$PHP_FIRST"
local PHP_VERSION
PHP_VERSION=$(getPHPVersion "$PHP_FIRST") PHP_VERSION=$(getPHPVersion "$PHP_FIRST")
assertNotNull "Failed to detect PHP version from string '$PHP_FIRST'" "$PHP_VERSION" assertNotNull "Failed to detect PHP version from string '$PHP_FIRST'" "$PHP_VERSION"
local PHP_POOL
PHP_POOL=$(sudo find "$PHP_RUN_DIR" -name "php${PHP_VERSION}*.sock" -type s 2>/dev/null | sort | head -n1) PHP_POOL=$(sudo find "$PHP_RUN_DIR" -name "php${PHP_VERSION}*.sock" -type s 2>/dev/null | sort | head -n1)
assertNotNull "Failed to get PHP${PHP_VERSION} socket" "$PHP_POOL" assertNotNull "Failed to get PHP${PHP_VERSION} socket" "$PHP_POOL"
echo "$PHP_POOL" echo "$PHP_POOL"
} }
getAnyPort() { getAnyPort() {
local PHP_PORT
PHP_PORT=$(sudo netstat -tulpn | grep -F "LISTEN" | grep -F "php-fpm" | head -n1 | awk '{print $4}' | rev | cut -d: -f1 | rev) PHP_PORT=$(sudo netstat -tulpn | grep -F "LISTEN" | grep -F "php-fpm" | head -n1 | awk '{print $4}' | rev | cut -d: -f1 | rev)
assertNotNull "Failed to get PHP port" "$PHP_PORT" assertNotNull "Failed to get PHP port" "$PHP_PORT"
echo "$PHP_PORT" echo "$PHP_PORT"
@ -560,12 +603,16 @@ tearDown() {
} }
testZabbixGetInstalled() { testZabbixGetInstalled() {
local ZABBIX_GET
ZABBIX_GET=$(type -P zabbix_get) ZABBIX_GET=$(type -P zabbix_get)
assertNotNull "Utility zabbix-get not installed" "$ZABBIX_GET" assertNotNull "Utility zabbix-get not installed" "$ZABBIX_GET"
printSuccess "${FUNCNAME[0]}" printSuccess "${FUNCNAME[0]}"
} }
testZabbixAgentVersion() { testZabbixAgentVersion() {
local REQUESTED_VERSION
local INSTALLED_VERSION
#Example: 4.4 #Example: 4.4
REQUESTED_VERSION=$(echo "$TRAVIS_JOB_NAME" | grep -i -F "zabbix" | head -n1 | cut -d "@" -f1 | cut -d " " -f2) REQUESTED_VERSION=$(echo "$TRAVIS_JOB_NAME" | grep -i -F "zabbix" | head -n1 | cut -d "@" -f1 | cut -d " " -f2)
INSTALLED_VERSION=$(zabbix_agentd -V | grep -F "zabbix" | head -n1 | rev | cut -d " " -f1 | rev | cut -d "." -f1,2) INSTALLED_VERSION=$(zabbix_agentd -V | grep -F "zabbix" | head -n1 | rev | cut -d " " -f1 | rev | cut -d "." -f1,2)
@ -574,6 +621,9 @@ testZabbixAgentVersion() {
} }
testZabbixGetVersion() { testZabbixGetVersion() {
local REQUESTED_VERSION
local INSTALLED_VERSION
#Example: 4.4 #Example: 4.4
REQUESTED_VERSION=$(echo "$TRAVIS_JOB_NAME" | grep -i -F "zabbix" | head -n1 | cut -d "@" -f1 | cut -d " " -f2) REQUESTED_VERSION=$(echo "$TRAVIS_JOB_NAME" | grep -i -F "zabbix" | head -n1 | cut -d "@" -f1 | cut -d " " -f2)
INSTALLED_VERSION=$(zabbix_get -V | grep -F "zabbix" | head -n1 | rev | cut -d " " -f1 | rev | cut -d "." -f1,2) INSTALLED_VERSION=$(zabbix_get -V | grep -F "zabbix" | head -n1 | rev | cut -d " " -f1 | rev | cut -d "." -f1,2)
@ -582,6 +632,9 @@ testZabbixGetVersion() {
} }
testNonRootUserPrivilegesDiscovery() { testNonRootUserPrivilegesDiscovery() {
local DATA
local IS_OK
#Run the script under non root user #Run the script under non root user
DATA=$(sudo -u zabbix "/etc/zabbix/zabbix_php_fpm_discovery.sh") DATA=$(sudo -u zabbix "/etc/zabbix/zabbix_php_fpm_discovery.sh")
IS_OK=$(echo "$DATA" | grep -F 'Insufficient privileges') IS_OK=$(echo "$DATA" | grep -F 'Insufficient privileges')
@ -591,6 +644,9 @@ testNonRootUserPrivilegesDiscovery() {
} }
testNonRootUserPrivilegesStatus() { testNonRootUserPrivilegesStatus() {
local DATA
local IS_OK
#Run the script under non root user #Run the script under non root user
assertNotNull "Test socket is not defined" "$TEST_SOCKET" assertNotNull "Test socket is not defined" "$TEST_SOCKET"
DATA=$(sudo -u zabbix "/etc/zabbix/zabbix_php_fpm_status.sh" "$TEST_SOCKET" "/php-fpm-status") DATA=$(sudo -u zabbix "/etc/zabbix/zabbix_php_fpm_status.sh" "$TEST_SOCKET" "/php-fpm-status")
@ -601,12 +657,17 @@ testNonRootUserPrivilegesStatus() {
} }
testPHPIsRunning() { testPHPIsRunning() {
local IS_OK
IS_OK=$(sudo ps ax | grep -F "php-fpm: pool " | grep -F -v "grep" | head -n1) IS_OK=$(sudo ps ax | grep -F "php-fpm: pool " | grep -F -v "grep" | head -n1)
assertNotNull "No running PHP-FPM instances found" "$IS_OK" assertNotNull "No running PHP-FPM instances found" "$IS_OK"
printSuccess "${FUNCNAME[0]}" printSuccess "${FUNCNAME[0]}"
} }
testStatusScriptSocket() { testStatusScriptSocket() {
local DATA
local IS_OK
assertNotNull "Test socket is not defined" "$TEST_SOCKET" assertNotNull "Test socket is not defined" "$TEST_SOCKET"
DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_status.sh" "$TEST_SOCKET" "/php-fpm-status") DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_status.sh" "$TEST_SOCKET" "/php-fpm-status")
IS_OK=$(echo "$DATA" | grep -F '{"pool":"') IS_OK=$(echo "$DATA" | grep -F '{"pool":"')
@ -618,8 +679,12 @@ testStatusScriptSocket() {
} }
testStatusScriptPort() { testStatusScriptPort() {
local DATA
local IS_OK
local PHP_PORT
PHP_PORT=$(getAnyPort) PHP_PORT=$(getAnyPort)
PHP_POOL="127.0.0.1:$PHP_PORT" local PHP_POOL="127.0.0.1:$PHP_PORT"
#Make the test: #Make the test:
DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_status.sh" "$PHP_POOL" "/php-fpm-status") DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_status.sh" "$PHP_POOL" "/php-fpm-status")
@ -632,6 +697,9 @@ testStatusScriptPort() {
} }
testZabbixStatusSocket() { testZabbixStatusSocket() {
local DATA
local IS_OK
DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.status["$TEST_SOCKET","/php-fpm-status"]) DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.status["$TEST_SOCKET","/php-fpm-status"])
IS_OK=$(echo "$DATA" | grep -F '{"pool":"') IS_OK=$(echo "$DATA" | grep -F '{"pool":"')
printElapsedTime printElapsedTime
@ -642,8 +710,12 @@ testZabbixStatusSocket() {
} }
testZabbixStatusPort() { testZabbixStatusPort() {
local DATA
local IS_OK
local PHP_PORT
PHP_PORT=$(getAnyPort) PHP_PORT=$(getAnyPort)
PHP_POOL="127.0.0.1:$PHP_PORT" local PHP_POOL="127.0.0.1:$PHP_PORT"
DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.status["$PHP_POOL","/php-fpm-status"]) DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.status["$PHP_POOL","/php-fpm-status"])
IS_OK=$(echo "$DATA" | grep -F '{"pool":"') IS_OK=$(echo "$DATA" | grep -F '{"pool":"')
@ -655,6 +727,9 @@ testZabbixStatusPort() {
} }
testDiscoverScriptReturnsData() { testDiscoverScriptReturnsData() {
local DATA
local IS_OK
DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "/php-fpm-status") DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "/php-fpm-status")
IS_OK=$(echo "$DATA" | grep -F '{"data":[{"{#POOLNAME}"') IS_OK=$(echo "$DATA" | grep -F '{"data":[{"{#POOLNAME}"')
printElapsedTime printElapsedTime
@ -670,6 +745,7 @@ testDiscoverScriptDebug() {
NUMBER_OF_ERRORS=$(echo "$DATA" | grep -o -F 'Error:' | wc -l) NUMBER_OF_ERRORS=$(echo "$DATA" | grep -o -F 'Error:' | wc -l)
if [[ $NUMBER_OF_ERRORS -gt 0 ]]; then if [[ $NUMBER_OF_ERRORS -gt 0 ]]; then
local ERRORS_LIST
ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:') ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:')
printYellow "Errors list:" printYellow "Errors list:"
printYellow "$ERRORS_LIST" printYellow "$ERRORS_LIST"
@ -684,10 +760,14 @@ testDiscoverScriptDebug() {
} }
testDiscoverScriptTimeout() { testDiscoverScriptTimeout() {
local DATA
local NUMBER_OF_ERRORS
local PHP_COUNT
DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "nosleep" "max_tasks" "1" "/php-fpm-status") 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) NUMBER_OF_ERRORS=$(echo "$DATA" | grep -o -F 'Error:' | wc -l)
PHP_COUNT=$(getNumberOfPHPVersions) PHP_COUNT=$(getNumberOfPHPVersions)
if [[ $PHP_COUNT != "$NUMBER_OF_ERRORS" ]]; then if [[ $PHP_COUNT != "$NUMBER_OF_ERRORS" ]]; then
local ERRORS_LIST
ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:') ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:')
printYellow "Errors list:" printYellow "Errors list:"
printYellow "$ERRORS_LIST" printYellow "$ERRORS_LIST"
@ -700,16 +780,26 @@ testDiscoverScriptTimeout() {
printSuccess "${FUNCNAME[0]}" printSuccess "${FUNCNAME[0]}"
} }
testZabbixDiscoverReturnsData() { function runZabbixDiscoverReturnsData() {
local DATA
local IS_OK
DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"])
IS_OK=$(echo "$DATA" | grep -F '{"data":[{"{#POOLNAME}"') IS_OK=$(echo "$DATA" | grep -F '{"data":[{"{#POOLNAME}"')
printElapsedTime printElapsedTime
assertNotNull "Discover script failed: $DATA" "$IS_OK" assertNotNull "Discover script failed: $DATA" "$IS_OK"
assertExecutionTime assertExecutionTime
}
testZabbixDiscoverReturnsData() {
runZabbixDiscoverReturnsData
printSuccess "${FUNCNAME[0]}" printSuccess "${FUNCNAME[0]}"
} }
testDiscoverScriptSleep() { testDiscoverScriptSleep() {
local DATA
local CHECK_OK_COUNT
local STOP_OK_COUNT
DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status") DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status")
CHECK_OK_COUNT=$(echo "$DATA" | grep -o -F "execution time OK" | wc -l) CHECK_OK_COUNT=$(echo "$DATA" | grep -o -F "execution time OK" | wc -l)
STOP_OK_COUNT=$(echo "$DATA" | grep -o -F "stop required" | wc -l) STOP_OK_COUNT=$(echo "$DATA" | grep -o -F "stop required" | wc -l)
@ -739,6 +829,10 @@ testZabbixDiscoverSleep() {
} }
testDiscoverScriptRunDuration() { testDiscoverScriptRunDuration() {
local DATA
local CHECK_OK_COUNT
local STOP_OK_COUNT
DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status") DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status")
CHECK_OK_COUNT=$(echo "$DATA" | grep -o -F "execution time OK" | wc -l) CHECK_OK_COUNT=$(echo "$DATA" | grep -o -F "execution time OK" | wc -l)
STOP_OK_COUNT=$(echo "$DATA" | grep -o -F "stop required" | wc -l) STOP_OK_COUNT=$(echo "$DATA" | grep -o -F "stop required" | wc -l)
@ -751,7 +845,19 @@ testDiscoverScriptRunDuration() {
printSuccess "${FUNCNAME[0]}" printSuccess "${FUNCNAME[0]}"
} }
testZabbixDiscoverRunDuration() {
#Add sleep
AddSleepToConfig
StartTimer
runZabbixDiscoverReturnsData
printSuccess "${FUNCNAME[0]}"
}
testDiscoverScriptDoubleRun() { testDiscoverScriptDoubleRun() {
local DATA_FIRST
local DATA_SECOND
DATA_FIRST=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status") DATA_FIRST=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status")
DATA_SECOND=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status") DATA_SECOND=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status")
@ -761,6 +867,9 @@ testDiscoverScriptDoubleRun() {
} }
testZabbixDiscoverDoubleRun() { testZabbixDiscoverDoubleRun() {
local DATA_FIRST
local DATA_SECOND
#Add sleep #Add sleep
AddSleepToConfig AddSleepToConfig
@ -773,8 +882,9 @@ testZabbixDiscoverDoubleRun() {
} }
function discoverAllZabbix() { function discoverAllZabbix() {
DATA_OLD=$1 local DATA_OLD=$1
DATA_COUNT=$2 local DATA_COUNT=$2
local DATA
if [[ -z $DATA_COUNT ]]; then if [[ -z $DATA_COUNT ]]; then
DATA_COUNT=0 DATA_COUNT=0
@ -785,6 +895,7 @@ function discoverAllZabbix() {
echo "$DATA" echo "$DATA"
return 0 return 0
else else
local DATA_COUNT
DATA_COUNT=$(echo "$DATA_COUNT + 1" | bc) DATA_COUNT=$(echo "$DATA_COUNT + 1" | bc)
if [[ $DATA_COUNT -gt $MAX_CHECKS ]]; then if [[ $DATA_COUNT -gt $MAX_CHECKS ]]; then
printYellow "Data old:" printYellow "Data old:"
@ -800,17 +911,21 @@ function discoverAllZabbix() {
} }
checkNumberOfPools() { checkNumberOfPools() {
POOL_TYPE=$1 local POOL_TYPE=$1
CHECK_COUNT=$2 local CHECK_COUNT=$2
local DATA
DATA=$(discoverAllZabbix) DATA=$(discoverAllZabbix)
STATUS=$? local STATUS=$?
if [[ $STATUS -ne 0 ]]; then if [[ $STATUS -ne 0 ]]; then
echo "$DATA" echo "$DATA"
return 1 return 1
fi fi
assertEquals "Failed to discover all data when checking pools '$POOL_TYPE'" "0" "$STATUS" assertEquals "Failed to discover all data when checking pools '$POOL_TYPE'" "0" "$STATUS"
local NUMBER_OF_POOLS
local PHP_COUNT
local POOLS_BY_DESIGN
NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F "{\"{#POOLNAME}\":\"$POOL_TYPE" | wc -l) NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F "{\"{#POOLNAME}\":\"$POOL_TYPE" | wc -l)
PHP_COUNT=$(getNumberOfPHPVersions) PHP_COUNT=$(getNumberOfPHPVersions)
if [[ -n "$CHECK_COUNT" ]] && [[ "$CHECK_COUNT" -ge 0 ]]; then if [[ -n "$CHECK_COUNT" ]] && [[ "$CHECK_COUNT" -ge 0 ]]; then
@ -869,15 +984,18 @@ testZabbixDiscoverNumberOfOndemandPoolsCache() {
startOndemandPoolsCache startOndemandPoolsCache
printAction "Empty cache test..." printAction "Empty cache test..."
local INITIAL_DATA
INITIAL_DATA=$(checkNumberOfPools "ondemand") INITIAL_DATA=$(checkNumberOfPools "ondemand")
travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response"
echo "$INITIAL_DATA" echo "$INITIAL_DATA"
travis_fold_end travis_fold_end
local WAIT_TIMEOUT
WAIT_TIMEOUT=$(echo "$ONDEMAND_TIMEOUT * 2" | bc) WAIT_TIMEOUT=$(echo "$ONDEMAND_TIMEOUT * 2" | bc)
sleep "$WAIT_TIMEOUT" sleep "$WAIT_TIMEOUT"
printAction "Full cache test..." printAction "Full cache test..."
local CACHED_DATA
CACHED_DATA=$(checkNumberOfPools "ondemand") CACHED_DATA=$(checkNumberOfPools "ondemand")
travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response"
echo "$CACHED_DATA" echo "$CACHED_DATA"
@ -889,6 +1007,7 @@ testZabbixDiscoverNumberOfOndemandPoolsCache() {
} }
testZabbixDiscoverNumberOfIPPools() { testZabbixDiscoverNumberOfIPPools() {
local PHP_COUNT
PHP_COUNT=$(getNumberOfPHPVersions) PHP_COUNT=$(getNumberOfPHPVersions)
local DATA local DATA
DATA=$(checkNumberOfPools "localhost" "$PHP_COUNT") DATA=$(checkNumberOfPools "localhost" "$PHP_COUNT")
@ -926,7 +1045,7 @@ testZabbixDiscoverManyPools() {
} }
testDiscoverScriptManyPoolsRunDuration() { testDiscoverScriptManyPoolsRunDuration() {
MAX_RUNS=5 local MAX_RUNS=5
for ((c = 1; c <= MAX_RUNS; c++)); do for ((c = 1; c <= MAX_RUNS; c++)); do
StartTimer StartTimer
printAction "Run #$c..." printAction "Run #$c..."
@ -936,7 +1055,7 @@ testDiscoverScriptManyPoolsRunDuration() {
} }
testZabbixDiscoverManyPoolsRunDuration() { testZabbixDiscoverManyPoolsRunDuration() {
MAX_RUNS=5 local MAX_RUNS=5
for ((c = 1; c <= MAX_RUNS; c++)); do for ((c = 1; c <= MAX_RUNS; c++)); do
StartTimer StartTimer
printAction "Run #$c..." printAction "Run #$c..."

View File

@ -90,6 +90,9 @@ oneTimeSetUp() {
} }
testMissingPackagesDiscoveryScript() { testMissingPackagesDiscoveryScript() {
local DATA
local IS_OK
DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_discovery.sh" "/php-fpm-status") DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_discovery.sh" "/php-fpm-status")
IS_OK=$(echo "$DATA" | grep -F ' not found.') IS_OK=$(echo "$DATA" | grep -F ' not found.')
assertNotNull "Discovery script didn't report error on missing utilities $DATA" "$IS_OK" assertNotNull "Discovery script didn't report error on missing utilities $DATA" "$IS_OK"
@ -97,6 +100,9 @@ testMissingPackagesDiscoveryScript() {
} }
testMissingPackagesStatusScript() { testMissingPackagesStatusScript() {
local DATA
local IS_OK
DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_status.sh" "localhost:9000" "/php-fpm-status") DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_status.sh" "localhost:9000" "/php-fpm-status")
IS_OK=$(echo "$DATA" | grep -F ' not found.') IS_OK=$(echo "$DATA" | grep -F ' not found.')
assertNotNull "Status script didn't report error on missing utilities $DATA" "$IS_OK" assertNotNull "Status script didn't report error on missing utilities $DATA" "$IS_OK"