@@ -266,9 +266,7 @@ def test_convert_from_histogram(self):
266
266
)
267
267
timeseries = self .exporter ._convert_from_histogram (histogram_record )
268
268
self .assertEqual (timeseries [0 ], expected_le_0_timeseries )
269
- self .assertEqual (
270
- timeseries [1 ], expected_le_inf_timeseries ,
271
- )
269
+ self .assertEqual (timeseries [1 ], expected_le_inf_timeseries )
272
270
self .assertEqual (timeseries [2 ], expected_count_timeseries )
273
271
274
272
# Ensures last value aggregator is correctly converted to timeseries
@@ -361,70 +359,64 @@ def create_label(name, value):
361
359
sample .value = 5.0
362
360
363
361
timeseries = self .exporter ._create_timeseries (
364
- export_record , "testname" , 5.0 ,
362
+ export_record , "testname" , 5.0
365
363
)
366
364
self .assertEqual (timeseries , expected_timeseries )
367
365
368
366
369
- class ResponseStub :
370
- def __init__ (self , status_code ):
371
- self .status_code = status_code
372
- self .reason = "dummy_reason"
373
- self .content = "dummy_content"
374
-
375
-
376
367
class TestExport (unittest .TestCase ):
377
368
# Initializes test data that is reused across tests
378
369
def setUp (self ):
379
- self ._exporter = PrometheusRemoteWriteMetricsExporter (
370
+ self .exporter = PrometheusRemoteWriteMetricsExporter (
380
371
endpoint = "/prom/test_endpoint"
381
372
)
382
373
383
374
# Ensures export is successful with valid export_records and config
384
- @mock . patch ("requests.post" , return_value = ResponseStub ( 200 ))
375
+ @patch ("requests.post" , return_value = Mock ( ))
385
376
def test_export (self , mock_post ):
377
+ mock_post .return_value .configure_mock (** {"status_code" : 200 })
386
378
test_metric = Counter ("testname" , "testdesc" , "testunit" , int , None )
387
- labels = {"environment" : "testing" }
379
+ labels = get_dict_as_key ( {"environment" : "testing" })
388
380
record = ExportRecord (
389
- test_metric , labels , SumAggregator (), Resource ({}),
381
+ test_metric , labels , SumAggregator (), Resource ({})
390
382
)
391
- result = self ._exporter .export ([record ])
383
+ result = self .exporter .export ([record ])
392
384
self .assertIs (result , MetricsExportResult .SUCCESS )
393
385
self .assertEqual (mock_post .call_count , 1 )
394
386
395
- @mock . patch ("requests.post" , return_value = ResponseStub ( 200 ))
387
+ @patch ("requests.post" , return_value = Mock ( ))
396
388
def test_valid_send_message (self , mock_post ):
397
- result = self ._exporter .send_message (bytes (), {})
389
+ mock_post .return_value .configure_mock (** {"status_code" : 200 })
390
+ result = self .exporter ._send_message (bytes (), {})
398
391
self .assertEqual (mock_post .call_count , 1 )
399
392
self .assertEqual (result , MetricsExportResult .SUCCESS )
400
393
401
- @mock . patch ("requests.post" , return_value = ResponseStub ( 404 ))
394
+ @patch ("requests.post" , return_value = Mock ( ))
402
395
def test_invalid_send_message (self , mock_post ):
403
- result = self ._exporter .send_message (bytes (), {})
396
+ mock_post .return_value .configure_mock (
397
+ ** {
398
+ "status_code" : 404 ,
399
+ "reason" : "test_reason" ,
400
+ "content" : "test_content" ,
401
+ }
402
+ )
403
+ result = self .exporter ._send_message (bytes (), {})
404
404
self .assertEqual (mock_post .call_count , 1 )
405
405
self .assertEqual (result , MetricsExportResult .FAILURE )
406
406
407
407
# Verifies that build_message calls snappy.compress and returns SerializedString
408
- @mock . patch ("snappy.compress" , return_value = bytes ())
408
+ @patch ("snappy.compress" , return_value = bytes ())
409
409
def test_build_message (self , mock_compress ):
410
- test_timeseries = [
411
- TimeSeries (),
412
- TimeSeries (),
413
- ]
414
- message = self ._exporter .build_message (test_timeseries )
410
+ message = self .exporter ._build_message ([TimeSeries ()])
415
411
self .assertEqual (mock_compress .call_count , 1 )
416
412
self .assertIsInstance (message , bytes )
417
413
418
414
# Ensure correct headers are added when valid config is provided
419
- def test_get_headers (self ):
420
- self ._exporter .headers = {"Custom Header" : "test_header" }
421
-
422
- headers = self ._exporter .get_headers ()
423
- self .assertEqual (headers .get ("Content-Encoding" , "" ), "snappy" )
424
- self .assertEqual (
425
- headers .get ("Content-Type" , "" ), "application/x-protobuf"
426
- )
427
- self .assertEqual (
428
- headers .get ("X-Prometheus-Remote-Write-Version" , "" ), "0.1.0"
429
- )
430
- self .assertEqual (headers .get ("Custom Header" , "" ), "test_header" )
415
+ def test_build_headers (self ):
416
+ self .exporter .headers = {"Custom Header" : "test_header" }
417
+
418
+ headers = self .exporter ._build_headers ()
419
+ self .assertEqual (headers ["Content-Encoding" ], "snappy" )
420
+ self .assertEqual (headers ["Content-Type" ], "application/x-protobuf" )
421
+ self .assertEqual (headers ["X-Prometheus-Remote-Write-Version" ], "0.1.0" )
422
+ self .assertEqual (headers ["Custom Header" ], "test_header" )
0 commit comments