Skip to content

Commit 4467e52

Browse files
authored
Update test framework (#62)
* Update test framework - Bring setup of test env to next level - Remove unused vcr stuff * fix some issues found by codacy
1 parent f5fc0f4 commit 4467e52

File tree

6 files changed

+43
-89
lines changed

6 files changed

+43
-89
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-20.04
1111
strategy:
1212
matrix:
13-
phpipam-version: ['1.4x', '1.5x']
13+
phpipam-version: ['v1.4x', 'v1.5x']
1414
python-version: ['3.7.x', '3.8.x', '3.9.x', '3.10.x']
1515
steps:
1616
- uses: actions/checkout@v2

Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ coverage-xml: test-all
8282

8383
setup-phpipam: test-setup
8484
docker-compose -f tests/docker/docker-compose.yml up -d
85-
sleep 30
8685
sh tests/docker/setup_database.sh
8786

8887
FORCE:

requirements-dev.txt

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ setuptools
1010
PyYAML
1111
bumpversion
1212
pytest
13-
pytest-replay
14-
pytest-vcr
1513
pytest-xdist
1614
coverage

tests/conftest.py

-69
Original file line numberDiff line numberDiff line change
@@ -48,72 +48,3 @@ def find_all_test_cases():
4848

4949

5050
TEST_CASES = list(find_all_test_cases())
51-
52-
53-
def pytest_addoption(parser):
54-
"""Change command line options defaults.
55-
56-
We want run our tests only in three modes
57-
`live` - interact with an existing API
58-
`record` - interact with an existing API and record the interactions
59-
`replay` - replay previouly recorded interactions with API
60-
61-
:param parser: A parser object
62-
:type parser: object parser
63-
"""
64-
parser.addoption(
65-
"--vcrmode",
66-
action="store",
67-
default="replay",
68-
choices=["replay", "record", "live"],
69-
help="mode for vcr recording; one of ['replay', 'record', 'live']",
70-
)
71-
72-
73-
@pytest.fixture
74-
def vcrmode(request):
75-
"""Return vcrmode of a request.
76-
77-
:param request: A request object
78-
:type request: object request
79-
:return: vcrmode
80-
:rtype: str
81-
"""
82-
return request.config.getoption("vcrmode")
83-
84-
85-
def cassette_name(test_name=None):
86-
"""Generate cassette_name."""
87-
return 'tests/fixtures/{0}.yml'.format(test_name)
88-
89-
90-
FILTER_REQUEST_HEADERS = ['Authorization', 'Cookie', 'Token']
91-
FILTER_RESPONSE_HEADERS = ['Apipie-Checksum', 'Date', 'ETag', 'Server', 'Set-Cookie', 'Via', 'X-Powered-By', 'X-Request-Id', 'X-Runtime']
92-
93-
94-
def filter_response(response):
95-
"""Filter headers before recording.
96-
97-
:param response: A response object where we want to filter the headers from.
98-
:type response: object response
99-
:return: response
100-
:rtype: object response
101-
"""
102-
for header in FILTER_RESPONSE_HEADERS:
103-
# headers should be case insensitive, but for some reason they weren't for me
104-
response['headers'].pop(header.lower(), None)
105-
response['headers'].pop(header, None)
106-
107-
return response
108-
109-
110-
def filter_request_uri(request):
111-
"""Filter uri before recording.
112-
113-
:param request: A request object where we want to filter the uri from.
114-
:type request: object request
115-
:return: request
116-
:rtype: object request
117-
"""
118-
request.uri = urlunparse(urlparse(request.uri)._replace(netloc="ipam.example.org"))
119-
return request

tests/docker/docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
version: '3'
22
services:
33
phpipam:
4-
image: "phpipam/phpipam-www:v${PHPIPAM_VERSION:-1.4.4}"
4+
image: "phpipam/phpipam-www:${PHPIPAM_VERSION:-v1.4.4}"
55
ports:
6-
- "${PHPIPAM_PORT:-443}:443"
6+
- "${PHPIPAM_PORT:-8443}:443"
77
environment:
88
IPAM_DATABASE_HOST: "database"
99
IPAM_DATABASE_USER: "phpipam"

tests/docker/setup_database.sh

+40-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
11
#!/bin/bash
22

3+
exec 10>&1
4+
exec > /dev/null 2>&1
5+
6+
function info() {
7+
echo "${@}" >&10
8+
}
9+
10+
MYSQL_PING="mysqladmin ping -h ${DB_HOST:-127.0.0.1} -P ${DB_PORT:-3306} -u ${MYSQL_ROOT_USER:-root} -p${MYSQL_ROOT_PASSWORD:-rootpw}"
11+
312
if grep -q podman <<< $(docker --version 2> /dev/null) ; then
4-
echo "Podman is installed"
13+
info "Podman is installed"
514
DOCKER_CMD=$(which podman)
615
fi
716

8-
while ! nc -z "${DB_HOST:-127.0.0.1}" "${DB_PORT:-3306}"; do
9-
echo "Waiting for database connection..."
10-
sleep 1
11-
done
17+
if "${DOCKER_CMD}" ps | grep -q docker_phpipam_1 && ! eval "${MYSQL_PING}" ; then
18+
19+
info -n "Waiting for database connection "
20+
while ! eval "${MYSQL_PING}" ; do
21+
info -n "."
22+
sleep 1
23+
done
24+
info
25+
fi
26+
27+
info "Database is up"
1228

13-
echo "Database is up"
29+
if [[ $(mysqlshow -u root -prootpw -h 127.0.0.1 -P 3306 phpipam | wc -l) -eq 5 ]] ; then
1430

15-
echo "Creating database ${DB_NAME:-phpipam}"
16-
${DOCKER_CMD} exec -ti docker_phpipam_1 sh -c 'mysql -h database -u phpipam -pphpipamadmin phpipam < /phpipam/db/SCHEMA.sql'
31+
info "Creating database ${DB_NAME:-phpipam}"
32+
${DOCKER_CMD} exec -ti docker_phpipam_1 sh -c 'mysql -h database -u phpipam -pphpipamadmin phpipam < /phpipam/db/SCHEMA.sql' && ((init_result++))
1733

18-
echo "Activating API"
19-
mysql -u phpipam -pphpipamadmin -h "${DB_HOST:-127.0.0.1}" phpipam --execute="UPDATE settings SET api=1 WHERE id=1;"
34+
info "Activating API"
35+
mysql -u phpipam -pphpipamadmin -h "${DB_HOST:-127.0.0.1}" phpipam --execute="UPDATE settings SET api=1 WHERE id=1;" && ((init_result++))
2036

21-
echo "Inserting API application"
22-
mysql -u phpipam -pphpipamadmin -h "${DB_HOST:-127.0.0.1}" phpipam --execute="INSERT INTO api (app_id, app_code, app_permissions, app_security, app_lock_wait) VALUES ('ansible','aAbBcCdDeEfF00112233445566778899',2,'ssl_token',0);"
37+
info "Inserting API application"
38+
mysql -u phpipam -pphpipamadmin -h "${DB_HOST:-127.0.0.1}" phpipam --execute="INSERT INTO api (app_id, app_code, app_permissions, app_security, app_lock_wait) VALUES ('ansible','aAbBcCdDeEfF00112233445566778899',2,'ssl_token',0);" && ((init_result++))
39+
40+
info "Disable forced password reset"
41+
mysql -u phpipam -pphpipamadmin -h "${DB_HOST:-127.0.0.1}" phpipam --execute="UPDATE users SET passChange = 'No' WHERE username = 'Admin';" && ((init_result++))
42+
43+
[ "$init_result" -eq 4 ] && result=successful || result=failed
44+
45+
else
46+
47+
info "Detabase already initiated" && exit 0
48+
49+
fi
2350

24-
echo "Disable forced password reset"
25-
mysql -u phpipam -pphpipamadmin -h "${DB_HOST:-127.0.0.1}" phpipam --execute="UPDATE users SET passChange = 'No' WHERE username = 'Admin';"
51+
info "Database initialisation $result"

0 commit comments

Comments
 (0)