Skip to content

Commit aa0ec2c

Browse files
committed
Introduce tests with testinfra tool
1 parent 68f22bd commit aa0ec2c

File tree

10 files changed

+171
-3
lines changed

10 files changed

+171
-3
lines changed

.circleci/config.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
version: 2
2+
3+
default_workflow_jobs: &default_workflow_jobs
4+
- lint
5+
- test-latest:
6+
requires:
7+
- lint
8+
- test-7.1:
9+
requires:
10+
- lint
11+
- test-7.2:
12+
requires:
13+
- lint
14+
- test-7.3:
15+
requires:
16+
- lint
17+
18+
workflows:
19+
version: 2
20+
lint-test:
21+
jobs: *default_workflow_jobs
22+
23+
jobs:
24+
lint:
25+
machine: true
26+
steps:
27+
- checkout
28+
- run: make lint
29+
test-latest:
30+
machine: true
31+
steps:
32+
- checkout
33+
- run: make test-image IMAGE="php:fpm-alpine"
34+
35+
test-7.1:
36+
machine: true
37+
steps:
38+
- checkout
39+
- run: make test-image IMAGE="php:7.1-fpm-alpine3.7"
40+
- run: make test-image IMAGE="php:7.1-fpm-alpine3.8"
41+
42+
test-7.2:
43+
machine: true
44+
steps:
45+
- checkout
46+
- run: make test-image IMAGE="php:7.2-fpm-alpine3.7"
47+
- run: make test-image IMAGE="php:7.2-fpm-alpine3.8"
48+
49+
test-7.3:
50+
machine: true
51+
steps:
52+
- checkout
53+
- run: make test-image IMAGE="php:7.3-rc-fpm-alpine3.8"

Makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ current_dir := $(abspath $(patsubst %/,%,$(dir $(mkfile_path))))
55

66
.PHONY: *
77

8-
lint:
9-
docker run --rm -v ${current_dir}:/mnt:ro koalaman/shellcheck ./php-fpm-healthcheck
8+
lint: php-fpm-healthcheck
9+
docker run --rm -v ${current_dir}:/mnt:ro koalaman/shellcheck ./php-fpm-healthcheck ./test/*.sh
1010

1111
test:
12-
test -f php-fpm-healthcheck
12+
$(MAKE) test-image IMAGE="php:fpm-alpine"
13+
$(MAKE) test-image IMAGE="php:7.1-fpm-alpine3.7"
14+
$(MAKE) test-image IMAGE="php:7.1-fpm-alpine3.8"
15+
$(MAKE) test-image IMAGE="php:7.2-fpm-alpine3.7"
16+
$(MAKE) test-image IMAGE="php:7.2-fpm-alpine3.8"
17+
$(MAKE) test-image IMAGE="php:7.3-rc-fpm-alpine3.8"
18+
19+
test-image:
20+
./test/docker.sh ${IMAGE}

php-fpm-healthcheck

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,14 @@ check_fpm_health() {
103103
done
104104
}
105105

106+
set +e
107+
106108
GETOPT=$(getopt -o v --long verbose,accepted-conn:,listen-queue-len:,active-processes:,slow-requests: -n 'php-fpm-healthcheck' -- "$@")
107109

108110
if test "$?" != 0; then >&2 echo "Invalid options, terminating." ; exit 3 ; fi
109111

112+
set -e
113+
110114
FCGI_CONNECT="${FCGI_CONNECT:-$FCGI_CONNECT_DEFAULT}"
111115

112116
eval set -- "$GETOPT"

test/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM php:7.2-fpm-alpine3.8
2+
3+
# Required software
4+
RUN apk add --no-cache fcgi
5+
6+
# Enable php fpm status page
7+
RUN set -xe && echo "pm.status_path = /status" >> /usr/local/etc/php-fpm.d/zz-docker.conf
8+
9+
COPY ./php-fpm-healthcheck /usr/local/bin/

test/docker.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
# Author: <Renato Mefi [email protected]> https://github.com/renatomefi
4+
#
5+
# This script is suppose to be ran via Makefile, i.e.:
6+
# $ make test-image IMAGE="php:7.2-fpm-alpine3.7"
7+
8+
set -eEuo pipefail
9+
10+
function cleanup {
11+
docker stop "$CONTAINER" 1> /dev/null
12+
echo "container $CONTAINER: stopped"
13+
docker rmi "$DOCKER_TAG_TEMPORARY"
14+
}
15+
trap cleanup EXIT
16+
17+
declare -r DOCKER_IMAGE="$1"
18+
19+
declare DOCKER_TAG_TEMPORARY
20+
DOCKER_TAG_TEMPORARY="$DOCKER_IMAGE-$(date +%s)"
21+
22+
sed "s/FROM .*/FROM $DOCKER_IMAGE/g" ./test/Dockerfile | docker build -t "$DOCKER_TAG_TEMPORARY" -f - .
23+
24+
declare CONTAINER
25+
CONTAINER=$(docker run -d --rm "$DOCKER_TAG_TEMPORARY")
26+
27+
declare TESTS_DIR
28+
TESTS_DIR="$(pwd)/test"
29+
30+
docker run --rm -t \
31+
-v "$TESTS_DIR:/tests" \
32+
-v /var/run/docker.sock:/var/run/docker.sock:ro \
33+
renatomefi/docker-testinfra:latest --verbose --hosts="docker://$CONTAINER"

