Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
openSUSE:Step:15-SP4
salt.21870
3002.2-xen-spicevmc-dns-srv-records-backports-3...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 3002.2-xen-spicevmc-dns-srv-records-backports-314.patch of Package salt.21870
From 0b90e8db82991daf19dd76ebccd651ffcffdecb1 Mon Sep 17 00:00:00 2001 From: Cedric Bosdonnat <cbosdonnat@suse.com> Date: Mon, 8 Feb 2021 16:42:47 +0100 Subject: [PATCH] 3002.2 Xen spicevmc, DNS SRV records backports (#314) * Fix virtual network generated DNS XML for SRV records libvirt network's srv element doesn't take a `name` property but a `service` one. * Add missing property in virt.network_define dns srv doc * virt: convert spice generation tests to pytests * virt: don't add spicevmc channel to Xen VMs Xen fully virtualized VMs with spicevmc channel fail to start, so better not write it out in such cases. * virt: inverse the remaining asserts in pytests --- changelog/59416.fixed | 1 + salt/modules/virt.py | 2 +- salt/templates/virt/libvirt_domain.jinja | 2 +- salt/templates/virt/libvirt_network.jinja | 2 +- .../pytests/unit/modules/virt/test_domain.py | 176 ++++++++++++------ .../pytests/unit/modules/virt/test_helpers.py | 6 +- tests/pytests/unit/modules/virt/test_host.py | 4 +- .../pytests/unit/modules/virt/test_network.py | 61 +++--- tests/pytests/unit/states/virt/test_domain.py | 144 +++++++------- .../pytests/unit/states/virt/test_network.py | 88 ++++----- tests/unit/modules/test_virt.py | 59 ------ 11 files changed, 281 insertions(+), 264 deletions(-) create mode 100644 changelog/59416.fixed diff --git a/changelog/59416.fixed b/changelog/59416.fixed new file mode 100644 index 0000000000..820124e99a --- /dev/null +++ b/changelog/59416.fixed @@ -0,0 +1 @@ +Don't create spicevmc channel for Xen virtual machines diff --git a/salt/modules/virt.py b/salt/modules/virt.py index 7da35445a3..da132630dd 100644 --- a/salt/modules/virt.py +++ b/salt/modules/virt.py @@ -7108,7 +7108,7 @@ def network_define( srvs: List of SRV DNS entries. Each entry is a dictionary with the mandatory ``name`` and ``protocol`` keys. - Entries can also have ``target``, ``port``, ``priority`` and ``weight`` optional properties. + Entries can also have ``target``, ``port``, ``priority``, ``domain`` and ``weight`` optional properties. CLI Example: diff --git a/salt/templates/virt/libvirt_domain.jinja b/salt/templates/virt/libvirt_domain.jinja index 4603dfd8de..6772b0db56 100644 --- a/salt/templates/virt/libvirt_domain.jinja +++ b/salt/templates/virt/libvirt_domain.jinja @@ -285,7 +285,7 @@ autoport='{{ yesno(not graphics.port and not graphics.tls_port) }}'> <listen type='{{ graphics.listen.type }}'{{ opt_attribute(graphics.listen, "address") }}/> </graphics> - {%- if graphics.type == "spice" %} + {%- if graphics.type == "spice" and hypervisor in ["qemu", "kvm"] %} <channel type='spicevmc'> <target type='virtio' name='com.redhat.spice.0'/> </channel> diff --git a/salt/templates/virt/libvirt_network.jinja b/salt/templates/virt/libvirt_network.jinja index ab14408712..98bd6567b8 100644 --- a/salt/templates/virt/libvirt_network.jinja +++ b/salt/templates/virt/libvirt_network.jinja @@ -65,7 +65,7 @@ </host> {%- endfor %} {%- for srv in dns.srvs %} - <srv name='{{ srv.name }}' protocol='{{ srv.protocol }}' + <srv service='{{ srv.name }}' protocol='{{ srv.protocol }}' {{ opt_attribute(srv, "port") }} {{ opt_attribute(srv, "target") }} {{ opt_attribute(srv, "priority") }} diff --git a/tests/pytests/unit/modules/virt/test_domain.py b/tests/pytests/unit/modules/virt/test_domain.py index 33357c60ea..72fa599a6c 100644 --- a/tests/pytests/unit/modules/virt/test_domain.py +++ b/tests/pytests/unit/modules/virt/test_domain.py @@ -60,13 +60,14 @@ def test_update_xen_disk_volumes(make_mock_vm, make_mock_storage_pool): assert ret["definition"] virt.libvirt.openAuth().defineXML = virt.libvirt.openAuth().defineXML setxml = ET.fromstring(virt.libvirt.openAuth().defineXML.call_args[0][0]) - assert "block" == setxml.find(".//disk[3]").get("type") - assert "/path/to/vdb/vdb1" == setxml.find(".//disk[3]/source").get("dev") + assert setxml.find(".//disk[3]").get("type") == "block" + assert setxml.find(".//disk[3]/source").get("dev") == "/path/to/vdb/vdb1" # Note that my_vm-file-data was not an existing volume before the update - assert "file" == setxml.find(".//disk[4]").get("type") - assert "/path/to/default/my_vm_file-data" == setxml.find(".//disk[4]/source").get( - "file" + assert setxml.find(".//disk[4]").get("type") == "file" + assert ( + setxml.find(".//disk[4]/source").get("file") + == "/path/to/default/my_vm_file-data" ) @@ -242,7 +243,7 @@ def test_get_disk_convert_volumes(make_mock_vm, make_mock_storage_pool): subprocess_mock.Popen = popen_mock with patch.dict(virt.__dict__, {"subprocess": subprocess_mock}): - assert { + assert virt.get_disks("srv01") == { "vda": { "type": "disk", "file": "default/srv01_system", @@ -265,7 +266,7 @@ def test_get_disk_convert_volumes(make_mock_vm, make_mock_storage_pool): "disk size": 340525056, "virtual size": 214748364800, }, - } == virt.get_disks("srv01") + } def test_update_approx_mem(make_mock_vm): @@ -308,7 +309,7 @@ def test_gen_hypervisor_features(): hypervisor_features={"kvm-hint-dedicated": True}, ) root = ET.fromstring(xml_data) - assert "on" == root.find("features/kvm/hint-dedicated").attrib["state"] + assert root.find("features/kvm/hint-dedicated").attrib["state"] == "on" def test_update_hypervisor_features(make_mock_vm): @@ -343,7 +344,7 @@ def test_update_hypervisor_features(make_mock_vm): ret = virt.update("my_vm", hypervisor_features={"kvm-hint-dedicated": False}) assert ret["definition"] setxml = ET.fromstring(virt.libvirt.openAuth().defineXML.call_args[0][0]) - assert "off" == setxml.find("features/kvm/hint-dedicated").get("state") + assert setxml.find("features/kvm/hint-dedicated").get("state") == "off" # Add the features xml_def = """ @@ -362,7 +363,7 @@ def test_update_hypervisor_features(make_mock_vm): ret = virt.update("my_vm", hypervisor_features={"kvm-hint-dedicated": True}) assert ret["definition"] setxml = ET.fromstring(virt.libvirt.openAuth().defineXML.call_args[0][0]) - assert "on" == setxml.find("features/kvm/hint-dedicated").get("state") + assert setxml.find("features/kvm/hint-dedicated").get("state") == "on" def test_gen_clock(): @@ -383,8 +384,8 @@ def test_gen_clock(): clock={"adjustment": 3600, "utc": False}, ) root = ET.fromstring(xml_data) - assert "localtime" == root.find("clock").get("offset") - assert "3600" == root.find("clock").get("adjustment") + assert root.find("clock").get("offset") == "localtime" + assert root.find("clock").get("adjustment") == "3600" # Specific timezone xml_data = virt._gen_xml( @@ -400,8 +401,8 @@ def test_gen_clock(): clock={"timezone": "CEST"}, ) root = ET.fromstring(xml_data) - assert "timezone" == root.find("clock").get("offset") - assert "CEST" == root.find("clock").get("timezone") + assert root.find("clock").get("offset") == "timezone" + assert root.find("clock").get("timezone") == "CEST" # UTC xml_data = virt._gen_xml( @@ -417,7 +418,7 @@ def test_gen_clock(): clock={"utc": True}, ) root = ET.fromstring(xml_data) - assert "utc" == root.find("clock").get("offset") + assert root.find("clock").get("offset") == "utc" # Timers xml_data = virt._gen_xml( @@ -444,14 +445,16 @@ def test_gen_clock(): }, ) root = ET.fromstring(xml_data) - assert "utc" == root.find("clock").get("offset") - assert "3504000000" == root.find("clock/timer[@name='tsc']").get("frequency") - assert "native" == root.find("clock/timer[@name='tsc']").get("mode") - assert "catchup" == root.find("clock/timer[@name='rtc']").get("tickpolicy") - assert {"slew": "4636", "threshold": "123", "limit": "2342"} == root.find( - "clock/timer[@name='rtc']/catchup" - ).attrib - assert "no" == root.find("clock/timer[@name='hpet']").get("present") + assert root.find("clock").get("offset") == "utc" + assert root.find("clock/timer[@name='tsc']").get("frequency") == "3504000000" + assert root.find("clock/timer[@name='tsc']").get("mode") == "native" + assert root.find("clock/timer[@name='rtc']").get("tickpolicy") == "catchup" + assert root.find("clock/timer[@name='rtc']/catchup").attrib == { + "slew": "4636", + "threshold": "123", + "limit": "2342", + } + assert root.find("clock/timer[@name='hpet']").get("present") == "no" def test_update_clock(make_mock_vm): @@ -510,21 +513,23 @@ def test_update_clock(make_mock_vm): ) assert ret["definition"] setxml = ET.fromstring(virt.libvirt.openAuth().defineXML.call_args[0][0]) - assert "timezone" == setxml.find("clock").get("offset") - assert "CEST" == setxml.find("clock").get("timezone") - assert {"rtc", "hpet"} == {t.get("name") for t in setxml.findall("clock/timer")} - assert "catchup" == setxml.find("clock/timer[@name='rtc']").get("tickpolicy") - assert "wall" == setxml.find("clock/timer[@name='rtc']").get("track") - assert {"slew": "4636", "threshold": "123", "limit": "2342"} == setxml.find( - "clock/timer[@name='rtc']/catchup" - ).attrib - assert "yes" == setxml.find("clock/timer[@name='hpet']").get("present") + assert setxml.find("clock").get("offset") == "timezone" + assert setxml.find("clock").get("timezone") == "CEST" + assert {t.get("name") for t in setxml.findall("clock/timer")} == {"rtc", "hpet"} + assert setxml.find("clock/timer[@name='rtc']").get("tickpolicy") == "catchup" + assert setxml.find("clock/timer[@name='rtc']").get("track") == "wall" + assert setxml.find("clock/timer[@name='rtc']/catchup").attrib == { + "slew": "4636", + "threshold": "123", + "limit": "2342", + } + assert setxml.find("clock/timer[@name='hpet']").get("present") == "yes" # Revert to UTC ret = virt.update("my_vm", clock={"utc": True, "adjustment": None, "timers": None}) assert ret["definition"] setxml = ET.fromstring(virt.libvirt.openAuth().defineXML.call_args[0][0]) - assert {"offset": "utc"} == setxml.find("clock").attrib + assert setxml.find("clock").attrib == {"offset": "utc"} assert setxml.find("clock/timer") is None @@ -550,7 +555,7 @@ def test_update_stop_on_reboot_reset(make_mock_vm): assert ret["definition"] virt.libvirt.openAuth().defineXML = virt.libvirt.openAuth().defineXML setxml = ET.fromstring(virt.libvirt.openAuth().defineXML.call_args[0][0]) - assert "restart" == setxml.find("./on_reboot").text + assert setxml.find("./on_reboot").text == "restart" def test_update_stop_on_reboot(make_mock_vm): @@ -574,7 +579,7 @@ def test_update_stop_on_reboot(make_mock_vm): assert ret["definition"] virt.libvirt.openAuth().defineXML = virt.libvirt.openAuth().defineXML setxml = ET.fromstring(virt.libvirt.openAuth().defineXML.call_args[0][0]) - assert "destroy" == setxml.find("./on_reboot").text + assert setxml.find("./on_reboot").text == "destroy" def test_init_no_stop_on_reboot(make_capabilities): @@ -587,7 +592,7 @@ def test_init_no_stop_on_reboot(make_capabilities): virt.init("test_vm", 2, 2048, start=False) virt.libvirt.openAuth().defineXML = virt.libvirt.openAuth().defineXML setxml = ET.fromstring(virt.libvirt.openAuth().defineXML.call_args[0][0]) - assert "restart" == setxml.find("./on_reboot").text + assert setxml.find("./on_reboot").text == "restart" def test_init_stop_on_reboot(make_capabilities): @@ -600,7 +605,7 @@ def test_init_stop_on_reboot(make_capabilities): virt.init("test_vm", 2, 2048, stop_on_reboot=True, start=False) virt.libvirt.openAuth().defineXML = virt.libvirt.openAuth().defineXML setxml = ET.fromstring(virt.libvirt.openAuth().defineXML.call_args[0][0]) - assert "destroy" == setxml.find("./on_reboot").text + assert setxml.find("./on_reboot").text == "destroy" def test_init_hostdev_usb(make_capabilities, make_mock_device): @@ -642,8 +647,8 @@ def test_init_hostdev_usb(make_capabilities, make_mock_device): </hostdev> """ ) - assert expected_xml == strip_xml( - ET.tostring(setxml.find("./devices/hostdev")) + assert ( + strip_xml(ET.tostring(setxml.find("./devices/hostdev"))) == expected_xml ) @@ -684,8 +689,8 @@ def test_init_hostdev_pci(make_capabilities, make_mock_device): </hostdev> """ ) - assert expected_xml == strip_xml( - ET.tostring(setxml.find("./devices/hostdev")) + assert ( + strip_xml(ET.tostring(setxml.find("./devices/hostdev"))) == expected_xml ) @@ -858,11 +863,11 @@ def test_update_hostdev_changes(running, live, make_mock_device, make_mock_vm, t ET.tostring(xmlutil.strip_spaces(node)) for node in set_xml.findall("./devices/hostdev") ] - assert [usb_device_xml] == actual_hostdevs + assert actual_hostdevs == [usb_device_xml] if not test and live: attach_xml = strip_xml(domain_mock.attachDevice.call_args[0][0]) - assert usb_device_xml == attach_xml + assert attach_xml == usb_device_xml pci_device_xml = strip_xml( """ @@ -875,7 +880,7 @@ def test_update_hostdev_changes(running, live, make_mock_device, make_mock_vm, t """ ) detach_xml = strip_xml(domain_mock.detachDevice.call_args[0][0]) - assert pci_device_xml == detach_xml + assert detach_xml == pci_device_xml else: domain_mock.attachDevice.assert_not_called() domain_mock.detachDevice.assert_not_called() @@ -932,14 +937,16 @@ def test_diff_nics(): """ ).findall("interface") ret = virt._diff_interface_lists(old_nics, new_nics) - assert ["52:54:00:39:02:b1"] == [ - nic.find("mac").get("address") for nic in ret["unchanged"] + assert [nic.find("mac").get("address") for nic in ret["unchanged"]] == [ + "52:54:00:39:02:b1" ] - assert ["52:54:00:39:02:b2", "52:54:00:39:02:b4"] == [ - nic.find("mac").get("address") for nic in ret["new"] + assert [nic.find("mac").get("address") for nic in ret["new"]] == [ + "52:54:00:39:02:b2", + "52:54:00:39:02:b4", ] - assert ["52:54:00:39:02:b2", "52:54:00:39:02:b3"] == [ - nic.find("mac").get("address") for nic in ret["deleted"] + assert [nic.find("mac").get("address") for nic in ret["deleted"]] == [ + "52:54:00:39:02:b2", + "52:54:00:39:02:b3", ] @@ -986,8 +993,9 @@ def test_diff_nics_live_nochange(): """ ) ret = virt._diff_interface_lists(old_nics, new_nics) - assert ["52:54:00:03:02:15", "52:54:00:ea:2e:89"] == [ - nic.find("mac").get("address") for nic in ret["unchanged"] + assert [nic.find("mac").get("address") for nic in ret["unchanged"]] == [ + "52:54:00:03:02:15", + "52:54:00:ea:2e:89", ] @@ -1270,7 +1278,7 @@ def test_update_bootdev_unchanged(make_mock_vm, boot_dev): """ ) ret = virt.update("my_vm", boot_dev=boot_dev) - assert (boot_dev != "hd") == ret["definition"] + assert ret["definition"] == (boot_dev != "hd") if boot_dev == "hd": virt.libvirt.openAuth().defineXML.assert_not_called() else: @@ -2048,3 +2056,65 @@ def test_update_failure(make_mock_vm): "disk": {"attached": [], "detached": [], "updated": []}, "interface": {"attached": [], "detached": []}, } + +@pytest.mark.parametrize("hypervisor", ["kvm", "xen"]) +def test_gen_xml_spice_default(hypervisor): + """ + Test virt._gen_xml() with default spice graphics device + """ + xml_data = virt._gen_xml( + virt.libvirt.openAuth.return_value, + "hello", + 1, + 512, + {}, + {}, + hypervisor, + "hvm", + "x86_64", + graphics={"type": "spice"}, + ) + root = ET.fromstring(xml_data) + assert root.find("devices/graphics").attrib["type"] == "spice" + assert root.find("devices/graphics").attrib["autoport"] == "yes" + assert root.find("devices/graphics").attrib["listen"] == "0.0.0.0" + assert root.find("devices/graphics/listen").attrib["type"] == "address" + assert root.find("devices/graphics/listen").attrib["address"] == "0.0.0.0" + if hypervisor == "kvm": + assert ( + root.find(".//channel[@type='spicevmc']/target").get("name") + == "com.redhat.spice.0" + ) + else: + assert root.find(".//channel[@type='spicevmc']") is None + + +def test_gen_xml_spice(): + """ + Test virt._gen_xml() with spice graphics device + """ + xml_data = virt._gen_xml( + virt.libvirt.openAuth.return_value, + "hello", + 1, + 512, + {}, + {}, + "kvm", + "hvm", + "x86_64", + graphics={ + "type": "spice", + "port": 1234, + "tls_port": 5678, + "listen": {"type": "none"}, + }, + ) + root = ET.fromstring(xml_data) + assert root.find("devices/graphics").attrib["type"] == "spice" + assert root.find("devices/graphics").attrib["autoport"] == "no" + assert root.find("devices/graphics").attrib["port"] == "1234" + assert root.find("devices/graphics").attrib["tlsPort"] == "5678" + assert "listen" not in root.find("devices/graphics").attrib + assert root.find("devices/graphics/listen").attrib["type"] == "none" + assert "address" not in root.find("devices/graphics/listen").attrib diff --git a/tests/pytests/unit/modules/virt/test_helpers.py b/tests/pytests/unit/modules/virt/test_helpers.py index 70f5a8a31f..30c4a66b7d 100644 --- a/tests/pytests/unit/modules/virt/test_helpers.py +++ b/tests/pytests/unit/modules/virt/test_helpers.py @@ -12,12 +12,12 @@ def append_to_XMLDesc(mocked, fragment): mocked.XMLDesc.return_value = ET.tostring(xml_doc) -def assert_xml_equals(expected, actual): +def assert_xml_equals(actual, expected): """ Assert that two ElementTree nodes are equal """ - assert xmlutil.to_dict(xmlutil.strip_spaces(expected), True) == xmlutil.to_dict( - xmlutil.strip_spaces(actual), True + assert xmlutil.to_dict(xmlutil.strip_spaces(actual), True) == xmlutil.to_dict( + xmlutil.strip_spaces(expected), True ) diff --git a/tests/pytests/unit/modules/virt/test_host.py b/tests/pytests/unit/modules/virt/test_host.py index 555deb23bb..6c9ac79337 100644 --- a/tests/pytests/unit/modules/virt/test_host.py +++ b/tests/pytests/unit/modules/virt/test_host.py @@ -173,7 +173,7 @@ def test_node_devices(make_mock_device): ] virt.libvirt.openAuth().listAllDevices.return_value = mock_devs - assert [ + assert virt.node_devices() == [ { "name": "pci_1002_71c4", "caps": "pci", @@ -216,4 +216,4 @@ def test_node_devices(make_mock_device): "state": "down", "device name": "pci_0000_02_10_7", }, - ] == virt.node_devices() + ] diff --git a/tests/pytests/unit/modules/virt/test_network.py b/tests/pytests/unit/modules/virt/test_network.py index e7e544c580..52aadc9519 100644 --- a/tests/pytests/unit/modules/virt/test_network.py +++ b/tests/pytests/unit/modules/virt/test_network.py @@ -18,10 +18,10 @@ def test_gen_xml(): """ xml_data = virt._gen_net_xml("network", "main", "bridge", "openvswitch") root = ET.fromstring(xml_data) - assert "network" == root.find("name").text - assert "main" == root.find("bridge").attrib["name"] - assert "bridge" == root.find("forward").attrib["mode"] - assert "openvswitch" == root.find("virtualport").attrib["type"] + assert root.find("name").text == "network" + assert root.find("bridge").attrib["name"] == "main" + assert root.find("forward").attrib["mode"] == "bridge" + assert root.find("virtualport").attrib["type"] == "openvswitch" def test_gen_xml_nat(): @@ -68,9 +68,9 @@ def test_gen_xml_nat(): mtu=9000, ) root = ET.fromstring(xml_data) - assert "network" == root.find("name").text - assert "main" == root.find("bridge").attrib["name"] - assert "nat" == root.find("forward").attrib["mode"] + assert root.find("name").text == "network" + assert root.find("bridge").attrib["name"] == "main" + assert root.find("forward").attrib["mode"] == "nat" expected_ipv4 = ET.fromstring( """ <ip family='ipv4' address='192.168.2.1' prefix='24'> @@ -84,7 +84,7 @@ def test_gen_xml_nat(): </ip> """ ) - assert_xml_equals(expected_ipv4, root.find("./ip[@address='192.168.2.1']")) + assert_xml_equals(root.find("./ip[@address='192.168.2.1']"), expected_ipv4) expected_ipv6 = ET.fromstring( """ @@ -96,7 +96,7 @@ def test_gen_xml_nat(): </ip> """ ) - assert_xml_equals(expected_ipv6, root.find("./ip[@address='2001:db8:ca2:2::1']")) + assert_xml_equals(root.find("./ip[@address='2001:db8:ca2:2::1']"), expected_ipv6) actual_nat = ET.tostring(xmlutil.strip_spaces(root.find("./forward/nat"))) expected_nat = strip_xml( @@ -107,10 +107,10 @@ def test_gen_xml_nat(): </nat> """ ) - assert expected_nat == actual_nat + assert actual_nat == expected_nat - assert {"name": "acme.lab", "localOnly": "yes"} == root.find("./domain").attrib - assert "9000" == root.find("mtu").get("size") + assert root.find("./domain").attrib == {"name": "acme.lab", "localOnly": "yes"} + assert root.find("mtu").get("size") == "9000" def test_gen_xml_dns(): @@ -166,12 +166,12 @@ def test_gen_xml_dns(): <hostname>mirror.acme.lab</hostname> <hostname>test.acme.lab</hostname> </host> - <srv name='srv1' protocol='tcp' port='1024' target='.' priority='10' weight='10' domain='test-domain-name'/> - <srv name='srv2' protocol='udp'/> + <srv service='srv1' protocol='tcp' port='1024' target='.' priority='10' weight='10' domain='test-domain-name'/> + <srv service='srv2' protocol='udp'/> </dns> """ ) - assert_xml_equals(expected_xml, root.find("./dns")) + assert_xml_equals(root.find("./dns"), expected_xml) def test_gen_xml_isolated(): @@ -190,9 +190,11 @@ def test_gen_xml_passthrough_interfaces(): "network", "virbr0", "passthrough", None, interfaces="eth10 eth11 eth12", ) root = ET.fromstring(xml_data) - assert "passthrough" == root.find("forward").get("mode") - assert ["eth10", "eth11", "eth12"] == [ - n.get("dev") for n in root.findall("forward/interface") + assert root.find("forward").get("mode") == "passthrough" + assert [n.get("dev") for n in root.findall("forward/interface")] == [ + "eth10", + "eth11", + "eth12", ] @@ -212,7 +214,7 @@ def test_gen_xml_hostdev_addresses(): </forward> """ ) - assert_xml_equals(expected_forward, root.find("./forward")) + assert_xml_equals(root.find("./forward"), expected_forward) def test_gen_xml_hostdev_pf(): @@ -231,7 +233,7 @@ def test_gen_xml_hostdev_pf(): """ ) actual_forward = ET.tostring(xmlutil.strip_spaces(root.find("./forward"))) - assert expected_forward == actual_forward + assert actual_forward == expected_forward def test_gen_xml_openvswitch(): @@ -267,7 +269,7 @@ def test_gen_xml_openvswitch(): </network> """ ) - assert_xml_equals(expected_xml, ET.fromstring(xml_data)) + assert_xml_equals(ET.fromstring(xml_data), expected_xml) @pytest.mark.parametrize( @@ -307,7 +309,7 @@ def test_define(make_mock_network, autostart, start): """ ) define_mock = virt.libvirt.openAuth().networkDefineXML - assert expected_xml == strip_xml(define_mock.call_args[0][0]) + assert strip_xml(define_mock.call_args[0][0]) == expected_xml if autostart: mock_network.setAutostart.assert_called_with(1) @@ -416,7 +418,7 @@ def test_update_nat_change(make_mock_network, test): </network> """ ) - assert expected_xml == strip_xml(define_mock.call_args[0][0]) + assert strip_xml(define_mock.call_args[0][0]) == expected_xml @pytest.mark.parametrize("change", [True, False], ids=["changed", "unchanged"]) @@ -437,11 +439,14 @@ def test_update_hostdev_pf(make_mock_network, change): </network> """ ) - assert change == virt.network_update( - "test-hostdev", - None, - "hostdev", - physical_function="eth0" if not change else "eth1", + assert ( + virt.network_update( + "test-hostdev", + None, + "hostdev", + physical_function="eth0" if not change else "eth1", + ) + == change ) define_mock = virt.libvirt.openAuth().networkDefineXML if change: diff --git a/tests/pytests/unit/states/virt/test_domain.py b/tests/pytests/unit/states/virt/test_domain.py index a4ae8c0694..c705785bf5 100644 --- a/tests/pytests/unit/states/virt/test_domain.py +++ b/tests/pytests/unit/states/virt/test_domain.py @@ -21,14 +21,14 @@ def test_defined_no_change(test): "virt.init": init_mock, }, ): - assert { + assert virt.defined("myvm") == { "name": "myvm", "changes": {"myvm": {"definition": False}}, "result": True, "comment": "Domain myvm unchanged", - } == virt.defined("myvm") + } init_mock.assert_not_called() - assert [domain_update_call("myvm", test=test)] == update_mock.call_args_list + assert update_mock.call_args_list == [domain_update_call("myvm", test=test)] def test_defined_new_with_connection(test): @@ -72,12 +72,7 @@ def test_defined_new_with_connection(test): {"type": "tcp", "port": 22223, "protocol": "telnet"}, {"type": "pty"}, ] - assert { - "name": "myvm", - "result": True if not test else None, - "changes": {"myvm": {"definition": True}}, - "comment": "Domain myvm defined", - } == virt.defined( + assert virt.defined( "myvm", cpu=2, mem=2048, @@ -103,7 +98,12 @@ def test_defined_new_with_connection(test): serials=serials, consoles=consoles, host_devices=["pci_0000_00_17_0"], - ) + ) == { + "name": "myvm", + "result": True if not test else None, + "changes": {"myvm": {"definition": True}}, + "comment": "Domain myvm defined", + } if not test: init_mock.assert_called_with( "myvm", @@ -160,16 +160,16 @@ def test_defined_update(test): "initrd": "/root/f8-i386-initrd", "cmdline": "console=ttyS0 ks=http://example.com/f8-i386/os/", } - assert { + assert virt.defined("myvm", cpu=2, boot=boot,) == { "name": "myvm", "changes": {"myvm": {"definition": True, "cpu": True}}, "result": True if not test else None, "comment": "Domain myvm updated", - } == virt.defined("myvm", cpu=2, boot=boot,) + } init_mock.assert_not_called() - assert [ + assert update_mock.call_args_list == [ domain_update_call("myvm", cpu=2, test=test, boot=boot) - ] == update_mock.call_args_list + ] def test_defined_update_error(test): @@ -189,7 +189,7 @@ def test_defined_update_error(test): "virt.init": init_mock, }, ): - assert { + assert virt.defined("myvm", cpu=2, boot_dev="cdrom hd") == { "name": "myvm", "changes": { "myvm": { @@ -200,7 +200,7 @@ def test_defined_update_error(test): }, "result": True if not test else None, "comment": "Domain myvm updated with live update(s) failures", - } == virt.defined("myvm", cpu=2, boot_dev="cdrom hd") + } init_mock.assert_not_called() update_mock.assert_called_with( "myvm", @@ -245,16 +245,16 @@ def test_defined_update_definition_error(test): "virt.init": init_mock, }, ): - assert { + assert virt.defined("myvm", cpu=2) == { "name": "myvm", "changes": {}, "result": False, "comment": "error message", - } == virt.defined("myvm", cpu=2) + } init_mock.assert_not_called() - assert [ + assert update_mock.call_args_list == [ domain_update_call("myvm", cpu=2, test=test) - ] == update_mock.call_args_list + ] @pytest.mark.parametrize("running", ["running", "shutdown"]) @@ -279,12 +279,12 @@ def test_running_no_change(test, running): if running == "shutdown": changes["started"] = True comment = "Domain myvm started" - assert { + assert virt.running("myvm") == { "name": "myvm", "result": True, "changes": {"myvm": changes}, "comment": comment, - } == virt.running("myvm") + } if running == "shutdown" and not test: start_mock.assert_called() else: @@ -326,12 +326,7 @@ def test_running_define(test): "listen": {"type": "address", "address": "192.168.0.1"}, } - assert { - "name": "myvm", - "result": True if not test else None, - "changes": {"myvm": {"definition": True, "started": True}}, - "comment": "Domain myvm defined and started", - } == virt.running( + assert virt.running( "myvm", cpu=2, mem=2048, @@ -353,7 +348,12 @@ def test_running_define(test): connection="someconnection", username="libvirtuser", password="supersecret", - ) + ) == { + "name": "myvm", + "result": True if not test else None, + "changes": {"myvm": {"definition": True, "started": True}}, + "comment": "Domain myvm defined and started", + } if not test: init_mock.assert_called_with( "myvm", @@ -412,12 +412,12 @@ def test_running_start_error(): "virt.list_domains": MagicMock(return_value=["myvm"]), }, ): - assert { + assert virt.running("myvm") == { "name": "myvm", "changes": {"myvm": {"definition": False}}, "result": False, "comment": "libvirt error msg", - } == virt.running("myvm") + } @pytest.mark.parametrize("running", ["running", "shutdown"]) @@ -441,14 +441,14 @@ def test_running_update(test, running): changes = {"myvm": {"definition": True, "cpu": True}} if running == "shutdown": changes["myvm"]["started"] = True - assert { + assert virt.running("myvm", cpu=2) == { "name": "myvm", "changes": changes, "result": True if not test else None, "comment": "Domain myvm updated" if running == "running" else "Domain myvm updated and started", - } == virt.running("myvm", cpu=2) + } if running == "shutdown" and not test: start_mock.assert_called() else: @@ -470,12 +470,12 @@ def test_running_definition_error(): "virt.list_domains": MagicMock(return_value=["myvm"]), }, ): - assert { + assert virt.running("myvm", cpu=3) == { "name": "myvm", "changes": {}, "result": False, "comment": "error message", - } == virt.running("myvm", cpu=3) + } def test_running_update_error(): @@ -494,7 +494,7 @@ def test_running_update_error(): "virt.list_domains": MagicMock(return_value=["myvm"]), }, ): - assert { + assert virt.running("myvm", cpu=2) == { "name": "myvm", "changes": { "myvm": { @@ -505,7 +505,7 @@ def test_running_update_error(): }, "result": True, "comment": "Domain myvm updated with live update(s) failures", - } == virt.running("myvm", cpu=2) + } update_mock.assert_called_with( "myvm", cpu=2, @@ -552,14 +552,14 @@ def test_stopped(test, running): if running == "running": changes = {"stopped": [{"domain": "myvm", "shutdown": True}]} comment = "Machine has been shut down" - assert { + assert virt.stopped( + "myvm", connection="myconnection", username="user", password="secret", + ) == { "name": "myvm", "changes": changes, "comment": comment, "result": True if not test or running == "shutdown" else None, - } == virt.stopped( - "myvm", connection="myconnection", username="user", password="secret", - ) + } if not test and running == "running": shutdown_mock.assert_called_with( "myvm", @@ -586,12 +586,12 @@ def test_stopped_error(): ), }, ): - assert { + assert virt.stopped("myvm") == { "name": "myvm", "changes": {"ignored": [{"domain": "myvm", "issue": "Some error"}]}, "result": False, "comment": "No changes had happened", - } == virt.stopped("myvm") + } def test_stopped_not_existing(test): @@ -603,12 +603,12 @@ def test_stopped_not_existing(test): with patch.dict( virt.__salt__, {"virt.list_domains": MagicMock(return_value=[])}, ): - assert { + assert virt.stopped("myvm") == { "name": "myvm", "changes": {}, "comment": "No changes had happened", "result": False, - } == virt.stopped("myvm") + } @pytest.mark.parametrize("running", ["running", "shutdown"]) @@ -631,14 +631,14 @@ def test_powered_off(test, running): if running == "running": changes = {"unpowered": [{"domain": "myvm", "stop": True}]} comment = "Machine has been powered off" - assert { + assert virt.powered_off( + "myvm", connection="myconnection", username="user", password="secret", + ) == { "name": "myvm", "result": True if not test or running == "shutdown" else None, "changes": changes, "comment": comment, - } == virt.powered_off( - "myvm", connection="myconnection", username="user", password="secret", - ) + } if not test and running == "running": stop_mock.assert_called_with( "myvm", @@ -666,12 +666,12 @@ def test_powered_off_error(): ), }, ): - assert { + assert virt.powered_off("myvm") == { "name": "myvm", "result": False, "changes": {"ignored": [{"domain": "myvm", "issue": "Some error"}]}, "comment": "No changes had happened", - } == virt.powered_off("myvm") + } def test_powered_off_not_existing(): @@ -686,12 +686,12 @@ def test_powered_off_not_existing(): ret.update( {"changes": {}, "result": False, "comment": "No changes had happened"} ) - assert { + assert virt.powered_off("myvm") == { "name": "myvm", "changes": {}, "result": False, "comment": "No changes had happened", - } == virt.powered_off("myvm") + } def test_snapshot(test): @@ -707,18 +707,18 @@ def test_snapshot(test): "virt.snapshot": snapshot_mock, }, ): - assert { - "name": "myvm", - "result": True if not test else None, - "changes": {"saved": [{"domain": "myvm", "snapshot": True}]}, - "comment": "Snapshot has been taken", - } == virt.snapshot( + assert virt.snapshot( "myvm", suffix="snap", connection="myconnection", username="user", password="secret", - ) + ) == { + "name": "myvm", + "result": True if not test else None, + "changes": {"saved": [{"domain": "myvm", "snapshot": True}]}, + "comment": "Snapshot has been taken", + } if not test: snapshot_mock.assert_called_with( "myvm", @@ -745,12 +745,12 @@ def test_snapshot_error(): ), }, ): - assert { + assert virt.snapshot("myvm") == { "name": "myvm", "result": False, "changes": {"ignored": [{"domain": "myvm", "issue": "Some error"}]}, "comment": "No changes had happened", - } == virt.snapshot("myvm") + } def test_snapshot_not_existing(test): @@ -761,12 +761,12 @@ def test_snapshot_not_existing(test): with patch.dict( virt.__salt__, {"virt.list_domains": MagicMock(return_value=[])} ): - assert { + assert virt.snapshot("myvm") == { "name": "myvm", "changes": {}, "result": False, "comment": "No changes had happened", - } == virt.snapshot("myvm") + } def test_rebooted(test): @@ -782,14 +782,14 @@ def test_rebooted(test): "virt.reboot": reboot_mock, }, ): - assert { + assert virt.rebooted( + "myvm", connection="myconnection", username="user", password="secret", + ) == { "name": "myvm", "result": True if not test else None, "changes": {"rebooted": [{"domain": "myvm", "reboot": True}]}, "comment": "Machine has been rebooted", - } == virt.rebooted( - "myvm", connection="myconnection", username="user", password="secret", - ) + } if not test: reboot_mock.assert_called_with( "myvm", @@ -816,12 +816,12 @@ def test_rebooted_error(): ), }, ): - assert { + assert virt.rebooted("myvm") == { "name": "myvm", "result": False, "changes": {"ignored": [{"domain": "myvm", "issue": "Some error"}]}, "comment": "No changes had happened", - } == virt.rebooted("myvm") + } def test_rebooted_not_existing(test): @@ -832,9 +832,9 @@ def test_rebooted_not_existing(test): with patch.dict( virt.__salt__, {"virt.list_domains": MagicMock(return_value=[])} ): - assert { + assert virt.rebooted("myvm") == { "name": "myvm", "changes": {}, "result": False, "comment": "No changes had happened", - } == virt.rebooted("myvm") + } diff --git a/tests/pytests/unit/states/virt/test_network.py b/tests/pytests/unit/states/virt/test_network.py index 668eee0c64..a68acfa236 100644 --- a/tests/pytests/unit/states/virt/test_network.py +++ b/tests/pytests/unit/states/virt/test_network.py @@ -19,12 +19,7 @@ def test_network_defined_not_existing(test): "virt.network_define": define_mock, }, ): - assert { - "name": "mynet", - "changes": {"mynet": "Network defined"}, - "result": None if test else True, - "comment": "Network mynet defined", - } == virt.network_defined( + assert virt.network_defined( "mynet", "br2", "bridge", @@ -58,7 +53,12 @@ def test_network_defined_not_existing(test): connection="myconnection", username="user", password="secret", - ) + ) == { + "name": "mynet", + "changes": {"mynet": "Network defined"}, + "result": None if test else True, + "comment": "Network mynet defined", + } if not test: define_mock.assert_called_with( "mynet", @@ -117,16 +117,16 @@ def test_network_defined_no_change(test): "virt.network_update": update_mock, }, ): - assert { + assert virt.network_defined("mynet", "br2", "bridge") == { "name": "mynet", "changes": {}, "result": True, "comment": "Network mynet unchanged", - } == virt.network_defined("mynet", "br2", "bridge") + } define_mock.assert_not_called() - assert [ + assert update_mock.call_args_list == [ network_update_call("mynet", "br2", "bridge", test=True) - ] == update_mock.call_args_list + ] def test_network_defined_change(test): @@ -148,12 +148,7 @@ def test_network_defined_change(test): "virt.network_set_autostart": autostart_mock, }, ): - assert { - "name": "mynet", - "changes": {"mynet": "Network updated, autostart flag changed"}, - "result": None if test else True, - "comment": "Network mynet updated, autostart flag changed", - } == virt.network_defined( + assert virt.network_defined( "mynet", "br2", "bridge", @@ -187,7 +182,12 @@ def test_network_defined_change(test): connection="myconnection", username="user", password="secret", - ) + ) == { + "name": "mynet", + "changes": {"mynet": "Network updated, autostart flag changed"}, + "result": None if test else True, + "comment": "Network mynet updated, autostart flag changed", + } define_mock.assert_not_called() expected_update_kwargs = { "vport": "openvswitch", @@ -226,7 +226,7 @@ def test_network_defined_change(test): ) ] if test: - assert calls == update_mock.call_args_list + assert update_mock.call_args_list == calls autostart_mock.assert_not_called() else: calls.append( @@ -234,7 +234,7 @@ def test_network_defined_change(test): "mynet", "br2", "bridge", **expected_update_kwargs, test=False ) ) - assert calls == update_mock.call_args_list + assert update_mock.call_args_list == calls autostart_mock.assert_called_with( "mynet", state="off", @@ -258,12 +258,12 @@ def test_network_defined_error(test): ) }, ): - assert { + assert virt.network_defined("mynet", "br2", "bridge") == { "name": "mynet", "changes": {}, "result": False, "comment": "Some error", - } == virt.network_defined("mynet", "br2", "bridge") + } define_mock.assert_not_called() @@ -285,12 +285,7 @@ def test_network_running_not_existing(test): "virt.network_start": start_mock, }, ): - assert { - "name": "mynet", - "changes": {"mynet": "Network defined and started"}, - "comment": "Network mynet defined and started", - "result": None if test else True, - } == virt.network_running( + assert virt.network_running( "mynet", "br2", "bridge", @@ -324,7 +319,12 @@ def test_network_running_not_existing(test): connection="myconnection", username="user", password="secret", - ) + ) == { + "name": "mynet", + "changes": {"mynet": "Network defined and started"}, + "comment": "Network mynet defined and started", + "result": None if test else True, + } if not test: define_mock.assert_called_with( "mynet", @@ -390,15 +390,15 @@ def test_network_running_nochange(test): "virt.network_update": update_mock, }, ): - assert { + assert virt.network_running("mynet", "br2", "bridge") == { "name": "mynet", "changes": {}, "comment": "Network mynet unchanged and is running", "result": None if test else True, - } == virt.network_running("mynet", "br2", "bridge") - assert [ + } + assert update_mock.call_args_list == [ network_update_call("mynet", "br2", "bridge", test=True) - ] == update_mock.call_args_list + ] def test_network_running_stopped(test): @@ -420,20 +420,20 @@ def test_network_running_stopped(test): "virt.network_update": update_mock, }, ): - assert { - "name": "mynet", - "changes": {"mynet": "Network started"}, - "comment": "Network mynet unchanged and started", - "result": None if test else True, - } == virt.network_running( + assert virt.network_running( "mynet", "br2", "bridge", connection="myconnection", username="user", password="secret", - ) - assert [ + ) == { + "name": "mynet", + "changes": {"mynet": "Network started"}, + "comment": "Network mynet unchanged and started", + "result": None if test else True, + } + assert update_mock.call_args_list == [ network_update_call( "mynet", "br2", @@ -443,7 +443,7 @@ def test_network_running_stopped(test): password="secret", test=True, ) - ] == update_mock.call_args_list + ] if not test: start_mock.assert_called_with( "mynet", @@ -468,9 +468,9 @@ def test_network_running_error(test): ), }, ): - assert { + assert virt.network_running("mynet", "br2", "bridge") == { "name": "mynet", "changes": {}, "comment": "Some error", "result": False, - } == virt.network_running("mynet", "br2", "bridge") + } diff --git a/tests/unit/modules/test_virt.py b/tests/unit/modules/test_virt.py index cac83e8717..a739efdbf6 100644 --- a/tests/unit/modules/test_virt.py +++ b/tests/unit/modules/test_virt.py @@ -520,65 +520,6 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin): root.find("devices/graphics/listen").attrib["address"], "myhost" ) - def test_gen_xml_spice_default(self): - """ - Test virt._gen_xml() with default spice graphics device - """ - diskp = virt._disk_profile(self.mock_conn, "default", "kvm", [], "hello") - nicp = virt._nic_profile("default", "kvm") - xml_data = virt._gen_xml( - self.mock_conn, - "hello", - 1, - 512, - diskp, - nicp, - "kvm", - "hvm", - "x86_64", - graphics={"type": "spice"}, - ) - root = ET.fromstring(xml_data) - self.assertEqual(root.find("devices/graphics").attrib["type"], "spice") - self.assertEqual(root.find("devices/graphics").attrib["autoport"], "yes") - self.assertEqual(root.find("devices/graphics").attrib["listen"], "0.0.0.0") - self.assertEqual(root.find("devices/graphics/listen").attrib["type"], "address") - self.assertEqual( - root.find("devices/graphics/listen").attrib["address"], "0.0.0.0" - ) - - def test_gen_xml_spice(self): - """ - Test virt._gen_xml() with spice graphics device - """ - diskp = virt._disk_profile(self.mock_conn, "default", "kvm", [], "hello") - nicp = virt._nic_profile("default", "kvm") - xml_data = virt._gen_xml( - self.mock_conn, - "hello", - 1, - 512, - diskp, - nicp, - "kvm", - "hvm", - "x86_64", - graphics={ - "type": "spice", - "port": 1234, - "tls_port": 5678, - "listen": {"type": "none"}, - }, - ) - root = ET.fromstring(xml_data) - self.assertEqual(root.find("devices/graphics").attrib["type"], "spice") - self.assertEqual(root.find("devices/graphics").attrib["autoport"], "no") - self.assertEqual(root.find("devices/graphics").attrib["port"], "1234") - self.assertEqual(root.find("devices/graphics").attrib["tlsPort"], "5678") - self.assertFalse("listen" in root.find("devices/graphics").attrib) - self.assertEqual(root.find("devices/graphics/listen").attrib["type"], "none") - self.assertFalse("address" in root.find("devices/graphics/listen").attrib) - def test_gen_xml_memory(self): """ Test virt._gen_xml() with advanced memory settings -- 2.30.0
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor