Skip to content

Commit

Permalink
Add regression tests for MySQL authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
cytopia committed Feb 11, 2019
1 parent 6f454ad commit 8fd006e
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 14 deletions.
42 changes: 28 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ build-mysql-8.0:
docker tag mysql:8.0 $(IMAGE):mysql-8.0

test-mysql-5.5:
docker run --rm -it $(IMAGE):mysql-5.5 -V | grep 'MySQL' | grep '5\.5'
./tests/01-version.sh "mysql" "5.5"
./tests/02-mysqli.sh "mysql" "5.5"
test-mysql-5.6:
docker run --rm -it $(IMAGE):mysql-5.6 -V | grep 'MySQL' | grep '5\.6'
./tests/01-version.sh "mysql" "5.6"
./tests/02-mysqli.sh "mysql" "5.6"
test-mysql-5.7:
docker run --rm -it $(IMAGE):mysql-5.7 -V | grep 'MySQL' | grep '5\.7'
./tests/01-version.sh "mysql" "5.7"
./tests/02-mysqli.sh "mysql" "5.7"
test-mysql-8.0:
docker run --rm -it $(IMAGE):mysql-8.0 -V | grep 'MySQL' | grep '8\.0'
./tests/01-version.sh "mysql" "8.0"
./tests/02-mysqli.sh "mysql" "8.0"


# -------------------------------------------------------------------------------------------------
Expand All @@ -70,17 +74,23 @@ build-mariadb-10.4:
docker tag mariadb:10.4 $(IMAGE):mariadb-10.4

test-mariadb-5.5:
docker run --rm -it $(IMAGE):mariadb-5.5 -V | grep 'MariaDB' | grep '5\.5'
./tests/01-version.sh "mariadb" "5.5"
./tests/02-mysqli.sh "mariadb" "5.5"
test-mariadb-10.0:
docker run --rm -it $(IMAGE):mariadb-10.0 -V | grep 'MariaDB' | grep '10\.0'
./tests/01-version.sh "mariadb" "10.0"
./tests/02-mysqli.sh "mariadb" "10.0"
test-mariadb-10.1:
docker run --rm -it $(IMAGE):mariadb-10.1 -V | grep 'MariaDB' | grep '10\.1'
./tests/01-version.sh "mariadb" "10.1"
./tests/02-mysqli.sh "mariadb" "10.1"
test-mariadb-10.2:
docker run --rm -it $(IMAGE):mariadb-10.2 -V | grep 'MariaDB' | grep '10\.2'
./tests/01-version.sh "mariadb" "10.2"
./tests/02-mysqli.sh "mariadb" "10.2"
test-mariadb-10.3:
docker run --rm -it $(IMAGE):mariadb-10.3 -V | grep 'MariaDB' | grep '10\.3'
./tests/01-version.sh "mariadb" "10.3"
./tests/02-mysqli.sh "mariadb" "10.3"
test-mariadb-10.4:
docker run --rm -it $(IMAGE):mariadb-10.4 -V | grep 'MariaDB' | grep '10\.4'
./tests/01-version.sh "mariadb" "10.4"
./tests/02-mysqli.sh "mariadb" "10.4"


# -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -109,10 +119,14 @@ build-percona-8.0:
docker tag percona:8.0 $(IMAGE):percona-8.0

test-percona-5.5:
docker run --rm -it $(IMAGE):percona-5.5 -V | grep 'Percona' | grep '5\.5'
./tests/01-version.sh "percona" "5.5"
./tests/02-mysqli.sh "percona" "5.5"
test-percona-5.6:
docker run --rm -it $(IMAGE):percona-5.6 -V | grep 'Percona' | grep '5\.6'
./tests/01-version.sh "percona" "5.6"
./tests/02-mysqli.sh "percona" "5.6"
test-percona-5.7:
docker run --rm -it $(IMAGE):percona-5.7 -V | grep 'Percona' | grep '5\.7'
./tests/01-version.sh "percona" "5.7"
./tests/02-mysqli.sh "percona" "5.7"
test-percona-8.0:
docker run --rm -it $(IMAGE):percona-8.0 -V | grep 'Percona' | grep '8\.0'
./tests/01-version.sh "percona" "8.0"
./tests/02-mysqli.sh "percona" "8.0"
25 changes: 25 additions & 0 deletions tests/01-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

echo "# --------------------------------------------------------------------------------------------------"
echo "# Testing MySQL server version"
echo "# --------------------------------------------------------------------------------------------------"

set -e
set -u
set -o pipefail

IMAGE="devilbox/mysql"
TYPE="${1}"
VERSION="${2}"


if [ "${TYPE}" = "mysql" ]; then
docker run --rm -it "${IMAGE}:${TYPE}-${VERSION}" -V | grep 'MySQL' | grep "${VERSION/./\\.}"
elif [ "${TYPE}" = "mariadb" ]; then
docker run --rm -it "${IMAGE}:${TYPE}-${VERSION}" -V | grep 'MariaDB' | grep "${VERSION/./\\.}"
elif [ "${TYPE}" = "percona" ]; then
docker run --rm -it "${IMAGE}:${TYPE}-${VERSION}" -V | grep 'Percona' | grep "${VERSION/./\\.}"
else
>&2 echo "[ERROR] Wrong type: ${TYPE}"
exit 1
fi
57 changes: 57 additions & 0 deletions tests/02-mysqli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash

echo "# --------------------------------------------------------------------------------------------------"
echo "# Testing mysqli connectivity to MySQL"
echo "# --------------------------------------------------------------------------------------------------"

set -e
set -u
set -o pipefail

IMAGE="devilbox/mysql"
TYPE="${1}"
VERSION="${2}"

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"

# Start MySQL
docker run \
-d \
-it \
--rm \
--hostname=mysql \
--name devilbox-test-mysql \
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
"${IMAGE}:${TYPE}-${VERSION}"

# Start PHP 7.2
docker run \
-d \
-it \
--rm \
--hostname=php \
--entrypoint=bash \
--name devilbox-test-php \
--volume="${SCRIPTPATH}:/tmp" \
--link devilbox-test-mysql php:7.2

# Install PHP mysqli module
docker exec -it devilbox-test-php docker-php-ext-install mysqli

# Test MySQL connectivity
max=100
i=0
while ! docker exec -it devilbox-test-php php /tmp/mysql.php >/dev/null 2>&1; do
sleep 1
i=$(( i + 1))
if [ "${i}" -ge "${max}" ]; then
docker stop devilbox-test-php || true
docker stop devilbox-test-mysql || true
>&2 echo "Failed"
exit 1
fi
done

docker stop devilbox-test-php || true
docker stop devilbox-test-mysql || true
echo "Success"
40 changes: 40 additions & 0 deletions tests/mysql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);


$MY_HOST = 'mysql';
$MY_USER = 'root';
$MY_PASS = '';

$link = mysqli_connect($MY_HOST, $MY_USER, $MY_PASS, 'mysql');

if (mysqli_connect_errno()) {
echo 'FAIL';
exit(1);
}

$query = "SELECT * FROM `user` WHERE `User` = 'root';";
if (!$result = mysqli_query($link, $query)) {
echo 'FAIL';
exit(1);
}

while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$data[] = $row;
}
mysqli_free_result($result);
mysqli_close($link);

if (!isset($data[0])) {
echo 'FAIL';
exit(1);
}
if ($data[0]['User'] == 'root') {
echo 'OK';
exit(0);
} else {
echo 'FAIL';
exit(1);
}

0 comments on commit 8fd006e

Please sign in to comment.