From 71e23b0d6d0f5162ddcf90905c3f4e6d9d5d0593 Mon Sep 17 00:00:00 2001 From: jmussmann Date: Wed, 11 Sep 2024 12:24:02 +0200 Subject: [PATCH] add tests for iosxrd --- .../fixtures/cliconf_xrd/iosxr/show_inventory | 2 + .../iosxr/show_running-config_hostname | 1 + .../iosxr/show_version__utility_head_-n_20 | 16 +++ .../modules/network/iosxr/test_iosxr_xrd.py | 98 +++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_inventory create mode 100644 tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_running-config_hostname create mode 100644 tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_version__utility_head_-n_20 create mode 100644 tests/unit/modules/network/iosxr/test_iosxr_xrd.py diff --git a/tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_inventory b/tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_inventory new file mode 100644 index 000000000..f7e852df0 --- /dev/null +++ b/tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_inventory @@ -0,0 +1,2 @@ + ^ +% Invalid input detected at '^' marker. diff --git a/tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_running-config_hostname b/tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_running-config_hostname new file mode 100644 index 000000000..ebc5bf85b --- /dev/null +++ b/tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_running-config_hostname @@ -0,0 +1 @@ +hostname iosxr01 diff --git a/tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_version__utility_head_-n_20 b/tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_version__utility_head_-n_20 new file mode 100644 index 000000000..2f9c97b84 --- /dev/null +++ b/tests/unit/modules/network/iosxr/fixtures/cliconf_xrd/iosxr/show_version__utility_head_-n_20 @@ -0,0 +1,16 @@ +Wed Sep 11 09:56:44.796 UTC +Cisco IOS XR Software, Version 7.9.1 LNT +Copyright (c) 2013-2023 by Cisco Systems, Inc. + +Build Information: + Built By : ingunawa + Built On : Sun Apr 02 06:50:19 UTC 2023 + Build Host : iox-ucs-075 + Workspace : /auto/srcarchive15/prod/7.9.1/xrd-control-plane/ws + Version : 7.9.1 + Label : 7.9.1 + +cisco XRd Control Plane +cisco XRd-CP-C-01 processor with 64GB of memory +iosxr01 uptime is 1 day, 2 hours, 0 minutes +XRd Control Plane Containe diff --git a/tests/unit/modules/network/iosxr/test_iosxr_xrd.py b/tests/unit/modules/network/iosxr/test_iosxr_xrd.py new file mode 100644 index 000000000..71bab469b --- /dev/null +++ b/tests/unit/modules/network/iosxr/test_iosxr_xrd.py @@ -0,0 +1,98 @@ +# +# (c) 2022 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +from os import path +from unittest import TestCase +from unittest.mock import MagicMock + +from ansible.errors import AnsibleConnectionFailure +from ansible.module_utils._text import to_bytes, to_text + +from ansible_collections.cisco.iosxr.plugins.cliconf import iosxr + + +class TestPluginCLIConfIOSXR(TestCase): + """Test class for IOSXR CLI Conf Methods""" + + def setUp(self): + self._mock_connection = MagicMock() + self._prepare() + self._cliconf = iosxr.Cliconf(self._mock_connection) + self.maxDiff = None + + def _prepare(self, platform="iosxr"): + b_FIXTURE_DIR = b"%s/fixtures/cliconf_xrd/%s" % ( + to_bytes( + path.dirname(path.abspath(__file__)), + errors="surrogate_or_strict", + ), + to_bytes(platform), + ) + + def _connection_side_effect(*args, **kwargs): + try: + if args: + value = args[0] + else: + value = kwargs.get("command") + if value == b"show inventory": + raise AnsibleConnectionFailure + if b"|" in value: + value = value.replace(b"|", b"") + fixture_path = path.abspath( + b"%s/%s" % (b_FIXTURE_DIR, b"_".join(value.split(b" "))), + ) + with open(fixture_path, "rb") as file_desc: + return to_text(file_desc.read()) + except (OSError, IOError): + if args: + value = args[0] + return value + elif kwargs.get("command"): + value = kwargs.get("command") + return value + return "NO-OP" + + self._mock_connection.send.side_effect = _connection_side_effect + + def tearDown(self): + pass + + def test_get_device_info_iosxr(self): + """Test get_device_info for nxos""" + device_info = self._cliconf.get_device_info() + + mock_device_info = { + "network_os_version": "7.9.1 LNT", + "network_os": "iosxr", + "network_os_hostname": "iosxr01", + } + + self.assertEqual(device_info, mock_device_info) + + def test_get_command_output_iosxr(self): + """Test _get_command_with_output for iosxr""" + self._prepare() + cmd = self._cliconf.get_command_output("show running-config hostname") + + self.assertEqual(cmd, "hostname iosxr01")