diff options
Diffstat (limited to '')
-rw-r--r-- | tests/__init__.py | 0 | ||||
-rw-r--r-- | tests/lib/__init__.py | 0 | ||||
-rw-r--r-- | tests/lib/dataset.py | 116 | ||||
-rw-r--r-- | tests/lib/fixtures.py | 69 | ||||
-rw-r--r-- | tests/lib/helpers.py | 40 | ||||
-rw-r--r-- | tests/system/__init__.py | 0 | ||||
-rw-r--r-- | tests/system/test_eos_download.py.old | 48 | ||||
-rw-r--r-- | tests/unit/__init__.py | 0 | ||||
-rw-r--r-- | tests/unit/test_eos_version.py | 130 | ||||
-rw-r--r-- | tests/unit/test_object_downloader.py | 141 |
10 files changed, 544 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/__init__.py diff --git a/tests/lib/__init__.py b/tests/lib/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/lib/__init__.py diff --git a/tests/lib/dataset.py b/tests/lib/dataset.py new file mode 100644 index 0000000..1286456 --- /dev/null +++ b/tests/lib/dataset.py @@ -0,0 +1,116 @@ +#!/usr/bin/python +# coding: utf-8 -*- +# pylint: disable=logger-format-interpolation +# pylint: disable=dangerous-default-value +# flake8: noqa: W503 +# flake8: noqa: W1202 + +from __future__ import (absolute_import, division, print_function) +import os +import eos_downloader +from eos_downloader.eos import EOSDownloader +from eos_downloader.data import DATA_MAPPING + + +# --------------------------------------------------------------- # +# MOOCK data to use for testing +# --------------------------------------------------------------- # + +# Get Auth token +# eos_token = os.getenv('ARISTA_TOKEN') +eos_token = os.getenv('ARISTA_TOKEN', 'invalid_token') +eos_token_invalid = 'invalid_token' + +eos_dataset_valid = [ + { + 'image': 'EOS', + 'version': '4.26.3M', + 'software': 'EOS', + 'filename': 'EOS-4.26.3M.swi', + 'expected_hash': 'sha512sum', + 'remote_path': '/support/download/EOS-USA/Active Releases/4.26/EOS-4.26.3M/EOS-4.26.3M.swi', + 'compute_checksum': True + }, + { + 'image': 'EOS', + 'version': '4.25.6M', + 'software': 'EOS', + 'filename': 'EOS-4.25.6M.swi', + 'expected_hash': 'md5sum', + 'remote_path': '/support/download/EOS-USA/Active Releases/4.25/EOS-4.25.6M/EOS-4.25.6M.swi', + 'compute_checksum': True + }, + { + 'image': 'vEOS-lab', + 'version': '4.25.6M', + 'software': 'EOS', + 'filename': 'vEOS-lab-4.25.6M.vmdk', + 'expected_hash': 'md5sum', + 'remote_path': '/support/download/EOS-USA/Active Releases/4.25/EOS-4.25.6M/vEOS-lab/vEOS-lab-4.25.6M.vmdk', + 'compute_checksum': False + } +] + + +eos_dataset_invalid = [ + { + 'image': 'default', + 'version': '4.26.3M', + 'software': 'EOS', + 'filename': 'EOS-4.26.3M.swi', + 'expected_hash': 'sha512sum', + 'remote_path': '/support/download/EOS-USA/Active Releases/4.26/EOS-4.26.3M/EOS-4.26.3M.swi', + 'compute_checksum': True + } +] + +eos_version = [ + { + 'version': 'EOS-4.23.1F', + 'is_valid': True, + 'major': 4, + 'minor': 23, + 'patch': 1, + 'rtype': 'F' + }, + { + 'version': 'EOS-4.23.0', + 'is_valid': True, + 'major': 4, + 'minor': 23, + 'patch': 0, + 'rtype': None + }, + { + 'version': 'EOS-4.23', + 'is_valid': True, + 'major': 4, + 'minor': 23, + 'patch': 0, + 'rtype': None + }, + { + 'version': 'EOS-4.23.1M', + 'is_valid': True, + 'major': 4, + 'minor': 23, + 'patch': 1, + 'rtype': 'M' + }, + { + 'version': 'EOS-4.23.1.F', + 'is_valid': True, + 'major': 4, + 'minor': 23, + 'patch': 1, + 'rtype': 'F' + }, + { + 'version': 'EOS-5.23.1F', + 'is_valid': False, + 'major': 4, + 'minor': 23, + 'patch': 1, + 'rtype': 'F' + }, +]
\ No newline at end of file diff --git a/tests/lib/fixtures.py b/tests/lib/fixtures.py new file mode 100644 index 0000000..4515f9b --- /dev/null +++ b/tests/lib/fixtures.py @@ -0,0 +1,69 @@ +#!/usr/bin/python +# coding: utf-8 -*- +# pylint: disable=logger-format-interpolation +# pylint: disable=dangerous-default-value +# flake8: noqa: W503 +# flake8: noqa: W1202 + +from __future__ import (absolute_import, division, print_function) +import os +import pytest +import eos_downloader +from typing import Dict, Any, List +from tests.lib.dataset import eos_dataset_valid, eos_dataset_invalid, eos_token, eos_token_invalid + + + +@pytest.fixture +@pytest.mark.parametrize("DOWNLOAD_INFO", eos_dataset_valid) +def create_download_instance(request, DOWNLOAD_INFO): + # logger.info("Execute fixture to create class elements") + request.cls.eos_downloader = eos_downloader.eos.EOSDownloader( + image=DOWNLOAD_INFO['image'], + software=DOWNLOAD_INFO['software'], + version=DOWNLOAD_INFO['version'], + token=eos_token, + hash_method='sha512sum') + yield + # logger.info('Cleanup test environment') + os.system('rm -f {}*'.format(DOWNLOAD_INFO['filename'])) + + +def generate_test_ids_dict(val: Dict[str, Any], key: str = 'name') -> str: + """ + generate_test_ids Helper to generate test ID for parametrize + + Only related to SYSTEM_CONFIGLETS_TESTS structure + + Parameters + ---------- + val : dict + A configlet test structure + + Returns + ------- + str + Name of the configlet + """ + if key in val.keys(): + # note this wouldn't show any hours/minutes/seconds + return val[key] + return "undefined_test" + +def generate_test_ids_list(val: List[Dict[str, Any]], key: str = 'name') -> str: + """ + generate_test_ids Helper to generate test ID for parametrize + + Only related to SYSTEM_CONFIGLETS_TESTS structure + + Parameters + ---------- + val : dict + A configlet test structure + + Returns + ------- + str + Name of the configlet + """ + return [ entry[key] if key in entry.keys() else 'unset_entry' for entry in val ] diff --git a/tests/lib/helpers.py b/tests/lib/helpers.py new file mode 100644 index 0000000..308f2a5 --- /dev/null +++ b/tests/lib/helpers.py @@ -0,0 +1,40 @@ +#!/usr/bin/python +# coding: utf-8 -*- +# pylint: disable=logger-format-interpolation +# pylint: disable=dangerous-default-value +# flake8: noqa: W503 +# flake8: noqa: W1202 + +from __future__ import (absolute_import, division, print_function) + +import os + +from eos_downloader.data import DATA_MAPPING + + + +def default_filename(version: str, info): + """ + default_filename Helper to build default filename + + Parameters + ---------- + version : str + EOS version + info : dict + TEST Inputs + + Returns + ------- + str + Filename + """ + if version is None or info is None: + return None + return DATA_MAPPING[info['software']]['default']['prepend'] + '-' + version + '.swi' + + +def is_on_github_actions(): + """Check if code is running on a CI runner""" + if "CI" not in os.environ or not os.environ["CI"] or "GITHUB_RUN_ID" not in os.environ: + return False
\ No newline at end of file diff --git a/tests/system/__init__.py b/tests/system/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/system/__init__.py diff --git a/tests/system/test_eos_download.py.old b/tests/system/test_eos_download.py.old new file mode 100644 index 0000000..6ae56fe --- /dev/null +++ b/tests/system/test_eos_download.py.old @@ -0,0 +1,48 @@ +#!/usr/bin/python +# coding: utf-8 -*- +# pylint: disable=logger-format-interpolation +# pylint: disable=dangerous-default-value +# flake8: noqa: W503 +# flake8: noqa: W1202 + +from __future__ import (absolute_import, division, print_function) +import sys +import os +import platform +from loguru import logger +import pytest +import eos_downloader +from eos_downloader.eos import EOSDownloader +from eos_downloader.data import DATA_MAPPING +from tests.lib.dataset import eos_dataset_valid, eos_token, eos_token_invalid +from tests.lib.fixtures import create_download_instance +from tests.lib.helpers import default_filename + + +# --------------------------------------------------------------- # +# TEST CASES +# --------------------------------------------------------------- # + + +@pytest.mark.usefixtures("create_download_instance") +@pytest.mark.parametrize("DOWNLOAD_INFO", eos_dataset_valid, ids=['EOS-sha512', 'EOS-md5' ,'vEOS-lab-no-hash']) +@pytest.mark.eos_download +class TestEosDownload_valid(): + def test_data(self, DOWNLOAD_INFO): + print(str(DOWNLOAD_INFO)) + + @pytest.mark.dependency(name='authentication') + @pytest.mark.skipif(eos_token == eos_token_invalid, reason="Token is not set correctly") + @pytest.mark.skipif(platform.system() != 'Darwin', reason="Incorrect Hardware") + # @pytest.mark.xfail(reason="Deliberate - CI not set for testing AUTH") + @pytest.mark.webtest + def test_eos_download_authenticate(self): + assert self.eos_downloader.authenticate() is True + + @pytest.mark.dependency(depends=["authentication"], scope='class') + @pytest.mark.webtest + @pytest.mark.slow + @pytest.mark.eos_download + def test_download_local(self, DOWNLOAD_INFO): + self.eos_downloader.download_local(file_path='.', checksum=DOWNLOAD_INFO['compute_checksum']) + diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/unit/__init__.py diff --git a/tests/unit/test_eos_version.py b/tests/unit/test_eos_version.py new file mode 100644 index 0000000..1b97ffc --- /dev/null +++ b/tests/unit/test_eos_version.py @@ -0,0 +1,130 @@ +#!/usr/bin/python +# coding: utf-8 -*- +# pylint: disable=logger-format-interpolation +# pylint: disable=dangerous-default-value +# flake8: noqa: W503 +# flake8: noqa: W1202 + +from __future__ import (absolute_import, division, print_function) + +import sys +from loguru import logger +import pytest +from eos_downloader.models.version import EosVersion, BASE_VERSION_STR +from tests.lib.dataset import eos_version +from tests.lib.fixtures import generate_test_ids_list + +logger.remove() +logger.add(sys.stderr, level="DEBUG") + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_from_str(EOS_VERSION): + version = EosVersion.from_str(EOS_VERSION['version']) + if EOS_VERSION['is_valid']: + assert version.major == EOS_VERSION['major'] + assert version.minor == EOS_VERSION['minor'] + assert version.patch == EOS_VERSION['patch'] + assert version.rtype == EOS_VERSION['rtype'] + else: + assert str(version) == BASE_VERSION_STR + + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_to_str(EOS_VERSION): + version = EosVersion(**EOS_VERSION) + if EOS_VERSION['is_valid']: + assert version.major == EOS_VERSION['major'] + assert version.minor == EOS_VERSION['minor'] + assert version.patch == EOS_VERSION['patch'] + assert version.rtype == EOS_VERSION['rtype'] + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_branch(EOS_VERSION): + if EOS_VERSION['is_valid']: + version = EosVersion(**EOS_VERSION) + assert version.branch == f'{EOS_VERSION["major"]}.{EOS_VERSION["minor"]}' + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_eq_operator(EOS_VERSION): + if not EOS_VERSION['is_valid']: + pytest.skip('not a valid version to test') + version = EosVersion(**EOS_VERSION) + logger.warning(f'version is: {version.dict()}') + assert version == version + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_ge_operator(EOS_VERSION): + if not EOS_VERSION['is_valid']: + pytest.skip('not a valid version to test') + version = EosVersion(**EOS_VERSION) + version_b = EosVersion.from_str(BASE_VERSION_STR) + assert version >= version_b + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_gs_operator(EOS_VERSION): + if not EOS_VERSION['is_valid']: + pytest.skip('not a valid version to test') + version = EosVersion(**EOS_VERSION) + version_b = EosVersion.from_str(BASE_VERSION_STR) + assert version > version_b + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_le_operator(EOS_VERSION): + if not EOS_VERSION['is_valid']: + pytest.skip('not a valid version to test') + version = EosVersion(**EOS_VERSION) + version_b = EosVersion.from_str(BASE_VERSION_STR) + assert version_b <= version + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_ls_operator(EOS_VERSION): + if not EOS_VERSION['is_valid']: + pytest.skip('not a valid version to test') + version = EosVersion(**EOS_VERSION) + version_b = EosVersion.from_str(BASE_VERSION_STR) + assert version_b < version + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_ne_operator(EOS_VERSION): + if not EOS_VERSION['is_valid']: + pytest.skip('not a valid version to test') + version = EosVersion(**EOS_VERSION) + version_b = EosVersion.from_str(BASE_VERSION_STR) + assert version_b != version + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_match(EOS_VERSION): + if not EOS_VERSION['is_valid']: + pytest.skip('not a valid version to test') + version = EosVersion(**EOS_VERSION) + assert version.match(f'=={EOS_VERSION["version"]}') + assert version.match(f'!={BASE_VERSION_STR}') + assert version.match(f'>={BASE_VERSION_STR}') + assert version.match(f'>{BASE_VERSION_STR}') + assert version.match('<=4.99.0F') + assert version.match('<4.99.0F') + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_is_in_branch(EOS_VERSION): + if not EOS_VERSION['is_valid']: + pytest.skip('not a valid version to test') + version = EosVersion(**EOS_VERSION) + assert version.is_in_branch(f"{EOS_VERSION['major']}.{EOS_VERSION['minor']}") + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_match_exception(EOS_VERSION): + if not EOS_VERSION['is_valid']: + pytest.skip('not a valid version to test') + with pytest.raises(Exception) as e_info: + version = EosVersion(**EOS_VERSION) + assert version.match(f'+={EOS_VERSION["version"]}') + logger.info(f'receive exception: {e_info}') + +@pytest.mark.parametrize("EOS_VERSION", eos_version, ids=generate_test_ids_list(eos_version,key='version')) +def test_eos_version_compare_exception(EOS_VERSION): + if not EOS_VERSION['is_valid']: + pytest.skip('not a valid version to test') + with pytest.raises(Exception) as e_info: + version = EosVersion(**EOS_VERSION) + version._compare(BASE_VERSION_STR) + logger.info(f'receive exception: {e_info}') diff --git a/tests/unit/test_object_downloader.py b/tests/unit/test_object_downloader.py new file mode 100644 index 0000000..8237b1c --- /dev/null +++ b/tests/unit/test_object_downloader.py @@ -0,0 +1,141 @@ +#!/usr/bin/python +# coding: utf-8 -*- +# pylint: disable=logger-format-interpolation +# pylint: disable=dangerous-default-value +# flake8: noqa: W503 +# flake8: noqa: W1202 + +# import platform +import sys + +import pytest +from loguru import logger + +import eos_downloader +from eos_downloader.data import DATA_MAPPING +from eos_downloader.eos import EOSDownloader +from tests.lib.dataset import eos_dataset_invalid, eos_dataset_valid, eos_token, eos_token_invalid +from tests.lib.fixtures import create_download_instance +from tests.lib.helpers import default_filename, is_on_github_actions + +logger.remove() +logger.add(sys.stderr, level="DEBUG") + +@pytest.mark.usefixtures("create_download_instance") +@pytest.mark.parametrize("DOWNLOAD_INFO", eos_dataset_valid, ids=['EOS-sha512', 'EOS-md5' ,'vEOS-lab-no-hash']) +@pytest.mark.eos_download +class TestEosDownload_valid(): + def test_data(self, DOWNLOAD_INFO): + logger.info(f'test input: {DOWNLOAD_INFO}') + logger.info(f'test build: {self.eos_downloader.__dict__}') + + def test_eos_download_create(self, DOWNLOAD_INFO): + my_download = eos_downloader.eos.EOSDownloader( + image=DOWNLOAD_INFO['image'], + software=DOWNLOAD_INFO['software'], + version=DOWNLOAD_INFO['version'], + token=eos_token, + hash_method='sha512sum') + logger.info(my_download) + assert isinstance(my_download, eos_downloader.eos.EOSDownloader) + + def test_eos_download_repr_string(self, DOWNLOAD_INFO): + expected = f"{DOWNLOAD_INFO['software']} - {DOWNLOAD_INFO['image']} - {DOWNLOAD_INFO['version']}" + logger.info(self.eos_downloader) + assert str(self.eos_downloader) == expected + + def test_eos_download_build_filename(self, DOWNLOAD_INFO): + assert self.eos_downloader._build_filename() == DOWNLOAD_INFO['filename'] + + @pytest.mark.dependency(name='authentication') + @pytest.mark.skipif(eos_token == eos_token_invalid, reason="Token is not set correctly") + @pytest.mark.skipif(is_on_github_actions(), reason="Running on Github Runner") + # @pytest.mark.xfail(reason="Deliberate - CI not set for testing AUTH") + @pytest.mark.webtest + def test_eos_download_authenticate(self): + assert self.eos_downloader.authenticate() is True + + @pytest.mark.dependency(depends=["authentication"], scope='class') + @pytest.mark.webtest + def test_eos_download_get_remote_file_path(self, DOWNLOAD_INFO): + assert self.eos_downloader._get_remote_filepath() == DOWNLOAD_INFO['remote_path'] + + @pytest.mark.dependency(depends=["authentication"], scope='class') + @pytest.mark.webtest + def test_eos_download_get_file_url(self, DOWNLOAD_INFO): + url = self.eos_downloader._get_url(remote_file_path = DOWNLOAD_INFO['remote_path']) + logger.info(url) + assert 'https://downloads.arista.com/EOS-USA/Active%20Releases/' in url + +@pytest.mark.usefixtures("create_download_instance") +@pytest.mark.parametrize("DOWNLOAD_INFO", eos_dataset_invalid, ids=['EOS-FAKE']) +class TestEosDownload_invalid(): + + def test_data(self, DOWNLOAD_INFO): + logger.info(f'test input: {dict(DOWNLOAD_INFO)}') + logger.info(f'test build: {self.eos_downloader.__dict__}') + + def test_eos_download_login_error(self, DOWNLOAD_INFO): + my_download = eos_downloader.eos.EOSDownloader( + image=DOWNLOAD_INFO['image'], + software=DOWNLOAD_INFO['software'], + version=DOWNLOAD_INFO['version'], + token=eos_token_invalid, + hash_method=DOWNLOAD_INFO['expected_hash']) + assert my_download.authenticate() is False + + @pytest.mark.dependency(name='authentication') + @pytest.mark.skipif(eos_token == eos_token_invalid, reason="Token is not set correctly") + @pytest.mark.skipif(is_on_github_actions(), reason="Running on Github Runner") + # @pytest.mark.xfail(reason="Deliberate - CI not set for testing AUTH") + @pytest.mark.webtest + def test_eos_download_authenticate(self): + assert self.eos_downloader.authenticate() is True + + # SOFTWARE/PLATFORM TESTING + + # @pytest.mark.skip(reason="Not yet implemented in lib") + def test_eos_file_name_with_incorrect_software(self, DOWNLOAD_INFO): + self.eos_downloader.software = 'FAKE' + logger.info(f'test build: {self.eos_downloader.__dict__}') + with pytest.raises(ValueError) as e_info: + result = self.eos_downloader._build_filename() + logger.info(f'receive exception: {e_info}') + self.eos_downloader.software = DOWNLOAD_INFO['software'] + + @pytest.mark.webtest + @pytest.mark.dependency(depends=["authentication"], scope='class') + def test_eos_download_get_remote_file_path_for_invlaid_software(self, DOWNLOAD_INFO): + self.eos_downloader.software = 'FAKE' + logger.info(f'Platform set to: {self.eos_downloader.software}') + logger.info(f'test build: {self.eos_downloader.__dict__}') + with pytest.raises(ValueError) as e_info: + result = self.eos_downloader._build_filename() + logger.info(f'receive exception: {e_info}') + self.eos_downloader.software = DOWNLOAD_INFO['software'] + + # IMAGE TESTING + + def test_eos_file_name_with_incorrect_image(self, DOWNLOAD_INFO): + self.eos_downloader.image = 'FAKE' + logger.info(f'Image set to: {self.eos_downloader.image}') + assert DOWNLOAD_INFO['filename'] == self.eos_downloader._build_filename() + self.eos_downloader.software == DOWNLOAD_INFO['image'] + + @pytest.mark.webtest + @pytest.mark.dependency(depends=["authentication"], scope='class') + def test_eos_download_get_remote_file_path_for_invlaid_image(self, DOWNLOAD_INFO): + self.eos_downloader.image = 'FAKE' + logger.info(f'Image set to: {self.eos_downloader.image}') + assert self.eos_downloader.authenticate() is True + assert DOWNLOAD_INFO['filename'] == self.eos_downloader._build_filename() + self.eos_downloader.image = DOWNLOAD_INFO['image'] + + # VERSION TESTING + + @pytest.mark.webtest + @pytest.mark.dependency(depends=["authentication"], scope='class') + def test_eos_download_get_remote_file_path_for_invlaid_version(self, DOWNLOAD_INFO): + self.eos_downloader.version = 'FAKE' + logger.info(f'Version set to: {self.eos_downloader.version}') + assert self.eos_downloader._get_remote_filepath() == ''
\ No newline at end of file |