From f7bd274a556070f27749609d179528de7f8add47 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Mon, 28 Sep 2020 13:44:34 -0400 Subject: [PATCH] domcaps: Fix error if enum not found https://bugzilla.redhat.com/show_bug.cgi?id=1883008 In the example above this is because the VM XML has an invalid machine type, so domcaps fetching entirely fails, and a get_enum() call then fails. But this could happen if using virt-manager against an older libvirt that doesn't advertise the enum Signed-off-by: Cole Robinson --- tests/test_capabilities.py | 1 + virtinst/domcapabilities.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/test_capabilities.py b/tests/test_capabilities.py index b6b870e59..0182e20f0 100644 --- a/tests/test_capabilities.py +++ b/tests/test_capabilities.py @@ -80,6 +80,7 @@ def testDomainCapabilities(): assert caps.os.loader.enum_names() == ["type", "readonly"] assert caps.os.loader.get_enum("type").get_values() == [ "rom", "pflash"] + assert caps.os.loader.get_enum("idontexist").get_values() == [] def testDomainCapabilitiesx86(): diff --git a/virtinst/domcapabilities.py b/virtinst/domcapabilities.py index d245c0f36..64e4307e1 100644 --- a/virtinst/domcapabilities.py +++ b/virtinst/domcapabilities.py @@ -45,8 +45,12 @@ class _CapsBlock(_HasValues): return [e.name for e in self.enums] def get_enum(self, name): - d = dict((e.name, e) for e in self.enums) - return d[name] + for enum in self.enums: + if enum.name == name: + return enum + # Didn't find a match. Could be talking to older libvirt, or + # driver with incomplete info. Return a stub enum + return _Enum(self.conn) def _make_capsblock(xml_root_name):