From 464547ad62b6b57dd2890c499575b164de4cd16b Mon Sep 17 00:00:00 2001 From: Steve Barnes Date: May 26 2011 14:27:01 +0000 Subject: Fixed issue with mysqld scripts not processing - crapy handling of stdin/ positional parameters in the t_Process function. Renamed t_ProcessFolder to t_Process. Added test scripts for procinfo, strace and iptraf. Added a helper function t_ServiceControl which just calls /sbin/service $1 $2 but sleeps for a bit to avoid subsequent test scripts that rely on open network ports from automatically failing because they're being executed too quickly. --- diff --git a/WritingTests b/WritingTests index c012056..bd1c705 100644 --- a/WritingTests +++ b/WritingTests @@ -1,7 +1,12 @@ -Any and every script can be a test. The only 2 things that are important -are that - (a) the script needs to exit with a non zero exit level in - order to siginal a 'Fail' - (b) the script needs to call t_InstallPackage install to install any - packages or libraries that it would need to run +This file provides guidance on things to bear in mind when writing test scripts for the QA process: + +(a) scripts should exit with either zero to signal success, or non-zero to signal a failure. A failure exit code causes the entire test script execution process to stop (which is fine - we need to see what failed) + +(b) several helper functions are available in test/0_lib/* that should be used in preference to directly calling the commands they implement. Some of the reasons for doing this are consistency in debug output and avoiding timing related test failures. Please review the functions so you're familiar with their existence/operation. + +(c) all test scripts should first make a call to t_Log, passing in $0 and a description of what the test is doing, something like: + + t_Log Running $0 - Postfix SMTP test. + +(d) test scripts are processed in alphabetical order, so it's best to install any required packages in a 0-install-blah.sh script. Anything starting with a _ is ignored, and so are files named `readme` (case insensitive) diff --git a/runtests.sh b/runtests.sh index c31c139..44a2591 100755 --- a/runtests.sh +++ b/runtests.sh @@ -18,10 +18,13 @@ shopt -s nocasematch # exit as soon as any script returns a non-zero exit status set -e -# process our test script folders -t_ProcessFolder <(/usr/bin/find ./tests/0_common/ -type f|sort) -t_ProcessFolder <(/usr/bin/find ./tests/p_*/ -type f|sort) -t_ProcessFolder <(/usr/bin/find ./tests/r_*/ -type f|sort) +# exit on undefined variables +set -u + +# process our test scripts +t_Process <(/usr/bin/find ./tests/0_common/ -type f|sort) +t_Process <(/usr/bin/find ./tests/p_*/ -type f|sort) +t_Process <(/usr/bin/find ./tests/r_*/ -type f|sort) # and, we're done. t_Log "Finished." diff --git a/tests/0_common/05_stop_yumupdatesd.sh b/tests/0_common/05_stop_yumupdatesd.sh index 00ce118..ecf1a06 100755 --- a/tests/0_common/05_stop_yumupdatesd.sh +++ b/tests/0_common/05_stop_yumupdatesd.sh @@ -1,7 +1,4 @@ #!/bin/sh t_Log "Running $0 - stopping yum-updatesd service" - -/sbin/service yum-updatesd stop - -sleep 2 +t_ServiceControl yum-updatesd stop diff --git a/tests/0_lib/functions.sh b/tests/0_lib/functions.sh index 552c52e..e943566 100755 --- a/tests/0_lib/functions.sh +++ b/tests/0_lib/functions.sh @@ -37,17 +37,19 @@ function t_RemovePackage # Description: call this to process a list of folders containing test scripts # Arguments: a list of folder paths to process (see example in runtests.sh) -function t_ProcessFolder +function t_Process { - while read f + exec 7< $@ + + while read -u 7 f do # skip files named readme or those that start with an _ [[ "${f}" =~ readme|^_ ]] && continue; - # handy tip: chmod ug-x to disable individual test scripts. + # handy tip: chmod -x to disable individual test scripts. [ -x ${f} ] && ${f} - done < $@ + done return 0 } @@ -63,10 +65,22 @@ function t_CheckDeps return 0 } +# Description: perform a service control and sleep for a few seconds to let +# the dust settle. Failing to do this means tests that check for an +# open network port or response banner will probably fail for no +# apparent reason. +function t_ServiceControl +{ + /sbin/service $1 $2 + + # aaaand relax... + sleep 3 +} + export -f t_Log export -f t_CheckExitStatus export -f t_InstallPackage export -f t_RemovePackage -export -f t_ProcessFolder +export -f t_Process export -f t_CheckDeps - +export -f t_ServiceControl diff --git a/tests/p_dovecot/0-install_dovecot.sh b/tests/p_dovecot/0-install_dovecot.sh index 7cfb758..fc56e12 100755 --- a/tests/p_dovecot/0-install_dovecot.sh +++ b/tests/p_dovecot/0-install_dovecot.sh @@ -5,4 +5,4 @@ t_Log "Running $0 - installation and startup of dovecot IMAP/POP3." t_InstallPackage dovecot chkconfig dovecot on -service dovecot start +t_ServiceControl dovecot start diff --git a/tests/p_dovecot/dovecot_imap_login.sh b/tests/p_dovecot/dovecot_imap_login.sh index 61ea8e7..a2bcb4d 100755 --- a/tests/p_dovecot/dovecot_imap_login.sh +++ b/tests/p_dovecot/dovecot_imap_login.sh @@ -6,6 +6,6 @@ t_Log "Running $0 - adding imaptest local user account + attempting IMAP login" { userdel imaptest; useradd imaptest && echo imaptest | passwd --stdin imaptest; } &>/dev/null t_Log "Dovecot IMAP login test" -echo -e "01 LOGIN imaptest imaptest\n" | nc localhost 143 | grep "01 OK Logged in." > /dev/null 2>&1 +echo -e "01 LOGIN imaptest imaptest\n" | nc localhost 143 | grep "01 OK Logged in." -t_CheckExitStatus $? \ No newline at end of file +t_CheckExitStatus $? diff --git a/tests/p_dovecot/dovecot_pop3_login.sh b/tests/p_dovecot/dovecot_pop3_login.sh index ab2f172..952c35d 100755 --- a/tests/p_dovecot/dovecot_pop3_login.sh +++ b/tests/p_dovecot/dovecot_pop3_login.sh @@ -6,6 +6,6 @@ t_Log "Running $0 - adding pop3test local user account + attempting POP3 login" { userdel pop3test; useradd pop3test && echo pop3test | passwd --stdin pop3test; } &>/dev/null t_Log "Dovecot POP3 login test" -echo -e "user pop3test\npass pop3test\n" | nc localhost 110 | grep "+OK Logged in." > /dev/null 2>&1 +echo -e "user pop3test\npass pop3test\n" | nc localhost 110 | grep "+OK Logged in." -t_CheckExitStatus $? \ No newline at end of file +t_CheckExitStatus $? diff --git a/tests/p_httpd/0-install_httpd.sh b/tests/p_httpd/0-install_httpd.sh index 6a3a307..f7911a1 100755 --- a/tests/p_httpd/0-install_httpd.sh +++ b/tests/p_httpd/0-install_httpd.sh @@ -3,7 +3,5 @@ # HTTPD / PHP t_InstallPackage httpd mod_ssl php php-mysql - chkconfig httpd on - -service httpd start +t_ServiceControl httpd start diff --git a/tests/p_httpd/httpd_php.sh b/tests/p_httpd/httpd_php.sh index e4b06a3..d69d441 100755 --- a/tests/p_httpd/httpd_php.sh +++ b/tests/p_httpd/httpd_php.sh @@ -5,6 +5,6 @@ echo "" > /var/www/html/test.php t_Log "Running $0 - httpd handle PHP test" -echo -e "GET /test.php HTTP/1.0\r\n" | nc localhost 80 | grep 'PHP Version' > /dev/null 2>&1 +echo -e "GET /test.php HTTP/1.0\r\n" | nc localhost 80 | grep 'PHP Version' -t_CheckExitStatus $? \ No newline at end of file +t_CheckExitStatus $? diff --git a/tests/p_httpd/httpd_servehtml.sh b/tests/p_httpd/httpd_servehtml.sh index 23650a0..30ca6e7 100755 --- a/tests/p_httpd/httpd_servehtml.sh +++ b/tests/p_httpd/httpd_servehtml.sh @@ -3,6 +3,6 @@ t_Log "Running $0 - httpd serve html page test." -echo -e "GET / HTTP/1.0\r\n" | nc localhost 80 | grep 'Test Page' > /dev/null 2>&1 +echo -e "GET / HTTP/1.0\r\n" | nc localhost 80 | grep 'Test Page' -t_CheckExitStatus $? \ No newline at end of file +t_CheckExitStatus $? diff --git a/tests/p_httpd/httpd_vhost.sh b/tests/p_httpd/httpd_vhost.sh index 3ee4b5b..77e01fe 100755 --- a/tests/p_httpd/httpd_vhost.sh +++ b/tests/p_httpd/httpd_vhost.sh @@ -16,12 +16,12 @@ EOF mkdir -p /var/www/vhosts/test/ echo "Virtual Host Test Page" > /var/www/vhosts/test/index.html -service httpd restart -echo -e "GET / HTTP/1.0\r\n" | nc test 80 | grep 'Virtual Host Test Page' > /dev/null 2>&1 +t_ServiceControl httpd reload +echo -e "GET / HTTP/1.0\r\n" | nc test 80 | grep 'Virtual Host Test Page' t_CheckExitStatus $? # SteveCB: remove vhost-test.conf to prevent later tests # that assume DocumentRoot is /var/www/html from failing rm /etc/httpd/conf.d/vhost-test.conf -service httpd reload \ No newline at end of file +t_ServiceControl httpd reload diff --git a/tests/p_iptraf/5-test_iptraf.sh b/tests/p_iptraf/5-test_iptraf.sh new file mode 100755 index 0000000..b5b5e79 --- /dev/null +++ b/tests/p_iptraf/5-test_iptraf.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +t_Log "Running $0 - checking iptraf runs and returns non-zero exit status." + +TMP=/tmp/iptraf.log + +# clean up after ourselves +trap "[ -e ${TMP} ] && { /bin/rm ${TMP}; }" EXIT + +# iptraf only be run by root +[ ${EUID} -eq 0 ] || { t_Log "Not running as root, skipping this test. Non-fatal."; exit $PASS; } + +IPTRAF=`which iptraf` +PING=`which ping` +STAT=`which stat` +KILL=`which kill` + +[ -z "${IPTRAF}" ] && { t_log "Failed to find iptraf binary. That ain't good..."; exit $FAIL; } +[ -z "${PING}" ] && { t_Log "Failed to find the ping binary. That ain't good..."; exit $FAIL; } +[ -z "${STAT}" ] && { t_Log "Failed to find the stat binary. That ain't good..."; exit $FAIL; } +[ -z "${KILL}" ] && { t_Log "Failed to find the kill binary. That ain't good..."; exit $FAIL; } + +# start iptraf running in the background on all interfaces, logging to a file. +${IPTRAF} -i all -t 1 -B -L ${TMP} &>/dev/null + +# give iptraf something to chew on +${PING} -c 5 127.0.0.1 &>/dev/null + +# check the our log file actually has some data in it, which it should, given that we just pinged ourselves... +LOGSIZE=`stat -c '%s' ${TMP}` + +# kill iptraf +${KILL} -USR2 `pidof iptraf` + +# confirm our iptraf log file has something in it +[ ${LOGSIZE} -gt 0 ] || { t_Log "iptraf failed to log any traffic?!. That ain't good..."; exit $FAIL; } + diff --git a/tests/p_mdadm/0-install-mdadm.sh b/tests/p_mdadm/0-install-mdadm.sh new file mode 100755 index 0000000..6097db2 --- /dev/null +++ b/tests/p_mdadm/0-install-mdadm.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +# mdadm controls Linux md devices (software RAID arrays) +t_InstallPackage mdadm diff --git a/tests/p_mdadm/5-test_mdadm.sh b/tests/p_mdadm/5-test_mdadm.sh new file mode 100755 index 0000000..782d272 --- /dev/null +++ b/tests/p_mdadm/5-test_mdadm.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +t_Log "Running $0 - checking mdadm utility works and returns non-zero exit status." + +# mdadm utility only available to root +[ ${EUID} -eq 0 ] || { t_Log "Not running as root, skipping this test. Non-fatal."; exit $PASS; } + +MDADM=`which mdadm` + +[ -z "${MDADM}" ] && { t_Log "Failed to find mdadm binary. That ain't good...."; exit $FAIL; } + +# even with no meta devices available, this should still return a 0 exit status +${MDADM} --detail --scan &>/dev/null + +[ $? -eq 0 ] || { t_log "mdadm exited with non-zero status. That ain't good..."; exit $FAIL; } diff --git a/tests/p_mysql/0-install_mysqld.sh b/tests/p_mysql/0-install_mysqld.sh index 07ab4e8..43099a2 100755 --- a/tests/p_mysql/0-install_mysqld.sh +++ b/tests/p_mysql/0-install_mysqld.sh @@ -4,4 +4,4 @@ # MySQL t_InstallPackage mysql-server chkconfig mysqld on -service mysqld start +t_ServiceControl mysqld start diff --git a/tests/p_mysql/mysqld_conn_test.sh b/tests/p_mysql/mysqld_conn_test.sh index 1223191..8a55a37 100755 --- a/tests/p_mysql/mysqld_conn_test.sh +++ b/tests/p_mysql/mysqld_conn_test.sh @@ -4,7 +4,7 @@ t_Log "Running $0 - mysqld listening test." # FIXME: Test is very basic -nc -w 1 localhost 3306 > /dev/null 2>&1 +nc -d -w 1 localhost 3306 >/dev/null 2>&1 t_CheckExitStatus $? diff --git a/tests/p_mysql/mysqld_create_db.sh b/tests/p_mysql/mysqld_create_db.sh index fa3e571..3c0b911 100755 --- a/tests/p_mysql/mysqld_create_db.sh +++ b/tests/p_mysql/mysqld_create_db.sh @@ -2,7 +2,5 @@ # Author: Athmane Madjoudj t_Log "Running $0 - MySQL create database test" - -mysql -u root -e 'create database mysql_test' > /dev/null 2>&1 - -t_CheckExitStatus $? \ No newline at end of file +mysql -u root -e 'create database mysql_test' >/dev/null 2>&1 +t_CheckExitStatus $? diff --git a/tests/p_mysql/mysqld_drop_db.sh b/tests/p_mysql/mysqld_drop_db.sh index 70e78ab..b1db254 100755 --- a/tests/p_mysql/mysqld_drop_db.sh +++ b/tests/p_mysql/mysqld_drop_db.sh @@ -2,7 +2,5 @@ # Author: Athmane Madjoudj t_Log "Running $0 - MySQL drop database test." - -mysql -u root -e 'drop database mysql_test' > /dev/null 2>&1 - -t_CheckExitStatus $? \ No newline at end of file +mysql -u root -e 'drop database mysql_test' >/dev/null 2>&1 +t_CheckExitStatus $? diff --git a/tests/p_ntp/0-install_ntp.sh b/tests/p_ntp/0-install_ntp.sh index 033d897..2191475 100755 --- a/tests/p_ntp/0-install_ntp.sh +++ b/tests/p_ntp/0-install_ntp.sh @@ -1,4 +1,6 @@ #!/bin/bash +# Author: Steve Barnes (steve@echo.id.au) + # NTPd -t_InstallPackage ntpd +t_InstallPackage ntp diff --git a/tests/p_ntp/5-start-check.sh b/tests/p_ntp/5-start-check.sh index d997fcf..d0bff73 100755 --- a/tests/p_ntp/5-start-check.sh +++ b/tests/p_ntp/5-start-check.sh @@ -1,10 +1,13 @@ #!/bin/bash # Start NTPd services and confirm it's running. - chkconfig ntpd on -service ntpd start +t_ServiceControl ntpd start NTPD_PID=$(pidof ntpd) [ -z "$NTPD_PID" ] && { t_Log "FAIL: couldn't find 'ntpd' in the process list."; exit $FAIL; } + +# this is here because otherwise the [ operator exit +# status would become the return value from the function +exit 0 diff --git a/tests/p_openssh/0-install_sshd.sh b/tests/p_openssh/0-install_sshd.sh index 05e7afd..a3ede2f 100755 --- a/tests/p_openssh/0-install_sshd.sh +++ b/tests/p_openssh/0-install_sshd.sh @@ -4,4 +4,4 @@ # SSH t_InstallPackage openssh-server chkconfig sshd on -service sshd start +t_ServiceControl sshd start diff --git a/tests/p_openssh/sshd_conn_test.sh b/tests/p_openssh/sshd_conn_test.sh index bfff865..50e3a5a 100755 --- a/tests/p_openssh/sshd_conn_test.sh +++ b/tests/p_openssh/sshd_conn_test.sh @@ -7,4 +7,4 @@ t_Log "Running $0 - SSHD is listening test." nc -w 1 localhost 22 > /dev/null 2>&1 -t_CheckExitStatus $? \ No newline at end of file +t_CheckExitStatus $? diff --git a/tests/p_postfix/0-install_postfix.sh b/tests/p_postfix/0-install_postfix.sh index 1a04440..13f8ff2 100755 --- a/tests/p_postfix/0-install_postfix.sh +++ b/tests/p_postfix/0-install_postfix.sh @@ -4,5 +4,4 @@ # Postfix t_InstallPackage postfix t_RemovePackage sendmail -service postfix start - +t_ServiceControl postfix start diff --git a/tests/p_postfix/postfix_smtp.sh b/tests/p_postfix/postfix_smtp.sh index 2b6f277..ff862af 100755 --- a/tests/p_postfix/postfix_smtp.sh +++ b/tests/p_postfix/postfix_smtp.sh @@ -3,6 +3,6 @@ t_Log "Running $0 - Postfix SMTP test." -echo "helo test" | nc localhost 25 | grep '250' > /dev/null 2>&1 +echo "helo test" | nc localhost 25 | grep '250' -t_CheckExitStatus $? \ No newline at end of file +t_CheckExitStatus $? diff --git a/tests/p_procinfo/5-test_procinfo.sh b/tests/p_procinfo/5-test_procinfo.sh new file mode 100755 index 0000000..a0dc6ce --- /dev/null +++ b/tests/p_procinfo/5-test_procinfo.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +t_Log "Running $0 - checking procinfo runs and returns non-zero exit status." + +PROCINFO=`which procinfo` + +[ -z "${PROCINFO}" ] && { t_log "Failed to find procinfo binary. Cannot continue."; exit $FAIL; } + +${PROCINFO} &>/dev/null + +[ $? -eq 0 ] || { t_log "Procinfo exited with non-zero status. That ain't good..."; exit $FAIL; } diff --git a/tests/p_strace/5-test_strace.sh b/tests/p_strace/5-test_strace.sh new file mode 100755 index 0000000..c6da3cd --- /dev/null +++ b/tests/p_strace/5-test_strace.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +t_Log "Running $0 - checking strace runs and returns non-zero exit status." + +STRACE=`which strace` + +[ -z "${STRACE}" ] && { t_log "Failed to find strace. Cannot continue."; exit $FAIL; } + +${STRACE} ls &>/dev/null + +[ $? -eq 0 ] || { t_log "Strace exited with non-zero status. That ain't good..."; exit $FAIL; } diff --git a/tests/p_vsftpd/0-install_vsftpd.sh b/tests/p_vsftpd/0-install_vsftpd.sh index c34d606..5af700d 100755 --- a/tests/p_vsftpd/0-install_vsftpd.sh +++ b/tests/p_vsftpd/0-install_vsftpd.sh @@ -4,5 +4,4 @@ # vsFTPd t_InstallPackage vsftpd chkconfig vsftpd on -service vsftpd start - +t_ServiceControl vsftpd start diff --git a/tests/p_vsftpd/vsftpd_anonymous_login.sh b/tests/p_vsftpd/vsftpd_anonymous_login.sh index 207ea0a..ad26f35 100755 --- a/tests/p_vsftpd/vsftpd_anonymous_login.sh +++ b/tests/p_vsftpd/vsftpd_anonymous_login.sh @@ -3,6 +3,6 @@ t_Log "Running $0 - vsFTPd anonymous can login test." -echo -e "user anonymous\npass password\nquit" | nc localhost 21 | grep "230 Login successful." > /dev/null 2>&1 +echo -e "user anonymous\npass password\nquit" | nc localhost 21 | grep "230 Login successful." -t_CheckExitStatus $? \ No newline at end of file +t_CheckExitStatus $? diff --git a/tests/r_lamp/40_basic_lamp.sh b/tests/r_lamp/40_basic_lamp.sh index 93368d4..3725303 100755 --- a/tests/r_lamp/40_basic_lamp.sh +++ b/tests/r_lamp/40_basic_lamp.sh @@ -3,8 +3,8 @@ t_Log "Running $0 - install a minimal lamp stack, and test it" t_InstallPackage httpd mysql mysql-server php php-mysql wget -service mysqld start >/dev/null 2>&1 -service httpd start +t_ServiceControl mysqld start +t_ServiceControl httpd start # Initializing a small MySQL db cat >>/tmp/mysql-QA.sql <