|
3 | 3 | import sys |
4 | 4 | import threading |
5 | 5 | import time |
| 6 | +import pytest |
6 | 7 |
|
7 | 8 | from prometheus_client import ( |
8 | 9 | CollectorRegistry, CONTENT_TYPE_LATEST, Counter, delete_from_gateway, Enum, |
|
11 | 12 | ) |
12 | 13 | from prometheus_client.core import GaugeHistogramMetricFamily, Timestamp |
13 | 14 | from prometheus_client.exposition import ( |
14 | | - basic_auth_handler, default_handler, MetricsHandler, |
| 15 | + basic_auth_handler, default_handler, MetricsHandler, generate_latest, |
15 | 16 | ) |
| 17 | +from prometheus_client import core |
16 | 18 |
|
17 | 19 | if sys.version_info < (2, 7): |
18 | 20 | # We need the skip decorators from unittest2 on Python 2.6. |
@@ -303,5 +305,84 @@ def test_metrics_handler_subclassing(self): |
303 | 305 | self.assertTrue(issubclass(handler, (MetricsHandler, subclass))) |
304 | 306 |
|
305 | 307 |
|
| 308 | +@pytest.fixture |
| 309 | +def registry(): |
| 310 | + return core.CollectorRegistry() |
| 311 | + |
| 312 | + |
| 313 | +class Collector: |
| 314 | + def __init__(self, metric_family, *values): |
| 315 | + self.metric_family = metric_family |
| 316 | + self.values = values |
| 317 | + |
| 318 | + def collect(self): |
| 319 | + self.metric_family.add_metric([], *self.values) |
| 320 | + return [self.metric_family] |
| 321 | + |
| 322 | + |
| 323 | +def _expect_metric_exception(registry, expected_error): |
| 324 | + try: |
| 325 | + generate_latest(registry) |
| 326 | + except expected_error as exception: |
| 327 | + assert isinstance(exception.args[-1], core.Metric) |
| 328 | + # Got a valid error as expected, return quietly |
| 329 | + return |
| 330 | + |
| 331 | + raise RuntimeError('Expected exception not raised') |
| 332 | + |
| 333 | + |
| 334 | +@pytest.mark.parametrize('MetricFamily', [ |
| 335 | + core.CounterMetricFamily, |
| 336 | + core.GaugeMetricFamily, |
| 337 | +]) |
| 338 | +@pytest.mark.parametrize('value,error', [ |
| 339 | + (None, TypeError), |
| 340 | + ('', ValueError), |
| 341 | + ('x', ValueError), |
| 342 | + ([], TypeError), |
| 343 | + ({}, TypeError), |
| 344 | +]) |
| 345 | +def test_basic_metric_families(registry, MetricFamily, value, error): |
| 346 | + metric_family = MetricFamily(MetricFamily.__name__, 'help') |
| 347 | + registry.register(Collector(metric_family, value)) |
| 348 | + _expect_metric_exception(registry, error) |
| 349 | + |
| 350 | + |
| 351 | +@pytest.mark.parametrize('count_value,sum_value,error', [ |
| 352 | + (None, 0, TypeError), |
| 353 | + (0, None, TypeError), |
| 354 | + ('', 0, ValueError), |
| 355 | + (0, '', ValueError), |
| 356 | + ([], 0, TypeError), |
| 357 | + (0, [], TypeError), |
| 358 | + ({}, 0, TypeError), |
| 359 | + (0, {}, TypeError), |
| 360 | +]) |
| 361 | +def test_summary_metric_family(registry, count_value, sum_value, error): |
| 362 | + metric_family = core.SummaryMetricFamily('summary', 'help') |
| 363 | + registry.register(Collector(metric_family, count_value, sum_value)) |
| 364 | + _expect_metric_exception(registry, error) |
| 365 | + |
| 366 | + |
| 367 | +@pytest.mark.parametrize('MetricFamily', [ |
| 368 | + core.HistogramMetricFamily, |
| 369 | + core.GaugeHistogramMetricFamily, |
| 370 | +]) |
| 371 | +@pytest.mark.parametrize('buckets,sum_value,error', [ |
| 372 | + ([('spam', 0), ('eggs', 0)], None, TypeError), |
| 373 | + ([('spam', 0), ('eggs', None)], 0, TypeError), |
| 374 | + ([('spam', 0), (None, 0)], 0, AttributeError), |
| 375 | + ([('spam', None), ('eggs', 0)], 0, TypeError), |
| 376 | + ([(None, 0), ('eggs', 0)], 0, AttributeError), |
| 377 | + ([('spam', 0), ('eggs', 0)], '', ValueError), |
| 378 | + ([('spam', 0), ('eggs', '')], 0, ValueError), |
| 379 | + ([('spam', ''), ('eggs', 0)], 0, ValueError), |
| 380 | +]) |
| 381 | +def test_histogram_metric_families(MetricFamily, registry, buckets, sum_value, error): |
| 382 | + metric_family = MetricFamily(MetricFamily.__name__, 'help') |
| 383 | + registry.register(Collector(metric_family, buckets, sum_value)) |
| 384 | + _expect_metric_exception(registry, error) |
| 385 | + |
| 386 | + |
306 | 387 | if __name__ == '__main__': |
307 | 388 | unittest.main() |
0 commit comments