Skip to content

Commit

Permalink
Fix default ports.
Browse files Browse the repository at this point in the history
  • Loading branch information
pszulczewski committed Nov 28, 2023
1 parent 74c99a4 commit 3c5da15
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
6 changes: 3 additions & 3 deletions pyntc/devices/aireos_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pyntc/devices/asa_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ 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.
Args:
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)
Expand Down
6 changes: 3 additions & 3 deletions pyntc/devices/ios_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -54,15 +54,15 @@ 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
"""
super().__init__(host, username, password, device_type="cisco_ios_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._fast_cli = fast_cli
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_devices/test_aireos_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion tests/unit/test_devices/test_asa_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
1 change: 0 additions & 1 deletion tests/unit/test_devices/test_eos_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/test_devices/test_f5_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_devices/test_ios_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 3c5da15

Please sign in to comment.