[BUILD] Add tests for docker downloader script 03/127503/3
authorBartek Grzybowski <b.grzybowski@partner.samsung.com>
Fri, 4 Mar 2022 15:35:02 +0000 (16:35 +0100)
committerBartek Grzybowski <b.grzybowski@partner.samsung.com>
Tue, 8 Mar 2022 10:41:33 +0000 (11:41 +0100)
This adds test routines for downloader script leveraging tox and pytest

Change-Id: I0c603434fd323741ad5230679c78c2c7e688edf3
Issue-ID: INT-1429
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
build/download/tests/conftest.py [new file with mode: 0644]
build/download/tests/test-requirements.txt [new file with mode: 0644]
build/download/tests/test_docker.py [new file with mode: 0644]
build/download/tests/test_image_list [new file with mode: 0644]
build/download/tests/test_settings.py [new file with mode: 0644]
build/download/tox.ini

diff --git a/build/download/tests/conftest.py b/build/download/tests/conftest.py
new file mode 100644 (file)
index 0000000..ca11363
--- /dev/null
@@ -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 (file)
index 0000000..e079f8a
--- /dev/null
@@ -0,0 +1 @@
+pytest
diff --git a/build/download/tests/test_docker.py b/build/download/tests/test_docker.py
new file mode 100644 (file)
index 0000000..4338c8a
--- /dev/null
@@ -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 (file)
index 0000000..70f647c
--- /dev/null
@@ -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 (file)
index 0000000..e5efb14
--- /dev/null
@@ -0,0 +1,2 @@
+TEST_REGISTRY_MIRROR = 'nexus3.onap.org:10001'
+TEST_IMAGE_LIST_FILE = 'tests/test_image_list'
index e4616ff..1b96465 100644 (file)
@@ -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 = .