Created
August 24, 2017 00:29
-
-
Save lmakarov/abb44468221e5b4369fc3bbe5afdacf3 to your computer and use it in GitHub Desktop.
Bats tests alternative with pure bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Abort on errors | |
#set -e | |
# Echo commands | |
#set -x | |
#-------------------------- Helper functions -------------------------------- | |
# Console colors | |
red='\033[0;31m' | |
green='\033[0;32m' | |
green_bg='\033[42m' | |
yellow='\033[1;33m' | |
NC='\033[0m' | |
echo-red () { echo -e "${red}$1${NC}"; } | |
echo-green () { echo -e "${green}$1${NC}"; } | |
echo-green-bg () { echo -e "${green_bg}$1${NC}"; } | |
echo-yellow () { echo -e "${yellow}$1${NC}"; } | |
run_test () | |
{ | |
bash -c "$1"; ret=$? | |
[[ "$ret" == "0" ]] && echo-green "Passed: ${NC}$1" || echo-red "Failed: ${NC}$1" | |
} | |
#-------------------------- Tests -------------------------------- | |
# Test basic HTTP/HTTPS requests | |
test_bare_server () | |
{ | |
echo-green "Testing bare server HTTP/HTTPS..." | |
fin docker rm -vf "$NAME" >/dev/null 2>&1 || true | |
fin docker run --name "$NAME" -d -p 2580:80 -p 25443:443 \ | |
"$IMAGE" >/dev/null | |
# TODO: replace with a healthcheck | |
sleep 2 | |
run_test "curl -sSk -I http://test.docksal:2580 | grep \"HTTP/1.1 200 OK\"" | |
run_test "curl -sSk https://test.docksal:25443 | grep \"It works!\"" | |
run_test "curl -sSk -I https://test.docksal:25443 | grep \"HTTP/1.1 200 OK\"" | |
run_test "curl -sSk https://test.docksal:25443 | grep \"It works!\"" | |
run_test "curl -sSk -I http://test.docksal:2580/nonsense | grep \"HTTP/1.1 404 Not Found\"" | |
fin docker rm -vf "$NAME" >/dev/null 2>&1 || true | |
} | |
# Test codebase mounts properly | |
test_docroot_mount () | |
{ | |
echo-green "Testing docroot mount..." | |
fin docker rm -vf "$NAME" >/dev/null 2>&1 || true | |
fin docker run --name "$NAME" -d -p 2580:80 -p 25443:443 \ | |
-v $(pwd)/../tests/docroot:/var/www/docroot \ | |
"$IMAGE" >/dev/null | |
# TODO: replace with a healthcheck | |
sleep 2 | |
run_test "curl -sSk -I http://test.docksal:2580 | grep \"HTTP/1.1 200 OK\"" | |
run_test "curl -sSk http://test.docksal:2580 | grep \"index.html\"" | |
fin docker rm -vf "$NAME" >/dev/null 2>&1 || true | |
} | |
# Test Apache DocumentRoot can be overridden | |
test_docroot_path_override () | |
{ | |
echo-green "Testing docroot path override..." | |
fin docker rm -vf "$NAME" >/dev/null 2>&1 || true | |
fin docker run --name "$NAME" -d -p 2580:80 -p 25443:443 \ | |
-v $(pwd)/../tests/docroot:/var/www/html \ | |
-e APACHE_DOCUMENTROOT=/var/www/html \ | |
"$IMAGE" >/dev/null | |
# TODO: replace with a healthcheck | |
sleep 2 | |
run_test "curl -sSk -I http://test.docksal:2580 | grep \"HTTP/1.1 200 OK\"" | |
run_test "curl -sSk http://test.docksal:2580 | grep \"index.html\"" | |
fin docker rm -vf "$NAME" >/dev/null 2>&1 || true | |
} | |
# Test Basic HTTP Auth is working | |
test_basic_http_auth () | |
{ | |
echo-green "Testing docroot path override..." | |
fin docker rm -vf "$NAME" >/dev/null 2>&1 || true | |
fin docker run --name "$NAME" -d -p 2580:80 -p 25443:443 \ | |
-v $(pwd)/../tests/docroot:/var/www/docroot \ | |
-e APACHE_BASIC_AUTH_USER=user \ | |
-e APACHE_BASIC_AUTH_PASS=pass \ | |
"$IMAGE" >/dev/null | |
# TODO: replace with a healthcheck | |
sleep 2 | |
# Check authorization is required | |
run_test "curl -sSk -I http://test.docksal:2580 | grep \"HTTP/1.1 401 Authorization Required\"" | |
# Check we can pass authorization | |
run_test "curl -sSk -I -u user:pass http://test.docksal:2580 | grep \"HTTP/1.1 200 OK\"" | |
fin docker rm -vf "$NAME" >/dev/null 2>&1 || true | |
} | |
# Test configuration overrides | |
test_config_overrides () | |
{ | |
echo-green "Testing docroot path override..." | |
fin docker rm -vf "$NAME" >/dev/null 2>&1 || true | |
fin docker run --name "$NAME" -d -p 2580:80 -p 25443:443 \ | |
-v $(pwd)/../tests/docroot:/var/www/docroot \ | |
-v $(pwd)/../tests/config:/var/www/.docksal/etc/apache \ | |
"$IMAGE" >/dev/null | |
# TODO: replace with a healthcheck | |
sleep 2 | |
# Test default virtual host config overrides | |
run_test "curl -sSk -I http://test.docksal:2580 | grep \"HTTP/1.1 200 OK\"" | |
run_test "curl -sSk http://test.docksal:2580 | grep \"index2.html\"" | |
# Test extra virtual hosts config | |
run_test "curl -sSk -I http://docs.test.docksal:2580 | grep \"HTTP/1.1 302 FOUND\"" | |
run_test "curl -sSk -L http://docs.test.docksal:2580 | grep \"Docksal Documentation\"" | |
fin docker rm -vf "$NAME" >/dev/null 2>&1 || true | |
} | |
#-------------------------- Execution -------------------------------- | |
test_bare_server | |
test_docroot_mount | |
test_docroot_path_override | |
test_basic_http_auth | |
test_config_overrides |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment