From 2dd0be1fee392e0f412b81f9368a13932e651963 Mon Sep 17 00:00:00 2001 From: Karanbir Singh Date: May 16 2011 10:35:17 +0000 Subject: import patch from Steve --- diff --git a/WritingTests b/WritingTests index 8aa69d2..c012056 100644 --- a/WritingTests +++ b/WritingTests @@ -3,5 +3,5 @@ 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 yum install to install any + (b) the script needs to call t_InstallPackage install to install any packages or libraries that it would need to run diff --git a/readme b/readme index 84a4c51..9d1f4d7 100644 --- a/readme +++ b/readme @@ -9,7 +9,7 @@ tests/0_lib/ : contains all the common functions and shared code for before any of the tests are run, which also means it can only contain bash code ( no subdir allowed ) -tests/_common/ : Contain's tests that are run before any other test, +tests/0_common/ : Contain's tests that are run before any other test, and immediately after the 0_lib/ code is sourced. These should be tests that check system sanity and environment. These tests should also not leave behind diff --git a/runtests.sh b/runtests.sh index 358e892..4b528f4 100644 --- a/runtests.sh +++ b/runtests.sh @@ -1,4 +1,22 @@ -#!/bin/sh +#!/bin/bash -echo 'this is where the stripped down script that runs the tests would come' +export readonly PASS=0 +export readonly FAIL=1 +echo -e "\n[+] `date` -> CentOS QA $0 starting." + +LIB_FUNCTIONS='./tests/0_lib/functions.sh' + +[ -f $LIB_FUNCTIONS ] && source $LIB_FUNCTIONS || { echo -e "\n[+] `date` -> Unable to source functions library. Cannot continue\n"; exit $FAIL; } + +# case insensitive filename matching +shopt -s nocasematch + +# exit as soon as any script returns a non-zero exit status +set -e + +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) + +t_Log "Finished." diff --git a/tests/0_common/00_qa_repo_config.sh b/tests/0_common/00_qa_repo_config.sh index a73abfb..bf8daf8 100644 --- a/tests/0_common/00_qa_repo_config.sh +++ b/tests/0_common/00_qa_repo_config.sh @@ -1,10 +1,11 @@ #!/bin/bash +t_Log "Running $0 - modifying yum repositories for QA purposes." + # Disable the normal repositories and points to the QA repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.disabled -touch /etc/yum.repos.d/CentOS-Base.repo -cat >> /etc/yum.repos.d/CentOS-QA.repo << EOF +cat << EOF > /etc/yum.repos.d/CentOS-QA.repo [QA-base] name=CentOS-\$releasever - OS @@ -37,13 +38,7 @@ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 EOF yum clean all -echo "Modifying yum repositories for QA purposes ..." yum repolist -if [ $? -eq 0 ]; then - echo ' PASS' -else - echo ' Fail' - exit 1 -fi +t_CheckExitStatus $? diff --git a/tests/0_common/05_stop_yumupdatesd.sh b/tests/0_common/05_stop_yumupdatesd.sh index a6c673f..00ce118 100644 --- a/tests/0_common/05_stop_yumupdatesd.sh +++ b/tests/0_common/05_stop_yumupdatesd.sh @@ -1,3 +1,7 @@ #!/bin/sh + +t_Log "Running $0 - stopping yum-updatesd service" + /sbin/service yum-updatesd stop + sleep 2 diff --git a/tests/0_common/10_remove_32bitpkgs.sh b/tests/0_common/10_remove_32bitpkgs.sh index 3d207f3..86e6596 100644 --- a/tests/0_common/10_remove_32bitpkgs.sh +++ b/tests/0_common/10_remove_32bitpkgs.sh @@ -1,22 +1,11 @@ #!/bin/sh -echo 'Test that all 32 rpms can be removed' +t_Log "Running $0 - test that all 32-bit rpms can be removed" # only run this test on x86_64 machines! is64=$(uname -m|grep x86_64) -if [ "$?" -ne '0' ]; then - echo ' Skip' - exit 1 -fi -yum -d0 -y erase *.i?86 -#yum -d0 -y erase *.i?86 > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo ' PASS' -else - echo ' Fail' - exit 1 -fi +# This is a non-fatal status, so return PASS. +[ $? -ne 0 ] && { t_Log 'Host is not 64bit, skipping.'; exit $PASS; } -# note, this does not imply the machine is usable after the remove! need -# to test that independantly +t_RemovePackage *.i?86 \ No newline at end of file diff --git a/tests/0_common/20_upgrade_all.sh b/tests/0_common/20_upgrade_all.sh index 8021164..9bfa00e 100755 --- a/tests/0_common/20_upgrade_all.sh +++ b/tests/0_common/20_upgrade_all.sh @@ -1,13 +1,7 @@ #!/bin/sh -echo 'Test that all updates can be applied to this machine cleanly' - +t_Log "Running $0 - test that all updates can be applied to this machine cleanly" yum -d0 -y upgrade -# yum -d0 -y upgrade > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo ' PASS' -else - echo ' Fail' - exit 1 -fi + +t_CheckExitStatus $? \ No newline at end of file diff --git a/tests/0_common/30_dns_works.sh b/tests/0_common/30_dns_works.sh index ff4f80f..d0a3cea 100644 --- a/tests/0_common/30_dns_works.sh +++ b/tests/0_common/30_dns_works.sh @@ -1,22 +1,14 @@ -#!/bin/sh +#!/bin/bash -echo 'Test to see if dns works' +t_Log "Running $0 - testing to see if DNS works" -# test # its important we dont hit a dns record with a wildcard like centos.org -ping -c 1 www.google.com > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo ' PASS' -else - echo ' Fail' - exit 1 -fi +/bin/ping -c 1 www.google.com &>/dev/null +t_CheckExitStatus $? # implied results: # - network works # - default route is really routeable # - atleast one network link on the machine is working -# - kernel' ip stack is functional - - +# - kernel' ip stack is functional \ No newline at end of file diff --git a/tests/0_lib/0_config b/tests/0_lib/0_config deleted file mode 100644 index 19940d6..0000000 --- a/tests/0_lib/0_config +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -# Description: All the common static content can go here - -PASS=0 -FAIL=1 - -YUM=/usr/bin/yum diff --git a/tests/0_lib/functions.sh b/tests/0_lib/functions.sh new file mode 100644 index 0000000..cac6b4c --- /dev/null +++ b/tests/0_lib/functions.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +# herein lies a set of common library functions +# any recurring code is welcome here + +function t_Log +{ + printf "[+] `date` -> $*\n" +} + +function t_CheckExitStatus +{ + [ $1 -eq 0 ] && { t_Log "PASS"; return $PASS; } + + t_Log "FAIL" + exit $FAIL +} + +function t_InstallPackage +{ + for P in $*; do + + t_Log "Attempting yum install of '$P'..." + yum -y -d0 install $P + t_CheckExitStatus $? + done +} + +function t_RemovePackage +{ + for P in $*; do + + t_Log "Attempting yum remove of '$P'..." + yum -y -d0 remove $P + t_CheckExitStatus $? + done +} + +function t_ProcessFolder +{ + while read f + do + # skip files named 'readme' or 'package_deps' + [[ "${f}" =~ (readme|package_deps) ]] && continue; + + # all test scripts have to be executable + # this allows us to enable/disable individual + # tests by adding/removing the executable flag + # the alternative is to have '/bin/bash $f' here + # but I think the executable flag approach gives + # us more flexibility... + [ -x $f ] && $f + + done < $@ +} + +export -f t_Log +export -f t_CheckExitStatus +export -f t_InstallPackage +export -f t_RemovePackage +export -f t_ProcessFolder diff --git a/tests/0_lib/install_package.sh b/tests/0_lib/install_package.sh deleted file mode 100644 index 7afefb8..0000000 --- a/tests/0_lib/install_package.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -function t_InstallPackage(){ - - for P in $*; do - echo -n "[+] Attempting yum install of '$P'..." - - $YUM -y -d0 install $P &>/dev/null - RETVAL=$? - if [ $RETVAL -ne 0 ]; then - echo "FAIL: yum install of '$P' failed ($RETVAL)" - exit $FAIL - fi - echo "OK" - done -} diff --git a/tests/p_dovecot/0-install_dovecot.sh b/tests/p_dovecot/0-install_dovecot.sh index 380f386..7cfb758 100755 --- a/tests/p_dovecot/0-install_dovecot.sh +++ b/tests/p_dovecot/0-install_dovecot.sh @@ -1,7 +1,8 @@ #!/bin/bash # Author: Athmane Madjoudj -# Dovecot -yum -y install dovecot +t_Log "Running $0 - installation and startup of dovecot IMAP/POP3." + +t_InstallPackage dovecot chkconfig dovecot on service dovecot start diff --git a/tests/p_dovecot/dovecot_imap_login.sh b/tests/p_dovecot/dovecot_imap_login.sh index bf6ff4b..61ea8e7 100755 --- a/tests/p_dovecot/dovecot_imap_login.sh +++ b/tests/p_dovecot/dovecot_imap_login.sh @@ -1,14 +1,11 @@ #!/bin/sh # Author: Athmane Madjoudj -echo "Adding a new test user ..." -userdel imaptest; useradd imaptest && echo imaptest | passwd --stdin imaptest +t_Log "Running $0 - adding imaptest local user account + attempting IMAP login" -echo -n "Dovecot IMAP login test: " +{ 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 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +t_CheckExitStatus $? \ No newline at end of file diff --git a/tests/p_dovecot/dovecot_pop3_login.sh b/tests/p_dovecot/dovecot_pop3_login.sh index 4c152e3..ab2f172 100755 --- a/tests/p_dovecot/dovecot_pop3_login.sh +++ b/tests/p_dovecot/dovecot_pop3_login.sh @@ -1,14 +1,11 @@ -#!/bin/sh +#!/bin/bash # Author: Athmane Madjoudj -echo "Adding a new test user ..." -userdel pop3test; useradd pop3test && echo pop3test | passwd --stdin pop3test +t_Log "Running $0 - adding pop3test local user account + attempting POP3 login" -echo -n "Dovecot POP3 login test: " +{ 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 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +t_CheckExitStatus $? \ No newline at end of file diff --git a/tests/p_httpd/0-install_httpd.sh b/tests/p_httpd/0-install_httpd.sh index 0bfd612..6a3a307 100755 --- a/tests/p_httpd/0-install_httpd.sh +++ b/tests/p_httpd/0-install_httpd.sh @@ -2,6 +2,8 @@ # Author: Athmane Madjoudj # HTTPD / PHP -yum -y install httpd mod_ssl php php-mysql +t_InstallPackage httpd mod_ssl php php-mysql + chkconfig httpd on + service httpd start diff --git a/tests/p_httpd/httpd_php.sh b/tests/p_httpd/httpd_php.sh index cf02dd7..e4b06a3 100755 --- a/tests/p_httpd/httpd_php.sh +++ b/tests/p_httpd/httpd_php.sh @@ -2,11 +2,9 @@ # Author: Athmane Madjoudj echo "" > /var/www/html/test.php -echo -n "HTTPD handle PHP test: " + +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 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +t_CheckExitStatus $? \ No newline at end of file diff --git a/tests/p_httpd/httpd_servehtml.sh b/tests/p_httpd/httpd_servehtml.sh index 1d95128..23650a0 100755 --- a/tests/p_httpd/httpd_servehtml.sh +++ b/tests/p_httpd/httpd_servehtml.sh @@ -1,11 +1,8 @@ #!/bin/sh # Author: Athmane Madjoudj -echo -n "HTTPD serve html page test: " +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 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +t_CheckExitStatus $? \ No newline at end of file diff --git a/tests/p_httpd/httpd_vhost.sh b/tests/p_httpd/httpd_vhost.sh index b6927ef..3ee4b5b 100755 --- a/tests/p_httpd/httpd_vhost.sh +++ b/tests/p_httpd/httpd_vhost.sh @@ -1,6 +1,8 @@ #!/bin/sh # Author: Athmane Madjoudj +t_Log "Running $0 - httpd virtual host test." + echo "127.0.0.1 test" >> /etc/hosts cat > /etc/httpd/conf.d/vhost-test.conf < /var/www/vhosts/test/index.html service httpd restart -echo -n "HTTPD Virtual Host test: " echo -e "GET / HTTP/1.0\r\n" | nc test 80 | grep 'Virtual Host Test Page' > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +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 diff --git a/tests/p_mysql/0-install_mysqld.sh b/tests/p_mysql/0-install_mysqld.sh index aafadba..07ab4e8 100755 --- a/tests/p_mysql/0-install_mysqld.sh +++ b/tests/p_mysql/0-install_mysqld.sh @@ -2,6 +2,6 @@ # Author: Athmane Madjoudj # MySQL -yum -y install mysql-server +t_InstallPackage mysql-server chkconfig mysqld on service mysqld start diff --git a/tests/p_mysql/mysqld_conn_test.sh b/tests/p_mysql/mysqld_conn_test.sh index 7b51350..1223191 100755 --- a/tests/p_mysql/mysqld_conn_test.sh +++ b/tests/p_mysql/mysqld_conn_test.sh @@ -1,15 +1,12 @@ #!/bin/sh # Author: Athmane Madjoudj +t_Log "Running $0 - mysqld listening test." + # FIXME: Test is very basic -echo -n "MySQL is listening test: " nc -w 1 localhost 3306 > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +t_CheckExitStatus $? # kb: Can we do something like : # hn=$(mysql -N -B -u root -e "show variables like 'hostname'" | cut -f 2) diff --git a/tests/p_mysql/mysqld_create_db.sh b/tests/p_mysql/mysqld_create_db.sh index 7831e85..fa3e571 100755 --- a/tests/p_mysql/mysqld_create_db.sh +++ b/tests/p_mysql/mysqld_create_db.sh @@ -1,11 +1,8 @@ #!/bin/sh # Author: Athmane Madjoudj -echo -n "MySQL create database test: " +t_Log "Running $0 - MySQL create database test" + mysql -u root -e 'create database mysql_test' > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +t_CheckExitStatus $? \ No newline at end of file diff --git a/tests/p_mysql/mysqld_drop_db.sh b/tests/p_mysql/mysqld_drop_db.sh index b4b75c2..70e78ab 100755 --- a/tests/p_mysql/mysqld_drop_db.sh +++ b/tests/p_mysql/mysqld_drop_db.sh @@ -1,11 +1,8 @@ #!/bin/sh # Author: Athmane Madjoudj -echo -n "MySQL drop database test: " +t_Log "Running $0 - MySQL drop database test." + mysql -u root -e 'drop database mysql_test' > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +t_CheckExitStatus $? \ No newline at end of file diff --git a/tests/p_openssh/0-install_sshd.sh b/tests/p_openssh/0-install_sshd.sh index 085659e..05e7afd 100755 --- a/tests/p_openssh/0-install_sshd.sh +++ b/tests/p_openssh/0-install_sshd.sh @@ -2,6 +2,6 @@ # Author: Athmane Madjoudj # SSH -yum -y openssh-server +t_InstallPackage openssh-server chkconfig sshd on service sshd start diff --git a/tests/p_openssh/sshd_conn_test.sh b/tests/p_openssh/sshd_conn_test.sh index 4a5cdae..bfff865 100755 --- a/tests/p_openssh/sshd_conn_test.sh +++ b/tests/p_openssh/sshd_conn_test.sh @@ -2,11 +2,9 @@ # Author: Athmane Madjoudj # FIXME: Test is very basic -echo -n "SSHD is listening test: " + +t_Log "Running $0 - SSHD is listening test." + nc -w 1 localhost 22 > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +t_CheckExitStatus $? \ No newline at end of file diff --git a/tests/p_php/01_prep.sh b/tests/p_php/01_prep.sh index 10bf0bc..d8b2af9 100644 --- a/tests/p_php/01_prep.sh +++ b/tests/p_php/01_prep.sh @@ -1,4 +1,5 @@ #!/bin/sh -yum -d0 install php-cli -exit $? +t_Log "Running $0 - attempting to install php-cli." +t_InstallPackage php-cli + diff --git a/tests/p_postfix/0-install_postfix.sh b/tests/p_postfix/0-install_postfix.sh index ca4c382..1a04440 100755 --- a/tests/p_postfix/0-install_postfix.sh +++ b/tests/p_postfix/0-install_postfix.sh @@ -2,7 +2,7 @@ # Author: Athmane Madjoudj # Postfix -yum -y install postfix -yum -y remove sendmail +t_InstallPackage postfix +t_RemovePackage sendmail service postfix start diff --git a/tests/p_postfix/postfix_smtp.sh b/tests/p_postfix/postfix_smtp.sh index 0150aed..2b6f277 100755 --- a/tests/p_postfix/postfix_smtp.sh +++ b/tests/p_postfix/postfix_smtp.sh @@ -1,11 +1,8 @@ #!/bin/sh # Author: Athmane Madjoudj -echo -n "Postfix SMTP test: " +t_Log "Running $0 - Postfix SMTP test." + echo "helo test" | nc localhost 25 | grep '250' > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +t_CheckExitStatus $? \ No newline at end of file diff --git a/tests/p_vsftpd/0-install_vsftpd.sh b/tests/p_vsftpd/0-install_vsftpd.sh index 7fe8eb5..c34d606 100755 --- a/tests/p_vsftpd/0-install_vsftpd.sh +++ b/tests/p_vsftpd/0-install_vsftpd.sh @@ -2,7 +2,7 @@ # Author: Athmane Madjoudj # vsFTPd -yum -y install vsftpd +t_InstallPackage vsftpd chkconfig vsftpd on service vsftpd start diff --git a/tests/p_vsftpd/vsftpd_anonymous_login.sh b/tests/p_vsftpd/vsftpd_anonymous_login.sh index 6c136e7..207ea0a 100755 --- a/tests/p_vsftpd/vsftpd_anonymous_login.sh +++ b/tests/p_vsftpd/vsftpd_anonymous_login.sh @@ -1,11 +1,8 @@ #!/bin/sh # Author: Athmane Madjoudj -echo -n "vsFTPd anonymous can login test: " +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 -if [ $? -eq 0 ]; then - echo 'PASS' -else - echo 'FAIL' - exit 1 -fi + +t_CheckExitStatus $? \ No newline at end of file diff --git a/tests/r_lamp/0_lamp_install.sh b/tests/r_lamp/0_lamp_install.sh index 4b2cc6e..8efc904 100644 --- a/tests/r_lamp/0_lamp_install.sh +++ b/tests/r_lamp/0_lamp_install.sh @@ -1,10 +1,10 @@ -#!/bin/bash - -# Author: Steve Barnes (steve@echo.id.au) -# Filename: 0_lamp_install.sh -# Version: 0.1.1 -# Last Updated: Saturday, 30 April 2011 3:52 PM AEST -# Description: A simple Bash script to install a LAMP stack (Apache, MySQL (server + client) and PHP)) via yum. - -t_InstallPackages httpd mysql-server php - +#!/bin/bash + +# Author: Steve Barnes (steve@echo.id.au) +# Filename: 0_lamp_install.sh +# Version: 0.1.1 +# Last Updated: Saturday, 30 April 2011 3:52 PM AEST +# Description: A simple Bash script to install a LAMP stack (Apache, MySQL (server + client) and PHP)) via yum. + +t_InstallPackage httpd mysql-server php + diff --git a/tests/r_lamp/1_lamp_check.sh b/tests/r_lamp/1_lamp_check.sh index eba0799..b6a05ee 100644 --- a/tests/r_lamp/1_lamp_check.sh +++ b/tests/r_lamp/1_lamp_check.sh @@ -1,72 +1,66 @@ -#!/bin/bash - -# Author: Steve Barnes (steve@echo.id.au) -# Filename: 1_lamp_check.sh -# Version: 0.1 -# Last Updated: Saturday, 30 April 2011 2:23 PM AEST -# Description: A simple Bash script to start LAMP daemons (httpd, mysqld), and confirm PHP is working. - -readonly PASS=0 -readonly FAIL=1 - -readonly DAEMONS=( httpd mysqld ) - -readonly SERVICE=/sbin/service -readonly PHP_BIN=/usr/bin/php -readonly PHP_CHECK=/tmp/check.php - -# Make sure we cleanup after ourselves. -trap "/bin/rm -f $PHP_CHECK" EXIT - -echo "[+] Starting LAMP daemon startup test" - -# Iterate through our daemons, start each and check for the presence of each process -for D in "${DAEMONS[@]}" -do - echo -n "[+] Attempting startup of '$D'..." - - $SERVICE $D start &>/dev/null - - RETVAL=$? - - if [ $RETVAL -ne 0 ]; then - - echo "FAIL: service startup for '$D' failed ($RETVAL)" - exit $FAIL - - fi - - # See if our process exists - PIDS=$(pidof $D) - - if [ -z "$PIDS" ]; then - - echo "FAIL: couldn't find '$D' in the process list." - exit $FAIL - fi - - echo "OK" - -done - -# Finally, a basic check to see if PHP is working correctly. - -echo -n "[+] Performing php script check..." - -cat < $PHP_CHECK - -EOL - -RETVAL=$PHP_BIN $PHP_CHECK &>/dev/null - -if [ $RETVAL -ne 0 ]; then - - echo "FAIL: php_info() check failed ($RETVAL)" - exit $FAIL - -fi - -echo "OK" -echo "[+] Finished" \ No newline at end of file +#!/bin/bash + +# Author: Steve Barnes (steve@echo.id.au) +# Filename: 1_lamp_check.sh +# Version: 0.1 +# Last Updated: Saturday, 30 April 2011 2:23 PM AEST +# Description: A simple Bash script to start LAMP daemons (httpd, mysqld), and confirm PHP is working. + +readonly DAEMONS=( httpd mysqld ) + +readonly SERVICE=/sbin/service +readonly PHP_BIN=/usr/bin/php +readonly PHP_CHECK=/tmp/check.php + +# Make sure we cleanup after ourselves. +trap "/bin/rm -f $PHP_CHECK" EXIT + +t_Log "Running $0 - starting LAMP daemon startup test" + +# Iterate through our daemons, start each and check for the presence of each process +for D in "${DAEMONS[@]}" +do + t_Log "Attempting startup of '$D'" + + $SERVICE $D start &>/dev/null + + RETVAL=$? + + if [ $RETVAL -ne 0 ]; then + + t_Log "FAIL: service startup for '$D' failed ($RETVAL)" + exit $FAIL + + fi + + # See if our process exists + PIDS=$(pidof $D) + + if [ -z "$PIDS" ]; then + + t_Log "FAIL: couldn't find '$D' in the process list." + exit $FAIL + fi + + echo "OK" + +done + +# Finally, a basic check to see if PHP is working correctly. + +t_Log "Performing php script check..." + +cat < $PHP_CHECK + +EOL + +RETVAL=$PHP_BIN $PHP_CHECK &>/dev/null + +if [ $RETVAL -ne 0 ]; then + + t_Log "FAIL: php_info() check failed ($RETVAL)" + exit $FAIL + +fi \ No newline at end of file diff --git a/tests/r_lamp/40_basic_lamp.sh b/tests/r_lamp/40_basic_lamp.sh index b8f8d68..80c0ed6 100644 --- a/tests/r_lamp/40_basic_lamp.sh +++ b/tests/r_lamp/40_basic_lamp.sh @@ -1,10 +1,8 @@ #!/bin/bash -# purpose : install a minimal lamp stack, and test it +t_Log "Running $0 - install a minimal lamp stack, and test it" -# Installing required bits - -yum install -y httpd mysql mysql-server php php-mysql wget > /dev/null 2>&1 +t_InstallPackage httpd mysql mysql-server php php-mysql wget service mysqld start service httpd start @@ -35,26 +33,22 @@ mysql_select_db("qatests", \$dbconnect); mysql_query("INSERT INTO tests (name) VALUES ('mysqltest')"); - mysql_close(\$dbconnect); ?> EOF - - - #################################################### # testing #################################################### wget http://localhost/mysql.php -echo "Basic LAMP test ..." +t_Log "Performing basic LAMP test" content=`echo "select * from qatests.tests where name='mysqltest'"|mysql -B --skip-column-names` if [ "$content" = "mysqltest" ] ; then - echo PASS; + t_Log PASS; exit 0; else - echo Fail; + t_Log FAIL; exit 1; fi