Skip to content

Commit

Permalink
Update test framework (#62)
Browse files Browse the repository at this point in the history
* Update test framework

- Bring setup of test env to next level
- Remove unused vcr stuff

* fix some issues found by codacy
  • Loading branch information
cmeissner authored Oct 30, 2023
1 parent f5fc0f4 commit 4467e52
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
phpipam-version: ['1.4x', '1.5x']
phpipam-version: ['v1.4x', 'v1.5x']
python-version: ['3.7.x', '3.8.x', '3.9.x', '3.10.x']
steps:
- uses: actions/checkout@v2
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ coverage-xml: test-all

setup-phpipam: test-setup
docker-compose -f tests/docker/docker-compose.yml up -d
sleep 30
sh tests/docker/setup_database.sh

FORCE:
Expand Down
2 changes: 0 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ setuptools
PyYAML
bumpversion
pytest
pytest-replay
pytest-vcr
pytest-xdist
coverage
69 changes: 0 additions & 69 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,72 +48,3 @@ def find_all_test_cases():


TEST_CASES = list(find_all_test_cases())


def pytest_addoption(parser):
"""Change command line options defaults.
We want run our tests only in three modes
`live` - interact with an existing API
`record` - interact with an existing API and record the interactions
`replay` - replay previouly recorded interactions with API
:param parser: A parser object
:type parser: object parser
"""
parser.addoption(
"--vcrmode",
action="store",
default="replay",
choices=["replay", "record", "live"],
help="mode for vcr recording; one of ['replay', 'record', 'live']",
)


@pytest.fixture
def vcrmode(request):
"""Return vcrmode of a request.
:param request: A request object
:type request: object request
:return: vcrmode
:rtype: str
"""
return request.config.getoption("vcrmode")


def cassette_name(test_name=None):
"""Generate cassette_name."""
return 'tests/fixtures/{0}.yml'.format(test_name)


FILTER_REQUEST_HEADERS = ['Authorization', 'Cookie', 'Token']
FILTER_RESPONSE_HEADERS = ['Apipie-Checksum', 'Date', 'ETag', 'Server', 'Set-Cookie', 'Via', 'X-Powered-By', 'X-Request-Id', 'X-Runtime']


def filter_response(response):
"""Filter headers before recording.
:param response: A response object where we want to filter the headers from.
:type response: object response
:return: response
:rtype: object response
"""
for header in FILTER_RESPONSE_HEADERS:
# headers should be case insensitive, but for some reason they weren't for me
response['headers'].pop(header.lower(), None)
response['headers'].pop(header, None)

return response


def filter_request_uri(request):
"""Filter uri before recording.
:param request: A request object where we want to filter the uri from.
:type request: object request
:return: request
:rtype: object request
"""
request.uri = urlunparse(urlparse(request.uri)._replace(netloc="ipam.example.org"))
return request
4 changes: 2 additions & 2 deletions tests/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: '3'
services:
phpipam:
image: "phpipam/phpipam-www:v${PHPIPAM_VERSION:-1.4.4}"
image: "phpipam/phpipam-www:${PHPIPAM_VERSION:-v1.4.4}"
ports:
- "${PHPIPAM_PORT:-443}:443"
- "${PHPIPAM_PORT:-8443}:443"
environment:
IPAM_DATABASE_HOST: "database"
IPAM_DATABASE_USER: "phpipam"
Expand Down
54 changes: 40 additions & 14 deletions tests/docker/setup_database.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
#!/bin/bash

exec 10>&1
exec > /dev/null 2>&1

function info() {
echo "${@}" >&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}"

if grep -q podman <<< $(docker --version 2> /dev/null) ; then
echo "Podman is installed"
info "Podman is installed"
DOCKER_CMD=$(which podman)
fi

while ! nc -z "${DB_HOST:-127.0.0.1}" "${DB_PORT:-3306}"; do
echo "Waiting for database connection..."
sleep 1
done
if "${DOCKER_CMD}" ps | grep -q docker_phpipam_1 && ! eval "${MYSQL_PING}" ; then

info -n "Waiting for database connection "
while ! eval "${MYSQL_PING}" ; do
info -n "."
sleep 1
done
info
fi

info "Database is up"

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

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

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

echo "Inserting API application"
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);"
info "Inserting API application"
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++))

info "Disable forced password reset"
mysql -u phpipam -pphpipamadmin -h "${DB_HOST:-127.0.0.1}" phpipam --execute="UPDATE users SET passChange = 'No' WHERE username = 'Admin';" && ((init_result++))

[ "$init_result" -eq 4 ] && result=successful || result=failed

else

info "Detabase already initiated" && exit 0

fi

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

0 comments on commit 4467e52

Please sign in to comment.