def setUp(self): super(TestRemoteClientWithServer, self).setUp() self.useFixture(fake_config.ConfigFixture()) self.patchobject(config, 'TempestConfigPrivate', fake_config.FakePrivate) cfg.CONF.set_default('ip_version_for_ssh', 4, group='validation') cfg.CONF.set_default('network_for_ssh', 'public', group='validation') cfg.CONF.set_default('connect_timeout', 1, group='validation') cfg.CONF.set_default('console_output', True, group='compute-feature-enabled') self.conn = remote_client.RemoteClient( '127.0.0.1', 'user', 'pass', server=self.server, servers_client=FakeServersClient()) self.useFixture( fixtures.MockPatch( 'tempest.lib.common.ssh.Client._get_ssh_connection', side_effect=lib_exc.SSHTimeout(host='127.0.0.1', user='******', password='******'))) self.log = self.useFixture( fixtures.FakeLogger( name='tempest.lib.common.utils.linux.remote_client', level='DEBUG'))
def _get_ssh_connection(self, sleep=1.5, backoff=1): """Returns an ssh connection to the specified host.""" bsleep = sleep ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) _start_time = time.time() if self.pkey is not None: LOG.info( "Creating ssh connection to '%s:%d' as '%s'" " with public key authentication", self.host, self.port, self.username) else: LOG.info( "Creating ssh connection to '%s:%d' as '%s'" " with password %s", self.host, self.port, self.username, str(self.password)) attempts = 0 while True: if self.proxy_client is not None: proxy_chan = self._get_proxy_channel() else: proxy_chan = None try: ssh.connect(self.host, port=self.port, username=self.username, password=self.password, look_for_keys=self.look_for_keys, key_filename=self.key_filename, timeout=self.channel_timeout, pkey=self.pkey, sock=proxy_chan) LOG.info("ssh connection to %s@%s successfully created", self.username, self.host) return ssh except (EOFError, socket.error, socket.timeout, paramiko.SSHException) as e: ssh.close() if self._is_timed_out(_start_time): LOG.exception( "Failed to establish authenticated ssh" " connection to %s@%s after %d attempts. " "Proxy client: %s", self.username, self.host, attempts, self._get_proxy_client_info()) raise exceptions.SSHTimeout(host=self.host, user=self.username, password=self.password) bsleep += backoff attempts += 1 LOG.warning( "Failed to establish authenticated ssh" " connection to %s@%s (%s). Number attempts: %s." " Retry after %d seconds.", self.username, self.host, e, attempts, bsleep) time.sleep(bsleep)
def test_wait_for_ssh_eventually_up(self): mock_ssh_client = mock.Mock() timeout = lib_exc.SSHTimeout( host='foo', username='******', password='******' ) mock_ssh_client.validate_authentication.side_effect = [ timeout, timeout, True ] # Assert that nothing is raised if validate_authentication passes # before the timeout waiters.wait_for_ssh(mock_ssh_client, 10)
def test_wait_for_ssh_timeout(self): mock_ssh_client = mock.Mock() timeout = lib_exc.SSHTimeout( host='foo', username='******', password='******' ) mock_ssh_client.validate_authentication.side_effect = timeout # Assert that TimeoutException is raised when validate_authentication # doesn't pass in time. self.assertRaises( lib_exc.TimeoutException, waiters.wait_for_ssh, mock_ssh_client, .1 )
def open_session(self): """Gets connection to SSH server and open a new paramiko.Channel :returns: new paramiko.Channel """ client = self.connect() try: return client.get_transport().open_session() except paramiko.SSHException: # the request is rejected, the session ends prematurely or # there is a timeout opening a channel LOG.exception("Unable to open SSH session") raise exceptions.SSHTimeout(host=self.host, user=self.username, password=self.password)