def local_matrix_server(transport_config): if not transport_config.protocol == TransportProtocol.MATRIX: yield None return server = transport_config.parameters.server # if command is none, assume server is already running if transport_config.parameters.command in (None, 'none'): yield server return # otherwise, run our own local server matrix = HTTPExecutor( transport_config.parameters.command, status=r'^[24]\d\d$', url=urljoin(server, '/_matrix'), timeout=120, sleep=0.1, shell=True, ) matrix.start() yield server matrix.stop()
def elasticsearch_proc_fixture(request): """Elasticsearch process starting fixture.""" elasticsearch_port = get_port(port) pidfile = '/tmp/elasticsearch.{0}.pid'.format(elasticsearch_port) home_path = '/tmp/elasticsearch_{0}'.format(elasticsearch_port) logsdir = path(request.config.getvalue('logsdir')) logs_path = logsdir / '{prefix}elasticsearch_{port}_logs'.format( prefix=logs_prefix, port=elasticsearch_port ) work_path = '/tmp/elasticsearch_{0}_tmp'.format(elasticsearch_port) cluster = cluster_name or 'dbfixtures.{0}'.format(elasticsearch_port) multicast_enabled = str(discovery_zen_ping_multicast_enabled).lower() command_exec = ''' {deamon} -p {pidfile} --http.port={port} --path.home={home_path} --default.path.logs={logs_path} --default.path.work={work_path} --default.path.conf=/etc/elasticsearch --cluster.name={cluster} --network.publish_host='{network_publish_host}' --discovery.zen.ping.multicast.enabled={multicast_enabled} --index.store.type={index_store_type} '''.format( deamon=executable, pidfile=pidfile, port=elasticsearch_port, home_path=home_path, logs_path=logs_path, work_path=work_path, cluster=cluster, network_publish_host=network_publish_host, multicast_enabled=multicast_enabled, index_store_type=index_store_type ) elasticsearch_executor = HTTPExecutor( command_exec, 'http://{host}:{port}'.format( host=host, port=elasticsearch_port ), timeout=60, ) elasticsearch_executor.start() def finalize_elasticsearch(): elasticsearch_executor.stop() shutil.rmtree(home_path) request.addfinalizer(finalize_elasticsearch) return elasticsearch_executor
def backend(request): """Run application backend.""" executor = HTTPExecutor( [ 'planningpoker', '--host', '127.0.0.1', '--port', str(PORT), '--cookie-secret-key', Fernet.generate_key().decode() ], SITE_ADDRESS + '/status', timeout=EXECUTOR_TIMEOUT ) executor.start() request.addfinalizer(executor.stop)
def test_executor_starts_and_waits(): """Test if process awaits for HEAD request to be completed.""" command = 'bash -c "sleep 3 && {0}"'.format(http_server_cmd) executor = HTTPExecutor( command, 'http://{0}:{1}/'.format(HOST, PORT), timeout=20 ) executor.start() assert executor.running() is True connect_to_server() executor.stop()
def test_executor_starts_and_waits(): """Test if process awaits for HEAD request to be completed.""" command = f'bash -c "sleep 3 && {HTTP_NORMAL_CMD}"' executor = HTTPExecutor(command, f'http://{HOST}:{PORT}/', timeout=20) executor.start() assert executor.running() is True connect_to_server() executor.stop() # check proper __str__ and __repr__ rendering: assert 'HTTPExecutor' in repr(executor) assert command in str(executor)
def test_slow_method_server_timed_out(method): """Check if timeout properly expires.""" http_method_slow_cmd = ( f'{sys.executable} {TEST_SERVER_PATH} {HOST}:{PORT} False {method}') executor = HTTPExecutor(http_method_slow_cmd, f'http://{HOST}:{PORT}/', method=method, timeout=1) with pytest.raises(TimeoutExpired) as exc: executor.start() assert executor.running() is False assert 'timed out after' in str(exc.value)
def test_executor_starts_and_waits(): """Test if process awaits for HEAD request to be completed.""" command = 'bash -c "sleep 3 && {0}"'.format(http_server_cmd) executor = HTTPExecutor(command, "http://{0}:{1}/".format(HOST, PORT), timeout=20) executor.start() assert executor.running() is True connect_to_server() executor.stop() # check proper __str__ and __repr__ rendering: assert "HTTPExecutor" in repr(executor) assert command in str(executor)
def test_default_port(): """ Test default port for the base TCP check. Check if HTTP executor fills in the default port for the TCP check from the base class if no port is provided in the URL. """ executor = HTTPExecutor(http_server_cmd, 'http://{}/'.format(HOST)) assert executor.url.port is None assert executor.port == PORT assert TCPExecutor.pre_start_check(executor) is False executor.start() assert TCPExecutor.pre_start_check(executor) is True executor.stop()
def test_default_port(): """ Test default port for the base TCP check. Check if HTTP executor fills in the default port for the TCP check from the base class if no port is provided in the URL. """ executor = HTTPExecutor(http_server_cmd, 'http://{0}/'.format(HOST)) assert executor.url.port is None assert executor.port == PORT assert TCPExecutor.pre_start_check(executor) is False executor.start() assert TCPExecutor.pre_start_check(executor) is True executor.stop()
def test_fail_if_other_executor_running(): """Test raising AlreadyRunning exception when port is blocked.""" executor = HTTPExecutor(http_server_cmd, "http://{0}:{1}/".format(HOST, PORT)) executor2 = HTTPExecutor(http_server_cmd, "http://{0}:{1}/".format(HOST, PORT)) with executor: assert executor.running() is True with pytest.raises(AlreadyRunning): executor2.start() with pytest.raises(AlreadyRunning) as exc: with executor2: pass assert "seems to be already running" in str(exc)
def test_default_port(): """ Test default port for the base TCP check. Check if HTTP executor fills in the default port for the TCP check from the base class if no port is provided in the URL. """ executor = HTTPExecutor(HTTP_NORMAL_CMD, f'http://{HOST}/') assert executor.url.port is None assert executor.port == PORT assert TCPExecutor.pre_start_check(executor) is False executor.start() assert TCPExecutor.pre_start_check(executor) is True executor.stop()
def test_executor_starts_and_waits(): """Test if process awaits for HEAD request to be completed.""" command = 'bash -c "sleep 3 && {0}"'.format(http_server_cmd) executor = HTTPExecutor(command, 'http://{0}:{1}/'.format(HOST, PORT), timeout=20) executor.start() assert executor.running() is True connect_to_server() executor.stop() # check proper __str__ and __repr__ rendering: assert 'HTTPExecutor' in repr(executor) assert command in str(executor)
def test_stopping_brutally(): """ Test if Executor is stopping insubordinate process. Check if the process that doesn't react to SIGTERM signal will be killed by executor with SIGKILL automatically. """ host_port = "127.0.0.1:8000" cmd = '{} {} {} True'.format(sys.executable, test_server_path, host_port) executor = HTTPExecutor(cmd, 'http://%s/' % host_port) executor.start() assert executor.running() is True stop_at = time.time() + 10 executor.stop() assert executor.running() is False assert stop_at <= time.time(), "Subprocess killed earlier than in 10 secs"
def test_stopping_brutally(): """ Test if SimpleExecutor is stopping insubordinate process. Check if the process that doesn't react to SIGTERM signal will be killed by executor with SIGKILL automatically. """ host_port = "127.0.0.1:8000" cmd = '{} {} {} True'.format(sys.executable, test_server_path, host_port) executor = HTTPExecutor(cmd, 'http://%s/' % host_port) executor.start() assert executor.running() is True stop_at = time.time() + 10 executor.stop() assert executor.running() is False assert stop_at <= time.time(), "Subprocess killed earlier than in 10 secs"
def test_stopping_brutally(): """ Test if SimpleExecutor is stopping insubordinate process. Check if the process that doesn't react to SIGTERM signal will be killed by executor with SIGKILL automatically. """ host_port = "127.0.0.1:8000" cmd = f"{sys.executable} {TEST_SERVER_PATH} {host_port} True" executor = HTTPExecutor(cmd, f"http://{host_port!s}/", timeout=20) executor.start() assert executor.running() is True stop_at = time.time() + 10 executor.stop() assert executor.running() is False assert stop_at <= time.time(), "Subprocess killed earlier than in 10 secs"
def test_fail_if_other_executor_running(): """Test raising AlreadyRunning exception when port is blocked.""" executor = HTTPExecutor( http_server_cmd, 'http://{0}:{1}/'.format(HOST, PORT), ) executor2 = HTTPExecutor( http_server_cmd, 'http://{0}:{1}/'.format(HOST, PORT), ) with executor: assert executor.running() is True with pytest.raises(AlreadyRunning): executor2.start() with pytest.raises(AlreadyRunning): with executor2: pass
def test_fail_if_other_running(): """Test raising AlreadyRunning exception when port is blocked.""" executor = HTTPExecutor( HTTP_NORMAL_CMD, f'http://{HOST}:{PORT}/', ) executor2 = HTTPExecutor( HTTP_NORMAL_CMD, f'http://{HOST}:{PORT}/', ) with executor: assert executor.running() is True with pytest.raises(AlreadyRunning): executor2.start() with pytest.raises(AlreadyRunning) as exc: with executor2: pass assert 'seems to be already running' in str(exc.value)
def test_fail_if_other_executor_running(): """Test raising AlreadyRunning exception when port is blocked.""" executor = HTTPExecutor( http_server_cmd, 'http://{0}:{1}/'.format(HOST, PORT), ) executor2 = HTTPExecutor( http_server_cmd, 'http://{0}:{1}/'.format(HOST, PORT), ) with executor: assert executor.running() is True with pytest.raises(AlreadyRunning): executor2.start() with pytest.raises(AlreadyRunning) as exc: with executor2: pass assert 'seems to be already running' in str(exc)
def test_http_status_codes(accepted_status, expected_timeout): """ Test how 'status' argument influences executor start. :param int|str accepted_status: Executor 'status' value :param bool expected_timeout: if Executor raises TimeoutExpired or not """ kwargs = { 'command': http_server_cmd, 'url': 'http://{0}:{1}/badpath'.format(HOST, PORT), 'timeout': 2 } if accepted_status: kwargs['status'] = accepted_status executor = HTTPExecutor(**kwargs) if not expected_timeout: executor.start() executor.stop() else: with pytest.raises(TimeoutExpired): executor.start() executor.stop()
def test_http_status_codes(accepted_status, expected_timeout): """ Test how 'status' argument influences executor start. :param int|str accepted_status: Executor 'status' value :param bool expected_timeout: if Executor raises TimeoutExpired or not """ kwargs: Dict[str, Any] = { 'command': HTTP_NORMAL_CMD, 'url': f'http://{HOST}:{PORT}/badpath', 'timeout': 2 } if accepted_status: kwargs['status'] = accepted_status executor = HTTPExecutor(**kwargs) if not expected_timeout: executor.start() executor.stop() else: with pytest.raises(TimeoutExpired): executor.start() executor.stop()
def es_process(es_binary, es_version): port = port_for.select_random() pid = es_binary + '.pid' # use a different garbage collector for better performance os.environ['ES_JAVA_OPTS'] = \ '-Xms1g -Xmx1g -XX:-UseConcMarkSweepGC -XX:+UseG1GC' command = ( f"{es_binary} -p {pid} -E http.port={port} " f"-E xpack.monitoring.enabled=false " f"-E xpack.monitoring.collection.enabled=false " f"> /dev/null" ) url = f'http://127.0.0.1:{port}/_cluster/health?wait_for_status=green' executor = HTTPExecutor(command, url, method='GET', shell=True) executor.start() yield executor executor.stop() executor.kill()
def test_http_status_codes(accepted_status: Union[None, int, str], expected_timeout: bool) -> None: """ Test how 'status' argument influences executor start. :param int|str accepted_status: Executor 'status' value :param bool expected_timeout: if Executor raises TimeoutExpired or not """ kwargs: Dict[str, Any] = { "command": HTTP_NORMAL_CMD, "url": f"http://{HOST}:{PORT}/badpath", "timeout": 2, } if accepted_status: kwargs["status"] = accepted_status executor = HTTPExecutor(**kwargs) if not expected_timeout: executor.start() executor.stop() else: with pytest.raises(TimeoutExpired): executor.start() executor.stop()
def elasticsearch_proc_fixture(request): """Elasticsearch process starting fixture.""" tmpdir = gettempdir() config = return_config(request) elasticsearch_host = host or config['host'] elasticsearch_port = get_port(port) or get_port(config['port']) elasticsearch_cluster_name = \ cluster_name or config['cluster_name'] or \ 'elasticsearch_cluster_{0}'.format(elasticsearch_port) elasticsearch_logs_prefix = logs_prefix or config['logs_prefix'] elasticsearch_index_store_type = index_store_type or \ config['index_store_type'] elasticsearch_network_publish_host = network_publish_host or \ config['network_publish_host'] logsdir = elasticsearch_logsdir or config['logsdir'] logs_path = os.path.join( logsdir, '{prefix}elasticsearch_{port}_logs'.format( prefix=elasticsearch_logs_prefix, port=elasticsearch_port)) pidfile = os.path.join( tmpdir, 'elasticsearch.{0}.pid'.format(elasticsearch_port)) home_path = os.path.join( tmpdir, 'elasticsearch_{0}'.format(elasticsearch_port)) work_path = '{0}_tmp'.format(home_path) if discovery_zen_ping_multicast_enabled is not None: multicast_enabled = str( discovery_zen_ping_multicast_enabled).lower() else: multicast_enabled = config['discovery_zen_ping_multicast_enabled'] command_exec = ''' {deamon} -p {pidfile} --http.port={port} --path.home={home_path} --default.path.logs={logs_path} --default.path.work={work_path} --default.path.conf=/etc/elasticsearch --cluster.name={cluster} --network.publish_host='{network_publish_host}' --discovery.zen.ping.multicast.enabled={multicast_enabled} --index.store.type={index_store_type} '''.format(deamon=executable, pidfile=pidfile, port=elasticsearch_port, home_path=home_path, logs_path=logs_path, work_path=work_path, cluster=elasticsearch_cluster_name, network_publish_host=elasticsearch_network_publish_host, multicast_enabled=multicast_enabled, index_store_type=elasticsearch_index_store_type) elasticsearch_executor = HTTPExecutor( command_exec, 'http://{host}:{port}'.format(host=elasticsearch_host, port=elasticsearch_port), timeout=60, ) elasticsearch_executor.start() def finalize_elasticsearch(): elasticsearch_executor.stop() shutil.rmtree(home_path) request.addfinalizer(finalize_elasticsearch) return elasticsearch_executor
def elasticsearch_proc_fixture(request): """Elasticsearch process starting fixture.""" tmpdir = gettempdir() config = return_config(request) es_ver = _get_elastic_version(executable) elasticsearch_host = host or config['host'] elasticsearch_port = get_port(port) or get_port(config['port']) elasticsearch_cluster_name = \ cluster_name or config['cluster_name'] or \ 'elasticsearch_cluster_{0}'.format(elasticsearch_port) elasticsearch_logs_prefix = logs_prefix or config['logs_prefix'] or '' elasticsearch_index_store_type = index_store_type or \ config['index_store_type'] or _default_index_store(es_ver) elasticsearch_network_publish_host = network_publish_host or \ config['network_publish_host'] logsdir = elasticsearch_logsdir or config['logsdir'] or tmpdir logs_path = os.path.join( logsdir, '{prefix}elasticsearch_{port}_logs'.format( prefix=elasticsearch_logs_prefix, port=elasticsearch_port)) data_dir = elasticsearch_datadir or tmpdir conf_path = elasticsearch_confdir or config[ 'confdir'] or _default_conf_dir(es_ver) pidfile = os.path.join( tmpdir, 'elasticsearch.{0}.pid'.format(elasticsearch_port)) home_path = os.path.join( tmpdir, 'elasticsearch_{0}'.format(elasticsearch_port)) work_path = '{0}_tmp'.format(home_path) if discovery_zen_ping_multicast_enabled is not None: multicast_enabled = str( discovery_zen_ping_multicast_enabled).lower() else: multicast_enabled = config['discovery_zen_ping_multicast_enabled'] command_exec = _generate_es_cmdline( es_ver, executable=executable, pid_file=pidfile, host=elasticsearch_host, port=elasticsearch_port, home_path=home_path, logs_path=logs_path, conf_path=conf_path, work_path=work_path, data_path=data_dir, cluster=elasticsearch_cluster_name, network_publish_host=elasticsearch_network_publish_host, multicast_enabled=multicast_enabled, index_store_type=elasticsearch_index_store_type, max_local_storage_nodes=multiprocessing.cpu_count()) elasticsearch_executor = HTTPExecutor(command_exec, 'http://{host}:{port}'.format( host=elasticsearch_host, port=elasticsearch_port), timeout=60) elasticsearch_executor.start() def finalize_elasticsearch(): elasticsearch_executor.stop() try: shutil.rmtree(home_path) except OSError as e: if e.errno != errno.ENOENT: raise request.addfinalizer(finalize_elasticsearch) return elasticsearch_executor
def elasticsearch_proc_fixture(request): """Elasticsearch process starting fixture.""" tmpdir = gettempdir() config = return_config(request) version_parts = get_version_parts(executable) elasticsearch_host = host or config['host'] elasticsearch_port = get_port(port) or get_port(config['port']) elasticsearch_transport_port = get_port(transport_tcp_port) or \ get_port(config['transport_tcp_port']) elasticsearch_cluster_name = \ cluster_name or config['cluster_name'] or \ 'elasticsearch_cluster_{0}'.format(elasticsearch_port) elasticsearch_logs_prefix = logs_prefix or config['logs_prefix'] elasticsearch_index_store_type = index_store_type or \ config['index_store_type'] elasticsearch_network_publish_host = network_publish_host or \ config['network_publish_host'] logsdir = elasticsearch_logsdir or config['logsdir'] logs_path = os.path.join( logsdir, '{prefix}elasticsearch_{port}_logs'.format( prefix=elasticsearch_logs_prefix, port=elasticsearch_port)) pidfile = os.path.join( tmpdir, 'elasticsearch.{0}.pid'.format(elasticsearch_port)) home_path = os.path.join( tmpdir, 'elasticsearch_{0}'.format(elasticsearch_port)) work_path = '{0}_tmp'.format(home_path) conf_path = configuration_path or config['configuration_path'] if discovery_zen_ping_multicast_enabled is not None: multicast_enabled = str( discovery_zen_ping_multicast_enabled).lower() else: multicast_enabled = config['discovery_zen_ping_multicast_enabled'] command = command_from(version=(version_parts['major'], version_parts['minor'], version_parts['patch'])) command_exec = command.format( deamon=executable, pidfile=pidfile, port=elasticsearch_port, transport_tcp_port=elasticsearch_transport_port, conf_path=conf_path, home_path=home_path, logs_path=logs_path, work_path=work_path, cluster=elasticsearch_cluster_name, network_publish_host=elasticsearch_network_publish_host, multicast_enabled=multicast_enabled, index_store_type=elasticsearch_index_store_type) elasticsearch_executor = HTTPExecutor( command_exec, 'http://{host}:{port}'.format(host=elasticsearch_host, port=elasticsearch_port), timeout=60, ) elasticsearch_executor.start() def finalize_elasticsearch(): elasticsearch_executor.stop() shutil.rmtree(work_path) shutil.rmtree(logs_path) request.addfinalizer(finalize_elasticsearch) return elasticsearch_executor
def elasticsearch_proc_fixture(request): """Elasticsearch process starting fixture.""" tmpdir = gettempdir() config = return_config(request) elasticsearch_host = host or config["host"] elasticsearch_port = get_port(port) or get_port(config["port"]) elasticsearch_cluster_name = ( cluster_name or config["cluster_name"] or "elasticsearch_cluster_{0}".format(elasticsearch_port) ) elasticsearch_logs_prefix = logs_prefix or config["logs_prefix"] elasticsearch_index_store_type = index_store_type or config["index_store_type"] elasticsearch_network_publish_host = network_publish_host or config["network_publish_host"] logsdir = elasticsearch_logsdir or config["logsdir"] logs_path = os.path.join( logsdir, "{prefix}elasticsearch_{port}_logs".format(prefix=elasticsearch_logs_prefix, port=elasticsearch_port), ) pidfile = os.path.join(tmpdir, "elasticsearch.{0}.pid".format(elasticsearch_port)) home_path = os.path.join(tmpdir, "elasticsearch_{0}".format(elasticsearch_port)) work_path = "{0}_tmp".format(home_path) if discovery_zen_ping_multicast_enabled is not None: multicast_enabled = str(discovery_zen_ping_multicast_enabled).lower() else: multicast_enabled = config["discovery_zen_ping_multicast_enabled"] command_exec = """ {deamon} -p {pidfile} --http.port={port} --path.home={home_path} --default.path.logs={logs_path} --default.path.work={work_path} --default.path.conf=/etc/elasticsearch --cluster.name={cluster} --network.publish_host='{network_publish_host}' --discovery.zen.ping.multicast.enabled={multicast_enabled} --index.store.type={index_store_type} """.format( deamon=executable, pidfile=pidfile, port=elasticsearch_port, home_path=home_path, logs_path=logs_path, work_path=work_path, cluster=elasticsearch_cluster_name, network_publish_host=elasticsearch_network_publish_host, multicast_enabled=multicast_enabled, index_store_type=elasticsearch_index_store_type, ) elasticsearch_executor = HTTPExecutor( command_exec, "http://{host}:{port}".format(host=elasticsearch_host, port=elasticsearch_port), timeout=60 ) elasticsearch_executor.start() def finalize_elasticsearch(): elasticsearch_executor.stop() shutil.rmtree(home_path) request.addfinalizer(finalize_elasticsearch) return elasticsearch_executor
def elasticsearch_proc_fixture(request): """Elasticsearch process starting fixture.""" config = return_config(request) elasticsearch_host = host or config['host'] elasticsearch_port = get_port(port) or get_port(config['port']) elasticsearch_transport_port = get_port(transport_tcp_port) or \ get_port(config['transport_tcp_port']) elasticsearch_cluster_name = \ cluster_name or config['cluster_name'] or \ 'elasticsearch_cluster_{0}'.format(elasticsearch_port) elasticsearch_logs_prefix = logs_prefix or config['logs_prefix'] elasticsearch_index_store_type = index_store_type or \ config['index_store_type'] elasticsearch_network_publish_host = network_publish_host or \ config['network_publish_host'] logsdir = elasticsearch_logsdir or config['logsdir'] logs_path = os.path.join( logsdir, '{prefix}elasticsearch_{port}_logs'.format( prefix=elasticsearch_logs_prefix, port=elasticsearch_port )) pidfile = os.path.join( gettempdir(), 'elasticsearch.{0}.pid'.format(elasticsearch_port)) home_path = os.path.join( gettempdir(), 'elasticsearch_{0}'.format(elasticsearch_port)) work_path = '{0}_tmp'.format(home_path) conf_path = configuration_path or config['configuration_path'] if discovery_zen_ping_multicast_enabled is not None: multicast_enabled = str( discovery_zen_ping_multicast_enabled).lower() else: multicast_enabled = config['discovery_zen_ping_multicast_enabled'] command = command_from(version=get_version_parts(executable)) command_exec = command.format( deamon=executable, pidfile=pidfile, port=elasticsearch_port, transport_tcp_port=elasticsearch_transport_port, conf_path=conf_path, home_path=home_path, logs_path=logs_path, work_path=work_path, cluster=elasticsearch_cluster_name, network_publish_host=elasticsearch_network_publish_host, multicast_enabled=multicast_enabled, index_store_type=elasticsearch_index_store_type ) elasticsearch_executor = HTTPExecutor( command_exec, 'http://{host}:{port}'.format( host=elasticsearch_host, port=elasticsearch_port ), timeout=60, ) elasticsearch_executor.start() def finalize_elasticsearch(): elasticsearch_executor.stop() shutil.rmtree(work_path) shutil.rmtree(logs_path) request.addfinalizer(finalize_elasticsearch) return elasticsearch_executor