def test_time(self): mock_time = self.patch_time() initial_time = mock_time.current_time self.assertEqual(mock_time.current_time, tobiko.time()) self.assertEqual(mock_time.current_time, tobiko.time()) self.assertEqual(mock_time.current_time, tobiko.time()) final_time = mock_time.current_time self.assertEqual(3 * mock_time.time_increment, final_time - initial_time) mock_time.time.assert_has_calls([mock.call()] * 3, any_order=True)
def run_operation(self): self.is_rebooted = False self.start_time = None for attempt in tobiko.retry( timeout=self.timeout, default_timeout=self.default_wait_timeout, default_count=self.default_wait_count, default_interval=self.default_wait_interval): try: channel = self.ssh_client.connect( connection_timeout=attempt.time_left, retry_count=1) LOG.info("Executing reboot command on host " f"'{self.hostname}' (command='{self.command}')... ") self.start_time = tobiko.time() channel.exec_command(str(self.command)) except Exception as ex: if attempt.time_left > 0.: LOG.debug(f"Unable to reboot remote host " f"(time_left={attempt.time_left}): {ex}") else: LOG.exception(f"Unable to reboot remote host: {ex}") raise RebootHostTimeoutError( hostname=self.hostname or self.ssh_client.host, timeout=attempt.timeout) from ex else: LOG.info(f"Host '{self.hostname}' is rebooting " f"(command='{self.command}').") break finally: # Ensure we close connection after rebooting command self.ssh_client.close()
def setup_fixture(self): tobiko_config = tobiko.tobiko_config() self.timeout = tobiko_config.testcase.test_runner_timeout if self.timeout is None: LOG.info('Test runner timeout is disabled') else: LOG.info('Test runner timeout is enabled: ' f'timeout is {self.timeout} seconds') self.deadline = tobiko.time() + self.timeout
def check_test_runner_timeout(cls): self = tobiko.setup_fixture(cls) if self.deadline is not None: time_left = self.deadline - tobiko.time() if time_left <= 0.: pytest.skip( f"Test runner execution timed out after {self.timeout} " f"seconds", allow_module_level=True) else: LOG.debug('Test runner timeout is enabled: ' f'{time_left} seconds left')
def pytest_runtest_logreport(self, report): super().pytest_runtest_logreport(report) # Avoid report regeneration ad an interval smaller than 10 seconds now = tobiko.time() if (self.last_html_generation is not None and now - self.last_html_generation < 10.): return self.last_html_generation = now LOG.debug("Update HTML test report files...") temp_report = html_plugin.HTMLReport(logfile=self.logfile, config=self.config) # pylint: disable=attribute-defined-outside-init temp_report.suite_start_time = self.suite_start_time temp_report.reports = dict(self.reports) temp_report.pytest_sessionfinish(self.session) LOG.debug("HTML test report files updated")
def elapsed_time(self) -> tobiko.Seconds: if self.start_time is None: return None else: return tobiko.time() - self.start_time
def open_unix_socket(self, socket_path: str, window_size: int = common.DEFAULT_WINDOW_SIZE, max_packet_size: int = common.DEFAULT_MAX_PACKET_SIZE, timeout: tobiko.Seconds = None) \ -> paramiko.Channel: # pylint: disable=protected-access transport: typing.Any = self.connect().get_transport() if transport is None or not transport.active: raise paramiko.SSHException("SSH session not active") timeout = 3600 if timeout is None else timeout transport.lock.acquire() try: window_size = transport._sanitize_window_size(window_size) max_packet_size = transport._sanitize_packet_size(max_packet_size) chanid = transport._next_channel() # Documented here: # https://github.com/openssh/openssh-portable/blob/master/PROTOCOL m = paramiko.Message() m.add_byte(b'Z') m.add_string('*****@*****.**') m.add_int(chanid) m.add_int(window_size) m.add_int(max_packet_size) m.add_string(socket_path) # Reserved stuff m.add_string('') m.add_int(0) sock: typing.Any = paramiko.Channel(chanid) transport._channels.put(chanid, sock) transport.channel_events[chanid] = event = threading.Event() transport.channels_seen[chanid] = True sock._set_transport(transport) sock._set_window(window_size, max_packet_size) finally: transport.lock.release() transport._send_user_message(m) start_ts = tobiko.time() while True: event.wait(0.1) if not transport.active: e = transport.get_exception() if e is None: e = paramiko.SSHException("Unable to open channel.") raise e if event.is_set(): break elif start_ts + timeout < time.time(): raise paramiko.SSHException("Timeout opening channel.") sock = transport._channels.get(chanid) if sock is None: e = transport.get_exception() if e is None: e = paramiko.SSHException("Unable to open channel.") raise e return sock