Skip to content

Commit 1f9fa6f

Browse files
authored
Fix flaky tests that rely on kafka in docker (#378)
* Fix flaky tests that rely on kafka in docker * Reduce log level in CI tests
1 parent 3a587e4 commit 1f9fa6f

File tree

5 files changed

+44
-40
lines changed

5 files changed

+44
-40
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ jobs:
6060
python -m pip install -U -r requirements.txt
6161
- name: Run tests
6262
run: |
63-
python -m pytest -s -v
63+
python -m pytest -v --log-cli-level=ERROR

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ line_length = 88
5454
[tool.pytest.ini_options]
5555
minversion = "6.0"
5656
# Ignore manual tests by and some loggers by default
57-
addopts = "--log-disable=urllib3.connectionpool --log-disable=parso --log-disable=docker"
57+
addopts = "--log-disable=urllib3.connectionpool --log-disable=parso --log-disable=docker --log-disable=asyncio"
5858

5959
# Print debug logs to the console in tests
6060
log_cli = true
61-
log_cli_level = "DEBUG"
61+
log_cli_level = "INFO"
62+
log_cli_format = "[%(levelname)s] %(name)s: %(message)s"

tests/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ def kafka_container() -> KafkaContainer:
3939
kafka_container,
4040
broker_address,
4141
kafka_port,
42-
zookeeper_port,
4342
) = ContainerHelper.create_kafka_container()
44-
print(f"Starting Kafka container on {broker_address}")
43+
test_logger.debug(f"Starting Kafka container on {broker_address}")
4544
ContainerHelper.start_kafka_container(kafka_container)
46-
print(f"Started Kafka container on {broker_address}")
45+
test_logger.debug(f"Started Kafka container on {broker_address}")
4746
yield KafkaContainer(broker_address=broker_address)
4847
kafka_container.stop()

tests/containerhelper.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
1+
import base64
12
import datetime
2-
import os
33
import random
44
import time
5+
import uuid
56
from typing import Tuple
67

78
from testcontainers.core.container import DockerContainer
89

910

1011
class ContainerHelper:
1112
@staticmethod
12-
def create_kafka_container() -> Tuple[DockerContainer, str, int, int]:
13+
def create_kafka_container() -> Tuple[DockerContainer, str, int]:
1314
"""
14-
Returns (kafka container, broker list, kafka port, zookeper port) tuple
15+
Returns (kafka container, broker list, kafka port) tuple
1516
"""
1617
kafka_address = "127.0.0.1"
17-
zookeeper_port = random.randint(12000, 15000)
1818
kafka_port = random.randint(16000, 20000)
19-
broker_list = "{0}:{1}".format(kafka_address, kafka_port)
19+
broker_list = f"{kafka_address}:{kafka_port}"
20+
docker_hostname = uuid.uuid4().hex
21+
docker_image_name = "confluentinc/cp-kafka:7.6.1"
22+
kraft_cluster_id = base64.urlsafe_b64encode(uuid.uuid4().bytes).decode()
2023

21-
archname = os.uname().machine
22-
if archname == "arm64":
23-
kafka_container = DockerContainer(image="dougdonohoe/fast-data-dev:latest")
24-
else:
25-
kafka_container = DockerContainer(image="lensesio/fast-data-dev:3.3.1")
26-
kafka_container = kafka_container.with_env("BROKER_PORT", kafka_port)
27-
kafka_container = kafka_container.with_bind_ports(kafka_port, kafka_port)
28-
kafka_container = kafka_container.with_env("ZK_PORT", zookeeper_port)
29-
kafka_container = kafka_container.with_bind_ports(
30-
zookeeper_port, zookeeper_port
24+
kafka_container = (
25+
DockerContainer(image=docker_image_name, hostname=docker_hostname)
26+
.with_env("KAFKA_NODE_ID", "0")
27+
.with_env("KAFKA_PROCESS_ROLES", "controller,broker")
28+
.with_env(
29+
"KAFKA_LISTENERS",
30+
f"PLAINTEXT://:9092,CONTROLLER://:9093,EXTERNAL://:{kafka_port}",
31+
)
32+
.with_env(
33+
"KAFKA_ADVERTISED_LISTENERS",
34+
f"PLAINTEXT://localhost:9092,EXTERNAL://{broker_list}",
35+
)
36+
.with_env(
37+
"KAFKA_LISTENER_SECURITY_PROTOCOL_MAP",
38+
"CONTROLLER:PLAINTEXT,EXTERNAL:PLAINTEXT,PLAINTEXT:PLAINTEXT",
39+
)
40+
.with_env("KAFKA_CONTROLLER_QUORUM_VOTERS", f"0@{docker_hostname}:9093")
41+
.with_env("KAFKA_CONTROLLER_LISTENER_NAMES", "CONTROLLER")
42+
.with_env("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", "1")
43+
.with_env("KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS", "10")
44+
.with_env("CLUSTER_ID", kraft_cluster_id)
45+
.with_bind_ports(kafka_port, kafka_port)
3146
)
32-
kafka_container = kafka_container.with_env("ADV_HOST", kafka_address)
33-
# disable rest
34-
kafka_container = kafka_container.with_env("REST_PORT", 0)
35-
kafka_container = kafka_container.with_env("WEB_PORT", 0)
36-
kafka_container = kafka_container.with_env("CONNECT_PORT", 0)
37-
kafka_container = kafka_container.with_env("REGISTRY_PORT", 0)
38-
kafka_container = kafka_container.with_env("RUNTESTS", 0)
39-
kafka_container = kafka_container.with_env("SAMPLEDATA", 0)
40-
kafka_container = kafka_container.with_env("FORWARDLOGS", 0)
41-
kafka_container = kafka_container.with_env("SUPERVISORWEB", 0)
42-
43-
return (kafka_container, broker_list, kafka_port, zookeeper_port)
47+
return kafka_container, broker_list, kafka_port
4448

4549
@staticmethod
4650
def start_kafka_container(kafka_container: DockerContainer) -> None:
@@ -50,8 +54,8 @@ def start_kafka_container(kafka_container: DockerContainer) -> None:
5054
while cut_off > datetime.datetime.utcnow():
5155
time.sleep(0.5)
5256
logs = kafka_container.get_logs()
53-
if "success: broker entered RUNNING state" in logs[0].decode(
54-
"utf-8"
55-
) and "success: zookeeper entered RUNNING state" in logs[0].decode("utf-8"):
56-
return
57-
raise Exception("Failed to start container")
57+
for line in logs:
58+
line = line.decode()
59+
if "Kafka Server started" in line:
60+
return
61+
raise TimeoutError("Failed to start container")

tests/test_quixstreams/test_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ def test_quix_app_no_state_management_warning(
933933
)
934934

935935
with pytest.warns(RuntimeWarning) as warned:
936-
executor.submit(_stop_app_on_timeout, app, 10.0)
936+
executor.submit(_stop_app_on_timeout, app, 5.0)
937937
app.run(sdf)
938938

939939
warnings = [w for w in warned.list if w.category is RuntimeWarning]
@@ -1063,7 +1063,7 @@ def test_quix_app_stateful_quix_deployment_no_state_management_warning(
10631063
)
10641064

10651065
with pytest.warns(RuntimeWarning) as warned:
1066-
executor.submit(_stop_app_on_timeout, app, 10.0)
1066+
executor.submit(_stop_app_on_timeout, app, 5.0)
10671067
app.run(sdf)
10681068

10691069
warnings = [w for w in warned.list if w.category is RuntimeWarning]

0 commit comments

Comments
 (0)