blob: fc995c075b4097508a35e4e46c0f14c4017f8780 [file] [log] [blame]
Chase Qi4a4caa32016-09-06 17:22:46 +08001#!/bin/sh
2
Chase Qi32508352016-11-23 19:22:52 +08003# shellcheck disable=SC1091
Chase Qi4a4caa32016-09-06 17:22:46 +08004. ../../lib/sh-test-lib
5OUTPUT="$(pwd)/output"
6RESULT_FILE="${OUTPUT}/result.txt"
Chase Qi32508352016-11-23 19:22:52 +08007export RESULT_FILE
Chase Qi4a4caa32016-09-06 17:22:46 +08008
9usage() {
10 echo "Usage: $0 [-s <true|false>]" 1>&2
11 exit 1
12}
13
14while getopts "s:" o; do
15 case "$o" in
16 s) SKIP_INSTALL="${OPTARG}" ;;
17 *) usage ;;
18 esac
19done
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)"
23mkdir -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.
28if [ "${SKIP_INSTALL}" = "True" ] || [ "${SKIP_INSTALL}" = "true" ]; then
29 warn_msg "LAMP package installation skipped"
30else
31 dist_name
Chase Qi32508352016-11-23 19:22:52 +080032 # shellcheck disable=SC2154
Chase Qi4a4caa32016-09-06 17:22:46 +080033 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
54fi
55
56cp ./html/* /var/www/html/
57
58# Test Apache.
Milosz Wasilewski9a975ba2016-10-20 20:55:05 +010059curl -o "${OUTPUT}/index.html" "http://localhost/index.html"
60grep "Test Page for the Apache HTTP Server" "${OUTPUT}/index.html"
Chase Qi4a4caa32016-09-06 17:22:46 +080061check_return "apache2-test-page"
62
63# Test MySQL.
Chase Qi32508352016-11-23 19:22:52 +080064mysqladmin -u root password lamptest > /dev/null 2>&1 || true
Chase Qi4a4caa32016-09-06 17:22:46 +080065mysql --user="root" --password="lamptest" -e "show databases"
66check_return "mysql-show-databases"
67
68# Test PHP.
69curl -o "${OUTPUT}/phpinfo.html" "http://localhost/info.php"
70grep "PHP Version" "${OUTPUT}/phpinfo.html"
71check_return "phpinfo"
72
73# PHP Connect to MySQL.
74curl -o "${OUTPUT}/connect-db" "http://localhost/connect-db.php"
75grep "Connected successfully" "${OUTPUT}/connect-db"
76exit_on_fail "php-connect-db"
77
78# PHP Create a MySQL Database.
79curl -o "${OUTPUT}/create-db" "http://localhost/create-db.php"
80grep "Database created successfully" "${OUTPUT}/create-db"
81check_return "php-create-db"
82
83# PHP Create MySQL table.
84curl -o "${OUTPUT}/create-table" "http://localhost/create-table.php"
85grep "Table MyGuests created successfully" "${OUTPUT}/create-table"
86check_return "php-create-table"
87
88# PHP add record to MySQL table.
89curl -o "${OUTPUT}/add-record" "http://localhost/add-record.php"
90grep "New record created successfully" "${OUTPUT}/add-record"
91check_return "php-add-record"
92
93# PHP select record from MySQL table.
94curl -o "${OUTPUT}/select-record" "http://localhost/select-record.php"
95grep "id: 1 - Name: John Doe" "${OUTPUT}/select-record"
96check_return "php-select-record"
97
98# PHP delete record from MySQL table.
99curl -o "${OUTPUT}/delete-record" "http://localhost/delete-record.php"
100grep "Record deleted successfully" "${OUTPUT}/delete-record"
101check_return "php-delete-record"
Chase Qi32508352016-11-23 19:22:52 +0800102
103# Delete myDB for the next run.
104mysql --user='root' --password='lamptest' -e 'DROP DATABASE myDB'