11package io .prometheus .metrics .exporter .pushgateway ;
22
3- import static org .assertj .core .api .Assertions .*;
4- import static org .assertj .core .api .Assertions .assertThat ;
5- import static org .mockito .Mockito .CALLS_REAL_METHODS ;
6- import static org .mockito .Mockito .mockStatic ;
7- import static org .mockserver .model .HttpRequest .request ;
8- import static org .mockserver .model .HttpResponse .response ;
9-
103import io .prometheus .metrics .core .metrics .Gauge ;
114import io .prometheus .metrics .model .registry .PrometheusRegistry ;
125import io .prometheus .metrics .model .snapshots .EscapingScheme ;
136import io .prometheus .metrics .model .snapshots .PrometheusNaming ;
14- import io .prometheus .metrics .model .snapshots .ValidationScheme ;
15- import java .io .IOException ;
16- import java .lang .reflect .Field ;
17- import java .net .InetAddress ;
18- import java .net .URL ;
197import org .junit .jupiter .api .AfterEach ;
208import org .junit .jupiter .api .BeforeEach ;
219import org .junit .jupiter .api .Test ;
22- import org .mockito . MockedStatic ;
10+ import org .junitpioneer . jupiter . SetSystemProperty ;
2311import org .mockserver .client .MockServerClient ;
2412import org .mockserver .integration .ClientAndServer ;
2513
14+ import java .io .IOException ;
15+ import java .lang .reflect .Field ;
16+ import java .net .InetAddress ;
17+ import java .net .URL ;
18+
19+ import static org .assertj .core .api .Assertions .assertThat ;
20+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
21+ import static org .mockserver .model .HttpRequest .request ;
22+ import static org .mockserver .model .HttpResponse .response ;
23+
2624class PushGatewayTest {
2725
2826 private MockServerClient mockServerClient ;
@@ -40,6 +38,7 @@ public void setUp() {
4038 @ AfterEach
4139 void tearDown () {
4240 mockServerClient .stop ();
41+ PrometheusNaming .resetForTest ();
4342 }
4443
4544 @ Test
@@ -147,25 +146,21 @@ public void testPushWithGroupingKey() throws IOException {
147146 pg .push ();
148147 }
149148
149+ @ SetSystemProperty (key = "io.prometheus.naming.validationScheme" , value = "utf-8" )
150150 @ Test
151151 public void testPushWithEscapedGroupingKey () throws IOException {
152- try (MockedStatic <PrometheusNaming > mock =
153- mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
154- mock .when (PrometheusNaming ::getValidationScheme )
155- .thenReturn (ValidationScheme .UTF_8_VALIDATION );
156-
157- mockServerClient
158- .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
159- .respond (response ().withStatusCode (202 ));
160- PushGateway pg =
161- PushGateway .builder ()
162- .address ("localhost:" + mockServerClient .getPort ())
163- .registry (registry )
164- .job ("j" )
165- .groupingKey ("l.1" , "v1" )
166- .build ();
167- pg .push ();
168- }
152+ PrometheusNaming .resetForTest ();
153+ mockServerClient
154+ .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
155+ .respond (response ().withStatusCode (202 ));
156+ PushGateway pg =
157+ PushGateway .builder ()
158+ .address ("localhost:" + mockServerClient .getPort ())
159+ .registry (registry )
160+ .job ("j" )
161+ .groupingKey ("l.1" , "v1" )
162+ .build ();
163+ pg .push ();
169164 }
170165
171166 @ Test
@@ -184,26 +179,23 @@ public void testPushWithMultiGroupingKey() throws IOException {
184179 pg .push ();
185180 }
186181
182+ @ SetSystemProperty (key = "io.prometheus.naming.validationScheme" , value = "utf-8" )
187183 @ Test
188184 public void testPushWithMultiEscapedGroupingKey () throws IOException {
189- try (MockedStatic <PrometheusNaming > mock =
190- mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
191- mock .when (PrometheusNaming ::getValidationScheme )
192- .thenReturn (ValidationScheme .UTF_8_VALIDATION );
193-
194- mockServerClient
195- .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1/U__l_2e_2/v2" ))
196- .respond (response ().withStatusCode (202 ));
197- PushGateway pg =
198- PushGateway .builder ()
199- .address ("localhost:" + mockServerClient .getPort ())
200- .registry (registry )
201- .job ("j" )
202- .groupingKey ("l.1" , "v1" )
203- .groupingKey ("l.2" , "v2" )
204- .build ();
205- pg .push ();
206- }
185+ PrometheusNaming .resetForTest ();
186+
187+ mockServerClient
188+ .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1/U__l_2e_2/v2" ))
189+ .respond (response ().withStatusCode (202 ));
190+ PushGateway pg =
191+ PushGateway .builder ()
192+ .address ("localhost:" + mockServerClient .getPort ())
193+ .registry (registry )
194+ .job ("j" )
195+ .groupingKey ("l.1" , "v1" )
196+ .groupingKey ("l.2" , "v2" )
197+ .build ();
198+ pg .push ();
207199 }
208200
209201 @ Test
@@ -254,25 +246,21 @@ public void testPushCollectorWithGroupingKey() throws IOException {
254246 pg .push (gauge );
255247 }
256248
249+ @ SetSystemProperty (key = "io.prometheus.naming.validationScheme" , value = "utf-8" )
257250 @ Test
258251 public void testPushCollectorWithEscapedGroupingKey () throws IOException {
259- try (MockedStatic <PrometheusNaming > mock =
260- mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
261- mock .when (PrometheusNaming ::getValidationScheme )
262- .thenReturn (ValidationScheme .UTF_8_VALIDATION );
263-
264- mockServerClient
265- .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
266- .respond (response ().withStatusCode (202 ));
267- PushGateway pg =
268- PushGateway .builder ()
269- .address ("localhost:" + mockServerClient .getPort ())
270- .registry (registry )
271- .job ("j" )
272- .groupingKey ("l.1" , "v1" )
273- .build ();
274- pg .push (gauge );
275- }
252+ PrometheusNaming .resetForTest ();
253+ mockServerClient
254+ .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
255+ .respond (response ().withStatusCode (202 ));
256+ PushGateway pg =
257+ PushGateway .builder ()
258+ .address ("localhost:" + mockServerClient .getPort ())
259+ .registry (registry )
260+ .job ("j" )
261+ .groupingKey ("l.1" , "v1" )
262+ .build ();
263+ pg .push (gauge );
276264 }
277265
278266 @ Test
@@ -314,25 +302,21 @@ public void testPushAddWithGroupingKey() throws IOException {
314302 pg .pushAdd ();
315303 }
316304
305+ @ SetSystemProperty (key = "io.prometheus.naming.validationScheme" , value = "utf-8" )
317306 @ Test
318307 public void testPushAddWithEscapedGroupingKey () throws IOException {
319- try (MockedStatic <PrometheusNaming > mock =
320- mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
321- mock .when (PrometheusNaming ::getValidationScheme )
322- .thenReturn (ValidationScheme .UTF_8_VALIDATION );
323-
324- mockServerClient
325- .when (request ().withMethod ("POST" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
326- .respond (response ().withStatusCode (202 ));
327- PushGateway pg =
328- PushGateway .builder ()
329- .address ("localhost:" + mockServerClient .getPort ())
330- .registry (registry )
331- .groupingKey ("l.1" , "v1" )
332- .job ("j" )
333- .build ();
334- pg .pushAdd ();
335- }
308+ PrometheusNaming .resetForTest ();
309+ mockServerClient
310+ .when (request ().withMethod ("POST" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
311+ .respond (response ().withStatusCode (202 ));
312+ PushGateway pg =
313+ PushGateway .builder ()
314+ .address ("localhost:" + mockServerClient .getPort ())
315+ .registry (registry )
316+ .groupingKey ("l.1" , "v1" )
317+ .job ("j" )
318+ .build ();
319+ pg .pushAdd ();
336320 }
337321
338322 @ Test
@@ -350,25 +334,22 @@ public void testPushAddCollectorWithGroupingKey() throws IOException {
350334 pg .pushAdd (gauge );
351335 }
352336
337+ @ SetSystemProperty (key = "io.prometheus.naming.validationScheme" , value = "utf-8" )
353338 @ Test
354339 public void testPushAddCollectorWithEscapedGroupingKey () throws IOException {
355- try (MockedStatic <PrometheusNaming > mock =
356- mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
357- mock .when (PrometheusNaming ::getValidationScheme )
358- .thenReturn (ValidationScheme .UTF_8_VALIDATION );
359-
360- mockServerClient
361- .when (request ().withMethod ("POST" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
362- .respond (response ().withStatusCode (202 ));
363- PushGateway pg =
364- PushGateway .builder ()
365- .address ("localhost:" + mockServerClient .getPort ())
366- .registry (registry )
367- .groupingKey ("l.1" , "v1" )
368- .job ("j" )
369- .build ();
370- pg .pushAdd (gauge );
371- }
340+ PrometheusNaming .resetForTest ();
341+
342+ mockServerClient
343+ .when (request ().withMethod ("POST" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
344+ .respond (response ().withStatusCode (202 ));
345+ PushGateway pg =
346+ PushGateway .builder ()
347+ .address ("localhost:" + mockServerClient .getPort ())
348+ .registry (registry )
349+ .groupingKey ("l.1" , "v1" )
350+ .job ("j" )
351+ .build ();
352+ pg .pushAdd (gauge );
372353 }
373354
374355 @ Test
@@ -395,24 +376,21 @@ public void testDeleteWithGroupingKey() throws IOException {
395376 pg .delete ();
396377 }
397378
379+ @ SetSystemProperty (key = "io.prometheus.naming.validationScheme" , value = "utf-8" )
398380 @ Test
399381 public void testDeleteWithEscapedGroupingKey () throws IOException {
400- try (MockedStatic <PrometheusNaming > mock =
401- mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
402- mock .when (PrometheusNaming ::getValidationScheme )
403- .thenReturn (ValidationScheme .UTF_8_VALIDATION );
404-
405- mockServerClient
406- .when (request ().withMethod ("DELETE" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
407- .respond (response ().withStatusCode (202 ));
408- PushGateway pg =
409- PushGateway .builder ()
410- .address ("localhost:" + mockServerClient .getPort ())
411- .job ("j" )
412- .groupingKey ("l.1" , "v1" )
413- .build ();
414- pg .delete ();
415- }
382+ PrometheusNaming .resetForTest ();
383+
384+ mockServerClient
385+ .when (request ().withMethod ("DELETE" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
386+ .respond (response ().withStatusCode (202 ));
387+ PushGateway pg =
388+ PushGateway .builder ()
389+ .address ("localhost:" + mockServerClient .getPort ())
390+ .job ("j" )
391+ .groupingKey ("l.1" , "v1" )
392+ .build ();
393+ pg .delete ();
416394 }
417395
418396 @ Test
@@ -432,30 +410,27 @@ public void testInstanceIpGroupingKey() throws IOException {
432410 pg .delete ();
433411 }
434412
413+ @ SetSystemProperty (key = "io.prometheus.naming.validationScheme" , value = "utf-8" )
435414 @ Test
436415 public void testInstanceIpEscapedGroupingKey () throws IOException {
437- try (MockedStatic <PrometheusNaming > mock =
438- mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
439- mock .when (PrometheusNaming ::getValidationScheme )
440- .thenReturn (ValidationScheme .UTF_8_VALIDATION );
441-
442- String ip = InetAddress .getLocalHost ().getHostAddress ();
443- assertThat (ip ).isNotEmpty ();
444- mockServerClient
445- .when (
446- request ()
447- .withMethod ("DELETE" )
448- .withPath ("/metrics/job/j/instance/" + ip + "/U__l_2e_1/v1" ))
449- .respond (response ().withStatusCode (202 ));
450- PushGateway pg =
451- PushGateway .builder ()
452- .address ("localhost:" + mockServerClient .getPort ())
453- .job ("j" )
454- .groupingKey ("l.1" , "v1" )
455- .instanceIpGroupingKey ()
456- .build ();
457- pg .delete ();
458- }
416+ PrometheusNaming .resetForTest ();
417+
418+ String ip = InetAddress .getLocalHost ().getHostAddress ();
419+ assertThat (ip ).isNotEmpty ();
420+ mockServerClient
421+ .when (
422+ request ()
423+ .withMethod ("DELETE" )
424+ .withPath ("/metrics/job/j/instance/" + ip + "/U__l_2e_1/v1" ))
425+ .respond (response ().withStatusCode (202 ));
426+ PushGateway pg =
427+ PushGateway .builder ()
428+ .address ("localhost:" + mockServerClient .getPort ())
429+ .job ("j" )
430+ .groupingKey ("l.1" , "v1" )
431+ .instanceIpGroupingKey ()
432+ .build ();
433+ pg .delete ();
459434 }
460435
461436 @ Test
0 commit comments