| 1 |
# |
| 2 |
# (c) 2019 Red Hat Inc. |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
# |
| 19 |
from __future__ import absolute_import, division, print_function |
| 20 |
|
| 21 |
|
| 22 |
__metaclass__ = type |
| 23 |
|
| 24 |
import json |
| 25 |
|
| 26 |
from os import path |
| 27 |
|
| 28 |
|
| 29 |
try: |
| 30 |
from unittest.mock import MagicMock |
| 31 |
except ImportError: |
| 32 |
from mock import MagicMock |
| 33 |
|
| 34 |
from unittest import TestCase |
| 35 |
|
| 36 |
from ansible.module_utils._text import to_bytes |
| 37 |
|
| 38 |
from ansible_collections.cisco.ios.plugins.cliconf import ios |
| 39 |
|
| 40 |
|
| 41 |
b_FIXTURE_DIR = b"%s/fixtures/ios" % ( |
| 42 |
to_bytes(path.dirname(path.abspath(__file__)), errors="surrogate_or_strict") |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
def _connection_side_effect(*args, **kwargs): |
| 47 |
try: |
| 48 |
if args: |
| 49 |
value = args[0] |
| 50 |
else: |
| 51 |
value = kwargs.get("command") |
| 52 |
|
| 53 |
fixture_path = path.abspath( |
| 54 |
b"%s/%s" % (b_FIXTURE_DIR, b"_".join(value.split(b" "))), |
| 55 |
) |
| 56 |
with open(fixture_path, "rb") as file_desc: |
| 57 |
return file_desc.read() |
| 58 |
except (OSError, IOError): |
| 59 |
if args: |
| 60 |
value = args[0] |
| 61 |
return value |
| 62 |
elif kwargs.get("command"): |
| 63 |
value = kwargs.get("command") |
| 64 |
return value |
| 65 |
|
| 66 |
return "Nope" |
| 67 |
|
| 68 |
|
| 69 |
class TestPluginCLIConfIOS(TestCase): |
| 70 |
"""Test class for IOS CLI Conf Methods""" |
| 71 |
|
| 72 |
def setUp(self): |
| 73 |
self._mock_connection = MagicMock() |
| 74 |
self._mock_connection.send.side_effect = _connection_side_effect |
| 75 |
self._cliconf = ios.Cliconf(self._mock_connection) |
| 76 |
self.maxDiff = None |
| 77 |
|
| 78 |
def tearDown(self): |
| 79 |
pass |
| 80 |
|
| 81 |
def test_get_device_info(self): |
| 82 |
"""Test get_device_info""" |
| 83 |
device_info = self._cliconf.get_device_info() |
| 84 |
|
| 85 |
mock_device_info = { |
| 86 |
"network_os": "ios", |
| 87 |
"network_os_model": "CSR1000V", |
| 88 |
"network_os_version": "16.06.01", |
| 89 |
"network_os_hostname": "an-csr-01", |
| 90 |
"network_os_image": "bootflash:packages.conf", |
| 91 |
"network_os_type": "L2", |
| 92 |
} |
| 93 |
|
| 94 |
self.assertEqual(device_info, mock_device_info) |
| 95 |
|
| 96 |
def test_get_capabilities(self): |
| 97 |
"""Test get_capabilities""" |
| 98 |
capabilities = json.loads(self._cliconf.get_capabilities()) |
| 99 |
mock_capabilities = { |
| 100 |
"device_info": { |
| 101 |
"network_os": "ios", |
| 102 |
"network_os_hostname": "an-csr-01", |
| 103 |
"network_os_image": "bootflash:packages.conf", |
| 104 |
"network_os_model": "CSR1000V", |
| 105 |
"network_os_type": "L2", |
| 106 |
"network_os_version": "16.06.01", |
| 107 |
}, |
| 108 |
"device_operations": { |
| 109 |
"supports_commit": False, |
| 110 |
"supports_commit_comment": False, |
| 111 |
"supports_defaults": True, |
| 112 |
"supports_diff_ignore_lines": True, |
| 113 |
"supports_diff_match": True, |
| 114 |
"supports_diff_replace": True, |
| 115 |
"supports_generate_diff": True, |
| 116 |
"supports_multiline_delimiter": True, |
| 117 |
"supports_onbox_diff": False, |
| 118 |
"supports_replace": False, |
| 119 |
"supports_rollback": False, |
| 120 |
}, |
| 121 |
"diff_match": ["line", "strict", "exact", "none"], |
| 122 |
"diff_replace": ["line", "block"], |
| 123 |
"format": ["text"], |
| 124 |
"network_api": "cliconf", |
| 125 |
"output": [], |
| 126 |
"rpc": [ |
| 127 |
"edit_config", |
| 128 |
"enable_response_logging", |
| 129 |
"get", |
| 130 |
"get_capabilities", |
| 131 |
"get_config", |
| 132 |
"disable_response_logging", |
| 133 |
"run_commands", |
| 134 |
"edit_banner", |
| 135 |
"get_diff", |
| 136 |
"run_commands", |
| 137 |
"get_defaults_flag", |
| 138 |
], |
| 139 |
} |
| 140 |
self.assertEqual(sorted(mock_capabilities), sorted(capabilities)) |