Add tests for audits and flavor utilities, update .gitignore, and enhance CPU data handling
This commit is contained in:
43
dashboard/tests/test_audits.py
Normal file
43
dashboard/tests/test_audits.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""Tests for dashboard.openstack_utils.audits."""
|
||||
from django.test import TestCase
|
||||
|
||||
from dashboard.openstack_utils.audits import convert_cpu_data
|
||||
|
||||
|
||||
class ConvertCpuDataTest(TestCase):
|
||||
"""Tests for convert_cpu_data."""
|
||||
|
||||
def test_aggregates_cpu_usage_per_host(self):
|
||||
data = [
|
||||
{
|
||||
"metric": {"host": "compute-0", "instanceName": "inst1"},
|
||||
"values": [[1000, "10.0"], [1001, "20.0"]],
|
||||
},
|
||||
{
|
||||
"metric": {"host": "compute-0", "instanceName": "inst2"},
|
||||
"values": [[1000, "5.0"]],
|
||||
},
|
||||
{
|
||||
"metric": {"host": "compute-1", "instanceName": "inst3"},
|
||||
"values": [[1000, "30.0"]],
|
||||
},
|
||||
]
|
||||
result = convert_cpu_data(data)
|
||||
self.assertIn("host", result.columns)
|
||||
self.assertIn("cpu_usage", result.columns)
|
||||
hosts = result["host"].tolist()
|
||||
self.assertEqual(len(hosts), 2)
|
||||
self.assertIn("compute-0", hosts)
|
||||
self.assertIn("compute-1", hosts)
|
||||
# compute-0: (10+20)/2 for ts 1000 and 5 for ts 1000 -> groupby host,timestamp sum -> then groupby host mean
|
||||
# For compute-0: two timestamps 1000 (10+5=15) and 1001 (20). Mean over timestamps = (15+20)/2 = 17.5
|
||||
# For compute-1: one value 30
|
||||
by_host = result.set_index("host")["cpu_usage"]
|
||||
self.assertAlmostEqual(by_host["compute-0"], 17.5)
|
||||
self.assertAlmostEqual(by_host["compute-1"], 30.0)
|
||||
|
||||
def test_empty_data_returns_empty_dataframe_with_columns(self):
|
||||
result = convert_cpu_data([])
|
||||
self.assertIn("host", result.columns)
|
||||
self.assertIn("cpu_usage", result.columns)
|
||||
self.assertEqual(len(result), 0)
|
||||
Reference in New Issue
Block a user