def create(self, root, password): super().create() self.base = root self.password = password self.port = portend.find_available_local_port() cmd = [ self.exe('devpi-init'), '--serverdir', str(root), '--root-passwd', password, ] subprocess.run(cmd, check=True)
def socket_infos(): """ Generate addr infos for connections to localhost """ host = None # all available interfaces port = portend.find_available_local_port() family = socket.AF_UNSPEC socktype = socket.SOCK_STREAM proto = 0 flags = socket.AI_PASSIVE return socket.getaddrinfo(host, port, family, socktype, proto, flags)
def test_unicode_value(self, monkeypatch): port = portend.find_available_local_port() monkeypatch.setitem(os.environ, 'LANG', 'C') server = PostgresServer(HOST, port) server.initdb() try: server.start() server.get_version() db = server.create('test_unicode') db.sql('CREATE TABLE records(name varchar(80))') db.sql("INSERT INTO records (name) VALUES (U&'\\2609')") finally: server.destroy()
def redis_instance(watcher_getter, request): port = portend.find_available_local_port() proc = watcher_getter( # noqa name='redis-server', arguments=[ '--port', str(port), ], checker=functools.partial(redis_running, port=port), request=request, ) client = redis.StrictRedis(host='localhost', port=port) return locals()
def test_serves_postgres(self): port = portend.find_available_local_port() server = PostgresServer(HOST, port) server.initdb() try: server.start() version = server.get_version() assert len(version) > 0 assert version[0] >= 8 finally: server.destroy()
def simple_wsgi_server(): """F*****g simple wsgi server fixture (duh).""" port = portend.find_available_local_port() def app(environ, start_response): status = '200 OK' response_headers = [('Content-type', 'text/plain')] start_response(status, response_headers) return [b'Hello world!'] host = '::' addr = host, port server = wsgi.Server(addr, app) url = 'http://localhost:{port}/'.format(**locals()) with server._run_in_thread() as thread: yield locals()
def test_serves_postgres_with_locale(self): port = portend.find_available_local_port() server = PostgresServer(HOST, port) locale = 'C' server.initdb(locale=locale) try: server.start() server.get_version() # To check we're able to talk to it. config = os.path.join(server.base_pathname, 'postgresql.conf') with io.open(config, encoding='utf-8') as strm: expect = "lc_messages = 'C'" assert any(expect in line for line in strm) finally: server.destroy()
def start(self): super(MongoDBInstance, self).start() if not hasattr(self, 'port') or not self.port: self.port = portend.find_available_local_port() self.data_dir = tempfile.mkdtemp() cmd = [ self.find_binary(), '--dbpath', self.data_dir, '--port', str(self.port), ] + list(self.mongod_args) if hasattr(self, 'bind_ip') and '--bind_ip' not in cmd: cmd.extend(['--bind_ip', self.bind_ip]) self.process = subprocess.Popen(cmd, **self.process_kwargs) portend.occupied('localhost', self.port, timeout=3) log.info(f'{self} listening on {self.port}')
def start_instance(self, number): port = portend.find_available_local_port() data_dir = os.path.join(self.data_root, repr(number)) os.mkdir(data_dir) cmd = [ self.find_binary(), '--dbpath', data_dir, '--port', str(port), '--replSet', self.replica_set_name, ] + list(self.mongod_parameters) log_file = self.get_log(number) process = subprocess.Popen(cmd, stdout=log_file) portend.occupied('localhost', port, timeout=50) log.info(f'{self}:{number} listening on {port}') return InstanceInfo(data_dir, port, process, log_file)
def simple_wsgi_server(): """F*****g simple wsgi server fixture (duh).""" port = portend.find_available_local_port() def app(_environ, start_response): status = '200 OK' response_headers = [('Content-type', 'text/plain')] start_response(status, response_headers) return [b'Hello world!'] host = '::' addr = host, port server = wsgi.Server(addr, app, timeout=600 if IS_SLOW_ENV else 20) # pylint: disable=possibly-unused-variable url = 'http://localhost:{port}/'.format(**locals()) # pylint: disable=possibly-unused-variable with server._run_in_thread() as thread: yield locals()
def memcached_instance(request, watcher_getter, memcached_server_present): """ Start up an instance of memcached. """ port = portend.find_available_local_port() def is_occupied(): try: portend.Checker().assert_free('localhost', port) except Exception: return True return False proc = watcher_getter( name='memcached', arguments=['-p', str(port)], checker=is_occupied, request=request, ) return locals()
def test_safe_wait_INADDR_ANY(): # pylint: disable=invalid-name """ Wait on INADDR_ANY should not raise IOError In cases where the loopback interface does not exist, CherryPy cannot effectively determine if a port binding to INADDR_ANY was effected. In this situation, CherryPy should assume that it failed to detect the binding (not that the binding failed) and only warn that it could not verify it. """ # At such a time that CherryPy can reliably determine one or more # viable IP addresses of the host, this test may be removed. # Simulate the behavior we observe when no loopback interface is # present by: finding a port that's not occupied, then wait on it. free_port = portend.find_available_local_port() servers = cherrypy.process.servers inaddr_any = '0.0.0.0' # Wait on the free port that's unbound with pytest.warns( UserWarning, match='Unable to verify that the server is bound on ', ) as warnings: # pylint: disable=protected-access with servers._safe_wait(inaddr_any, free_port): portend.occupied(inaddr_any, free_port, timeout=1) assert len(warnings) == 1 # The wait should still raise an IO error if INADDR_ANY was # not supplied. with pytest.raises(IOError): # pylint: disable=protected-access with servers._safe_wait('127.0.0.1', free_port): portend.occupied('127.0.0.1', free_port, timeout=1)
def test_safe_wait_INADDR_ANY(self): """ Wait on INADDR_ANY should not raise IOError In cases where the loopback interface does not exist, CherryPy cannot effectively determine if a port binding to INADDR_ANY was effected. In this situation, CherryPy should assume that it failed to detect the binding (not that the binding failed) and only warn that it could not verify it. """ # At such a time that CherryPy can reliably determine one or more # viable IP addresses of the host, this test may be removed. # Simulate the behavior we observe when no loopback interface is # present by: finding a port that's not occupied, then wait on it. free_port = portend.find_available_local_port() servers = cherrypy.process.servers inaddr_any = '0.0.0.0' # Wait on the free port that's unbound with warnings.catch_warnings(record=True) as w: with servers._safe_wait(inaddr_any, free_port): portend.occupied(inaddr_any, free_port, timeout=1) self.assertEqual(len(w), 1) self.assertTrue(isinstance(w[0], warnings.WarningMessage)) self.assertTrue( 'Unable to verify that the server is bound on ' in str(w[0])) # The wait should still raise an IO error if INADDR_ANY was # not supplied. with pytest.raises(IOError): with servers._safe_wait('127.0.0.1', free_port): portend.occupied('127.0.0.1', free_port, timeout=1)
def port(self): return portend.find_available_local_port()
def setup(self): self.port = portend.find_available_local_port() self.server = PostgresServer(HOST, self.port) self.server.initdb() self.server.start()
def find_free_port(): msg = "Use portend.find_available_local_port" warnings.warn(msg, DeprecationWarning, stacklevel=2) return portend.find_available_local_port()
def test_creates_user_and_database(self): database = PostgresDatabase('tests', user='******', host=HOST, port=self.port) database.create_user() database.create() rows = database.sql('SELECT 1') assert rows == [(1, )] UNUSED_PORT = portend.find_available_local_port() class Test_PostgresDatabase: @pytest.fixture(scope='class', autouse=True) def dbms(self, request): cls = request.cls cls.port = UNUSED_PORT cls.dbms = pgtools.PostgresServer(port=cls.port) cls.dbms.initdb() cls.dbms.start() yield cls.dbms.destroy() del cls.dbms @pytest.fixture(scope='function', autouse=True)