-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
0ffad67
commit db529c7
Showing
9 changed files
with
767 additions
and
536 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.