Skip to content

Commit 1134e43

Browse files
Use tenacity to retry checking exporter endpoints instead of using time.sleep()
1 parent 0a36768 commit 1134e43

File tree

1 file changed

+65
-45
lines changed

1 file changed

+65
-45
lines changed

tests/integration/test_exporter.py

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import asyncio
66
import logging
7-
import time
7+
import tenacity
88

99
import pytest
1010
import urllib3
@@ -135,29 +135,37 @@ async def test_exporter_endpoint(ops_test: OpsTest, mysql_router_charm_series: s
135135
f"{GRAFANA_AGENT_APP_NAME}:cos-agent", f"{MYSQL_ROUTER_APP_NAME}:cos-agent"
136136
)
137137

138-
time.sleep(60)
139-
140-
jmx_resp = http.request("GET", f"http://{unit_address}:49152/metrics")
141-
assert jmx_resp.status == 200, "❌ cannot connect to metrics endpoint with relation with cos"
142-
assert "mysqlrouter_route_health" in str(
143-
jmx_resp.data
144-
), "❌ did not find expected metric in response"
138+
for attempt in tenacity.Retrying(
139+
reraise=True,
140+
stop=tenacity.stop_after_delay(120),
141+
wait=tenacity.wait_fixed(10),
142+
):
143+
with attempt:
144+
jmx_resp = http.request("GET", f"http://{unit_address}:49152/metrics")
145+
assert jmx_resp.status == 200, "❌ cannot connect to metrics endpoint with relation with cos"
146+
assert "mysqlrouter_route_health" in str(
147+
jmx_resp.data
148+
), "❌ did not find expected metric in response"
145149

146150
logger.info("Removing relation between mysqlrouter and grafana agent")
147151
await mysql_router_app.remove_relation(
148152
f"{GRAFANA_AGENT_APP_NAME}:cos-agent", f"{MYSQL_ROUTER_APP_NAME}:cos-agent"
149153
)
150154

151-
time.sleep(60)
152-
153-
try:
154-
http.request("GET", f"http://{unit_address}:49152/metrics")
155-
except urllib3.exceptions.MaxRetryError as e:
156-
assert (
157-
"[Errno 111] Connection refused" in e.reason.args[0]
158-
), "❌ expected connection refused error"
159-
else:
160-
assert False, "❌ can connect to metrics endpoint without relation with cos"
155+
for attempt in tenacity.Retrying(
156+
reraise=True,
157+
stop=tenacity.stop_after_delay(120),
158+
wait=tenacity.wait_fixed(10),
159+
):
160+
with attempt:
161+
try:
162+
http.request("GET", f"http://{unit_address}:49152/metrics")
163+
except urllib3.exceptions.MaxRetryError as e:
164+
assert (
165+
"[Errno 111] Connection refused" in e.reason.args[0]
166+
), "❌ expected connection refused error"
167+
else:
168+
assert False, "❌ can connect to metrics endpoint without relation with cos"
161169

162170

163171
@pytest.mark.group(1)
@@ -184,48 +192,60 @@ async def test_exporter_endpoint_with_tls(ops_test: OpsTest) -> None:
184192
f"{MYSQL_ROUTER_APP_NAME}:certificates", f"{TLS_APP_NAME}:certificates"
185193
)
186194

187-
time.sleep(60)
188-
189195
mysql_test_app = ops_test.model.applications[APPLICATION_APP_NAME]
190196
unit_address = await mysql_test_app.units[0].get_public_address()
191197

192-
try:
193-
http.request("GET", f"http://{unit_address}:49152/metrics")
194-
except urllib3.exceptions.MaxRetryError as e:
195-
assert (
196-
"[Errno 111] Connection refused" in e.reason.args[0]
197-
), "❌ expected connection refused error"
198-
else:
199-
assert False, "❌ can connect to metrics endpoint without relation with cos"
198+
for attempt in tenacity.Retrying(
199+
reraise=True,
200+
stop=tenacity.stop_after_delay(120),
201+
wait=tenacity.wait_fixed(10),
202+
):
203+
with attempt:
204+
try:
205+
http.request("GET", f"http://{unit_address}:49152/metrics")
206+
except urllib3.exceptions.MaxRetryError as e:
207+
assert (
208+
"[Errno 111] Connection refused" in e.reason.args[0]
209+
), "❌ expected connection refused error"
210+
else:
211+
assert False, "❌ can connect to metrics endpoint without relation with cos"
200212

201213
logger.info("Relating mysqlrouter with grafana agent")
202214
await ops_test.model.relate(
203215
f"{GRAFANA_AGENT_APP_NAME}:cos-agent", f"{MYSQL_ROUTER_APP_NAME}:cos-agent"
204216
)
205217

206-
time.sleep(60)
207-
208-
jmx_resp = http.request("GET", f"http://{unit_address}:49152/metrics")
209-
assert jmx_resp.status == 200, "❌ cannot connect to metrics endpoint with relation with cos"
210-
assert "mysqlrouter_route_health" in str(
211-
jmx_resp.data
212-
), "❌ did not find expected metric in response"
218+
for attempt in tenacity.Retrying(
219+
reraise=True,
220+
stop=tenacity.stop_after_delay(120),
221+
wait=tenacity.wait_fixed(10),
222+
):
223+
with attempt:
224+
jmx_resp = http.request("GET", f"http://{unit_address}:49152/metrics")
225+
assert jmx_resp.status == 200, "❌ cannot connect to metrics endpoint with relation with cos"
226+
assert "mysqlrouter_route_health" in str(
227+
jmx_resp.data
228+
), "❌ did not find expected metric in response"
213229

214230
logger.info("Removing relation between mysqlrouter and grafana agent")
215231
await mysql_router_app.remove_relation(
216232
f"{GRAFANA_AGENT_APP_NAME}:cos-agent", f"{MYSQL_ROUTER_APP_NAME}:cos-agent"
217233
)
218234

219-
time.sleep(60)
220-
221-
try:
222-
http.request("GET", f"http://{unit_address}:49152/metrics")
223-
except urllib3.exceptions.MaxRetryError as e:
224-
assert (
225-
"[Errno 111] Connection refused" in e.reason.args[0]
226-
), "❌ expected connection refused error"
227-
else:
228-
assert False, "❌ can connect to metrics endpoint without relation with cos"
235+
for attempt in tenacity.Retrying(
236+
reraise=True,
237+
stop=tenacity.stop_after_delay(120),
238+
wait=tenacity.wait_fixed(10),
239+
):
240+
with attempt:
241+
try:
242+
http.request("GET", f"http://{unit_address}:49152/metrics")
243+
except urllib3.exceptions.MaxRetryError as e:
244+
assert (
245+
"[Errno 111] Connection refused" in e.reason.args[0]
246+
), "❌ expected connection refused error"
247+
else:
248+
assert False, "❌ can connect to metrics endpoint without relation with cos"
229249

230250
logger.info(f"Removing relation between mysqlrouter and {TLS_APP_NAME}")
231251
await mysql_router_app.remove_relation(

0 commit comments

Comments
 (0)