- 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.
This commit is contained in:
@@ -1,18 +1,17 @@
|
||||
"""Tests for dashboard.views."""
|
||||
import json
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from django.test import TestCase, RequestFactory
|
||||
import json
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from django.core.cache import cache
|
||||
from django.test import RequestFactory, TestCase
|
||||
|
||||
from dashboard.views import (
|
||||
index,
|
||||
collect_context,
|
||||
collect_stats,
|
||||
collect_audits,
|
||||
api_stats,
|
||||
api_audits,
|
||||
api_source_status,
|
||||
api_stats,
|
||||
collect_context,
|
||||
index,
|
||||
)
|
||||
|
||||
|
||||
@@ -21,10 +20,31 @@ def _minimal_render_context(region_name="test", first_flavor_name="f1", vm_count
|
||||
return {
|
||||
"region": {"name": region_name, "hosts_total": 1},
|
||||
"pcpu": {"total": 1, "usage": 0, "free": 1, "used_percentage": 0},
|
||||
"vcpu": {"total": 2, "allocated": 1, "free": 1, "allocated_percentage": 50, "overcommit_ratio": 1, "overcommit_max": 2},
|
||||
"vcpu": {
|
||||
"total": 2,
|
||||
"allocated": 1,
|
||||
"free": 1,
|
||||
"allocated_percentage": 50,
|
||||
"overcommit_ratio": 1,
|
||||
"overcommit_max": 2,
|
||||
},
|
||||
"pram": {"total": 1024**3, "usage": 0, "free": 1024**3, "used_percentage": 0},
|
||||
"vram": {"total": 1024**3, "allocated": 0, "free": 1024**3, "allocated_percentage": 0, "overcommit_ratio": 0, "overcommit_max": 1},
|
||||
"vm": {"count": vm_count, "active": vm_count, "stopped": 0, "avg_cpu": 1, "avg_ram": 0, "density": float(vm_count)},
|
||||
"vram": {
|
||||
"total": 1024**3,
|
||||
"allocated": 0,
|
||||
"free": 1024**3,
|
||||
"allocated_percentage": 0,
|
||||
"overcommit_ratio": 0,
|
||||
"overcommit_max": 1,
|
||||
},
|
||||
"vm": {
|
||||
"count": vm_count,
|
||||
"active": vm_count,
|
||||
"stopped": 0,
|
||||
"avg_cpu": 1,
|
||||
"avg_ram": 0,
|
||||
"density": float(vm_count),
|
||||
},
|
||||
"flavors": {
|
||||
"first_common_flavor": {"name": first_flavor_name, "count": vm_count},
|
||||
"second_common_flavor": {"name": "—", "count": 0},
|
||||
@@ -81,10 +101,18 @@ class CollectContextTest(TestCase):
|
||||
@patch("dashboard.views.get_flavor_list")
|
||||
@patch("dashboard.views.get_connection")
|
||||
def test_collect_context_structure_and_calculation(
|
||||
self, mock_get_connection, mock_get_flavor_list, mock_get_audits, mock_fetch_metrics, mock_get_current_cluster_cpu
|
||||
self,
|
||||
mock_get_connection,
|
||||
mock_get_flavor_list,
|
||||
mock_get_audits,
|
||||
mock_fetch_metrics,
|
||||
mock_get_current_cluster_cpu,
|
||||
):
|
||||
mock_get_connection.return_value = self._make_mock_connection("my-region")
|
||||
mock_get_current_cluster_cpu.return_value = {"host_labels": ["h0", "h1"], "cpu_current": [30.0, 40.0]}
|
||||
mock_get_current_cluster_cpu.return_value = {
|
||||
"host_labels": ["h0", "h1"],
|
||||
"cpu_current": [30.0, 40.0],
|
||||
}
|
||||
mock_get_flavor_list.return_value = {
|
||||
"first_common_flavor": {"name": "m1.small", "count": 5},
|
||||
"second_common_flavor": {"name": "—", "count": 0},
|
||||
@@ -125,6 +153,7 @@ class CollectContextTest(TestCase):
|
||||
self.assertEqual(len(context["audits"]), 1)
|
||||
# Serialized for JS
|
||||
import json
|
||||
|
||||
self.assertIsInstance(context["audits"][0]["migrations"], str)
|
||||
self.assertEqual(json.loads(context["audits"][0]["host_labels"]), ["h0", "h1"])
|
||||
self.assertIn("current_cluster", context)
|
||||
@@ -187,7 +216,15 @@ class ApiStatsTest(TestCase):
|
||||
@patch("dashboard.views.settings")
|
||||
def test_api_stats_uses_cache(self, mock_settings, mock_collect_stats):
|
||||
mock_settings.DASHBOARD_CACHE_TTL = 120
|
||||
cached = {"region": {"name": "cached", "hosts_total": 1}, "pcpu": {}, "pram": {}, "vcpu": {}, "vram": {}, "vm": {}, "flavors": {}}
|
||||
cached = {
|
||||
"region": {"name": "cached", "hosts_total": 1},
|
||||
"pcpu": {},
|
||||
"pram": {},
|
||||
"vcpu": {},
|
||||
"vram": {},
|
||||
"vm": {},
|
||||
"flavors": {},
|
||||
}
|
||||
cache.clear()
|
||||
cache.set("dashboard_stats", cached, timeout=120)
|
||||
request = self.factory.get("/api/stats/")
|
||||
@@ -219,13 +256,24 @@ class ApiAuditsTest(TestCase):
|
||||
"scope": "Full Cluster",
|
||||
"cpu_weight": "1.0",
|
||||
"ram_weight": "1.0",
|
||||
"migrations": [{"instanceName": "i1", "source": "h0", "destination": "h1", "flavor": "m1.small", "impact": "Low"}],
|
||||
"migrations": [
|
||||
{
|
||||
"instanceName": "i1",
|
||||
"source": "h0",
|
||||
"destination": "h1",
|
||||
"flavor": "m1.small",
|
||||
"impact": "Low",
|
||||
}
|
||||
],
|
||||
"host_labels": ["h0", "h1"],
|
||||
"cpu_current": [30.0, 40.0],
|
||||
"cpu_projected": [35.0, 35.0],
|
||||
}
|
||||
]
|
||||
mock_get_current_cluster_cpu.return_value = {"host_labels": ["h0", "h1"], "cpu_current": [30.0, 40.0]}
|
||||
mock_get_current_cluster_cpu.return_value = {
|
||||
"host_labels": ["h0", "h1"],
|
||||
"cpu_current": [30.0, 40.0],
|
||||
}
|
||||
cache.clear()
|
||||
request = self.factory.get("/api/audits/")
|
||||
with patch("dashboard.views.settings") as mock_settings:
|
||||
@@ -246,9 +294,20 @@ class ApiAuditsTest(TestCase):
|
||||
@patch("dashboard.views.get_current_cluster_cpu")
|
||||
@patch("dashboard.views.collect_audits")
|
||||
@patch("dashboard.views.settings")
|
||||
def test_api_audits_uses_cache(self, mock_settings, mock_collect_audits, mock_get_current_cluster_cpu):
|
||||
def test_api_audits_uses_cache(
|
||||
self, mock_settings, mock_collect_audits, mock_get_current_cluster_cpu
|
||||
):
|
||||
mock_settings.DASHBOARD_CACHE_TTL = 120
|
||||
cached_audits = [{"id": "cached-1", "name": "Cached Audit", "migrations": "[]", "host_labels": "[]", "cpu_current": "[]", "cpu_projected": "[]"}]
|
||||
cached_audits = [
|
||||
{
|
||||
"id": "cached-1",
|
||||
"name": "Cached Audit",
|
||||
"migrations": "[]",
|
||||
"host_labels": "[]",
|
||||
"cpu_current": "[]",
|
||||
"cpu_projected": "[]",
|
||||
}
|
||||
]
|
||||
cached_cluster = {"host_labels": ["cached-h0"], "cpu_current": [10.0]}
|
||||
cache.clear()
|
||||
cache.set("dashboard_audits", cached_audits, timeout=120)
|
||||
|
||||
Reference in New Issue
Block a user