3
3
4
4
from kubernetes .client import V1ContainerPort
5
5
6
+ from suite .custom_resources_utils import (
7
+ create_ts_from_yaml ,
8
+ patch_ts_from_yaml ,
9
+ patch_ts , delete_ts ,
10
+ create_gc_from_yaml ,
11
+ delete_gc ,
12
+ )
6
13
from suite .resources_utils import (
7
14
ensure_connection_to_public_endpoint ,
8
15
create_items_from_yaml ,
@@ -82,6 +89,7 @@ def fin():
82
89
return IngressSetup (req_url , ingress_host )
83
90
84
91
92
+
85
93
@pytest .mark .ingresses
86
94
@pytest .mark .smoke
87
95
class TestPrometheusExporter :
@@ -200,3 +208,100 @@ def test_https_metrics(
200
208
resp_content = resp .content .decode ("utf-8" )
201
209
for item in expected_metrics :
202
210
assert item in resp_content
211
+
212
+
213
+ @pytest .fixture (scope = "class" )
214
+ def ts_setup (request , kube_apis , crd_ingress_controller ):
215
+ global_config_file = f"{ TEST_DATA } /prometheus/transport-server/global-configuration.yaml"
216
+
217
+ gc_resource = create_gc_from_yaml (kube_apis .custom_objects , global_config_file , "nginx-ingress" )
218
+
219
+ def fin ():
220
+ delete_gc (kube_apis .custom_objects , gc_resource , "nginx-ingress" )
221
+
222
+ request .addfinalizer (fin )
223
+
224
+
225
+ def assert_ts_total_metric (ingress_controller_endpoint , ts_type , value ):
226
+ req_url = f"http://{ ingress_controller_endpoint .public_ip } :{ ingress_controller_endpoint .metrics_port } /metrics"
227
+ resp = requests .get (req_url )
228
+ resp_content = resp .content .decode ("utf-8" )
229
+
230
+ assert resp .status_code == 200 , f"Expected 200 code for /metrics but got { resp .status_code } "
231
+ assert f'nginx_ingress_controller_transportserver_resources_total{{class="nginx",type="{ ts_type } "}} { value } ' in resp_content
232
+
233
+
234
+ @pytest .mark .ts
235
+ @pytest .mark .parametrize (
236
+ "crd_ingress_controller" ,
237
+ [
238
+ pytest .param (
239
+ {
240
+ "type" : "complete" ,
241
+ "extra_args" :
242
+ [
243
+ "-global-configuration=nginx-ingress/nginx-configuration" ,
244
+ "-enable-tls-passthrough" ,
245
+ "-enable-prometheus-metrics"
246
+ ]
247
+ },
248
+ )
249
+ ],
250
+ indirect = True ,
251
+ )
252
+ class TestTransportServerMetrics :
253
+ @pytest .mark .parametrize ("ts" , [
254
+ (f"{ TEST_DATA } /prometheus/transport-server/passthrough.yaml" , "passthrough" ),
255
+ (f"{ TEST_DATA } /prometheus/transport-server/tcp.yaml" , "tcp" ),
256
+ (f"{ TEST_DATA } /prometheus/transport-server/udp.yaml" , "udp" )
257
+ ])
258
+ def test_total_metrics (
259
+ self ,
260
+ crd_ingress_controller ,
261
+ ts_setup ,
262
+ ingress_controller_endpoint ,
263
+ kube_apis ,
264
+ test_namespace ,
265
+ ts
266
+ ):
267
+ """
268
+ Tests nginx_ingress_controller_transportserver_resources_total metric for a given TransportServer type.
269
+ """
270
+ ts_file = ts [0 ]
271
+ ts_type = ts [1 ]
272
+
273
+ # initially, the number of TransportServers is 0
274
+
275
+ assert_ts_total_metric (ingress_controller_endpoint , ts_type , 0 )
276
+
277
+ # create a TS and check the metric is 1
278
+
279
+ ts_resource = create_ts_from_yaml (kube_apis .custom_objects , ts_file , test_namespace )
280
+ wait_before_test ()
281
+
282
+ assert_ts_total_metric (ingress_controller_endpoint , ts_type , 1 )
283
+
284
+ # make the TS invalid and check the metric is 0
285
+
286
+ ts_resource ["spec" ]["listener" ]["protocol" ] = "invalid"
287
+
288
+ patch_ts (kube_apis .custom_objects , test_namespace , ts_resource )
289
+ wait_before_test ()
290
+
291
+ assert_ts_total_metric (ingress_controller_endpoint , ts_type , 0 )
292
+
293
+ # restore the TS and check the metric is 1
294
+
295
+ patch_ts_from_yaml (
296
+ kube_apis .custom_objects , ts_resource ["metadata" ]["name" ], ts_file , test_namespace
297
+ )
298
+ wait_before_test ()
299
+
300
+ assert_ts_total_metric (ingress_controller_endpoint , ts_type , 1 )
301
+
302
+ # delete the TS and check the metric is 0
303
+
304
+ delete_ts (kube_apis .custom_objects , ts_resource , test_namespace )
305
+ wait_before_test ()
306
+
307
+ assert_ts_total_metric (ingress_controller_endpoint , ts_type , 0 )
0 commit comments