Skip to content

Commit bcc2436

Browse files
committed
tests: integration: clean up valgrind services
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent 6807c19 commit bcc2436

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

tests/integration/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import logging
2020
import pytest
2121

22+
from utils.test_service import stop_active_services
23+
2224
# Configure logging
2325
def configure_logging():
2426
logger = logging.getLogger(__name__)
@@ -52,6 +54,10 @@ def pytest_sessionstart(session):
5254
#flb = FluentBitManager(GLOBAL_CONFIG['fluent_bit']['config_path'])
5355
#flb = FluentBitManager()
5456

57+
@pytest.hookimpl(trylast=True)
58+
def pytest_runtest_teardown(item, nextitem):
59+
stop_active_services()
60+
5561
@pytest.hookimpl(trylast=True)
5662
def pytest_sessionfinish(session, exitstatus):
5763
pass #logger.info("Finishing pytest session")

tests/integration/scenarios/out_http/tests/test_out_http_001.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
logger = logging.getLogger(__name__)
1818

19+
def _valgrind_timeout(timeout):
20+
if os.environ.get("VALGRIND"):
21+
return max(timeout * 3, 30)
22+
23+
return timeout
24+
25+
1926
def _wait_for_http_server(port, timeout=5):
2027
deadline = time.time() + timeout
2128

@@ -128,7 +135,7 @@ def stop(self):
128135
def wait_for_requests(self, minimum_count, timeout=10):
129136
return self.service.wait_for_condition(
130137
lambda: data_storage["requests"] if len(data_storage["requests"]) >= minimum_count else None,
131-
timeout=timeout,
138+
timeout=_valgrind_timeout(timeout),
132139
interval=0.5,
133140
description=f"{minimum_count} outbound HTTP requests",
134141
)
@@ -148,7 +155,7 @@ def _read_log():
148155

149156
return self.service.wait_for_condition(
150157
_read_log,
151-
timeout=timeout,
158+
timeout=_valgrind_timeout(timeout),
152159
interval=0.25,
153160
description=f"log message '{pattern}'",
154161
)

tests/integration/src/utils/test_service.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@
77
from utils.network import find_available_port
88

99

10+
_active_services = set()
11+
12+
13+
def stop_active_services():
14+
errors = []
15+
16+
for service in list(_active_services):
17+
try:
18+
service.stop()
19+
except Exception as exc:
20+
errors.append(exc)
21+
22+
if errors:
23+
raise errors[0]
24+
25+
1026
class FluentBitTestService:
1127
def __init__(
1228
self,
@@ -69,20 +85,29 @@ def start(self):
6985
for key, value in self.extra_env.items():
7086
self._set_env(key, str(value))
7187

72-
if self.pre_start:
73-
self.pre_start(self)
88+
_active_services.add(self)
7489

75-
self.flb.start()
90+
try:
91+
if self.pre_start:
92+
self.pre_start(self)
93+
94+
self.flb.start()
95+
except Exception:
96+
self.stop()
97+
raise
7698

7799
def stop(self):
100+
had_allocated_ports = bool(self._allocated_ports)
101+
78102
try:
79103
if self.flb:
80104
self.flb.stop()
81105
finally:
82-
if self.post_stop:
106+
if self.post_stop and had_allocated_ports:
83107
self.post_stop(self)
84108
self._restore_env()
85109
self._allocated_ports.clear()
110+
_active_services.discard(self)
86111

87112
def wait_for_http_endpoint(self, url, *, timeout=10, interval=0.5):
88113
deadline = time.time() + timeout

0 commit comments

Comments
 (0)