From 4467e52e2c833963f8ccaaa1fdbbffce8fa1e0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Mei=C3=9Fner?= Date: Mon, 30 Oct 2023 12:53:27 +0100 Subject: [PATCH] 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 --- .github/workflows/main.yml | 2 +- Makefile | 1 - requirements-dev.txt | 2 - tests/conftest.py | 69 --------------------------------- tests/docker/docker-compose.yml | 4 +- tests/docker/setup_database.sh | 54 +++++++++++++++++++------- 6 files changed, 43 insertions(+), 89 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8560136..a319caa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/Makefile b/Makefile index 55247ed..b3f9405 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/requirements-dev.txt b/requirements-dev.txt index b56771f..dac648a 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -10,7 +10,5 @@ setuptools PyYAML bumpversion pytest -pytest-replay -pytest-vcr pytest-xdist coverage diff --git a/tests/conftest.py b/tests/conftest.py index 7af0dd1..b8358ab 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/docker/docker-compose.yml b/tests/docker/docker-compose.yml index 6a83638..4681ee9 100644 --- a/tests/docker/docker-compose.yml +++ b/tests/docker/docker-compose.yml @@ -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" diff --git a/tests/docker/setup_database.sh b/tests/docker/setup_database.sh index 0ba763a..ec27dc6 100644 --- a/tests/docker/setup_database.sh +++ b/tests/docker/setup_database.sh @@ -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"