test/pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
python_files = testinfra/test_*.py

test/testinfra/test_command.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
def test_healthcheck_script_is_available(host):
4+
cmd = host.run("which php-fpm-healthcheck")
5+
assert cmd.rc == 0
6+
7+
def test_healthcheck_script_is_executable(host):
8+
scriptFile = host.check_output("which php-fpm-healthcheck")
9+
10+
assert host.file(scriptFile).exists is True
11+
assert host.file(scriptFile).mode == 0o775

test/testinfra/test_execution.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
def test_invalid_option_exits_properly(host):
4+
cmd = host.run("php-fpm-healthcheck --invalid-option")
5+
assert cmd.rc == 3
6+
7+
cmd = host.run("php-fpm-healthcheck --invalid-option=")
8+
assert cmd.rc == 3
9+
10+
def test_valid_with_empty_value_exits_properly(host):
11+
cmd = host.run("php-fpm-healthcheck --listen-queue-len=")
12+
assert cmd.rc == 3
13+
assert "option value must be an integer" in cmd.stderr
14+
15+
def test_valid_with_non_integer_value_exits_properly(host):
16+
cmd = host.run("php-fpm-healthcheck --listen-queue-len=abc")
17+
assert cmd.rc == 3
18+
assert "option value must be an integer" in cmd.stderr
19+
20+
def test_missing_fcgi(host):
21+
host.run("apk del fcgi")
22+
cmd = host.run("php-fpm-healthcheck")
23+
assert cmd.rc == 4
24+
assert "Make sure fcgi is installed" in cmd.stderr
25+
26+
# Fail safe for other tests, maybe we could use a docker fixture
27+
# to start a new container everytime
28+
host.run("apk add --no-cache fcgi")

test/testinfra/test_metrics.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
3+
def test_metric_accepted_conn(host):
4+
cmd = host.run("php-fpm-healthcheck -v")
5+
assert cmd.rc == 0
6+
assert "Trying to connect to php-fpm via:" in cmd.stdout
7+
assert "status output:" in cmd.stdout
8+
assert "pool:" in cmd.stdout

test/testinfra/test_ping.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
def test_ping(host):
4+
cmd = host.run("php-fpm-healthcheck")
5+
assert cmd.rc == 0
6+
7+
def test_ping_verbose(host):
8+
cmd = host.run("php-fpm-healthcheck -v")
9+
assert cmd.rc == 0
10+
assert "Trying to connect to php-fpm via:" in cmd.stdout
11+
assert "status output:" in cmd.stdout
12+
assert "pool:" in cmd.stdout

0 commit comments

Comments
 (0)