From db529c763781d3eebe14e2f1420cb59e1b76cd48 Mon Sep 17 00:00:00 2001 From: Allen Robel Date: Sat, 23 Dec 2023 15:12:42 -1000 Subject: [PATCH] Move unit tests for common classes out of image_upgrade ControllerVersion and Log were initially written as specific to image_upgrade, and their unit tests lived in: tests/unit/modules/dcnm/dcnm_image_upgrade These classes have moved to plugins/module_utils/common and so we've moved their unit test scripts, fixtures, and other supporting utilities to: tests/unit/module_utils/common --- tests/unit/module_utils/__init__.py | 0 tests/unit/module_utils/common/__init__.py | 0 .../unit/module_utils/common/common_utils.py | 114 +++++ tests/unit/module_utils/common/fixture.py | 50 ++ .../fixtures/responses_ControllerVersion.json | 453 ++++++++++++++++++ .../common/test_controller_version.py} | 4 +- tests/unit/module_utils/common/test_log.py | 146 ++++++ .../common/test_params_validate.py} | 85 +--- ...e_upgrade_responses_ControllerVersion.json | 451 ----------------- 9 files changed, 767 insertions(+), 536 deletions(-) create mode 100644 tests/unit/module_utils/__init__.py create mode 100644 tests/unit/module_utils/common/__init__.py create mode 100644 tests/unit/module_utils/common/common_utils.py create mode 100644 tests/unit/module_utils/common/fixture.py create mode 100644 tests/unit/module_utils/common/fixtures/responses_ControllerVersion.json rename tests/unit/{modules/dcnm/dcnm_image_upgrade/test_image_upgrade_controller_version.py => module_utils/common/test_controller_version.py} (99%) create mode 100644 tests/unit/module_utils/common/test_log.py rename tests/unit/{modules/dcnm/dcnm_image_upgrade/test_image_upgrade_params_validate.py => module_utils/common/test_params_validate.py} (89%) diff --git a/tests/unit/module_utils/__init__.py b/tests/unit/module_utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit/module_utils/common/__init__.py b/tests/unit/module_utils/common/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit/module_utils/common/common_utils.py b/tests/unit/module_utils/common/common_utils.py new file mode 100644 index 000000000..d234b812a --- /dev/null +++ b/tests/unit/module_utils/common/common_utils.py @@ -0,0 +1,114 @@ +# Copyright (c) 2024 Cisco and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + + +from contextlib import contextmanager +from typing import Any, Dict + +import pytest +from ansible_collections.ansible.netcommon.tests.unit.modules.utils import \ + AnsibleFailJson +from ansible_collections.cisco.dcnm.plugins.module_utils.common.controller_version import \ + ControllerVersion +from ansible_collections.cisco.dcnm.plugins.module_utils.common.log import \ + Log +from ansible_collections.cisco.dcnm.plugins.module_utils.common.params_validate import \ + ParamsValidate + +from .fixture import load_fixture + + +class MockAnsibleModule: + """ + Mock the AnsibleModule class + """ + + params = {"config": {"switches": [{"ip_address": "172.22.150.105"}]}} + argument_spec = { + "config": {"required": True, "type": "dict"}, + "state": {"default": "merged", "choices": ["merged", "deleted", "query"]}, + } + supports_check_mode = True + + @staticmethod + def fail_json(msg) -> AnsibleFailJson: + """ + mock the fail_json method + """ + raise AnsibleFailJson(msg) + + def public_method_for_pylint(self) -> Any: + """ + Add one public method to appease pylint + """ + + +# See the following for explanation of why fixtures are explicitely named +# https://pylint.pycqa.org/en/latest/user_guide/messages/warning/redefined-outer-name.html + + +@pytest.fixture(name="controller_version") +def controller_version_fixture(): + """ + mock ControllerVersion + """ + return ControllerVersion(MockAnsibleModule) + + +@pytest.fixture(name="log") +def log_fixture(): + """ + mock Log + """ + return Log(MockAnsibleModule) + + +@pytest.fixture(name="params_validate") +def params_validate_fixture(): + """ + mock ParamsValidate + """ + return ParamsValidate(MockAnsibleModule) + + +@contextmanager +def does_not_raise(): + """ + A context manager that does not raise an exception. + """ + yield + + +def load_playbook_config(key: str) -> Dict[str, str]: + """ + Return playbook configs for common + """ + playbook_file = "common_playbook_configs" + playbook_config = load_fixture(playbook_file).get(key) + print(f"load_playbook_config: {key} : {playbook_config}") + return playbook_config + + +def responses_controller_version(key: str) -> Dict[str, str]: + """ + Return ControllerVersion controller responses + """ + response_file = "responses_ControllerVersion" + response = load_fixture(response_file).get(key) + print(f"responses_controller_version: {key} : {response}") + return response diff --git a/tests/unit/module_utils/common/fixture.py b/tests/unit/module_utils/common/fixture.py new file mode 100644 index 000000000..bb3730787 --- /dev/null +++ b/tests/unit/module_utils/common/fixture.py @@ -0,0 +1,50 @@ +# Copyright (c) 2024 Cisco and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import json +import os +import sys + +fixture_path = os.path.join(os.path.dirname(__file__), "fixtures") + + +def load_fixture(filename): + """ + load test inputs from json files + """ + path = os.path.join(fixture_path, f"{filename}.json") + + try: + with open(path, encoding="utf-8") as file_handle: + data = file_handle.read() + except IOError as exception: + msg = f"Exception opening test input file {filename}.json : " + msg += f"Exception detail: {exception}" + print(msg) + sys.exit(1) + + try: + fixture = json.loads(data) + except json.JSONDecodeError as exception: + msg = "Exception reading JSON contents in " + msg += f"test input file {filename}.json : " + msg += f"Exception detail: {exception}" + print(msg) + sys.exit(1) + + return fixture diff --git a/tests/unit/module_utils/common/fixtures/responses_ControllerVersion.json b/tests/unit/module_utils/common/fixtures/responses_ControllerVersion.json new file mode 100644 index 000000000..e14e07e28 --- /dev/null +++ b/tests/unit/module_utils/common/fixtures/responses_ControllerVersion.json @@ -0,0 +1,453 @@ +{ + "test_common_version_00009a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00010a": { + "RETURN_CODE": 404, + "METHOD": "GET", + "REQUEST_PATH": "https://foo/noop", + "MESSAGE": "Not Found", + "DATA": {} + }, + "test_common_version_00011a": { + "RETURN_CODE": 500, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "Internal Server Error", + "DATA": {} + }, + "test_common_version_00002a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00002b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "true", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00002c": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00003a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00003b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00004b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00004a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "true", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00004c": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00006b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00006a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "true" + } + }, + "test_common_version_00006c": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "" + } + }, + "test_common_version_00005b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00005a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "true", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00005c": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00007a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00008a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK" + }, + "test_common_version_00012a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00012b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00013a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "foo-uuid", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00013b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00014a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00014b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00015a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00015b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00016a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00016b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00017a": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "version": "12.1.3b", + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "install": "EASYFABRIC", + "uuid": "", + "is_upgrade_inprogress": "false" + } + }, + "test_common_version_00017b": { + "RETURN_CODE": 200, + "METHOD": "GET", + "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", + "MESSAGE": "OK", + "DATA": { + "mode": "LAN", + "isMediaController": "false", + "dev": "false", + "isHaEnabled": "false", + "uuid": "", + "is_upgrade_inprogress": "false" + } + } +} \ No newline at end of file diff --git a/tests/unit/modules/dcnm/dcnm_image_upgrade/test_image_upgrade_controller_version.py b/tests/unit/module_utils/common/test_controller_version.py similarity index 99% rename from tests/unit/modules/dcnm/dcnm_image_upgrade/test_image_upgrade_controller_version.py rename to tests/unit/module_utils/common/test_controller_version.py index cd3c64456..ff108d6b6 100644 --- a/tests/unit/modules/dcnm/dcnm_image_upgrade/test_image_upgrade_controller_version.py +++ b/tests/unit/module_utils/common/test_controller_version.py @@ -32,8 +32,8 @@ from ansible_collections.ansible.netcommon.tests.unit.modules.utils import \ AnsibleFailJson -from .image_upgrade_utils import (controller_version_fixture, - responses_controller_version) +from ansible_collections.cisco.dcnm.tests.unit.module_utils.common.common_utils import ( + controller_version_fixture, responses_controller_version) PATCH_MODULE_UTILS = "ansible_collections.cisco.dcnm.plugins.module_utils." PATCH_COMMON = PATCH_MODULE_UTILS + "common." diff --git a/tests/unit/module_utils/common/test_log.py b/tests/unit/module_utils/common/test_log.py new file mode 100644 index 000000000..b25ada2c1 --- /dev/null +++ b/tests/unit/module_utils/common/test_log.py @@ -0,0 +1,146 @@ +# Copyright (c) 2024 Cisco and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# See the following regarding *_fixture imports +# https://pylint.pycqa.org/en/latest/user_guide/messages/warning/redefined-outer-name.html +# Due to the above, we also need to disable unused-import +# pylint: disable=unused-import +# Some fixtures need to use *args to match the signature of the function they are mocking +# pylint: disable=unused-argument +# Some tests require calling protected methods +# pylint: disable=protected-access + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +__copyright__ = "Copyright (c) 2024 Cisco and/or its affiliates." +__author__ = "Allen Robel" + +from typing import Any, Dict + +import pytest +from ansible_collections.ansible.netcommon.tests.unit.modules.utils import \ + AnsibleFailJson +from ansible_collections.cisco.dcnm.tests.unit.module_utils.common.common_utils import ( + does_not_raise, log_fixture) + + +def test_log_00010(log) -> None: + """ + Function + - log_msg + + Test + - log_msg returns None when debug is False + """ + with does_not_raise(): + instance = log + + error_message = "This is an error message" + instance.debug = False + assert instance.log_msg(error_message) is None + + +def test_log_00011(tmp_path, log) -> None: + """ + Function + - log_msg + + Test + - log_msg does not write to the logfile when debug is False + """ + directory = tmp_path / "test_log_msg" + directory.mkdir() + filename = directory / "test_log_msg.txt" + + msg = "This is an error message" + + with does_not_raise(): + instance = log + instance.debug = False + instance.logfile = filename + instance.log_msg(msg) + + match = r"\[Errno 2\] " + match += "No such file or directory" + with pytest.raises(FileNotFoundError, match=match): + filename.read_text(encoding="UTF-8") + + +def test_log_00012(tmp_path, log) -> None: + """ + Function + - log_msg + + Test + - log_msg writes to the logfile when debug is True + """ + directory = tmp_path / "test_log_msg" + directory.mkdir() + filename = directory / "test_log_msg.txt" + + msg = "This is an error message" + + with does_not_raise(): + instance = log + instance.debug = True + instance.logfile = filename + instance.log_msg(msg) + + assert filename.read_text(encoding="UTF-8") == msg + "\n" + + +def test_log_00013(tmp_path, log) -> None: + """ + Function + - log_msg + + Test + - log_msg calls fail_json if the logfile cannot be opened + + Description + To ensure an error is generated, we attempt a write to a filename + that is too long for the target OS. + """ + directory = tmp_path / "test_log_msg" + directory.mkdir() + filename = directory / f"test_{'a' * 2000}_log_msg.txt" + + msg = "This is an error message" + + with does_not_raise(): + instance = log + instance.debug = True + instance.logfile = filename + + match = "error writing to logfile" + with pytest.raises(AnsibleFailJson, match=match): + instance.log_msg(msg) + + +def test_log_00020(log) -> None: + """ + Function + - debug + + Test + - log_msg calls fail_json if debug is not a boolean + """ + with does_not_raise(): + instance = log + + match = "Invalid type for debug. Expected bool. " + with pytest.raises(AnsibleFailJson, match=match): + instance.debug = 10 diff --git a/tests/unit/modules/dcnm/dcnm_image_upgrade/test_image_upgrade_params_validate.py b/tests/unit/module_utils/common/test_params_validate.py similarity index 89% rename from tests/unit/modules/dcnm/dcnm_image_upgrade/test_image_upgrade_params_validate.py rename to tests/unit/module_utils/common/test_params_validate.py index 3b59e0a04..41910d2a0 100644 --- a/tests/unit/modules/dcnm/dcnm_image_upgrade/test_image_upgrade_params_validate.py +++ b/tests/unit/module_utils/common/test_params_validate.py @@ -35,22 +35,8 @@ AnsibleFailJson from ansible_collections.cisco.dcnm.plugins.module_utils.common.params_validate import \ ParamsValidate - -from .image_upgrade_utils import (MockAnsibleModule, does_not_raise, - image_upgrade_fixture, - issu_details_by_ip_address_fixture, - params_validate_fixture, - payloads_image_upgrade, - responses_image_install_options, - responses_image_upgrade, - responses_switch_issu_details) - -PATCH_MODULE_UTILS = "ansible_collections.cisco.dcnm.plugins.module_utils." -PATCH_IMAGE_MGMT = PATCH_MODULE_UTILS + "image_mgmt." - -DCNM_SEND_IMAGE_UPGRADE = PATCH_IMAGE_MGMT + "image_upgrade.dcnm_send" -DCNM_SEND_INSTALL_OPTIONS = PATCH_IMAGE_MGMT + "install_options.dcnm_send" -DCNM_SEND_ISSU_DETAILS = PATCH_IMAGE_MGMT + "switch_issu_details.dcnm_send" +from ansible_collections.cisco.dcnm.tests.unit.module_utils.common.common_utils import ( + does_not_raise, params_validate_fixture) def test_params_validate_00001(params_validate) -> None: @@ -875,70 +861,3 @@ def test_params_validate_00093(params_validate) -> None: with pytest.raises(AnsibleFailJson, match=match): instance.validate() - - -# tests 00110 - 00112 are taken from test_image_upgrade_common.py -# and have the same numbering. These should be moved to a common -# test module when log_msg (or an equivilent) is moved to a -# common module -def test_params_validate_00110(params_validate) -> None: - """ - Function - - log.log_msg - - Test - - log.log_msg returns None when debug is False - """ - instance = params_validate - - error_message = "This is an error message" - instance.debug = False - assert instance.log.log_msg(error_message) is None - - -def test_params_validate_00111(tmp_path, params_validate) -> None: - """ - Function - - log_msg - - Test - - log_msg writes to the logfile when debug is True - """ - instance = params_validate - - directory = tmp_path / "test_log_msg" - directory.mkdir() - filename = directory / "test_log_msg.txt" - - error_message = "This is an error message" - instance.debug = True - instance.logfile = filename - instance.log.log_msg(error_message) - - assert filename.read_text(encoding="UTF-8") == error_message + "\n" - assert len(list(tmp_path.iterdir())) == 1 - - -def test_params_validate_00112(tmp_path, params_validate) -> None: - """ - Function - - log_msg - - Test - - log_msg calls fail_json if the logfile cannot be opened - - Description - To ensure an error is generated, we attempt a write to a filename - that is too long for the target OS. - """ - instance = params_validate - - directory = tmp_path / "test_log_msg" - directory.mkdir() - filename = directory / f"test_{'a' * 2000}_log_msg.txt" - - error_message = "This is an error message" - instance.debug = True - instance.logfile = filename - with pytest.raises(AnsibleFailJson, match="error writing to logfile"): - instance.log.log_msg(error_message) diff --git a/tests/unit/modules/dcnm/dcnm_image_upgrade/fixtures/image_upgrade_responses_ControllerVersion.json b/tests/unit/modules/dcnm/dcnm_image_upgrade/fixtures/image_upgrade_responses_ControllerVersion.json index 049321107..a4905185c 100644 --- a/tests/unit/modules/dcnm/dcnm_image_upgrade/fixtures/image_upgrade_responses_ControllerVersion.json +++ b/tests/unit/modules/dcnm/dcnm_image_upgrade/fixtures/image_upgrade_responses_ControllerVersion.json @@ -1,455 +1,4 @@ { - "test_common_version_00009a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00010a": { - "RETURN_CODE": 404, - "METHOD": "GET", - "REQUEST_PATH": "https://foo/noop", - "MESSAGE": "Not Found", - "DATA": {} - }, - "test_common_version_00011a": { - "RETURN_CODE": 500, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "Internal Server Error", - "DATA": {} - }, - "test_common_version_00002a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00002b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "true", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00002c": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00003a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00003b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00004b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00004a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "true", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00004c": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00006b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00006a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "true" - } - }, - "test_common_version_00006c": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "" - } - }, - "test_common_version_00005b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00005a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "true", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00005c": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00007a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00008a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK" - }, - "test_common_version_00012a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00012b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00013a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "foo-uuid", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00013b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00014a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00014b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00015a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00015b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00016a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00016b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00017a": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "version": "12.1.3b", - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "install": "EASYFABRIC", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, - "test_common_version_00017b": { - "RETURN_CODE": 200, - "METHOD": "GET", - "REQUEST_PATH": "https://172.22.150.244:443/appcenter/cisco/ndfc/api/v1/fm/about/version", - "MESSAGE": "OK", - "DATA": { - "mode": "LAN", - "isMediaController": "false", - "dev": "false", - "isHaEnabled": "false", - "uuid": "", - "is_upgrade_inprogress": "false" - } - }, "test_image_mgmt_stage_00003a": { "RETURN_CODE": 200, "METHOD": "GET",