From 3c5da158c9914eec4342ab61f1d94501e4c7db18 Mon Sep 17 00:00:00 2001 From: pszulczewski Date: Tue, 28 Nov 2023 11:21:08 +0100 Subject: [PATCH] Fix default ports. --- pyntc/devices/aireos_device.py | 6 +++--- pyntc/devices/asa_device.py | 6 +++--- pyntc/devices/ios_device.py | 6 +++--- tests/unit/test_devices/test_aireos_device.py | 6 ++++++ tests/unit/test_devices/test_asa_device.py | 7 ++++++- tests/unit/test_devices/test_eos_device.py | 1 - tests/unit/test_devices/test_f5_device.py | 14 +++++++------- tests/unit/test_devices/test_ios_device.py | 8 ++++++++ 8 files changed, 36 insertions(+), 18 deletions(-) diff --git a/pyntc/devices/aireos_device.py b/pyntc/devices/aireos_device.py index 31fc8f6b..e1e76ec0 100644 --- a/pyntc/devices/aireos_device.py +++ b/pyntc/devices/aireos_device.py @@ -70,7 +70,7 @@ class AIREOSDevice(BaseDevice): active_redundancy_states = {None, "active"} def __init__( # nosec # pylint: disable=too-many-arguments - self, host, username, password, secret="", port=22, confirm_active=True, **kwargs + self, host, username, password, secret="", port=None, confirm_active=True, **kwargs ): # noqa: D403 """ PyNTC Device implementation for Cisco WLC. @@ -80,13 +80,13 @@ def __init__( # nosec # pylint: disable=too-many-arguments username (str): The username to authenticate with the device. password (str): The password to authenticate with the device. secret (str): The password to escalate privilege on the device. - port (int): The port to use to establish the connection. + port (int): The port to use to establish the connection. Defaults to 22. confirm_active (bool): Determines if device's high availability state should be validated before leaving connection open. """ super().__init__(host, username, password, device_type="cisco_aireos_ssh") self.native = None self.secret = secret - self.port = int(port) + self.port = int(port) if port else 22 self.global_delay_factor = kwargs.get("global_delay_factor", 1) self.delay_factor = kwargs.get("delay_factor", 1) self._connected = False diff --git a/pyntc/devices/asa_device.py b/pyntc/devices/asa_device.py index a69b4a01..a7f8c317 100644 --- a/pyntc/devices/asa_device.py +++ b/pyntc/devices/asa_device.py @@ -38,7 +38,7 @@ class ASADevice(BaseDevice): vendor = "cisco" active_redundancy_states = {None, "active"} - def __init__(self, host: str, username: str, password: str, secret="", port=22, **kwargs): # nosec + def __init__(self, host: str, username: str, password: str, secret="", port=None, **kwargs): # nosec """ Pyntc Device constructor for Cisco ASA. @@ -46,14 +46,14 @@ def __init__(self, host: str, username: str, password: str, secret="", port=22, host (str): The address of the network device. username (str): The username to authenticate to the device. password (str): The password to authenticate to the device. - secret (str, optional): The password to escalate privilege on the device. Defaults to "". + secret (str, optional): The password to escalate privilege on the device. Defaults to 22. port (int, optional): Port used to establish connection. Defaults to 22. """ super().__init__(host, username, password, device_type="cisco_asa_ssh") self.native: Optional[CiscoAsaSSH] = None self.secret = secret - self.port = int(port) + self.port = int(port) if port else 22 self.kwargs = kwargs self.global_delay_factor: int = kwargs.get("global_delay_factor", 1) self.delay_factor: int = kwargs.get("delay_factor", 1) diff --git a/pyntc/devices/ios_device.py b/pyntc/devices/ios_device.py index d277acb7..1b0fa6e4 100644 --- a/pyntc/devices/ios_device.py +++ b/pyntc/devices/ios_device.py @@ -44,7 +44,7 @@ class IOSDevice(BaseDevice): active_redundancy_states = {None, "active"} def __init__( # nosec - self, host, username, password, secret="", port=22, confirm_active=True, fast_cli=True, **kwargs + self, host, username, password, secret="", port=None, confirm_active=True, fast_cli=True, **kwargs ): # noqa: D403 """ PyNTC Device implementation for Cisco IOS. @@ -54,7 +54,7 @@ def __init__( # nosec username (str): The username to authenticate with the device. password (str): The password to authenticate with the device. secret (str): The password to escalate privilege on the device. - port (int): The port to use to establish the connection. + port (int): The port to use to establish the connection. Defaults to 22. confirm_active (bool): Determines if device's high availability state should be validated before leaving connection open. fast_cli (bool): Fast CLI mode for Netmiko, it is recommended to use False when opening the client on code upgrades """ @@ -62,7 +62,7 @@ def __init__( # nosec self.native = None self.secret = secret - self.port = int(port) + self.port = int(port) if port else 22 self.global_delay_factor = kwargs.get("global_delay_factor", 1) self.delay_factor = kwargs.get("delay_factor", 1) self._fast_cli = fast_cli diff --git a/tests/unit/test_devices/test_aireos_device.py b/tests/unit/test_devices/test_aireos_device.py index 9dfdde67..39c9a82c 100644 --- a/tests/unit/test_devices/test_aireos_device.py +++ b/tests/unit/test_devices/test_aireos_device.py @@ -1671,3 +1671,9 @@ def test_uptime_string(mock_uptime_components, aireos_device): def test_wlans(aireos_show, aireos_expected_wlans): device = aireos_show(["show_wlan_summary.txt"]) assert device.wlans == aireos_expected_wlans + + +@mock.patch.object(AIREOSDevice, "open") +def test_port_none(patch): + device = AIREOSDevice("host", "user", "pass", port=None) + assert device.port == 22 diff --git a/tests/unit/test_devices/test_asa_device.py b/tests/unit/test_devices/test_asa_device.py index 870f209c..f8521589 100644 --- a/tests/unit/test_devices/test_asa_device.py +++ b/tests/unit/test_devices/test_asa_device.py @@ -41,7 +41,6 @@ class TestASADevice: def setup(self, api): with mock.patch("pyntc.devices.asa_device.ConnectHandler") as api: - if not getattr(self, "device", None): self.device = ASADevice("host", "user", "password") @@ -891,3 +890,9 @@ def test_vlan(mock_get_vlans, asa_device): mock_get_vlans.return_value = expected vlans = asa_device.vlans assert vlans == [10, 20] + + +@mock.patch.object(ASADevice, "open") +def test_port_none(patch): + device = ASADevice("host", "user", "pass", port=None) + assert device.port == 22 diff --git a/tests/unit/test_devices/test_eos_device.py b/tests/unit/test_devices/test_eos_device.py index 5aa82099..cc0bc78d 100644 --- a/tests/unit/test_devices/test_eos_device.py +++ b/tests/unit/test_devices/test_eos_device.py @@ -212,7 +212,6 @@ def test_file_copy_remote_exists_bad_md5(self, mock_open, mock_close, mock_ssh, @mock.patch.object(EOSDevice, "close") @mock.patch("netmiko.arista.arista.AristaSSH", autospec=True) def test_file_copy_remote_not_exist(self, mock_open, mock_close, mock_ssh, mock_ft): - self.device.native_ssh = mock_open self.device.native_ssh.send_command_timing.side_effect = None self.device.native_ssh.send_command_timing.return_value = "flash: /dev/null" diff --git a/tests/unit/test_devices/test_f5_device.py b/tests/unit/test_devices/test_f5_device.py index 38d8d9ce..3e779798 100644 --- a/tests/unit/test_devices/test_f5_device.py +++ b/tests/unit/test_devices/test_f5_device.py @@ -132,7 +132,7 @@ def test_reboot(self): volume = VOLUME # skip the wait_for_device_reboot - with (mock.patch.object(self.device, "_wait_for_device_reboot", return_value=True)): + with mock.patch.object(self.device, "_wait_for_device_reboot", return_value=True): self.device.reboot(volume=volume) # # Check if _get_active_volume worked @@ -146,7 +146,7 @@ def test_reboot_with_timer(self): api.tm.sys.software.volumes.volume.load.return_value.active = True # skipping timeout! It's too long!! - with (mock.patch.object(self.device, "_wait_for_device_reboot", timeout=0)): + with mock.patch.object(self.device, "_wait_for_device_reboot", timeout=0): self.device.reboot(volume=volume) # # Check if _get_active_volume worked @@ -157,7 +157,7 @@ def test_reboot_with_timer(self): def test_reboot_no_volume(self): api = self.device.api_handler - with (mock.patch.object(self.device, "_wait_for_device_reboot", return_value=True)): + with mock.patch.object(self.device, "_wait_for_device_reboot", return_value=True): self.device.reboot() # Check if _reboot_to_volume worked @@ -175,7 +175,7 @@ def test_set_boot_options(self): # Patching out _volume_exists for _image_install api.tm.sys.software.volumes.volume.exists.return_value = True - with (mock.patch.object(self.device, "_wait_for_image_installed", timeout=0, return_value=None)): + with mock.patch.object(self.device, "_wait_for_image_installed", timeout=0, return_value=None): self.device.set_boot_options(image_name=image_name, volume=volume) api.tm.util.bash.exec_cmd.assert_called() @@ -194,7 +194,7 @@ def test_set_boot_options_no_image(self): # Patching out _volume_exists for _image_install api.tm.sys.software.volumes.volume.exists.return_value = False - with (mock.patch.object(self.device, "_wait_for_image_installed", timeout=0, return_value=None)): + with mock.patch.object(self.device, "_wait_for_image_installed", timeout=0, return_value=None): self.device.set_boot_options(image_name=image_name, volume=volume) api.tm.util.bash.exec_cmd.assert_called() @@ -215,7 +215,7 @@ def test_set_boot_options_bad_boot(self): # Patching out _volume_exists for _image_install api.tm.sys.software.volumes.volume.exists.return_value = False - with (mock.patch.object(self.device, "_wait_for_image_installed", timeout=0, return_value=None)): + with mock.patch.object(self.device, "_wait_for_image_installed", timeout=0, return_value=None): with pytest.raises(NTCFileNotFoundError): self.device.set_boot_options(image_name="bad_image", volume=volume) @@ -248,7 +248,7 @@ def test_install_os(self): # Patching out _image_install api.tm.sys.software.volumes.volume.exists.return_value = True - with (mock.patch.object(self.device, "_wait_for_image_installed", timeout=0, return_value=None)): + with mock.patch.object(self.device, "_wait_for_image_installed", timeout=0, return_value=None): self.device.install_os(image_name=image_name, volume=volume) api.tm.util.bash.exec_cmd.assert_called() diff --git a/tests/unit/test_devices/test_ios_device.py b/tests/unit/test_devices/test_ios_device.py index a6d4663b..e2e0ba52 100644 --- a/tests/unit/test_devices/test_ios_device.py +++ b/tests/unit/test_devices/test_ios_device.py @@ -393,6 +393,12 @@ def test_install_os_error(self, mock_wait, mock_reboot, mock_set_boot, mock_imag unittest.main() +@mock.patch.object(IOSDevice, "open") +def test_port_none(patch): + device = IOSDevice("host", "user", "pass", port=None) + assert device.port == 22 + + def test_check_command_output_for_errors(ios_device): command_passes = ios_device._check_command_output_for_errors("valid command", "valid output") assert command_passes is None @@ -978,6 +984,7 @@ def test_set_boot_options_image_packages_conf_file( # TESTS FOR IOS INSTALL MODE METHOD # + # Test install mode upgrade for install mode with latest method @mock.patch.object(IOSDevice, "os_version", new_callable=mock.PropertyMock) @mock.patch.object(IOSDevice, "_image_booted") @@ -1106,6 +1113,7 @@ def test_install_os_install_mode_no_upgrade( # FROM CISCO IOS EVEREST VERSION TESTS # + # Test install mode upgrade for install mode with interim method on OS Version @mock.patch.object(IOSDevice, "os_version", new_callable=mock.PropertyMock) @mock.patch.object(IOSDevice, "_image_booted")