mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 19:26:48 +00:00
fix(networks): a network QoS rate that is not a whole number is refused
A network's QoS form whose rate was not a whole number (abc, 1.5) made wvmNetwork.set_qos raise a ValueError that the view did not catch: the request ended in a server error. The page's pattern stops this in a browser, but the server must refuse it too. set_qos now says "QoS rates must be whole numbers", and the view reports that, as it does a libvirt error, and goes back to the network's page.
This commit is contained in:
parent
0a1219c659
commit
8c1d7d33bb
3 changed files with 32 additions and 5 deletions
26
networks/tests/test_network_qos.py
Normal file
26
networks/tests/test_network_qos.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
"""A network QoS the user typed wrong is refused with a message on the
|
||||
network's page, not a server error."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import libvirt
|
||||
from computes.models import Compute
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.messages import get_messages
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
class NetworkQosTests(TestCase):
|
||||
def test_a_rate_that_is_not_a_whole_number_is_refused(self):
|
||||
conn = libvirt.open("test:///default")
|
||||
self.addCleanup(conn.close)
|
||||
compute = Compute.objects.create(name="qos-test", hostname="localhost", login="", password="", type=4)
|
||||
self.client.force_login(get_user_model().objects.create_superuser("qos", "qos@example.com", "pw"))
|
||||
url = reverse("network", args=[compute.id, "default"])
|
||||
for rates in ({"qos_average": "abc"}, {"qos_average": "1", "qos_peak": "1.5"}):
|
||||
with self.subTest(rates=rates), \
|
||||
patch("vrtManager.connection.connection_manager.get_connection", return_value=conn):
|
||||
response = self.client.post(url, {"set_qos": "", "qos_direction": "inbound", **rates})
|
||||
self.assertRedirects(response, url, fetch_redirect_response=False)
|
||||
self.assertIn("QoS rates must be whole numbers", [str(m) for m in get_messages(response.wsgi_request)])
|
||||
|
|
@ -247,8 +247,8 @@ def network(request, compute_id, pool):
|
|||
request,
|
||||
_("%(qos_dir)s QoS is set") % {"qos_dir": qos_dir.capitalize()},
|
||||
)
|
||||
except libvirtError as lib_err:
|
||||
messages.error(request, lib_err)
|
||||
except (libvirtError, ValueError) as err:
|
||||
messages.error(request, err)
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
if "unset_qos" in request.POST:
|
||||
qos_dir = request.POST.get("qos_direction", "")
|
||||
|
|
|
|||
|
|
@ -356,9 +356,10 @@ class wvmNetwork(wvmConnect):
|
|||
if direction not in ("inbound", "outbound"):
|
||||
raise ValueError("Direction must be inbound or outbound")
|
||||
|
||||
average = int(average)
|
||||
peak = int(peak)
|
||||
burst = int(burst)
|
||||
try:
|
||||
average, peak, burst = int(average), int(peak), int(burst)
|
||||
except ValueError:
|
||||
raise ValueError("QoS rates must be whole numbers")
|
||||
|
||||
xml = f"<{direction} average='{average}' peak='{peak}' burst='{burst}'/>"
|
||||
tree = etree.fromstring(self._XMLDesc(0))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue