From eeb48e3df41a9b13204a3886eea2d1d709629bb8 Mon Sep 17 00:00:00 2001 From: Chase Qi Date: Wed, 9 Nov 2016 19:10:24 +0800 Subject: automated: added Linux LEMP test Change-Id: I48d54b6c4c89e7a5c4d0bac0b30a1b48b26e3ca0 Signed-off-by: Chase Qi --- automated/linux/lemp/centos-nginx.conf | 10 ++ automated/linux/lemp/debian-nginx.conf | 17 ++++ automated/linux/lemp/html/add-record.php | 24 +++++ automated/linux/lemp/html/connect-db.php | 14 +++ automated/linux/lemp/html/create-db.php | 22 ++++ automated/linux/lemp/html/create-table.php | 30 ++++++ automated/linux/lemp/html/delete-record.php | 24 +++++ automated/linux/lemp/html/index.html | 11 ++ automated/linux/lemp/html/info.php | 1 + automated/linux/lemp/html/select-record.php | 26 +++++ automated/linux/lemp/lemp.sh | 153 ++++++++++++++++++++++++++++ automated/linux/lemp/lemp.yaml | 26 +++++ 12 files changed, 358 insertions(+) create mode 100644 automated/linux/lemp/centos-nginx.conf create mode 100644 automated/linux/lemp/debian-nginx.conf create mode 100644 automated/linux/lemp/html/add-record.php create mode 100644 automated/linux/lemp/html/connect-db.php create mode 100644 automated/linux/lemp/html/create-db.php create mode 100644 automated/linux/lemp/html/create-table.php create mode 100644 automated/linux/lemp/html/delete-record.php create mode 100644 automated/linux/lemp/html/index.html create mode 100644 automated/linux/lemp/html/info.php create mode 100644 automated/linux/lemp/html/select-record.php create mode 100755 automated/linux/lemp/lemp.sh create mode 100644 automated/linux/lemp/lemp.yaml (limited to 'automated') diff --git a/automated/linux/lemp/centos-nginx.conf b/automated/linux/lemp/centos-nginx.conf new file mode 100644 index 0000000..ce9a4ae --- /dev/null +++ b/automated/linux/lemp/centos-nginx.conf @@ -0,0 +1,10 @@ +index index.php index.html index.htm; +server_name localhost; +# pass the PHP scripts to FastCGI server listening on the php-fpm socket +location ~ \.php$ { + try_files $uri =404; + fastcgi_pass unix:/run/php-fpm/php-fpm.sock; + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; +} diff --git a/automated/linux/lemp/debian-nginx.conf b/automated/linux/lemp/debian-nginx.conf new file mode 100644 index 0000000..3e3e3e7 --- /dev/null +++ b/automated/linux/lemp/debian-nginx.conf @@ -0,0 +1,17 @@ +server { + listen 80 default_server; + listen [::]:80 default_server; + + root /usr/share/nginx/html; + index index.php index.html index.htm; + server_name localhost; + + location / { + try_files $uri $uri/ =404; + } + + location ~ \.php$ { + include snippets/fastcgi-php.conf; + fastcgi_pass unix:/var/run/php5-fpm.sock; + } +} diff --git a/automated/linux/lemp/html/add-record.php b/automated/linux/lemp/html/add-record.php new file mode 100644 index 0000000..955652a --- /dev/null +++ b/automated/linux/lemp/html/add-record.php @@ -0,0 +1,24 @@ +" . mysqli_error($conn); +} + +mysqli_close($conn); +?> diff --git a/automated/linux/lemp/html/connect-db.php b/automated/linux/lemp/html/connect-db.php new file mode 100644 index 0000000..5bcdeed --- /dev/null +++ b/automated/linux/lemp/html/connect-db.php @@ -0,0 +1,14 @@ +connect_error) { + die("Connection failed: " . $conn->connect_error); +} +echo "Connected successfully"; +?> diff --git a/automated/linux/lemp/html/create-db.php b/automated/linux/lemp/html/create-db.php new file mode 100644 index 0000000..e6bf219 --- /dev/null +++ b/automated/linux/lemp/html/create-db.php @@ -0,0 +1,22 @@ +connect_error) { + die("Connection failed: " . $conn->connect_error); +} + +// Create database +$sql = "CREATE DATABASE myDB"; +if ($conn->query($sql) === TRUE) { + echo "Database created successfully"; +} else { + echo "Error creating database: " . $conn->error; +} + +$conn->close(); +?> diff --git a/automated/linux/lemp/html/create-table.php b/automated/linux/lemp/html/create-table.php new file mode 100644 index 0000000..cf60a50 --- /dev/null +++ b/automated/linux/lemp/html/create-table.php @@ -0,0 +1,30 @@ +connect_error) { + die("Connection failed: " . $conn->connect_error); +} + +// sql to create table +$sql = "CREATE TABLE MyGuests ( +id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, +firstname VARCHAR(30) NOT NULL, +lastname VARCHAR(30) NOT NULL, +email VARCHAR(50), +reg_date TIMESTAMP +)"; + +if ($conn->query($sql) === TRUE) { + echo "Table MyGuests created successfully"; +} else { + echo "Error creating table: " . $conn->error; +} + +$conn->close(); +?> diff --git a/automated/linux/lemp/html/delete-record.php b/automated/linux/lemp/html/delete-record.php new file mode 100644 index 0000000..d011f4e --- /dev/null +++ b/automated/linux/lemp/html/delete-record.php @@ -0,0 +1,24 @@ +connect_error) { + die("Connection failed: " . $conn->connect_error); +} + +// sql to delete a record +$sql = "DELETE FROM MyGuests WHERE id=6"; + +if ($conn->query($sql) === TRUE) { + echo "Record deleted successfully"; +} else { + echo "Error deleting record: " . $conn->error; +} + +$conn->close(); +?> diff --git a/automated/linux/lemp/html/index.html b/automated/linux/lemp/html/index.html new file mode 100644 index 0000000..1b1b31b --- /dev/null +++ b/automated/linux/lemp/html/index.html @@ -0,0 +1,11 @@ + + + + + + + +

