def default_url_on_new_port(oldport, newport): """Creates context where the default smart-proxy is forwarded on a new port :param int oldport: Port to be forwarded. :param int newport: New port to be used to forward `oldport`. :return: A string containing the new capsule URL with port. :rtype: str """ logger = logging.getLogger('robottelo') domain = conf.properties['main.server.hostname'] user = conf.properties['main.server.ssh.username'] key = conf.properties['main.server.ssh.key_private'] ssh.upload_file(key, '/tmp/dsa_{0}'.format(newport)) ssh.command('chmod 700 /tmp/dsa_{0}'.format(newport)) with ssh._get_connection() as connection: command = u'ssh -i {0} -L {1}:{2}:{3} {4}@{5}'.format( '/tmp/dsa_{0}'.format(newport), newport, domain, oldport, user, domain) logger.debug('Creating tunnel {0}'.format(command)) # Run command and timeout in 30 seconds. _, _, stderr = connection.exec_command(command, 30) stderr = stderr.read() if len(stderr) > 0: logger.debug('Tunnel failed: {0}'.format(stderr)) # Something failed, so raise an exception. raise SSHTunnelError(stderr) yield 'https://{0}:{1}'.format(domain, newport)
def test__get_connection(self): """Test method ``_get_connection``. Mock up ``paramiko.SSHClient`` (by overriding method ``_call_paramiko_sshclient``) before calling ``_get_connection``. Assert that certain parameters are passed to the (mock) ``paramiko.SSHClient`` object, and that certain methods on that object are called. """ ssh._call_paramiko_sshclient = MockSSHClient # pylint:disable=W0212 backup = conf.properties key_filename = os.path.join( get_app_root(), 'tests', 'robottelo', 'data', 'test_dsa.key' ) conf.properties['main.server.hostname'] = 'example.com' conf.properties['main.server.ssh.username'] = '******' conf.properties['main.server.ssh.key_private'] = key_filename with ssh._get_connection() as connection: # pylint:disable=W0212 self.assertEqual(connection.set_missing_host_key_policy_, 1) self.assertEqual(connection.connect_, 1) self.assertEqual(connection.close_, 0) self.assertEqual(connection.hostname, 'example.com') self.assertEqual(connection.username, 'nobody') self.assertEqual(connection.key_filename, key_filename) self.assertEqual(connection.set_missing_host_key_policy_, 1) self.assertEqual(connection.connect_, 1) self.assertEqual(connection.close_, 1) conf.properties = backup
def default_url_on_new_port(oldport, newport): """ Creates context where the default smart-proxy is forwarded on a new port """ logger = logging.getLogger("robottelo") domain = conf.properties['main.server.hostname'] user = conf.properties['main.server.ssh.username'] key = conf.properties['main.server.ssh.key_private'] ssh.upload_file(key, "/tmp/dsa_%s" % newport) ssh.command("chmod 700 /tmp/dsa_%s" % newport) with ssh._get_connection() as connection: command = "ssh -i %s -L %s:%s:%s %s@%s" % ( "/tmp/dsa_%s" % newport, newport, domain, oldport, user, domain ) logger.debug("Creating tunell %s", command) _, _, stderr = connection.exec_command(command, 1000) if len(stderr) > 0: logger.debug("Tunell failed %s", stderr) yield "https://%s:%s" % (domain, newport)