1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/vrtManager/tests/test_console_graphics.py
catborise 7fe79463fc fix(instances): saving a SPICE console's settings keeps it SPICE
The console type list offers VNC and Serial only, so the page of a SPICE
VM showed VNC and any save, of the keymap alone, switched the console:
libvirt refused the definition when the VM had a SPICE channel, and
without one the VM silently got a new VNC console. A console type the
panel does not create is now offered as it is, and kept on save.

A switch from SPICE that is chosen removes the SPICE channels, USB
redirection and smartcards libvirt refuses without SPICE graphics and
mutes SPICE audio. A new VNC console keeps the keymap and a password VNC
can take; a longer one is replaced by a generated password.

The console settings were read from the first graphics device that had
each attribute: with VNC and SPICE devices the page showed VNC's port
with SPICE's password. All are read from the first device, the one the
settings change.
2026-11-13 22:49:21 +03:00

56 lines
2.9 KiB
Python

"""A VM's console is its first graphics device: each setting is read from
it, and a switch away from SPICE leaves a definition libvirt accepts."""
import unittest
from lxml import etree
from vrtManager.instance import wvmInstance
SPICE = """<domain><devices>
<graphics type='spice' autoport='yes' keymap='de' passwd='%s'><listen type='address' address='0.0.0.0'/></graphics>
<channel type='spicevmc'><target type='virtio' name='com.redhat.spice.0'/></channel>
<channel type='unix'><target type='virtio' name='org.qemu.guest_agent.0'/></channel>
<redirdev bus='usb' type='spicevmc'/><smartcard mode='passthrough' type='spicevmc'/>
<audio id='1' type='spice'/><video><model type='qxl'/></video></devices></domain>"""
def vm(xml):
p = wvmInstance.__new__(wvmInstance) # no connection: only these calls
p._XMLDesc = lambda flags: xml
p.defined = []
p._defineXML = p.defined.append
return p
class ConsoleGraphicsTestCase(unittest.TestCase):
def test_a_switch_from_spice_drops_the_devices_that_need_it(self):
for console_type in ("vnc", "pty"):
with self.subTest(console_type):
p = vm(SPICE % "spice123")
p.set_console_type(console_type)
devices = etree.fromstring(p.defined[0]).find("devices")
self.assertEqual([c.get("type") for c in devices.findall("channel")], ["unix"])
self.assertEqual(devices.findall("redirdev") + devices.findall("smartcard"), [])
self.assertEqual(devices.find("audio").get("type"), "none")
self.assertEqual(devices.find("video/model").get("type"), "qxl")
def test_vnc_keeps_the_spice_keymap_and_a_password_it_can_take(self):
for passwd, kept in (("spice123", True), ("a-longer-spice-password", False)):
with self.subTest(passwd):
p = vm(SPICE % passwd)
p.set_console_type("vnc")
(graphic,) = etree.fromstring(p.defined[0]).findall("devices/graphics")
self.assertEqual((graphic.get("type"), graphic.get("listen"), graphic.get("keymap")),
("vnc", "127.0.0.1", "de"))
self.assertEqual(graphic.get("passwd") == passwd, kept)
self.assertEqual(len(graphic.get("passwd")), 8)
def test_every_setting_is_read_from_the_first_graphics_device(self):
# VNC without a password or keymap first: SPICE's must not be shown as its
p = vm("""<domain><devices><graphics type='vnc' port='5901'><listen type='address' address='127.0.0.1'/></graphics>
<graphics type='spice' port='5902' passwd='spice123' keymap='de'>
<listen type='address' address='0.0.0.0'/></graphics></devices></domain>""")
self.assertEqual((p.get_console_type(), p.get_console_port(), p.get_console_passwd(),
p.get_console_keymap(), p.get_console_listener_addr()),
("vnc", "5901", None, "", "127.0.0.1"))