Test Page for the Nginx HTTP Server

+ + + diff --git a/automated/linux/lemp/html/info.php b/automated/linux/lemp/html/info.php new file mode 100644 index 0000000..147cebc --- /dev/null +++ b/automated/linux/lemp/html/info.php @@ -0,0 +1 @@ + diff --git a/automated/linux/lemp/html/select-record.php b/automated/linux/lemp/html/select-record.php new file mode 100644 index 0000000..d67dc6e --- /dev/null +++ b/automated/linux/lemp/html/select-record.php @@ -0,0 +1,26 @@ +connect_error) { + die("Connection failed: " . $conn->connect_error); +} + +$sql = "SELECT id, firstname, lastname FROM MyGuests"; +$result = $conn->query($sql); + +if ($result->num_rows > 0) { + // output data of each row + while($row = $result->fetch_assoc()) { + echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; + } +} else { + echo "0 results"; +} +$conn->close(); +?> diff --git a/automated/linux/lemp/lemp.sh b/automated/linux/lemp/lemp.sh new file mode 100755 index 0000000..e21dbc6 --- /dev/null +++ b/automated/linux/lemp/lemp.sh @@ -0,0 +1,153 @@ +#!/bin/sh + +# shellcheck disable=SC1091 +. ../../lib/sh-test-lib +OUTPUT="$(pwd)/output" +RESULT_FILE="${OUTPUT}/result.txt" +export RESULT_FILE +TEST_LIST="test-nginx-server mysql-show-databases test-phpinfo + php-connect-db php-create-db php-create-table php-add-record + php-select-record php-delete-record" + +! check_root && error_msg "This script must be run as root" +[ -d "${OUTPUT}" ] && mv "${OUTPUT}" "${OUTPUT}_$(date +%Y%m%d%H%M%S)" +mkdir -p "${OUTPUT}" + +dist_name +# Install and configure LEMP. +# systemctl available on Debian 8, CentOS 7 and newer releases. +# shellcheck disable=SC2154 +case "${dist}" in + Debian) + pkgs="nginx mysql-server php5-mysql php5-fpm curl" + install_deps "${pkgs}" + + # Stop apache server in case it is installed and running. + systemctl stop apache2 > /dev/null 2>&1 || true + + systemctl restart nginx + systemctl restart mysql + + # Configure PHP. + cp /etc/php5/fpm/php.ini /etc/php5/fpm/php.ini.bak + sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php5/fpm/php.ini + systemctl restart php5-fpm + + # Configure NGINX for PHP. + mv -f /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak + cp ./debian-nginx.conf /etc/nginx/sites-available/default + systemctl restart nginx + ;; + CentOS) + # x86_64 nginx package can be installed from epel repo. However, epel + # project doesn't support ARM arch yet. RPB repo should provide nginx. + [ "$(uname -m)" = "x86_64" ] && install_deps "epel-release" + pkgs="nginx mariadb-server mariadb php php-mysql php-fpm curl" + install_deps "${pkgs}" + + # Stop apache server in case it is installed and running. + systemctl stop httpd.service > /dev/null 2>&1 || true + + systemctl restart nginx + systemctl restart mariadb + + # Configure PHP. + cp /etc/php.ini /etc/php.ini.bak + sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php.ini + sed -i "s/listen.allowed_clients = 127.0.0.1/listen = \/run\/php-fpm\/php-fpm.sock/" /etc/php-fpm.d/www.conf + sed -i "s/;listen.owner = nobody/listen.owner = nginx/" /etc/php-fpm.d/www.conf + sed -i "s/;listen.group = nobody/listen.group = nginx/" /etc/php-fpm.d/www.conf + sed -i "s/user = apache/user = nginx/" /etc/php-fpm.d/www.conf + sed -i "s/group = apache/group = nginx/" /etc/php-fpm.d/www.conf + # This creates the needed php-fpm.sock file + systemctl restart php-fpm + chmod 666 /run/php-fpm/php-fpm.sock + chown nginx:nginx /run/php-fpm/php-fpm.sock + systemctl restart php-fpm + + # Configure NGINX for PHP. + cp ./centos-nginx.conf /etc/nginx/default.d/default.conf + systemctl restart nginx + ;; + *) + info_msg "Supported distributions: Debian, CentOS" + error_msg "Unsupported distribution: ${dist}" + ;; +esac + +# Copy pre-defined html/php files to root directory. +mv -f /usr/share/nginx/html /usr/share/nginx/html.bak +mkdir -p /usr/share/nginx/html +cp ./html/* /usr/share/nginx/html/ + +# Test Nginx. +skip_list="$(echo "${TEST_LIST}" | awk '{ for (i=2; i<=NF; i++) print $i}')" +curl -o "${OUTPUT}/index.html" "http://localhost/index.html" +test_command="grep 'Test Page for the Nginx HTTP Server' ${OUTPUT}/index.html" +run_test_case "${test_command}" "test-nginx-server" "${skip_list}" + +# Test MySQL. +skip_list="$(echo "${skip_list}" | awk '{ for (i=2; i<=NF; i++) print $i}')" +mysqladmin -u root password lemptest > /dev/null 2>&1 || true +test_command="mysql --user='root' --password='lemptest' -e 'show databases'" +run_test_case "${test_command}" "mysql-show-databases" "${skip_list}" + +# Test PHP. +skip_list="$(echo "${skip_list}" | awk '{ for (i=2; i<=NF; i++) print $i}')" +curl -o "${OUTPUT}/phpinfo.html" "http://localhost/info.php" +test_command="grep 'PHP Version' ${OUTPUT}/phpinfo.html" +run_test_case "${test_command}" "test-phpinfo" "${skip_list}" + +# PHP Connect to MySQL. +skip_list="$(echo "${skip_list}" | awk '{ for (i=2; i<=NF; i++) print $i}')" +curl -o "${OUTPUT}/connect-db" "http://localhost/connect-db.php" +test_command="grep 'Connected successfully' ${OUTPUT}/connect-db" +run_test_case "${test_command}" "php-connect-db" "${skip_list}" + +# PHP Create a MySQL Database. +skip_list="$(echo "${skip_list}" | awk '{ for (i=2; i<=NF; i++) print $i}')" +curl -o "${OUTPUT}/create-db" "http://localhost/create-db.php" +test_command="grep 'Database created successfully' ${OUTPUT}/create-db" +run_test_case "${test_command}" "php-create-db" "${skip_list}" + +# PHP Create MySQL table. +skip_list="$(echo "${skip_list}" | awk '{ for (i=2; i<=NF; i++) print $i}')" +curl -o "${OUTPUT}/create-table" "http://localhost/create-table.php" +test_command="grep 'Table MyGuests created successfully' ${OUTPUT}/create-table" +run_test_case "${test_command}" "php-create-table" "${skip_list}" + +# PHP add record to MySQL table. +skip_list="$(echo "${skip_list}" | awk '{ for (i=2; i<=NF; i++) print $i}')" +curl -o "${OUTPUT}/add-record" "http://localhost/add-record.php" +test_command="grep 'New record created successfully' ${OUTPUT}/add-record" +run_test_case "${test_command}" "php-create-recoard" "${skip_list}" + +# PHP select record from MySQL table. +skip_list="$(echo "${skip_list}" | awk '{ for (i=2; i<=NF; i++) print $i}')" +curl -o "${OUTPUT}/select-record" "http://localhost/select-record.php" +test_command="grep 'id: 1 - Name: John Doe' ${OUTPUT}/select-record" +run_test_case "${test_command}" "php-select-record" "${skip_list}" + +# PHP delete record from MySQL table. +curl -o "${OUTPUT}/delete-record" "http://localhost/delete-record.php" +test_command="grep 'Record deleted successfully' ${OUTPUT}/delete-record" +run_test_case "${test_command}" "php-delete-record" + +# Cleanup. +# Delete myDB for the next run. +mysql --user='root' --password='lemptest' -e 'DROP DATABASE myDB' + +# Restore from backups. +rm -rf /usr/share/nginx/html +mv /usr/share/nginx/html.bak /usr/share/nginx/html +# shellcheck disable=SC2154 +case "${dist}" in + Debian) + mv -f /etc/php5/fpm/php.ini.bak /etc/php5/fpm/php.ini + mv -f /etc/nginx/sites-available/default.bak /etc/nginx/sites-available/default + ;; + CentOS) + mv -f /etc/php.ini.bak /etc/php.ini + rm -f /etc/nginx/default.d/default.conf + ;; +esac diff --git a/automated/linux/lemp/lemp.yaml b/automated/linux/lemp/lemp.yaml new file mode 100644 index 0000000..199ce02 --- /dev/null +++ b/automated/linux/lemp/lemp.yaml @@ -0,0 +1,26 @@ +metadata: + format: Lava-Test Test Definition 1.0 + name: lemp + description: "LEMP stack is a group of open source software to get web + server up and running. The acronym stands for Linux, nginx, + MySQL and PHP." + maintainer: + - chase.qi@linaro.org + os: + - debian + - centos + scope: + - functional + devices: + - mustang + - overdrive + - moonshot + - thunderX + - d03 + - d05 + +run: + steps: + - cd ./automated/linux/lemp/ + - ./lemp.sh + - ../../utils/send-to-lava.sh ./output/result.txt -- cgit v1.2.3