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