Skip to content

Commit 85b2093

Browse files
authored
Provide testing facilities (#67)
* Add github workflow Here we spin up two service containers (db and phpipam). Load database schema into it. Activate API and add API application to use API. The test-setup target will be called before running tests against the created phpipam environment. * Change some test data As the subnet for address tests does not exists the test will fail on a fresh phpipam installation. So we switched to a subnet wich already exists. Provide valid postal address. * Add command line parameters for connection data We add command line parameter parsing for connection data. So it is possible to override data e.g. cert validation with command line variables. * Provide local phpipam enviroment With this change we provide a facility to setup a local phpipam setup based on docker-compose. If it is up and running tests can run against this installation.
1 parent 807ca85 commit 85b2093

File tree

11 files changed

+134
-14
lines changed

11 files changed

+134
-14
lines changed

.github/workflows/tests.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: "Test Collection"
2+
on: push
3+
4+
jobs:
5+
test:
6+
name: run tests
7+
runs-on: ubuntu-latest
8+
services:
9+
database:
10+
image: mariadb:10.3.18
11+
ports:
12+
- "3306:3306"
13+
env:
14+
MYSQL_ROOT_PASSWORD: "rootpw"
15+
MYSQL_USER: "phpipam"
16+
MYSQL_PASSWORD: "phpipamadmin"
17+
MYSQL_DATABASE: "phpipam"
18+
phpipam:
19+
image: phpipam/phpipam-www:v1.4.4
20+
ports:
21+
- "443:443"
22+
env:
23+
IPAM_DATABASE_HOST: "database"
24+
IPAM_DATABASE_USER: "phpipam"
25+
IPAM_DATABASE_PASS: "phpipamadmin"
26+
IPAM_DATABASE_NAME: "phpipam"
27+
steps:
28+
- uses: actions/checkout@v2
29+
- name: Checkout phpipam repo
30+
uses: actions/checkout@v2
31+
with:
32+
repository: phpipam/phpipam
33+
ref: v1.4.4
34+
path: phpipam
35+
- name: Set up Python
36+
uses: actions/setup-python@v2
37+
with:
38+
python-version: '3.x'
39+
- name: setup test environment
40+
run: |
41+
make test-setup
42+
env:
43+
PHPIPAM_URL: "https://localhost"
44+
PHPIPAM_APPID: "ansible"
45+
PHPIPAM_USERNAME: "admin"
46+
PHPIPAM_PASSWORD: "ipamadmin"
47+
# - name: "waiting for database to come online"
48+
# run: |
49+
# for i in `seq 1 10`;
50+
# do
51+
# nc -z 127.0.0.1 3306 && echo Success && exit 0
52+
# echo -n .
53+
# sleep 1
54+
# done
55+
# echo Failed waiting for MySQL && exit 1
56+
- name: load data into database
57+
run: |
58+
mysql -h 127.0.0.1 -u phpipam -pphpipamadmin phpipam < phpipam/db/SCHEMA.sql
59+
- name: activate api
60+
run: |
61+
mysql -h 127.0.0.1 -u phpipam -pphpipamadmin phpipam --execute="UPDATE settings SET api=1 WHERE id=1;"
62+
- name: add api key for tests
63+
run: |
64+
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);"
65+
- name: run single test
66+
run: |
67+
make test-example_setup
68+
env:
69+
PHPIPAM_VALIDATE_CERTS: false
70+
- name: run tests
71+
run: |
72+
make test-all
73+
env:
74+
PHPIPAM_VALIDATE_CERTS: "false"

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ clean:
6565
find . -name '*~' -exec rm -f {} +
6666
find . -name '__pycache__' -exec rm -rf {} +
6767
find . -name '*.tar.gz' -delete
68+
docker-compose -f docker/docker-compose.yml stop
69+
docker-compose -f docker/docker-compose.yml rm --force
6870

6971
doc-setup:
7072
pip install -r docs/requirements.txt
@@ -89,6 +91,11 @@ tests/test_playbooks/vars/server.yml:
8991
install-deps:
9092
pip install -r requirements-dev.txt
9193

94+
setup-phpipam: test-setup
95+
docker-compose -f docker/docker-compose.yml up -d
96+
sleep 30
97+
docker/setup_database.sh
98+
9299
FORCE:
93100

94101
.PHONY: help dist lint doc-setup doc publish test-setup test-all test-% install-deps FORCE
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- fix \#68 - add automatic testing facility for all modules
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- fix \#69 - add facility to setup local phpipam environment
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- fix \#70 - provide environment variable support for connection data

docker/docker-compose.yml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
version: '3'
22
services:
3-
phpIPAM-www:
4-
image: phpipam/phpipam-www
3+
phpipam:
4+
image: phpipam/phpipam-www:v1.4.4
55
ports:
6-
- "8080:80"
7-
phpIPAM-DB:
8-
image: "mysql"
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"

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);"

plugins/module_utils/phpipam_helper.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from collections import defaultdict
2525

26-
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
26+
from ansible.module_utils.basic import AnsibleModule, missing_required_lib, env_fallback
2727

2828

2929
class PhpipamAnsibleException(Exception):
@@ -57,11 +57,11 @@ def __init__(self, **kwargs):
5757

5858
self.phpipam_spec, gen_args = self._phpipam_spec_helper(kwargs.pop('phpipam_spec', {}))
5959
argument_spec = dict(
60-
server_url=dict(required=True),
61-
app_id=dict(required=True),
62-
username=dict(required=True),
63-
password=dict(required=True, no_log=True),
64-
validate_certs=dict(type='bool', required=False, default=True),
60+
server_url=dict(required=True, fallback=(env_fallback, ['PHPIPAM_SERVER_URL'])),
61+
app_id=dict(required=True, fallback=(env_fallback, ['PHPIPAM_APP_ID'])),
62+
username=dict(required=True, fallback=(env_fallback, ['PHPIPAM_USERNAME'])),
63+
password=dict(required=True, fallback=(env_fallback, ['PHPIPAM_PASSWORD']), no_log=True),
64+
validate_certs=dict(type='bool', fallback=(env_fallback, ['PHPIPAM_VALIDATE_CERTS']), required=False, default=True),
6565
)
6666
argument_spec.update(gen_args)
6767
argument_spec.update(kwargs.pop('argument_spec', {}))

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
ansible
22
ansible_runner<2.0; python_version < '3.6'
33
ansible_runner; python_version >= '3.6'
4+
colour
45
coverage
6+
geopy
57
wheel
68
jinja2 # pyup: ignore
79
PyYAML~=5.3

tests/test_playbooks/location.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
vars:
2525
name: update location
2626
override:
27-
address: "Alexander Platz, Berlin, Germany"
27+
address: "Alexanderstraße. 1, 10115 Berlin, Deutschland"
2828
latitude: "{{ omit }}"
2929
longitude: "{{ omit }}"
3030
location: "{{ base_location_data | combine(override) }}"

0 commit comments

Comments
 (0)