mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 19:26:48 +00:00
Reviewing the error handlers outside instances/ turned up three that masked or swallowed the failure they caught: - appsettings/settings.get_settings() runs on every request (middleware) and loads app_settings from the database. Its bare `except: pass` left `entries` unbound on a database error, so the next line raised UnboundLocalError instead; it also caught KeyboardInterrupt/SystemExit. It now catches DatabaseError and returns, keeping the already-loaded values. - vrtManager.network.refresh_dhcp_leases raised a formatted string (`raise f"..."`), which Python rejects with TypeError, hiding the real DHCPLeases failure. It now raises util.OperationError, which the API/HTML error handling already renders as a libvirt error, and clears self.leases. - storages.views.get_volumes caught libvirtError and answered 200 with an empty body, so the caller could not tell a failed pool read from an empty pool. It now returns 400 with the error. The other broad handlers reviewed (appsettings/nwfilters/virtsecrets views, storages/upload cleanup and recovery paths, vrtManager display getters) report the error or are best-effort and were left as they are. Tests: get_settings() survives a database error during the query and during iteration without raising and still loads a real value; refresh_dhcp_leases raises OperationError (keeping the cause) not TypeError; get_volumes returns 400 on a listing failure and the sorted volumes on success. The tests fail on the previous code.
36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
"""refresh_dhcp_leases must raise a real exception (handled as a libvirt error),
|
|
not a bare string (which Python turns into TypeError, masking the cause)."""
|
|
|
|
import unittest
|
|
from unittest.mock import Mock
|
|
|
|
from django.conf import settings
|
|
|
|
if not settings.configured:
|
|
settings.configure(MAC_OUI="52:54:10")
|
|
|
|
from vrtManager import util
|
|
from vrtManager.network import wvmNetwork
|
|
|
|
|
|
def network_with_leases(side_effect):
|
|
net = wvmNetwork.__new__(wvmNetwork)
|
|
net.leases = None
|
|
net.net = Mock()
|
|
net.net.DHCPLeases.side_effect = side_effect
|
|
return net
|
|
|
|
|
|
class RefreshDhcpLeasesTestCase(unittest.TestCase):
|
|
def test_a_lease_error_raises_an_operation_error(self):
|
|
net = network_with_leases(Exception("no DHCP on this network"))
|
|
with self.assertRaises(util.OperationError):
|
|
net.refresh_dhcp_leases()
|
|
|
|
def test_the_error_keeps_the_cause_and_clears_leases(self):
|
|
cause = Exception("boom")
|
|
net = network_with_leases(cause)
|
|
with self.assertRaises(util.OperationError) as caught:
|
|
net.refresh_dhcp_leases()
|
|
self.assertIs(caught.exception.__cause__, cause)
|
|
self.assertEqual(net.leases, [])
|