blob: c25b1c1d6ca2de4f841b315a5ec0503dcf617ea2 [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
Chase Qidb0522c2016-11-28 12:00:11 +080031 # Stop nginx server in case it is installed and running.
32 systemctl stop nginx > /dev/null 2>&1 || true
33
Chase Qi4a4caa32016-09-06 17:22:46 +080034 dist_name
Chase Qi32508352016-11-23 19:22:52 +080035 # shellcheck disable=SC2154
Chase Qi4a4caa32016-09-06 17:22:46 +080036 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
57fi
58
59cp ./html/* /var/www/html/
60
61# Test Apache.
Milosz Wasilewski9a975ba2016-10-20 20:55:05 +010062curl -o "${OUTPUT}/index.html" "http://localhost/index.html"
63grep "Test Page for the Apache HTTP Server" "${OUTPUT}/index.html"
Chase Qi4a4caa32016-09-06 17:22:46 +080064check_return "apache2-test-page"
65
66# Test MySQL.
Chase Qia64e9be2016-11-28 12:25:57 +080067mysqladmin -u root password lxmptest > /dev/null 2>&1 || true
68mysql --user="root" --password="lxmptest" -e "show databases"
Chase Qi4a4caa32016-09-06 17:22:46 +080069check_return "mysql-show-databases"
70
71# Test PHP.
72curl -o "${OUTPUT}/phpinfo.html" "http://localhost/info.php"
73grep "PHP Version" "${OUTPUT}/phpinfo.html"
74check_return "phpinfo"
75
76# PHP Connect to MySQL.
77curl -o "${OUTPUT}/connect-db" "http://localhost/connect-db.php"
78grep "Connected successfully" "${OUTPUT}/connect-db"
79exit_on_fail "php-connect-db"
80
81# PHP Create a MySQL Database.
82curl -o "${OUTPUT}/create-db" "http://localhost/create-db.php"
83grep "Database created successfully" "${OUTPUT}/create-db"
84check_return "php-create-db"
85
86# PHP Create MySQL table.
87curl -o "${OUTPUT}/create-table" "http://localhost/create-table.php"
88grep "Table MyGuests created successfully" "${OUTPUT}/create-table"
89check_return "php-create-table"
90
91# PHP add record to MySQL table.
92curl -o "${OUTPUT}/add-record" "http://localhost/add-record.php"
93grep "New record created successfully" "${OUTPUT}/add-record"
94check_return "php-add-record"
95
96# PHP select record from MySQL table.
97curl -o "${OUTPUT}/select-record" "http://localhost/select-record.php"
98grep "id: 1 - Name: John Doe" "${OUTPUT}/select-record"
99check_return "php-select-record"
100
101# PHP delete record from MySQL table.
102curl -o "${OUTPUT}/delete-record" "http://localhost/delete-record.php"
103grep "Record deleted successfully" "${OUTPUT}/delete-record"
104check_return "php-delete-record"
Chase Qi32508352016-11-23 19:22:52 +0800105
106# Delete myDB for the next run.
Chase Qia64e9be2016-11-28 12:25:57 +0800107mysql --user='root' --password='lxmptest' -e 'DROP DATABASE myDB'