|
| 1 | +from __future__ import unicode_literals |
| 2 | + |
| 3 | +import unittest |
| 4 | + |
| 5 | +from prometheus_client import CollectorRegistry, PlatformCollector |
| 6 | + |
| 7 | + |
| 8 | +class TestPlatformCollector(unittest.TestCase): |
| 9 | + def setUp(self): |
| 10 | + self.registry = CollectorRegistry() |
| 11 | + self.platform = _MockPlatform() |
| 12 | + |
| 13 | + def test_python_info(self): |
| 14 | + PlatformCollector(registry=self.registry, platform=self.platform) |
| 15 | + self.assertLabels("python_info", { |
| 16 | + "version": "python_version", |
| 17 | + "implementation": "python_implementation", |
| 18 | + "major": "pvt_major", |
| 19 | + "minor": "pvt_minor", |
| 20 | + "patchlevel": "pvt_patchlevel" |
| 21 | + }) |
| 22 | + |
| 23 | + def test_system_info_java(self): |
| 24 | + self.platform._system = "Java" |
| 25 | + PlatformCollector(registry=self.registry, platform=self.platform) |
| 26 | + self.assertLabels("python_info", { |
| 27 | + "version": "python_version", |
| 28 | + "implementation": "python_implementation", |
| 29 | + "major": "pvt_major", |
| 30 | + "minor": "pvt_minor", |
| 31 | + "patchlevel": "pvt_patchlevel", |
| 32 | + "jvm_version": "jv_release", |
| 33 | + "jvm_release": "vm_release", |
| 34 | + "jvm_vendor": "vm_vendor", |
| 35 | + "jvm_name": "vm_name" |
| 36 | + }) |
| 37 | + |
| 38 | + def assertLabels(self, name, labels): |
| 39 | + for metric in self.registry.collect(): |
| 40 | + for n, l, value in metric.samples: |
| 41 | + if n == name: |
| 42 | + assert l == labels |
| 43 | + return |
| 44 | + assert False |
| 45 | + |
| 46 | + |
| 47 | +class _MockPlatform(object): |
| 48 | + def __init__(self): |
| 49 | + self._system = "system" |
| 50 | + |
| 51 | + def python_version_tuple(self): |
| 52 | + return "pvt_major", "pvt_minor", "pvt_patchlevel" |
| 53 | + |
| 54 | + def python_version(self): |
| 55 | + return "python_version" |
| 56 | + |
| 57 | + def python_implementation(self): |
| 58 | + return "python_implementation" |
| 59 | + |
| 60 | + def system(self): |
| 61 | + return self._system |
| 62 | + |
| 63 | + def java_ver(self): |
| 64 | + return ( |
| 65 | + "jv_release", |
| 66 | + "jv_vendor", |
| 67 | + ("vm_name", "vm_release", "vm_vendor"), |
| 68 | + ("os_name", "os_version", "os_arch") |
| 69 | + ) |
0 commit comments