Some checks failed
CI / ci (push) Failing after 14s
- Standardized string quotes across multiple files to use double quotes for consistency. - Improved formatting of JSON dumps in mock data for better readability. - Enhanced the structure of various functions and data definitions for clarity. - Updated test cases to reflect changes in data structure and ensure accuracy.
78 lines
2.9 KiB
Python
78 lines
2.9 KiB
Python
"""Tests for dashboard.openstack_utils.audits."""
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from django.test import TestCase
|
|
|
|
from dashboard.openstack_utils.audits import convert_cpu_data, get_current_cluster_cpu
|
|
|
|
|
|
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)
|
|
|
|
|
|
class GetCurrentClusterCpuTest(TestCase):
|
|
"""Tests for get_current_cluster_cpu."""
|
|
|
|
@patch("dashboard.openstack_utils.audits.query_prometheus")
|
|
def test_returns_empty_lists_when_no_data(self, mock_query):
|
|
mock_query.return_value = []
|
|
conn = MagicMock()
|
|
result = get_current_cluster_cpu(conn)
|
|
self.assertEqual(result["host_labels"], [])
|
|
self.assertEqual(result["cpu_current"], [])
|
|
|
|
@patch("dashboard.openstack_utils.audits.convert_cpu_data")
|
|
@patch("dashboard.openstack_utils.audits.query_prometheus")
|
|
def test_returns_host_labels_and_cpu_current(self, mock_query, mock_convert):
|
|
import pandas as pd
|
|
|
|
mock_query.return_value = [{"metric": {"host": "h0"}, "values": [[0, "1.0"]]}]
|
|
mock_convert.return_value = pd.DataFrame(
|
|
{
|
|
"host": ["compute-0", "compute-1"],
|
|
"cpu_usage": [25.0, 35.0],
|
|
}
|
|
)
|
|
conn = MagicMock()
|
|
result = get_current_cluster_cpu(conn)
|
|
self.assertEqual(result["host_labels"], ["compute-0", "compute-1"])
|
|
self.assertEqual(result["cpu_current"], [25.0, 35.0])
|