From dd6d4d5f50e741923ab2e95de1a5f34e870bdab3 Mon Sep 17 00:00:00 2001 From: Bartek Grzybowski Date: Fri, 4 Mar 2022 16:35:02 +0100 Subject: [BUILD] Add tests for docker downloader script This adds test routines for downloader script leveraging tox and pytest Change-Id: I0c603434fd323741ad5230679c78c2c7e688edf3 Issue-ID: INT-1429 Signed-off-by: Bartek Grzybowski --- build/download/tests/conftest.py | 63 ++++++++++++++++++++++++++++++ build/download/tests/test-requirements.txt | 1 + build/download/tests/test_docker.py | 33 ++++++++++++++++ build/download/tests/test_image_list | 6 +++ build/download/tests/test_settings.py | 2 + build/download/tox.ini | 14 ++++++- 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 build/download/tests/conftest.py create mode 100644 build/download/tests/test-requirements.txt create mode 100644 build/download/tests/test_docker.py create mode 100644 build/download/tests/test_image_list create mode 100644 build/download/tests/test_settings.py diff --git a/build/download/tests/conftest.py b/build/download/tests/conftest.py new file mode 100644 index 00000000..ca113630 --- /dev/null +++ b/build/download/tests/conftest.py @@ -0,0 +1,63 @@ +import pytest +from download import * +from sys import version_info,exit +from test_settings import * +from timeit import default_timer +from docker import from_env + +@pytest.fixture +def check_python_version(): + if version_info.major < 3: + print('Python 2 is not supported.') + exit(1) + +@pytest.fixture +def init_cli(check_python_version): # pylint: disable=E0613,W0613 + # initialize DockerDownloader + args = parse_args().parse_args(['--docker', TEST_IMAGE_LIST_FILE]) # pylint: disable=E0602 + interval_start = default_timer() + docker = docker_downloader.DockerDownloader(False, *args.docker, mirror=args.docker_private_registry_mirror, mirror_exclude=args.docker_private_registry_exclude, workers=1) # pylint: disable=E0602 + handle_download(docker, args.check, [], interval_start) # pylint: disable=E0602 + +@pytest.fixture +def init_cli_with_registry_mirror(check_python_version): # pylint: disable=E0613,W0613 + # initialize DockerDownloader + args = parse_args().parse_args(['--docker-private-registry-mirror', TEST_REGISTRY_MIRROR, '--docker', TEST_IMAGE_LIST_FILE]) # pylint: disable=E0602 + interval_start = default_timer() + docker = docker_downloader.DockerDownloader(False, *args.docker, mirror=args.docker_private_registry_mirror, mirror_exclude=args.docker_private_registry_exclude, workers=1) # pylint: disable=E0602 + handle_download(docker, args.check, [], interval_start) # pylint: disable=E0602 + +@pytest.fixture +def init_cli_check(check_python_version, capfd): # pylint: disable=W0613 + # initialize DockerDownloader + args = parse_args().parse_args(['--docker', TEST_IMAGE_LIST_FILE, '--check']) # pylint: disable=E0602 + interval_start = default_timer() + docker = docker_downloader.DockerDownloader(False, *args.docker, mirror=args.docker_private_registry_mirror, mirror_exclude=args.docker_private_registry_exclude, workers=1) # pylint: disable=E0602 + handle_download(docker, args.check, [], interval_start) # pylint: disable=E0602 + msg = capfd.readouterr() + return msg.out + +@pytest.fixture +def test_image_list(): + img_list = [] + with open(TEST_IMAGE_LIST_FILE, 'r') as list: + for line in list: + img_list.append(line.strip()) + return img_list + +@pytest.fixture +def docker_image_list(): + docker_client = from_env() + image_list = [] + for img in docker_client.images.list(): + for tag in img.tags: + image_list.append(tag) + return image_list + +@pytest.fixture +def drop_test_images(test_image_list, docker_image_list): + dc = from_env() + image_list = docker_image_list + for img in test_image_list: + if img in image_list: + dc.images.remove(img) diff --git a/build/download/tests/test-requirements.txt b/build/download/tests/test-requirements.txt new file mode 100644 index 00000000..e079f8a6 --- /dev/null +++ b/build/download/tests/test-requirements.txt @@ -0,0 +1 @@ +pytest diff --git a/build/download/tests/test_docker.py b/build/download/tests/test_docker.py new file mode 100644 index 00000000..4338c8a8 --- /dev/null +++ b/build/download/tests/test_docker.py @@ -0,0 +1,33 @@ +import pytest # pylint: disable=W0611 +from docker import from_env # pylint: disable=W0611 + +# Library routines + +def parse_check_output(mode, test_image_list, output): + '''mode - True|False''' + # for each test image check if it's marked as "True or False" (depending on mode) in fixture output + for image in test_image_list: + found = 0 + for line in output.split('\n'): + if (image in line) and (mode in line): + found = 1 + break + if not found: + print('ERROR: Image {} was not reported by "--check" option as {}'.format(image,mode)) + assert 0 + assert 1 + +# Actual test routines + +def test_check_mode_images_not_pulled(drop_test_images, init_cli_check, test_image_list): # pylint: disable=W0613 + parse_check_output("False", test_image_list, init_cli_check) + +def test_pull_images_from_mirror(init_cli_with_registry_mirror, test_image_list, init_cli_check): # pylint: disable=W0613 + parse_check_output("True", test_image_list, init_cli_check) + +def test_pull_images(drop_test_images, init_cli, test_image_list, init_cli_check): # pylint: disable=W0613 + parse_check_output("True", test_image_list, init_cli_check) + +def test_cleanup_images(drop_test_images): # pylint: disable=W0613 + # Noop routine to cleanup test images + assert 1 diff --git a/build/download/tests/test_image_list b/build/download/tests/test_image_list new file mode 100644 index 00000000..70f647c8 --- /dev/null +++ b/build/download/tests/test_image_list @@ -0,0 +1,6 @@ +nexus3.onap.org:10001/onap/dmaap/dbc-client:1.0.9 +grafana/grafana:8.3.5 +docker.io/busybox:1.32 +docker.io/dibi/envsubst:1 +nexus3.onap.org:10001/onap/clamp-frontend:5.1.5 +alpine:latest diff --git a/build/download/tests/test_settings.py b/build/download/tests/test_settings.py new file mode 100644 index 00000000..e5efb148 --- /dev/null +++ b/build/download/tests/test_settings.py @@ -0,0 +1,2 @@ +TEST_REGISTRY_MIRROR = 'nexus3.onap.org:10001' +TEST_IMAGE_LIST_FILE = 'tests/test_image_list' diff --git a/build/download/tox.ini b/build/download/tox.ini index e4616fff..1b96465c 100644 --- a/build/download/tox.ini +++ b/build/download/tox.ini @@ -2,6 +2,18 @@ envlist = download skipsdist = true -[testenv:download] +[testenv] basepython = python3 + +[testenv:download] deps = -r{toxinidir}/requirements.txt + +[testenv:pytest] +deps = + -r{toxinidir}/requirements.txt + -r{toxinidir}/tests/test-requirements.txt +commands = pytest -v + +[pytest] +# required for app modules discovery within tests directory +pythonpath = . -- cgit 1.2.3-korg