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 |
| 31 | dist_name |
Chase Qi | 3250835 | 2016-11-23 19:22:52 +0800 | [diff] [blame^] | 32 | # shellcheck disable=SC2154 |
Chase Qi | 4a4caa3 | 2016-09-06 17:22:46 +0800 | [diff] [blame] | 33 | case "${dist}" in |
| 34 | Debian|Ubuntu) |
| 35 | if [ "${dist}" = "Debian" ]; then |
| 36 | pkgs="apache2 mysql-server php5-mysql php5-common libapache2-mod-php5" |
| 37 | elif [ "${dist}" = "Ubuntu" ]; then |
| 38 | pkgs="apache2 mysql-server php-mysql php-common libapache2-mod-php" |
| 39 | fi |
| 40 | install_deps "curl ${pkgs}" |
| 41 | echo "extension=mysqli.so" >> /etc/php5/apache2/php.ini |
| 42 | systemctl restart apache2 |
| 43 | systemctl restart mysql |
| 44 | ;; |
| 45 | CentOS|Fedora) |
| 46 | pkgs="httpd mariadb-server mariadb php php-mysql" |
| 47 | install_deps "curl ${pkgs}" |
| 48 | systemctl start httpd.service |
| 49 | systemctl start mariadb |
| 50 | ;; |
| 51 | *) |
| 52 | error_msg "Unsupported distribution!" |
| 53 | esac |
| 54 | fi |
| 55 | |
| 56 | cp ./html/* /var/www/html/ |
| 57 | |
| 58 | # Test Apache. |
Milosz Wasilewski | 9a975ba | 2016-10-20 20:55:05 +0100 | [diff] [blame] | 59 | curl -o "${OUTPUT}/index.html" "http://localhost/index.html" |
| 60 | grep "Test Page for the Apache HTTP Server" "${OUTPUT}/index.html" |
Chase Qi | 4a4caa3 | 2016-09-06 17:22:46 +0800 | [diff] [blame] | 61 | check_return "apache2-test-page" |
| 62 | |
| 63 | # Test MySQL. |
Chase Qi | 3250835 | 2016-11-23 19:22:52 +0800 | [diff] [blame^] | 64 | mysqladmin -u root password lamptest > /dev/null 2>&1 || true |
Chase Qi | 4a4caa3 | 2016-09-06 17:22:46 +0800 | [diff] [blame] | 65 | mysql --user="root" --password="lamptest" -e "show databases" |
| 66 | check_return "mysql-show-databases" |
| 67 | |
| 68 | # Test PHP. |
| 69 | curl -o "${OUTPUT}/phpinfo.html" "http://localhost/info.php" |
| 70 | grep "PHP Version" "${OUTPUT}/phpinfo.html" |
| 71 | check_return "phpinfo" |
| 72 | |
| 73 | # PHP Connect to MySQL. |
| 74 | curl -o "${OUTPUT}/connect-db" "http://localhost/connect-db.php" |
| 75 | grep "Connected successfully" "${OUTPUT}/connect-db" |
| 76 | exit_on_fail "php-connect-db" |
| 77 | |
| 78 | # PHP Create a MySQL Database. |
| 79 | curl -o "${OUTPUT}/create-db" "http://localhost/create-db.php" |
| 80 | grep "Database created successfully" "${OUTPUT}/create-db" |
| 81 | check_return "php-create-db" |
| 82 | |
| 83 | # PHP Create MySQL table. |
| 84 | curl -o "${OUTPUT}/create-table" "http://localhost/create-table.php" |
| 85 | grep "Table MyGuests created successfully" "${OUTPUT}/create-table" |
| 86 | check_return "php-create-table" |
| 87 | |
| 88 | # PHP add record to MySQL table. |
| 89 | curl -o "${OUTPUT}/add-record" "http://localhost/add-record.php" |
| 90 | grep "New record created successfully" "${OUTPUT}/add-record" |
| 91 | check_return "php-add-record" |
| 92 | |
| 93 | # PHP select record from MySQL table. |
| 94 | curl -o "${OUTPUT}/select-record" "http://localhost/select-record.php" |
| 95 | grep "id: 1 - Name: John Doe" "${OUTPUT}/select-record" |
| 96 | check_return "php-select-record" |
| 97 | |
| 98 | # PHP delete record from MySQL table. |
| 99 | curl -o "${OUTPUT}/delete-record" "http://localhost/delete-record.php" |
| 100 | grep "Record deleted successfully" "${OUTPUT}/delete-record" |
| 101 | check_return "php-delete-record" |
Chase Qi | 3250835 | 2016-11-23 19:22:52 +0800 | [diff] [blame^] | 102 | |
| 103 | # Delete myDB for the next run. |
| 104 | mysql --user='root' --password='lamptest' -e 'DROP DATABASE myDB' |