Skip to content

Commit e7cc62e

Browse files
authored
Switch test to containerized services (#50)
Like in codeaffen/phpipam-ansible-modules we want to run tests agains phpipam installation with containerized services here too. So we adapt the solution from there and switch our CI workflow to that approach For that we have to add ssl_verify to connection params and set default in Makefile
1 parent 2ee22ff commit e7cc62e

File tree

6 files changed

+111
-22
lines changed

6 files changed

+111
-22
lines changed

.github/workflows/main.yml

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,70 @@ on:
44
- push
55

66
jobs:
7-
tests:
8-
7+
e2e_tests:
8+
name: end to end tests
99
runs-on: ubuntu-latest
10-
10+
services:
11+
database:
12+
image: mariadb:10.3.18
13+
ports:
14+
- "3306:3306"
15+
env:
16+
MYSQL_ROOT_PASSWORD: "rootpw"
17+
MYSQL_USER: "phpipam"
18+
MYSQL_PASSWORD: "phpipamadmin"
19+
MYSQL_DATABASE: "phpipam"
20+
phpipam:
21+
image: phpipam/phpipam-www:v1.4.4
22+
ports:
23+
- "443:443"
24+
env:
25+
IPAM_DATABASE_HOST: "database"
26+
IPAM_DATABASE_USER: "phpipam"
27+
IPAM_DATABASE_PASS: "phpipamadmin"
28+
IPAM_DATABASE_NAME: "phpipam"
1129
steps:
12-
- uses: actions/checkout@v2
13-
- name: Set up Python
14-
uses: actions/setup-python@v2
15-
with:
16-
python-version: '3.x'
17-
- name: Prepare test environment
18-
env:
19-
PHPIPAM_URL: ${{ secrets.PHPIPAM_URL }}
20-
PHPIPAM_APPID: ${{ secrets.PHPIPAM_APPID }}
21-
PHPIPAM_USERNAME: ${{ secrets.PHPIPAM_USERNAME }}
22-
PHPIPAM_PASSWORD: ${{ secrets.PHPIPAM_PASSWORD }}
23-
run: |
24-
python -m pip install --upgrade pip
25-
make test-setup
26-
- name: Run all tests
27-
run:
28-
make test-all
30+
- uses: actions/checkout@v2
31+
- name: Checkout phpipam repo
32+
uses: actions/checkout@v2
33+
with:
34+
repository: phpipam/phpipam
35+
ref: v1.4.4
36+
path: phpipam
37+
- name: Set up Python
38+
uses: actions/setup-python@v2
39+
with:
40+
python-version: '3.x'
41+
- name: setup test environment
42+
run: |
43+
make test-setup
44+
env:
45+
PHPIPAM_URL: "https://localhost"
46+
PHPIPAM_APPID: "ansible"
47+
PHPIPAM_USERNAME: "admin"
48+
PHPIPAM_PASSWORD: "ipamadmin"
49+
PHPIPAM_VALIDATE_CERTS: False
50+
- name: "waiting for database to come online"
51+
run: |
52+
for i in `seq 1 10`;
53+
do
54+
nc -z 127.0.0.1 3306 && echo Success && exit 0
55+
echo -n .
56+
sleep 1
57+
done
58+
echo Failed waiting for MySQL && exit 1
59+
- name: load data into database
60+
run: |
61+
mysql -h 127.0.0.1 -u phpipam -pphpipamadmin phpipam < phpipam/db/SCHEMA.sql
62+
- name: activate api
63+
run: |
64+
mysql -h 127.0.0.1 -u phpipam -pphpipamadmin phpipam --execute="UPDATE settings SET api=1 WHERE id=1;"
65+
- name: add api key for tests
66+
run: |
67+
mysql -h 127.0.0.1 -u phpipam -pphpipamadmin phpipam --execute="INSERT INTO api (app_id, app_code, app_permissions, app_security, app_lock_wait) VALUES ('ansible','aAbBcCdDeEfF00112233445566778899',2,'ssl_token',0);"
68+
- name: run all tests
69+
run: |
70+
make test-all
71+
env:
72+
PYTHONWARNINGS: "ignore:Unverified HTTPS request"
73+
PHPIPAM_VALIDATE_CERTS: False

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
ifndef PHPIPAM_VALIDATE_CERTS
2+
override PHPIPAM_VALIDATE_CERTS = true
3+
endif
4+
15
ifdef PYPI_REPO
26
TWINE_OPTIONS = --repository $(PYPI_REPO)
37
endif
@@ -60,7 +64,7 @@ test-setup: | tests/vars/server.yml
6064
pip install --upgrade -r requirements-dev.txt
6165

6266
tests/vars/server.yml:
63-
sed -e "s#~~url~~#$(PHPIPAM_URL)#" -e "s#~~app_id~~#$(PHPIPAM_APPID)#" -e "s#~~username~~#$(PHPIPAM_USERNAME)#" -e "s#~~password~~#$(PHPIPAM_PASSWORD)#" $@.example > $@
67+
sed -e "s#~~url~~#$(PHPIPAM_URL)#" -e "s#~~app_id~~#$(PHPIPAM_APPID)#" -e "s#~~username~~#$(PHPIPAM_USERNAME)#" -e "s#~~password~~#$(PHPIPAM_PASSWORD)#" -e "s#~~ssl_verify~~#$(PHPIPAM_VALIDATE_CERTS)#" $@.example > $@
6468

6569
test-all:
6670
coverage run -m pytest tests/test_cases/* -v

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def pi(*arg, **kwargs):
2323
app_id = kwargs.pop('app_id', server['app_id'])
2424
username = kwargs.pop('username', server['username'])
2525
password = kwargs.pop('password', server['password'])
26-
ssl_verify = kwargs.pop('ssl_verify', True)
26+
ssl_verify = kwargs.pop('ssl_verify', server['ssl_verify'])
2727

2828
return phpypam.api(
2929
url=url,

tests/docker/docker-compose.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: '3'
2+
services:
3+
phpipam:
4+
image: phpipam/phpipam-www:v1.4.4
5+
ports:
6+
- "443:443"
7+
environment:
8+
IPAM_DATABASE_HOST: "database"
9+
IPAM_DATABASE_USER: "phpipam"
10+
IPAM_DATABASE_PASS: "phpipamadmin"
11+
IPAM_DATABASE_NAME: "phpipam"
12+
depends_on:
13+
- database
14+
database:
15+
image: mariadb:10.3.18
16+
ports:
17+
- "3306:3306"
18+
environment:
19+
MYSQL_ROOT_PASSWORD: "rootpw"
20+
MYSQL_USER: "phpipam"
21+
MYSQL_PASSWORD: "phpipamadmin"
22+
MYSQL_DATABASE: "phpipam"

tests/docker/setup_database.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
while ! nc -z "${DB_HOST:-127.0.0.1}" "${DB_PORT:-3306}"; do
4+
echo "Waiting for database connection..."
5+
sleep 1
6+
done
7+
8+
echo "Database is up"
9+
10+
echo "Creating database ${DB_NAME:-phpipam}"
11+
docker exec -ti docker_phpipam_1 sh -c 'mysql -h database -u phpipam -pphpipamadmin phpipam < /phpipam/db/SCHEMA.sql'
12+
13+
echo "Activating API"
14+
mysql -u phpipam -pphpipamadmin -h "${DB_HOST:-127.0.0.1}" phpipam --execute="UPDATE settings SET api=1 WHERE id=1;"
15+
16+
echo "Inserting API application"
17+
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);"

tests/vars/server.yml.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ url: ~~url~~
33
app_id: ~~app_id~~
44
username: ~~username~~
55
password: ~~password~~
6+
ssl_verify: ~~ssl_verify~~

0 commit comments

Comments
 (0)