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