From 325857ef07822fa5e6a1330d96be55276d9e664a Mon Sep 17 00:00:00 2001
From: thiagoftsm <thiagoftsm@gmail.com>
Date: Sun, 29 Sep 2019 09:10:35 +0000
Subject: [PATCH] Common pattern for web and alarms together with two bug fixes
 (#6783)

* script_pattern: Fix script and bring pattern

This commit fixes the error given by shellckeck on stress.sh and brings a pattern for the other scripts, no
less important its given the possibility to change the url from outside of the scripts

* script_pattern: shellcheke

Correct the request.sh after to change it to avoid warning errors from shellchecker

* script_pattern: Remove of garbage value

With this PR I am removing the garbage value of a script

* script_pattern: Restore color

These 3 scripts changed in this commit was not restoring the terminal color,
this commit fixes this
---
 tests/acls/acl.sh.in               | 37 ++++++++------
 tests/alarm_repetition/alarm.sh.in | 23 +++++----
 tests/stress.sh                    | 10 ++--
 tests/urls/request.sh.in           | 78 ++++++++++++++++--------------
 4 files changed, 82 insertions(+), 66 deletions(-)

diff --git a/tests/acls/acl.sh.in b/tests/acls/acl.sh.in
index 772d664088..9ac404c075 100644
--- a/tests/acls/acl.sh.in
+++ b/tests/acls/acl.sh.in
@@ -8,6 +8,7 @@ NETDATA_VARLIB_DIR="/var/lib/netdata"
 RED='\033[0;31m'
 GREEN='\033[0;32m'
 YELLOW='\033[0;43m'
+NOCOLOR='\033[0m'
 
 #change the previous acl file and with a new 
 #and store it on a new file
@@ -15,6 +16,8 @@ change_file(){
     sed "s/$1/$2/g" netdata.cfg > "$4"
 }
 
+NETDATAPID=""
+
 change_ssl_file(){
     KEYROW="ssl key = $3/key.pem"
     CERTROW="ssl certificate = $3/cert.pem"
@@ -35,24 +38,24 @@ run_acl_tests() {
 
     TOT=$(grep -c "HTTP/1.1 301" log_*.txt | cut -d: -f2| grep -c 1)
     if [ "$TOT" -ne "$4" ]; then
-        echo -e "${RED}I got a wrong number of redirects($TOT) when SSL is activated, It was expected $4"
+        echo -e "${RED}I got a wrong number of redirects($TOT) when SSL is activated, It was expected $4 ${NOCOLOR}"
         rm log_* netdata.conf.test* netdata.txt health.csv index.html badge.csv tmp* key.pem cert.pem info.txt
-        killall netdata
+        kill $NETDATAPID
         exit 1
     elif [ "$TOT" -eq "$4" ] && [ "$4" -ne "0" ]; then
-        echo -e "${YELLOW}I got the correct number of redirects($4) when SSL is activated and I try to access with HTTP."
+        echo -e "${YELLOW}I got the correct number of redirects($4) when SSL is activated and I try to access with HTTP. ${NOCOLOR}"
         return
     fi
 
     TOT=$(grep -c "HTTP/1.1 200 OK" log_* | cut -d: -f2| grep -c 1)
     if [ "$TOT" -ne "$3" ]; then
-        echo -e "${RED}I got a wrong number of \"200 OK\" from the queries, it was expected $3."
-        killall netdata
+        echo -e "${RED}I got a wrong number of \"200 OK\" from the queries, it was expected $3. ${NOCOLOR}"
+        kill $NETDATAPID
         rm log_* netdata.conf.test* netdata.txt health.csv index.html badge.csv tmp* key.pem cert.pem info.txt
         exit 1
     fi
 
-    echo -e "${GREEN}ACLs were applied correctly"
+    echo -e "${GREEN}ACLs were applied correctly ${NOCOLOR}"
 }
 
 CONF=$(grep "bind" netdata.cfg)
@@ -69,12 +72,14 @@ else
 fi
 
 change_file "$CONF" "    bind to = *" "$CWD" "netdata.conf.test0"
-netdata -c "netdata.conf.test0" 
+netdata -c "netdata.conf.test0" -D &
+NETDATAPID=$!
 run_acl_tests $TOKEN "$BASICURL:19999" 5 0
-killall netdata
+kill $NETDATAPID
 
 change_ssl_file "$CONF" "    bind to = *=dashboard|registry|badges|management|netdata.conf *:20000=dashboard|registry|badges|management *:20001=dashboard|registry|netdata.conf^SSL=optional *:20002=dashboard|registry" "$CWD" "netdata.conf.test1"
-netdata -c "netdata.conf.test1"
+netdata -c "netdata.conf.test1" -D &
+NETDATAPID=$!
 run_acl_tests $TOKEN "$BASICURL:19999" 5 5
 run_acl_tests $TOKEN "$BASICURLS:19999" 5 0
 
@@ -86,10 +91,11 @@ run_acl_tests $TOKEN "$BASICURLS:20001" 4 0
 
 run_acl_tests $TOKEN "$BASICURL:20002" 3 5 
 run_acl_tests $TOKEN "$BASICURLS:20002" 3 0 
-killall netdata
+kill $NETDATAPID
 
 change_ssl_file "$CONF" "    bind to = *=dashboard|registry|badges|management|netdata.conf *:20000=dashboard|registry|badges|management *:20001=dashboard|registry|netdata.conf^SSL=force *:20002=dashboard|registry" "$CWD" "netdata.conf.test2"
-netdata -c "netdata.conf.test2"
+netdata -c "netdata.conf.test2" -D &
+NETDATAPID=$!
 run_acl_tests $TOKEN "$BASICURL:19999" 5 5
 run_acl_tests $TOKEN "$BASICURLS:19999" 5 0
 
@@ -101,10 +107,11 @@ run_acl_tests $TOKEN "$BASICURLS:20001" 4 0
 
 run_acl_tests $TOKEN "$BASICURL:20002" 3 5 
 run_acl_tests $TOKEN "$BASICURLS:20002" 3 0 
-killall netdata
+kill $NETDATAPID
 
 change_ssl_file "$CONF" "    bind to = *=dashboard|registry|badges|management|netdata.conf *:20000=dashboard|registry|badges|management^SSL=optional *:20001=dashboard|registry|netdata.conf^SSL=force" "$CWD" "netdata.conf.test3"
-netdata -c "netdata.conf.test3"
+netdata -c "netdata.conf.test3" -D &
+NETDATAPID=$!
 run_acl_tests $TOKEN "$BASICURL:19999" 5 5
 run_acl_tests $TOKEN "$BASICURLS:19999" 5 0
 
@@ -113,7 +120,7 @@ run_acl_tests $TOKEN "$BASICURLS:20000" 4 0
 
 run_acl_tests $TOKEN "$BASICURL:20001" 4 5
 run_acl_tests $TOKEN "$BASICURLS:20001" 4 0
-killall netdata
+kill $NETDATAPID
 
 rm log_* netdata.conf.test* netdata.txt health.csv index.html badge.csv tmp* key.pem cert.pem info.txt
-echo "All the tests were successful"
+echo "All the tests were successful ${NOCOLOR}"
diff --git a/tests/alarm_repetition/alarm.sh.in b/tests/alarm_repetition/alarm.sh.in
index 8555e0a3c0..09d6aaf463 100644
--- a/tests/alarm_repetition/alarm.sh.in
+++ b/tests/alarm_repetition/alarm.sh.in
@@ -7,7 +7,8 @@ HEALTHDIR="@configdir_POST@/health.d/"
 OUTDIR="workdir/"
 
 #url to do download
-MURL="http://localhost:19999/api/v1/alarms?active"
+QUERY="/api/v1/alarms?active"
+MURL="http://localhost:19999$QUERY"
 
 #error messages
 RED='\033[0;31m'
@@ -35,24 +36,26 @@ netdata_test_download() {
   grep "HTTP/1.1 200 OK" "$1" 2>/dev/null 1>/dev/null
   TEST="$?"
   if [ "$TEST" -ne "0" ]; then
-    echo -e "${RED} Error to get the alarms"
-    killall netdata
+    echo -e "${RED} Error to get the alarms. ${NOCOLOR}"
+    kill "$5"
+    rm "$HEALTHDIR/ram.conf"
     exit 1
   fi
 
   COUNT=$(grep -w "\"last_repeat\":" "$2" | grep -c "$OPT" "\"0\"")
   if [ "$COUNT" -eq "0" ]; then
-    echo -e "${RED} Netdata gave an unexpected result when alarm repetition is $4"
-    killall netdata
+    echo -e "${RED} Netdata gave an unexpected result when alarm repetition is $4 ${NOCOLOR}"
+    killall "$5"
+    rm "$HEALTHDIR/ram.conf"
     exit 1
   fi
 
-  echo -e "${GREEN} I got the expected result"
+  echo -e "${GREEN} I got the expected result ${NOCOLOR}"
 }
 
 get_the_logs() {
   curl -v -k --create-dirs -o "$OUTDIR/$1.out" "$MURL" 2> "$OUTDIR/$1.err"
-  netdata_test_download "$OUTDIR/$1.err" "$OUTDIR/$1.out" "$2" "$3"
+  netdata_test_download "$OUTDIR/$1.err" "$OUTDIR/$1.out" "$2" "$3" "$4"
 }
 
 process_data() {
@@ -61,14 +64,14 @@ process_data() {
   NETDATAPID=$!
   echo -e "${NOCOLOR}Sleeping during $SEC seconds to create alarm entries"
   sleep $SEC
-  get_the_logs "$1" "$2" "$3"
+  get_the_logs "$1" "$2" "$3" "$NETDATAPID"
   kill $NETDATAPID
 }
 
 mkdir "$OUTDIR"
 CREATEDIR="$?"
 if [ "$CREATEDIR" -ne "0" ]; then
-  echo  -e "${RED}Cannot create the output directory, it already exists. The test will overwrite previous results."
+  echo  -e "${RED}Cannot create the output directory, it already exists. The test will overwrite previous results. ${NOCOLOR}"
 fi
 
 change_alarm_file "./0" "ram_without_repetition.conf" "$HEALTHDIR/ram.conf"
@@ -81,6 +84,6 @@ cp -a netdata.conf_with_repetition netdata.conf
 process_data "ram_with" "I" "activated."
 rm netdata.conf
 
-echo  -e "${GREEN} all the tests were sucessful"
+echo  -e "${GREEN} all the tests were sucessful ${NOCOLOR}"
 rm "$HEALTHDIR/ram.conf"
 rm -rf $OUTDIR
diff --git a/tests/stress.sh b/tests/stress.sh
index cef5327776..97cced0325 100755
--- a/tests/stress.sh
+++ b/tests/stress.sh
@@ -8,7 +8,7 @@ then
 fi
 
 # set the host to connect to
-if [ ! -z "$1" ]
+if [ -n "$1" ]
 then
 	host="$1"
 else
@@ -17,17 +17,17 @@ fi
 echo "using netdata server at: $host"
 
 # shellcheck disable=SC2207 disable=SC1117
-charts=($(curl "$host/netdata.conf" 2>/dev/null | grep "^\[" | cut -d '[' -f 2 | cut -d ']' -f 1 | grep -v ^global$ | grep -v "^plugin" | sort -u))
+charts=($(curl -k "$host/netdata.conf" 2>/dev/null | grep "^\[" | cut -d '[' -f 2 | cut -d ']' -f 1 | grep -v ^global$ | grep -v "^plugin" | sort -u))
 if [ "${#charts[@]}" -eq 0 ]
 then
 	echo "Cannot download charts from server: $host"
 	exit 1
 fi
 
-update_every="$(curl "$host/netdata.conf" 2>/dev/null | grep "update every = " | head -n 1 | cut -d '=' -f 2)"
+update_every="$(curl -k "$host/netdata.conf" 2>/dev/null | grep "update every = " | head -n 1 | cut -d '=' -f 2)"
 [ $(( update_every + 1 - 1)) -eq 0 ] && update_every=1
 
-entries="$(curl "$host/netdata.conf" 2>/dev/null | grep "history = " | head -n 1 | cut -d '=' -f 2)"
+entries="$(curl -k "$host/netdata.conf" 2>/dev/null | grep "history = " | head -n 1 | cut -d '=' -f 2)"
 [ $(( entries + 1 - 1)) -eq 0 ] && entries=3600
 
 # to compare equal things, set the entries to 3600 max
@@ -55,7 +55,7 @@ trap cleanup EXIT
 
 while true
 do
-	echo "curl --compressed --keepalive-time 120 --header \"Connection: keep-alive\" \\" >"$file"
+	echo "curl -k --compressed --keepalive-time 120 --header \"Connection: keep-alive\" \\" >"$file"
 	# shellcheck disable=SC2034
 	for x in {1..100}
 	do
diff --git a/tests/urls/request.sh.in b/tests/urls/request.sh.in
index 6cbe773841..ebdfc094c2 100644
--- a/tests/urls/request.sh.in
+++ b/tests/urls/request.sh.in
@@ -18,6 +18,9 @@ OUTDIR="tests"
 OUTEDIR="encoded_tests"
 OUTOPTDIR="options"
 ERRDIR="etests"
+NOCOLOR='\033[0'
+RED='\033[0;31m'
+GREEN='\033[0;32m'
 
 ################################################################################################
 ####																						####
@@ -27,7 +30,7 @@ ERRDIR="etests"
 
 # Print error message and close script
 netdata_print_error(){
-    echo "Closing due error \"$1\" code \"$2\""
+    echo "${RED} Closing due error \"$1\" code \"$2\" ${NOCOLOR}"
     exit 1
 }
 
@@ -200,36 +203,40 @@ netdata_download_allmetrics() {
 }
 
 
-################################################################################################
+####################################################
 ####																						####
-####									MAIN ROUTINE										####
+####									MAIN ROUTINE              ####
 ####																						####
-################################################################################################
+####################################################
 MURL="http://127.0.0.1:19999"
 
+if [ -n "$1" ]; then
+    MURL="$1"
+fi
+
 netdata_create_directory $OUTDIR
 netdata_create_directory $OUTEDIR
 netdata_create_directory $OUTOPTDIR
 netdata_create_directory $ERRDIR
 
-wget --no-check-certificate --execute="robots = off" --mirror --convert-links --no-parent $MURL
+wget --no-check-certificate --execute="robots = off" --mirror --convert-links --no-parent "$MURL"
 TEST=$?
 if [ $TEST -ne "0" ] ; then
     echo "Cannot connect to Netdata"
     exit 1
 fi
 
-netdata_download_various $MURL "netdata.conf" "netdata.conf"
+netdata_download_various "$MURL" "netdata.conf" "netdata.conf"
 
-netdata_download_various_with_options $MURL "netdata.conf" "netdata.conf"
+netdata_download_various_with_options "$MURL" "netdata.conf" "netdata.conf"
 
-netdata_wrong_request_various $MURL "api/v15/info?this%20could%20not%20be%20here" "err_version"
+netdata_wrong_request_various "$MURL" "api/v15/info?this%20could%20not%20be%20here" "err_version"
 
-netdata_wrong_request_various $MURL "api/v1/\(*@&$\!$%%5E\)\!$*%&\)\!$*%%5E*\!%5E%\!%5E$%\!%5E%\(\!*%5E*%5E%\(*@&$%5E%\(\!%5E#*&\!^#$*&\!^%\)@\($%^\)\!*&^\(\!*&^#$&#$\)\!$%^\)\!$*%&\)#$\!^#*$^\!\(*#^#\)\!%^\!\)$*%&\!\(*&$\!^#$*&^\!*#^$\!*^\)%\(\!*&$%\)\(\!&#$\!^*#&$^\!*^%\)\!$%\)\!\(&#$\!^#*&^$" "err_version2"
+netdata_wrong_request_various "$MURL" "api/v1/\(*@&$\!$%%5E\)\!$*%&\)\!$*%%5E*\!%5E%\!%5E$%\!%5E%\(\!*%5E*%5E%\(*@&$%5E%\(\!%5E#*&\!^#$*&\!^%\)@\($%^\)\!*&^\(\!*&^#$&#$\)\!$%^\)\!$*%&\)#$\!^#*$^\!\(*#^#\)\!%^\!\)$*%&\!\(*&$\!^#$*&^\!*#^$\!*^\)%\(\!*&$%\)\(\!&#$\!^*#&$^\!*^%\)\!$%\)\!\(&#$\!^#*&^$" "err_version2"
 
-netdata_download_various $MURL "api/v1/info" "info"
-netdata_download_various_with_options $MURL "api/v1/info" "info"
-netdata_download_various $MURL "api/v1/info?this%20could%20not%20be%20here" "err_info"
+netdata_download_various "$MURL" "api/v1/info" "info"
+netdata_download_various_with_options "$MURL" "api/v1/info" "info"
+netdata_download_various "$MURL" "api/v1/info?this%20could%20not%20be%20here" "err_info"
 
 netdata_print_header "Getting all the netdata charts"
 CHARTS=$( netdata_download_charts "$MURL" "api/v1/charts" )
@@ -244,58 +251,57 @@ elif [ ${#CHARTS[@]} -ne ${#WCHARTS2[@]} ]; then
     exit 3;
 fi
 
-netdata_wrong_request_various $MURL "api/v1/chart" "err_chart_without_chart"
-netdata_wrong_request_various $MURL "api/v1/chart?_=234231424242" "err_chart_arg"
+netdata_wrong_request_various "$MURL" "api/v1/chart" "err_chart_without_chart"
+netdata_wrong_request_various "$MURL" "api/v1/chart?_=234231424242" "err_chart_arg"
 
-netdata_download_various $MURL "api/v1/chart?chart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args"
-netdata_download_various_with_options $MURL "api/v1/chart?chart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args"
+netdata_download_various "$MURL" "api/v1/chart?chart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args"
+netdata_download_various_with_options "$MURL" "api/v1/chart?chart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args"
 
-netdata_download_various $MURL "api/v1/chart%3Fchart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args_encoded"
-netdata_download_various_with_options $MURL "api/v1/chart%3Fchart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args_encoded"
-netdata_download_various $MURL "api/v1/chart%3Fchart=cpu.cpu0_interrupts%26_=234231424242" "chart_cpu_with_more_args_encoded2"
-netdata_download_various $MURL "api/v1/chart%3Fchart%3Dcpu.cpu0_interrupts%26_%3D234231424242" "chart_cpu_with_more_args_encoded3"
+netdata_download_various "$MURL" "api/v1/chart%3Fchart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args_encoded"
+netdata_download_various_with_options "$MURL" "api/v1/chart%3Fchart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args_encoded"
+netdata_download_various "$MURL" "api/v1/chart%3Fchart=cpu.cpu0_interrupts%26_=234231424242" "chart_cpu_with_more_args_encoded2"
+netdata_download_various "$MURL" "api/v1/chart%3Fchart%3Dcpu.cpu0_interrupts%26_%3D234231424242" "chart_cpu_with_more_args_encoded3"
 
 netdata_create_directory "$OUTDIR/chart"
 for I in $CHARTS ; do
 	NAME=${I//\"/}
-    netdata_download_various $MURL "api/v1/chart?chart=$NAME"  "chart/$NAME"
+    netdata_download_various "$MURL" "api/v1/chart?chart=$NAME"  "chart/$NAME"
 done
 
-netdata_wrong_request_various $MURL "api/v1/alarm_variables" "err_alarm_variables_without_chart"
-netdata_wrong_request_various $MURL "api/v1/alarm_variables?_=234231424242" "err_alarm_variables_arg"
-netdata_download_various $MURL "api/v1/alarm_variables?chart=cpu.cpu0_interrupts&_=234231424242" "alarm_cpu_with_more_args"
+netdata_wrong_request_various "$MURL" "api/v1/alarm_variables" "err_alarm_variables_without_chart"
+netdata_wrong_request_various "$MURL" "api/v1/alarm_variables?_=234231424242" "err_alarm_variables_arg"
+netdata_download_various "$MURL" "api/v1/alarm_variables?chart=cpu.cpu0_interrupts&_=234231424242" "alarm_cpu_with_more_args"
 
 netdata_create_directory "$OUTDIR/alarm_variables"
 for I in $CHARTS ; do
 	NAME=${I//\"/}
-    netdata_download_various $MURL "api/v1/alarm_variables?chart=$NAME"  "alarm_variables/$NAME"
+    netdata_download_various "$MURL" "api/v1/alarm_variables?chart=$NAME"  "alarm_variables/$NAME"
 done
 
 netdata_create_directory "$OUTDIR/badge"
 netdata_create_directory "$OUTEDIR/badge"
 for I in $CHARTS ; do
-    netdata_download_chart $MURL "api/v1/badge.svg?chart" "$I" "badge"
+    netdata_download_chart "$MURL" "api/v1/badge.svg?chart" "$I" "badge"
 done
 
 netdata_create_directory "$OUTDIR/allmetrics"
-netdata_download_allmetrics $MURL "allmetrics"
+netdata_download_allmetrics "$MURL" "allmetrics"
 
-netdata_download_various $MURL "api/v1/alarms?all"  "alarms_all"
-netdata_download_various $MURL "api/v1/alarms?active"  "alarms_active"
-netdata_download_various $MURL "api/v1/alarms"  "alarms_nothing"
+netdata_download_various "$MURL" "api/v1/alarms?all"  "alarms_all"
+netdata_download_various "$MURL" "api/v1/alarms?active"  "alarms_active"
+netdata_download_various "$MURL" "api/v1/alarms"  "alarms_nothing"
 
-netdata_download_various $MURL "api/v1/alarm_log?after"  "alarm_without"
-netdata_download_various $MURL "api/v1/alarm_log"  "alarm_nothing"
-netdata_download_various $MURL "api/v1/alarm_log?after&_=$PT"  "alarm_log"
+netdata_download_various "$MURL" "api/v1/alarm_log?after"  "alarm_without"
+netdata_download_various "$MURL" "api/v1/alarm_log"  "alarm_nothing"
+netdata_download_various "$MURL" "api/v1/alarm_log?after&_=$PT"  "alarm_log"
 
 netdata_create_directory "$OUTDIR/data"
 netdata_create_directory "$OUTEDIR/data"
 for I in $CHARTS ; do
-    netdata_download_chart $MURL "api/v1/data?chart" "$I" "data"
+    netdata_download_chart "$MURL" "api/v1/data?chart" "$I" "data"
     break;
 done
 
-WHITE='\033[0;37m'
-echo -e "${WHITE}ALL the URLS got 200 as answer!"
+echo -e "${GREEN}ALL the URLS got 200 as answer! ${NOCOLOR}"
 
 exit 0