def test_verify_module_status_negative(self): device = Device(name='Router', os='iosxe', custom=dict(abstraction=dict(order=['os']))) device.is_connected = Mock(return_value=True) device.cli = Mock() device.cli.execute = Mock(return_value=dedent(''' Chassis type: ISR4451-X/K9 Slot Type State Insert time (ago) --------- ------------------- --------------------- ----------------- 0 ISR4451-X/K9 ok 00:02:59 0/0 ISR4451-X-4x1GE ok 00:01:58 1 ISR4451-X/K9 ok 00:02:59 2 ISR4451-X/K9 ok 00:02:59 R0 ISR4451-X/K9 fail 00:02:59 F0 ISR4451-X/K9 ok, active 00:02:59 P0 PWR-4450-AC ok 00:02:32 P1 Unknown empty never P2 ACS-4450-FANASSY ok 00:02:32 Slot CPLD Version Firmware Version --------- ------------------- --------------------------------------- 0 16092742 16.12(2r) 1 16092742 16.12(2r) 2 16092742 16.12(2r) R0 16092742 16.12(2r) F0 16092742 16.12(2r) ''')) with self.assertRaisesRegex( Exception, "Modules on 'Router' are not in stable state"): verify_module_status(device, timeout=0.3, interval=0.1)
def test_get_mgmt_ip_and_mgmt_src_ip_addresses(self): device = Device( name='Router', os='iosxr', platform='c8000', custom=dict(abstraction=dict(order=['os', 'platform'])) ) device.is_connected = Mock(return_value=True) device.execute = Mock(return_value=dedent(''' Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:179 0.0.0.0:* LISTEN 1131/bgp tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7756/sshd tcp 0 0 0.0.0.0:830 0.0.0.0:* LISTEN 7756/sshd tcp 0 188 1.1.1.95:22 2.2.2.145:50173 ESTABLISHED 12021/sshd: admin [ tcp6 0 0 :::179 :::* LISTEN 1131/bgp tcp6 0 0 :::22 :::* LISTEN 7756/sshd tcp6 0 0 :::830 :::* LISTEN 7756/sshd ''')) with self.assertLogs('genie.libs.sdk.apis.iosxr.c8000.utils') as cm: result = get_mgmt_ip_and_mgmt_src_ip_addresses(device) self.assertEqual(cm.output,[ "INFO:genie.libs.sdk.apis.iosxr.c8000.utils:Device management IP: 1.1.1.95", "INFO:genie.libs.sdk.apis.iosxr.c8000.utils:Device management source IP addresses: {'2.2.2.145'}" ]) self.assertEqual(result, ('1.1.1.95', {'2.2.2.145'})) device.execute.assert_has_calls([call('bash netstat -antp')])
def add_device(hostname, os_type, testbed, device_type='switch', ip_addr=None, log_stdout=False): """ This function will create a Device instance and initiate the connection with log_stdout disabled by default. If the device already exists and is connected then this function will return what already exists. Examples -------- dev = add_device('switch1', 'nxos', testbed) dev.parse('show version') Parameters ---------- hostname : str The hostname of the device os_type : str The OS type of the device. Must be one of the values listed on the docs website: https://pubhub.devnetcloud.com/media/pyats-getting-started/docs/quickstart/manageconnections.html#manage-connections testbed : Testbed The testbed attributed from the loaded testbed file device_type : str User device device-type string ip_addr : str Optional. The IP address for the hostname. If given, this value will be used to open the connection. If not given, then the `hostname` parameter must be in DNS. log_stdout : bool Optional, default=False. Controls the initial connection setting to disable/enable stdout logging. Returns ------- Device Connected device instance """ # see if the device already exists and is connected. If it is, then return # what we have, otherwise proceed to create a new device and connect. has_device = testbed.devices.get(hostname) if has_device: if has_device.is_connected(): return has_device else: del testbed.devices[hostname] dev = Device(hostname, os=os_type, # required type=device_type, # optional # genie uses the 'custom' field to select parsers by os_type custom={'abstraction': {'order': ['os']}}, # connect only using SSH, prevent genie from making config # changes to the device during the login process. connections=make_ssh_conn(ip_addr or hostname)) testbed.add_device(dev) return dev
def add_device(hostname, os_type, testbed, device_type='switch', ip_addr=None): """ This function will create a Device instance that can then be used to connect, execute, and parse commands. Examples -------- dev = add_device('switch1', 'nxos', testbed) dev.connect() dev.parse('show version') Parameters ---------- hostname : str The hostname of the device os_type : str The OS type of the device. Must be one of the values listed on the docs website: https://pubhub.devnetcloud.com/media/pyats-getting-started/docs/quickstart/manageconnections.html#manage-connections testbed : Testbed The testbed attributed from the loaded testbed file device_type : str User device device-type string ip_addr : str Optional. The IP address for the hostname. If given, this value will be used to open the connection. If not given, then the `hostname` parameter must be in DNS. Returns ------- Device New device instance that you can then use to execute the `.connect()` method. """ # if the hostname already exists in the testbed, then remove it so that it # can be re-added. if hostname in testbed.devices: del testbed.devices[hostname] dev = Device( hostname, os=os_type, type=device_type, custom={'abstraction': { 'order': ['os'] }}, # genie uses this to select parsers by os_type connections=make_ssh_conn(ip_addr or hostname)) testbed.add_device(dev) return dev
def test_verify_module_status(self): device = Device(name='Router', os='iosxe', custom=dict(abstraction=dict(order=['os']))) device.is_connected = Mock(return_value=True) device.cli = Mock() device.cli.execute = Mock(return_value=dedent(''' Chassis type: ISR4451-X/K9 Slot Type State Insert time (ago) --------- ------------------- --------------------- ----------------- 0 ISR4451-X/K9 ok 00:02:59 0/0 ISR4451-X-4x1GE ok 00:01:58 1 ISR4451-X/K9 ok 00:02:59 2 ISR4451-X/K9 ok 00:02:59 R0 ISR4451-X/K9 ok, active 00:02:59 F0 ISR4451-X/K9 ok, active 00:02:59 P0 PWR-4450-AC ok 00:02:32 P1 Unknown empty never P2 ACS-4450-FANASSY ok 00:02:32 Slot CPLD Version Firmware Version --------- ------------------- --------------------------------------- 0 16092742 16.12(2r) 1 16092742 16.12(2r) 2 16092742 16.12(2r) R0 16092742 16.12(2r) F0 16092742 16.12(2r) ''')) with self.assertLogs( 'genie.libs.sdk.apis.iosxe.platform.verify') as cm: verify_module_status(device) self.assertEqual(cm.output, [ "INFO:genie.libs.sdk.apis.iosxe.platform.verify:All modules on 'Router' are in stable state" ]) device.cli.execute.assert_has_calls([call('show platform')])
def _make_device(_os): return Device(hostname, os=_os, # required # genie uses the 'custom' field to select parsers by os_type custom={'abstraction': {'order': ['os']}}, # connect only using SSH, prevent genie from making config # changes to the device during the login process. connections={'default': dict(host=(ipaddr or hostname), arguments=dict(init_config_commands=[], init_exec_commands=[]), protocol='ssh')})