|
| 1 | +from asyncio import run |
1 | 2 | from unittest import TestCase, expectedFailure, main |
2 | 3 | from uuid import uuid4 |
3 | 4 |
|
|
6 | 7 | from zenlib.logging import loggify |
7 | 8 |
|
8 | 9 |
|
| 10 | +def generate_random_metric_config(count: int) -> dict: |
| 11 | + """Generate a random metric configuration""" |
| 12 | + metrics = {} |
| 13 | + for i in range(count): |
| 14 | + name = "x" + str(uuid4()).replace("-", "_") |
| 15 | + metrics[name] = {"type": "counter", "help": str(uuid4())} |
| 16 | + return metrics |
| 17 | + |
| 18 | + |
9 | 19 | class TestExporter(TestCase): |
10 | 20 | @expectedFailure |
11 | 21 | def test_no_config(self): |
12 | 22 | Exporter(config_file=str(uuid4())) # Pass a random string as config |
13 | 23 |
|
14 | 24 | def test_proper_no_config(self): |
15 | | - """Test init with no_config_file set to True""" |
16 | | - Exporter(no_config_file=True) |
| 25 | + e = Exporter(no_config_file=True) |
| 26 | + self.assertIsNotNone(run(e.export())) |
| 27 | + |
| 28 | + def test_random_metrics(self): |
| 29 | + """Ensure metrics are the same run to run and properly export 100 random metrics""" |
| 30 | + e = Exporter(no_config_file=True) |
| 31 | + random_metrics = generate_random_metric_config(100) |
| 32 | + e.config["metrics"] = random_metrics |
| 33 | + export1 = run(e.export()) |
| 34 | + export2 = run(e.export()) |
| 35 | + self.assertEqual(export1, export2) |
| 36 | + for metric in random_metrics: |
| 37 | + self.assertIn(f"{metric} 0", export1) |
17 | 38 |
|
18 | 39 |
|
19 | 40 | @loggify |
@@ -41,6 +62,18 @@ async def test_filter(self): |
41 | 62 | text = await response.text() |
42 | 63 | self.assertEqual(text, expected_response) |
43 | 64 |
|
| 65 | + async def test_random_metrics(self): |
| 66 | + """Ensure the server can properly serve 1000 random metrics""" |
| 67 | + random_metrics = generate_random_metric_config(1000) |
| 68 | + self.exporter.config["metrics"] = random_metrics |
| 69 | + expected_response = await self.exporter.export() |
| 70 | + async with self.client.get("/metrics") as response: |
| 71 | + self.assertEqual(response.status, 200) |
| 72 | + text = await response.text() |
| 73 | + self.assertEqual(text, expected_response) |
| 74 | + for metric in random_metrics: |
| 75 | + self.assertIn(f"{metric} 0", text) |
| 76 | + |
44 | 77 |
|
45 | 78 | if __name__ == "__main__": |
46 | 79 | main() |
0 commit comments