@@ -288,25 +288,68 @@ def test_create_timeseries(self):
288
288
self .assertEqual (timeseries , expected_timeseries )
289
289
290
290
291
+ class ResponseStub :
292
+ def __init__ (self , status_code ):
293
+ self .status_code = status_code
294
+ self .reason = "dummy_reason"
295
+ self .content = "dummy_content"
296
+
297
+
291
298
class TestExport (unittest .TestCase ):
292
299
# Initializes test data that is reused across tests
293
300
def setUp (self ):
294
- pass
301
+ self ._exporter = PrometheusRemoteWriteMetricsExporter (
302
+ endpoint = "/prom/test_endpoint"
303
+ )
295
304
296
305
# Ensures export is successful with valid export_records and config
297
- def test_export (self ):
298
- pass
299
-
300
- def test_valid_send_message (self ):
301
- pass
302
-
303
- def test_invalid_send_message (self ):
304
- pass
306
+ @mock .patch ("requests.post" , return_value = ResponseStub (200 ))
307
+ def test_export (self , mock_post ):
308
+ test_metric = Counter ("testname" , "testdesc" , "testunit" , int , None )
309
+ labels = {"environment" : "testing" }
310
+ record = ExportRecord (
311
+ test_metric , labels , SumAggregator (), Resource ({}),
312
+ )
313
+ result = self ._exporter .export ([record ])
314
+ self .assertIs (result , MetricsExportResult .SUCCESS )
315
+ self .assertEqual (mock_post .call_count , 1 )
316
+
317
+ @mock .patch ("requests.post" , return_value = ResponseStub (200 ))
318
+ def test_valid_send_message (self , mock_post ):
319
+ result = self ._exporter .send_message (bytes (), {})
320
+ self .assertEqual (mock_post .call_count , 1 )
321
+ self .assertEqual (result , MetricsExportResult .SUCCESS )
322
+
323
+ @mock .patch ("requests.post" , return_value = ResponseStub (404 ))
324
+ def test_invalid_send_message (self , mock_post ):
325
+ result = self ._exporter .send_message (bytes (), {})
326
+ self .assertEqual (mock_post .call_count , 1 )
327
+ self .assertEqual (result , MetricsExportResult .FAILURE )
305
328
306
329
# Verifies that build_message calls snappy.compress and returns SerializedString
307
- def test_build_message (self ):
308
- pass
330
+ @mock .patch ("snappy.compress" , return_value = bytes ())
331
+ def test_build_message (self , mock_compress ):
332
+ test_timeseries = [
333
+ TimeSeries (),
334
+ TimeSeries (),
335
+ ]
336
+ message = self ._exporter .build_message (test_timeseries )
337
+ self .assertEqual (mock_compress .call_count , 1 )
338
+ self .assertIsInstance (message , bytes )
309
339
310
340
# Ensure correct headers are added when valid config is provided
311
341
def test_get_headers (self ):
312
- pass
342
+ self ._exporter .headers = {"Custom Header" : "test_header" }
343
+ self ._exporter .headers = {"Custom Header" : "test_header" }
344
+ self ._exporter .bearer_token = "test_token"
345
+
346
+ headers = self ._exporter .get_headers ()
347
+ self .assertEqual (headers .get ("Content-Encoding" , "" ), "snappy" )
348
+ self .assertEqual (
349
+ headers .get ("Content-Type" , "" ), "application/x-protobuf"
350
+ )
351
+ self .assertEqual (
352
+ headers .get ("X-Prometheus-Remote-Write-Version" , "" ), "0.1.0"
353
+ )
354
+ self .assertEqual (headers .get ("Authorization" , "" ), "Bearer test_token" )
355
+ self .assertEqual (headers .get ("Custom Header" , "" ), "test_header" )
0 commit comments