|
1 | 1 | import os |
| 2 | +import tempfile |
2 | 3 | import unittest |
3 | 4 |
|
4 | 5 | from prometheus_client import CollectorRegistry, ProcessCollector |
@@ -47,6 +48,27 @@ def test_working_584(self): |
47 | 48 | self.assertEqual(None, self.registry.get_sample_value('process_max_fds')) |
48 | 49 | self.assertEqual(None, self.registry.get_sample_value('process_open_fds')) |
49 | 50 |
|
| 51 | + def test_limits_without_max_open_files(self): |
| 52 | + # A readable limits file is not guaranteed to contain the |
| 53 | + # "Max open files" line, see |
| 54 | + # https://github.com/home-assistant/core/issues/140045. The collector |
| 55 | + # used to raise UnboundLocalError (and with it break the import of |
| 56 | + # prometheus_client), instead of just omitting process_max_fds. |
| 57 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 58 | + proc = os.path.join(tmpdir, 'proc') |
| 59 | + os.makedirs(os.path.join(proc, '123', 'fd')) |
| 60 | + with open(os.path.join(proc, 'stat'), 'wb') as stat: |
| 61 | + stat.write(b'btime 1234567890\n') |
| 62 | + with open(os.path.join(proc, '123', 'limits'), 'wb') as limits: |
| 63 | + limits.write(b'Limit Soft Limit Hard Limit Units\n' |
| 64 | + b'Max cpu time unlimited unlimited seconds\n' |
| 65 | + b'Max open fides 2048 4096 files\n') |
| 66 | + registry = CollectorRegistry(auto_describe=True) |
| 67 | + ProcessCollector(proc=proc, pid=lambda: 123, registry=registry) |
| 68 | + |
| 69 | + self.assertEqual(0.0, registry.get_sample_value('process_open_fds')) |
| 70 | + self.assertEqual(None, registry.get_sample_value('process_max_fds')) |
| 71 | + |
50 | 72 | def test_working_fake_pid(self): |
51 | 73 | collector = ProcessCollector(proc=self.test_proc, pid=lambda: 123, registry=self.registry) |
52 | 74 | collector._ticks = 100 |
|
0 commit comments