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 <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2020-09-28 13:44:34 -04:00
parent 9c13d2f878
commit f7bd274a55
2 changed files with 7 additions and 2 deletions

View File

@ -80,6 +80,7 @@ def testDomainCapabilities():
assert caps.os.loader.enum_names() == ["type", "readonly"] assert caps.os.loader.enum_names() == ["type", "readonly"]
assert caps.os.loader.get_enum("type").get_values() == [ assert caps.os.loader.get_enum("type").get_values() == [
"rom", "pflash"] "rom", "pflash"]
assert caps.os.loader.get_enum("idontexist").get_values() == []
def testDomainCapabilitiesx86(): def testDomainCapabilitiesx86():

View File

@ -45,8 +45,12 @@ class _CapsBlock(_HasValues):
return [e.name for e in self.enums] return [e.name for e in self.enums]
def get_enum(self, name): def get_enum(self, name):
d = dict((e.name, e) for e in self.enums) for enum in self.enums:
return d[name] 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): def _make_capsblock(xml_root_name):