1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/instances/tests/test_change_network.py
catborise b664e34161 fix(nwfilters): two changes at once do not undo each other
A rule or reference change reads a filter and defines it again whole:
two admins changing one filter at once each defined their copy, and the
second removed the first's rule. Changes to a host's filters (create,
clone, delete, rules, references, XML) now run one at a time; one that
finds another running is told to try again.

A VM's NIC form sends back its source, model, filter and MAC whole: a
form opened before another admin changed the NIC's filter put the old
filter back. The form now sends a fingerprint of the NIC as it showed
it, and a NIC changed meanwhile is refused.
2026-11-13 11:46:01 +03:00

86 lines
4.3 KiB
Python

from unittest.mock import patch
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
from instances.models import Instance
class ChangeNetworkViewTestCase(TestCase):
def setUp(self):
admin = get_user_model().objects.create_superuser("net_admin", "n@example.com", "pw")
self.client.force_login(admin)
compute = Compute.objects.create(name="net-compute", hostname="127.0.0.1", login="", password="", type=4)
self.instance = Instance.objects.create(
compute=compute, name="net-vm", uuid="11111111-2222-3333-4444-555555555555"
)
self.url = reverse("instances:change_network", args=[self.instance.id])
def post(self, data):
with patch("instances.models.wvmInstance") as wvm:
wvm.return_value.get_status.return_value = 5
self.client.post(self.url, data, HTTP_REFERER="/instances/%d/" % self.instance.id)
return wvm.return_value.change_network
def test_changes_the_nic_of_the_submitted_form(self):
change = self.post({
"net-old-mac-1": "52:54:00:00:00:02",
"net-mac-1": "52:54:00:00:00:03",
"net-source-1": "net:default",
"net-nwfilter-1": "",
"net-model-1": "e1000",
})
change.assert_called_once_with("52:54:00:00:00:02", "52:54:00:00:00:03", "default", "net", "e1000", "")
def test_no_source_keeps_the_nics_own(self):
change = self.post({"net-old-mac-1": "52:54:00:00:00:02", "net-source-1": "", "net-model-1": "e1000"})
change.assert_called_once_with("52:54:00:00:00:02", "52:54:00:00:00:02", None, None, "e1000", "")
def test_a_shut_off_vm_is_told_the_change_is_made(self):
with patch("instances.models.wvmInstance") as wvm:
wvm.return_value.get_status.return_value = 5
response = self.client.post(self.url, {"net-old-mac-1": "52:54:00:00:00:02", "net-source-1": "net:default",
"net-model-1": "virtio"}, HTTP_REFERER="/instances/%d/" % self.instance.id)
self.assertEqual([str(m) for m in get_messages(response.wsgi_request)], ["Network device changed"])
def test_a_form_shown_before_another_change_is_refused(self):
# another admin changed the NIC's filter meanwhile: saving the model would undo it
def post(seen):
with patch("instances.models.wvmInstance") as wvm:
wvm.return_value.get_status.return_value = 5
wvm.return_value.get_net_devices.return_value = [{"mac": "52:54:00:00:00:02", "seen": "now"}]
response = self.client.post(self.url, {"net-old-mac-1": "52:54:00:00:00:02", "net-model-1": "e1000",
"net-seen-1": seen}, HTTP_REFERER="/instances/%d/" % self.instance.id)
self.client.cookies.pop("messages", None)
return wvm.return_value.change_network, [str(m) for m in get_messages(response.wsgi_request)]
change, messages = post("before")
change.assert_not_called()
self.assertEqual(messages, ["Network interface 52:54:00:00:00:02 was changed meanwhile; reload the page"])
change, messages = post("now")
change.assert_called_once()
def test_invalid_mac_is_rejected(self):
change = self.post({
"net-old-mac-0": "52:54:00:00:00:01",
"net-mac-0": "52:54:00:00:00:01'/><evil/>",
"net-source-0": "net:default",
})
change.assert_not_called()
def test_missing_old_mac_is_rejected(self):
change = self.post({"net-mac-0": "52:54:00:00:00:03", "net-source-0": "net:default"})
change.assert_not_called()
def test_missing_referer_redirects_to_the_instance(self):
with patch("instances.models.wvmInstance") as wvm:
wvm.return_value.get_status.return_value = 5
response = self.client.post(self.url, {
"net-old-mac-0": "52:54:00:00:00:01",
"net-source-0": "net:default",
"net-model-0": "virtio",
})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, f"/instances/{self.instance.id}/#network")