4
4
5
5
import asyncio
6
6
import logging
7
- import time
7
+ import tenacity
8
8
9
9
import pytest
10
10
import urllib3
@@ -135,29 +135,37 @@ async def test_exporter_endpoint(ops_test: OpsTest, mysql_router_charm_series: s
135
135
f"{ GRAFANA_AGENT_APP_NAME } :cos-agent" , f"{ MYSQL_ROUTER_APP_NAME } :cos-agent"
136
136
)
137
137
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"
145
149
146
150
logger .info ("Removing relation between mysqlrouter and grafana agent" )
147
151
await mysql_router_app .remove_relation (
148
152
f"{ GRAFANA_AGENT_APP_NAME } :cos-agent" , f"{ MYSQL_ROUTER_APP_NAME } :cos-agent"
149
153
)
150
154
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"
161
169
162
170
163
171
@pytest .mark .group (1 )
@@ -184,48 +192,60 @@ async def test_exporter_endpoint_with_tls(ops_test: OpsTest) -> None:
184
192
f"{ MYSQL_ROUTER_APP_NAME } :certificates" , f"{ TLS_APP_NAME } :certificates"
185
193
)
186
194
187
- time .sleep (60 )
188
-
189
195
mysql_test_app = ops_test .model .applications [APPLICATION_APP_NAME ]
190
196
unit_address = await mysql_test_app .units [0 ].get_public_address ()
191
197
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"
200
212
201
213
logger .info ("Relating mysqlrouter with grafana agent" )
202
214
await ops_test .model .relate (
203
215
f"{ GRAFANA_AGENT_APP_NAME } :cos-agent" , f"{ MYSQL_ROUTER_APP_NAME } :cos-agent"
204
216
)
205
217
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"
213
229
214
230
logger .info ("Removing relation between mysqlrouter and grafana agent" )
215
231
await mysql_router_app .remove_relation (
216
232
f"{ GRAFANA_AGENT_APP_NAME } :cos-agent" , f"{ MYSQL_ROUTER_APP_NAME } :cos-agent"
217
233
)
218
234
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"
229
249
230
250
logger .info (f"Removing relation between mysqlrouter and { TLS_APP_NAME } " )
231
251
await mysql_router_app .remove_relation (
0 commit comments