async def fixture_registry_async( tmp_path: Path, kafka_server: Optional[KafkaConfig] ) -> AsyncIterator[KarapaceSchemaRegistry]: if not kafka_server: assert REGISTRY_URI in os.environ or REST_URI in os.environ instance, _ = mock_factory("registry")() yield instance else: config_path = tmp_path / "karapace_config.json" kafka_port = kafka_server.kafka_port config = set_config_defaults({ "log_level": "WARNING", "bootstrap_uri": f"127.0.0.1:{kafka_port}", "topic_name": new_random_name(), "group_id": new_random_name("schema_registry") }) write_config(config_path, config) registry = KarapaceSchemaRegistry(config_file_path=str(config_path), config=set_config_defaults(config)) await registry.get_master() try: yield registry finally: registry.close()
async def fixture_registry_async( request, loop, # pylint: disable=unused-argument tmp_path: Path, kafka_servers: KafkaServers, ) -> AsyncIterator[Optional[KarapaceSchemaRegistry]]: # Do not start a registry when the user provided an external service. Doing # so would cause this node to join the existing group and participate in # the election process. Without proper configuration for the listeners that # won't work and will cause test failures. rest_url = request.config.getoption("registry_url") if rest_url: yield None return config_path = tmp_path / "karapace_config.json" config = set_config_defaults({ "bootstrap_uri": kafka_servers.bootstrap_servers, # Using the default settings instead of random values, otherwise it # would not be possible to run the tests with external services. # Because of this every test must be written in such a way that it can # be executed twice with the same servers. # "topic_name": new_random_name("topic"), # "group_id": new_random_name("schema_registry") }) write_config(config_path, config) registry = KarapaceSchemaRegistry(config=config) await registry.get_master() try: yield registry finally: await registry.close()
async def fixture_rest_async( request, loop, # pylint: disable=unused-argument tmp_path: Path, kafka_servers: KafkaServers, registry_async_client: Client, ) -> AsyncIterator[Optional[KafkaRest]]: # Do not start a REST api when the user provided an external service. Doing # so would cause this node to join the existing group and participate in # the election process. Without proper configuration for the listeners that # won't work and will cause test failures. rest_url = request.config.getoption("rest_url") if rest_url: yield None return config_path = tmp_path / "karapace_config.json" config = set_config_defaults({ "bootstrap_uri": kafka_servers.bootstrap_servers, "admin_metadata_max_age": 0 }) write_config(config_path, config) rest = KafkaRest(config=config) assert rest.serializer.registry_client assert rest.consumer_manager.deserializer.registry_client rest.serializer.registry_client.client = registry_async_client rest.consumer_manager.deserializer.registry_client.client = registry_async_client try: yield rest finally: await rest.close()
async def fixture_rest_async( session_tmpdir: TempDirCreator, kafka_server: Optional[KafkaConfig], registry_async_client: Client ) -> AsyncIterator[KafkaRest]: if not kafka_server: assert REST_URI in os.environ instance, _ = mock_factory("rest")() yield instance else: config_path = os.path.join(session_tmpdir(), "karapace_config.json") kafka_port = kafka_server.kafka_port config = set_config_defaults({ "log_level": "WARNING", "bootstrap_uri": f"127.0.0.1:{kafka_port}", "admin_metadata_max_age": 0 }) write_config(config_path, config) rest = KafkaRest(config_file_path=config_path, config=config) assert rest.serializer.registry_client assert rest.consumer_manager.deserializer.registry_client rest.serializer.registry_client.client = registry_async_client rest.consumer_manager.deserializer.registry_client.client = registry_async_client try: yield rest finally: rest.close() await rest.close_producers()
def create_service(self, topic_name=None): karapace_config = { "log_level": "INFO", "bootstrap_uri": f"127.0.0.1:{self.kafka_port}" } if topic_name: karapace_config["topic_name"] = topic_name write_config(self.config_path, karapace_config) self.kc = karapace_class(self.config_path) return self.kc, self.datadir
def fixture_registry_async_pair(tmp_path: Path, kafka_server: Optional[KafkaConfig]): assert kafka_server, f"registry_async_pair can not be used if the env variable `{REGISTRY_URI}` or `{REST_URI}` is set" master_config_path = tmp_path / "karapace_config_master.json" slave_config_path = tmp_path / "karapace_config_slave.json" master_port = get_random_port(port_range=REGISTRY_PORT_RANGE, blacklist=[]) slave_port = get_random_port(port_range=REGISTRY_PORT_RANGE, blacklist=[master_port]) kafka_port = kafka_server.kafka_port topic_name = new_random_name("schema_pairs") group_id = new_random_name("schema_pairs") write_config( master_config_path, { "log_level": "WARNING", "bootstrap_uri": f"127.0.0.1:{kafka_port}", "topic_name": topic_name, "group_id": group_id, "advertised_hostname": "127.0.0.1", "karapace_registry": True, "port": master_port, }) write_config( slave_config_path, { "log_level": "WARNING", "bootstrap_uri": f"127.0.0.1:{kafka_port}", "topic_name": topic_name, "group_id": group_id, "advertised_hostname": "127.0.0.1", "karapace_registry": True, "port": slave_port, }) master_process = Popen( ["python", "-m", "karapace.karapace_all", str(master_config_path)]) slave_process = Popen( ["python", "-m", "karapace.karapace_all", str(slave_config_path)]) try: wait_for_port(master_port) wait_for_port(slave_port) yield f"http://127.0.0.1:{master_port}", f"http://127.0.0.1:{slave_port}" finally: master_process.kill() slave_process.kill()
async def fixture_registry_async(session_tmpdir, kafka_server): if REGISTRY_URI in os.environ or REST_URI in os.environ: instance, _ = mock_factory("registry")() yield instance else: config_path = os.path.join(session_tmpdir(), "karapace_config.json") kafka_port = kafka_server["kafka_port"] write_config( config_path, { "log_level": "WARNING", "bootstrap_uri": f"127.0.0.1:{kafka_port}", "topic_name": new_random_name(), "group_id": new_random_name("schema_registry") }) registry = KarapaceSchemaRegistry(config_path) await registry.get_master() try: yield registry finally: registry.close()
def fixture_registry_async_pair(session_tmpdir: TempDirCreator, kafka_server: Optional[KafkaConfig]): assert kafka_server, f"registry_async_pair can not be used if the env variable `{REGISTRY_URI}` or `{REST_URI}` is set" master_config_path = os.path.join(session_tmpdir(), "karapace_config_master.json") slave_config_path = os.path.join(session_tmpdir(), "karapace_config_slave.json") master_port, slave_port = 1234, 5678 kafka_port = kafka_server.kafka_port topic_name = new_random_name("schema_pairs") group_id = new_random_name("schema_pairs") write_config( master_config_path, { "log_level": "WARNING", "bootstrap_uri": f"127.0.0.1:{kafka_port}", "topic_name": topic_name, "group_id": group_id, "advertised_hostname": "127.0.0.1", "karapace_registry": True, "port": master_port, } ) write_config( slave_config_path, { "log_level": "WARNING", "bootstrap_uri": f"127.0.0.1:{kafka_port}", "topic_name": topic_name, "group_id": group_id, "advertised_hostname": "127.0.0.1", "karapace_registry": True, "port": slave_port, } ) master_process = subprocess.Popen(["python", "-m", "karapace.karapace_all", master_config_path]) slave_process = subprocess.Popen(["python", "-m", "karapace.karapace_all", slave_config_path]) wait_for_port(1234) wait_for_port(5678) try: yield f"http://127.0.0.1:{master_port}", f"http://127.0.0.1:{slave_port}" finally: master_process.kill() slave_process.kill()
def fixture_registry_async_pair(session_tmpdir, kafka_server): master_config_path = os.path.join(session_tmpdir(), "karapace_config_master.json") slave_config_path = os.path.join(session_tmpdir(), "karapace_config_slave.json") master_port, slave_port = 1234, 5678 kafka_port = kafka_server["kafka_port"] topic_name = new_random_name("schema_pairs") group_id = new_random_name("schema_pairs") write_config( master_config_path, { "log_level": "WARNING", "bootstrap_uri": f"127.0.0.1:{kafka_port}", "topic_name": topic_name, "group_id": group_id, "advertised_hostname": "127.0.0.1", "karapace_registry": True, "port": master_port, }) write_config( slave_config_path, { "log_level": "WARNING", "bootstrap_uri": f"127.0.0.1:{kafka_port}", "topic_name": topic_name, "group_id": group_id, "advertised_hostname": "127.0.0.1", "karapace_registry": True, "port": slave_port, }) master_process = subprocess.Popen( ["python", "-m", "karapace.karapace_all", master_config_path]) slave_process = subprocess.Popen( ["python", "-m", "karapace.karapace_all", slave_config_path]) wait_for_port(1234) wait_for_port(5678) try: yield f"http://127.0.0.1:{master_port}", f"http://127.0.0.1:{slave_port}" finally: master_process.kill() slave_process.kill()
def fixture_registry_async_pair(tmp_path: Path, kafka_servers: KafkaServers): master_config_path = tmp_path / "karapace_config_master.json" slave_config_path = tmp_path / "karapace_config_slave.json" master_port = get_random_port(port_range=REGISTRY_PORT_RANGE, blacklist=[]) slave_port = get_random_port(port_range=REGISTRY_PORT_RANGE, blacklist=[master_port]) topic_name = new_random_name("schema_pairs") group_id = new_random_name("schema_pairs") write_config( master_config_path, { "bootstrap_uri": kafka_servers.bootstrap_servers, "topic_name": topic_name, "group_id": group_id, "advertised_hostname": "127.0.0.1", "karapace_registry": True, "port": master_port, }) write_config( slave_config_path, { "bootstrap_uri": kafka_servers.bootstrap_servers, "topic_name": topic_name, "group_id": group_id, "advertised_hostname": "127.0.0.1", "karapace_registry": True, "port": slave_port, }) master_process = Popen( ["python", "-m", "karapace.karapace_all", str(master_config_path)]) slave_process = Popen( ["python", "-m", "karapace.karapace_all", str(slave_config_path)]) try: wait_for_port(master_port) wait_for_port(slave_port) yield f"http://127.0.0.1:{master_port}", f"http://127.0.0.1:{slave_port}" finally: master_process.kill() slave_process.kill()
async def fixture_rest_async(session_tmpdir, kafka_server, registry_async_client): if REST_URI in os.environ: instance, _ = mock_factory("rest")() yield instance else: config_path = os.path.join(session_tmpdir(), "karapace_config.json") kafka_port = kafka_server["kafka_port"] write_config( config_path, { "log_level": "WARNING", "bootstrap_uri": f"127.0.0.1:{kafka_port}", "admin_metadata_max_age": 0 }) rest = KafkaRest(config_path) rest.serializer.registry_client.client = registry_async_client rest.consumer_manager.deserializer.registry_client.client = registry_async_client try: yield rest finally: rest.close() await rest.close_producers()