1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/vrtManager/tests/test_xpath_values.py
catborise 1262559793 fix(nwfilters): a rule is deleted and added exactly as clicked and entered
- Deleting a rule removes the row clicked, by its position, instead of the
  first rule with the same action, direction and priority; a filter changed
  since the page was read is refused instead of losing another rule.
- An added rule is added as entered, with all its attributes. It was merged
  into a rule with the same action, direction and priority, where libvirt
  keeps only the first protocol, so the new rule was silently dropped.
- A new filter shows in the list after it is created.
2026-10-26 15:36:57 +03:00

65 lines
2.6 KiB
Python

"""Values from requests do not change the XPath queries they are used in."""
import unittest
from django.conf import settings
if not settings.configured:
settings.configure(MAC_OUI="52:54:10")
from vrtManager.connection import wvmConnect
from vrtManager.network import wvmNetwork
from vrtManager.nwfilters import wvmNWFilter
CAPS = """<capabilities><guest><arch name='x86_64'><wordsize>64</wordsize><emulator>/usr/bin/qemu-x86</emulator>
<machine canonical='pc-q35'>q35</machine></arch>
<arch name='aarch64'><emulator>/usr/bin/qemu-arm</emulator><machine>virt</machine></arch></guest></capabilities>"""
FILTER = """<filter name='f'><rule action='drop' direction='in' priority='1'><tcp/></rule>
<rule action='drop' direction='in' priority='2'><udp/></rule></filter>"""
class FakeConn:
def __init__(self):
self.defined = None
def networkDefineXML(self, xml):
self.defined = xml
class XPathValueTests(unittest.TestCase):
def setUp(self):
self.conn = wvmConnect.__new__(wvmConnect)
self.conn.get_cap_xml = lambda: CAPS
def test_arch_lookups(self):
self.assertEqual(self.conn.get_emulator("x86_64"), "/usr/bin/qemu-x86")
self.assertEqual(self.conn.get_machine_types("aarch64"), ["virt"])
injected = "nope' or '1'='1"
self.assertIsNone(self.conn.get_emulator(injected))
self.assertEqual(self.conn.get_machine_types(injected), [])
self.assertEqual(self.conn.get_capabilities(injected), {})
def test_nwfilter_rule_is_matched_by_its_attributes(self):
nwf = wvmNWFilter.__new__(wvmNWFilter)
nwf._XMLDesc = lambda flags: FILTER
# values with quotes or brackets are compared, not parsed as XPath
for action in ("drop'][@priority='2", "it's"):
with self.subTest(action=action), self.assertRaises(ValueError):
nwf.delete_rule(0, action, "in", "1")
kept = nwf.delete_rule(1, "drop", "in", "2")
self.assertEqual(kept.count("<rule "), 1)
self.assertIn("priority=\"1\"", kept)
# add_rule adds a rule of its own
added = nwf.add_rule("<rule action='drop' direction='in' priority='2'><icmp/></rule>")
self.assertEqual(added.count("<rule "), 3)
def test_qos_direction_must_be_known(self):
net = wvmNetwork.__new__(wvmNetwork)
net.wvm = FakeConn()
net._XMLDesc = lambda flags: "<network><bandwidth><inbound average='1'/><outbound average='2'/></bandwidth></network>"
with self.assertRaises(ValueError):
net.unset_qos("*")
net.unset_qos("inbound")
self.assertNotIn("inbound", net.wvm.defined)
self.assertIn("outbound", net.wvm.defined)