Chase Qi | 4a4caa3 | 2016-09-06 17:22:46 +0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Chase Qi | 3250835 | 2016-11-23 19:22:52 +0800 | [diff] [blame] | 3 | # shellcheck disable=SC1091 |
Chase Qi | 4a4caa3 | 2016-09-06 17:22:46 +0800 | [diff] [blame] | 4 | . ../../lib/sh-test-lib |
| 5 | OUTPUT="$(pwd)/output" |
| 6 | RESULT_FILE="${OUTPUT}/result.txt" |
Chase Qi | 3250835 | 2016-11-23 19:22:52 +0800 | [diff] [blame] | 7 | export RESULT_FILE |
Chase Qi | 4a4caa3 | 2016-09-06 17:22:46 +0800 | [diff] [blame] | 8 | |
| 9 | usage() { |
| 10 | echo "Usage: $0 [-s <true|false>]" 1>&2 |
| 11 | exit 1 |
| 12 | } |
| 13 | |
| 14 | while getopts "s:" o; do |
| 15 | case "$o" in |
| 16 | s) SKIP_INSTALL="${OPTARG}" ;; |
| 17 | *) usage ;; |
| 18 | esac |
| 19 | done |
| 20 | |
| 21 | ! check_root && error_msg "This script must be run as root" |
| 22 | [ -d "${OUTPUT}" ] && mv "${OUTPUT}" "${OUTPUT}_$(date +%Y%m%d%H%M%S)" |
| 23 | mkdir -p "${OUTPUT}" |
| 24 | |
| 25 | # Install lamp and use systemctl for service management. Tested on Ubuntu 16.04, |
| 26 | # Debian 8, CentOS 7 and Fedora 24. systemctl should available on newer releases |
| 27 | # as well. |
| 28 | if [ "${SKIP_INSTALL}" = "True" ] || [ "${SKIP_INSTALL}" = "true" ]; then |
| 29 | warn_msg "LAMP package installation skipped" |
| 30 | else |
Chase Qi | db0522c | 2016-11-28 12:00:11 +0800 | [diff] [blame] | 31 | # Stop nginx server in case it is installed and running. |
| 32 | systemctl stop nginx > /dev/null 2>&1 || true |
| 33 | |
Chase Qi | 4a4caa3 | 2016-09-06 17:22:46 +0800 | [diff] [blame] | 34 | dist_name |
Chase Qi | 3250835 | 2016-11-23 19:22:52 +0800 | [diff] [blame] | 35 | # shellcheck disable=SC2154 |
Chase Qi | 4a4caa3 | 2016-09-06 17:22:46 +0800 | [diff] [blame] | 36 | case "${dist}" in |
| 37 | Debian|Ubuntu) |
| 38 | if [ "${dist}" = "Debian" ]; then |
| 39 | pkgs="apache2 mysql-server php5-mysql php5-common libapache2-mod-php5" |
| 40 | elif [ "${dist}" = "Ubuntu" ]; then |
| 41 | pkgs="apache2 mysql-server php-mysql php-common libapache2-mod-php" |
| 42 | fi |
| 43 | install_deps "curl ${pkgs}" |
| 44 | echo "extension=mysqli.so" >> /etc/php5/apache2/php.ini |
| 45 | systemctl restart apache2 |
| 46 | systemctl restart mysql |
| 47 | ;; |
| 48 | CentOS|Fedora) |
| 49 | pkgs="httpd mariadb-server mariadb php php-mysql" |
| 50 | install_deps "curl ${pkgs}" |
| 51 | systemctl start httpd.service |
| 52 | systemctl start mariadb |
| 53 | ;; |
| 54 | *) |
| 55 | error_msg "Unsupported distribution!" |
| 56 | esac |
| 57 | fi |
| 58 | |
| 59 | cp ./html/* /var/www/html/ |
| 60 | |
| 61 | # Test Apache. |
Milosz Wasilewski | 9a975ba | 2016-10-20 20:55:05 +0100 | [diff] [blame] | 62 | curl -o "${OUTPUT}/index.html" "http://localhost/index.html" |
| 63 | grep "Test Page for the Apache HTTP Server" "${OUTPUT}/index.html" |
Chase Qi | 4a4caa3 | 2016-09-06 17:22:46 +0800 | [diff] [blame] | 64 | check_return "apache2-test-page" |
| 65 | |
| 66 | # Test MySQL. |
Chase Qi | a64e9be | 2016-11-28 12:25:57 +0800 | [diff] [blame] | 67 | mysqladmin -u root password lxmptest > /dev/null 2>&1 || true |
| 68 | mysql --user="root" --password="lxmptest" -e "show databases" |
Chase Qi | 4a4caa3 | 2016-09-06 17:22:46 +0800 | [diff] [blame] | 69 | check_return "mysql-show-databases" |
| 70 | |
| 71 | # Test PHP. |
| 72 | curl -o "${OUTPUT}/phpinfo.html" "http://localhost/info.php" |
| 73 | grep "PHP Version" "${OUTPUT}/phpinfo.html" |
| 74 | check_return "phpinfo" |
| 75 | |
| 76 | # PHP Connect to MySQL. |
| 77 | curl -o "${OUTPUT}/connect-db" "http://localhost/connect-db.php" |
| 78 | grep "Connected successfully" "${OUTPUT}/connect-db" |
| 79 | exit_on_fail "php-connect-db" |
| 80 | |
| 81 | # PHP Create a MySQL Database. |
| 82 | curl -o "${OUTPUT}/create-db" "http://localhost/create-db.php" |
| 83 | grep "Database created successfully" "${OUTPUT}/create-db" |
| 84 | check_return "php-create-db" |
| 85 | |
| 86 | # PHP Create MySQL table. |
| 87 | curl -o "${OUTPUT}/create-table" "http://localhost/create-table.php" |
| 88 | grep "Table MyGuests created successfully" "${OUTPUT}/create-table" |
| 89 | check_return "php-create-table" |
| 90 | |
| 91 | # PHP add record to MySQL table. |
| 92 | curl -o "${OUTPUT}/add-record" "http://localhost/add-record.php" |
| 93 | grep "New record created successfully" "${OUTPUT}/add-record" |
| 94 | check_return "php-add-record" |
| 95 | |
| 96 | # PHP select record from MySQL table. |
| 97 | curl -o "${OUTPUT}/select-record" "http://localhost/select-record.php" |
| 98 | grep "id: 1 - Name: John Doe" "${OUTPUT}/select-record" |
| 99 | check_return "php-select-record" |
| 100 | |
| 101 | # PHP delete record from MySQL table. |
| 102 | curl -o "${OUTPUT}/delete-record" "http://localhost/delete-record.php" |
| 103 | grep "Record deleted successfully" "${OUTPUT}/delete-record" |
| 104 | check_return "php-delete-record" |
Chase Qi | 3250835 | 2016-11-23 19:22:52 +0800 | [diff] [blame] | 105 | |
| 106 | # Delete myDB for the next run. |
Chase Qi | a64e9be | 2016-11-28 12:25:57 +0800 | [diff] [blame] | 107 | mysql --user='root' --password='lxmptest' -e 'DROP DATABASE myDB' |