def issu_runversion(device, timeout=300, reconnect_via=None): """ Execute issu runversion on device Args: device ('obj'): Device object timeout ('int'): Timeout in seconds Raise: Exception Failed to reconnect to device Returns: None """ log.info("Running version") try: output = device.execute("issu runversion", timeout=timeout) except SubCommandFailure: # Timeout Unicon SubCommandFailure expected # Wait a bit as the device is booting with the ISSU upgrade image pass log.info("Reconnecting device") try: reconnect_device(device=device, max_time=timeout, via=reconnect_via) except Exception as e: log.error("Failed to reconnect to device {dev}") raise ConnectionError( "Failed to connect to device {dev}".format(dev=device.name))
def test_stage_connect(self): self.device.connect = Mock(side_effect=ConnectionError('negative test')) self.device.destroy_all = Mock(return_value="") # Execute stage: connect with self.assertRaises(AEtestFailedSignal): connect(self.section, self.device)
def test_run_error(self, mock_members, mock_import, mock_os, mock_logger): test_arguments = '' self.device.is_connected.return_value = False self.device.connect.side_effect = ConnectionError() ut_gen = TestGenerator( self.testbed, self.device.name, module='fake_module', api='fake_api', test_arguments=test_arguments ) ut_gen.destination = '' mock_os.makedirs.return_value = True mock_os.path.isdir.return_value = False self.assertRaises(SystemExit, ut_gen.run)
def reconnect_device(device, max_time=300, interval=30, sleep_disconnect=30, via=None): """ Reconnect device Args: device ('obj'): Device object max_time ('int'): Max time in seconds trying to connect to device interval ('int'): Interval in seconds of checking connection sleep_disconnect ('int'): Waiting time after device disconnection Raise: ConnectionError Returns: N/A """ destroy_connection(device=device) time.sleep(sleep_disconnect) timeout = Timeout(max_time=max_time, interval=interval) while timeout.iterate(): try: if via: device.connect(via=via) else: device.connect() _error_patterns(device=device) except Exception as e: log.info("Device {dev} is not connected".format(dev=device.name)) destroy_connection(device=device) timeout.sleep() continue if device.is_connected(): break timeout.sleep() if not device.is_connected(): raise ConnectionError( "Could not reconnect to device {dev}".format(dev=device.name)) log.info("Reconnected to device {dev}".format(dev=device.name))
def downgrade_issu_image_on_router(device, upgraded_image, downgrade_image, disk="bootflash:", timeout=500): """ Execute software downgrade on router Args: device ('obj'): Device object upgraded_image ('str'): Name of current installed image downgrade_image ('str'): Name of image to be used in downgrade disk ('str'): Disk name timeout ('int'): Timeout in seconds Raise: SubCommandFailure: Failed downgrading image on device ConnectionError: Failed reconnecting to device Returns: None """ commands = ("no boot system {disk}{upgraded_image}\n" "boot system {disk}{downgrade_image}".format( downgrade_image=downgrade_image, disk=disk, upgraded_image=upgraded_image, )) slot_number = get_platform_standby_rp(device=device) if not slot_number: raise ValueError("Could not retrieve standby rp slot number") standby_slot = "R{}".format(slot_number) if not is_platform_slot_in_state( device=device, slot=standby_slot, state="ok, standby"): raise ValueError("Slot {slot} is not in 'ok, standby' state".format( slot=standby_slot)) log.info("Applying configuration to device {dev}".format(dev=device.name)) try: device.configure(commands) except SubCommandFailure as e: raise SubCommandFailure( "Could not apply the following configuratin on " "device {dev}:\n{config}".format(dev=device.name, config=commands)) log.info("Saving changes on device {dev}".format(dev=device.name)) try: device.execute("write memory", timeout=timeout) except SubCommandFailure as e: raise SubCommandFailure("Could not execute command 'write memory' on " "device {dev}".format(dev=device.name)) log.info("Reloading device {dev}".format(dev=device.name)) try: device.reload(timeout=timeout) except SubCommandFailure as e: # Timeout Unicon SubCommandFailure expected pass log.info("Waiting {sec} seconds before reconnecting to device".format( sec=timeout)) time.sleep(timeout) log.info("Reconnecting device") try: reconnect_device(device=device, max_time=timeout) except Exception as e: raise ConnectionError( "Failed to connect to device {dev}".format(dev=device.name))
def permission_denied(spawn): """ handles connection refused scenarios """ raise ConnectionError('Permission denied for device "%s"' % (str(spawn)))
def permission_denied(spawn): raise ConnectionError('Permission denied for device {}'.format(spawn))