virt-xml: Fix device lookup by integer properties

We were not correctly accounting for the internal representation of
some fields, and just trying to a string comparison. We need to be
a bit smarter than that

Fixes: https://github.com/virt-manager/virt-manager/issues/356

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2022-01-25 13:43:58 -05:00
parent 9c45f4a2e9
commit dfef112b2a
2 changed files with 10 additions and 2 deletions

View File

@ -1226,6 +1226,7 @@ c.add_valid("test-state-shutoff --edit --boot menu=on", grep="XML did not change
c.add_valid("test-for-virtxml --edit --graphics password=foo --update --confirm", input_text="no\nno\n") # prompt exiting
c.add_valid("test-for-virtxml --edit --cpu host-passthrough --no-define --start --confirm", input_text="no") # transient prompt exiting
c.add_valid("test-for-virtxml --edit --metadata name=test-for-virtxml", grep="requested changes will have no effect")
c.add_valid("--print-diff test-for-virtxml --remove-device --disk boot.order=5", grep="boot order=\"5")
c.add_invalid("test --edit 2 --events on_poweroff=destroy", grep="'--edit 2' doesn't make sense with --events")
c.add_invalid("test --os-variant fedora26 --edit --cpu host-passthrough", grep="--os-variant is not supported")
c.add_invalid("test-for-virtxml --os-variant fedora26 --remove-device --disk 1", grep="--os-variant is not supported")

View File

@ -1166,8 +1166,15 @@ class _VirtCLIArgument(object):
if self._virtarg.lookup_cb:
return self._virtarg.lookup_cb(parser,
inst, self.val, self)
else:
return xmlutil.get_prop_path(inst, self.propname) == self.val
# To reliably compare between CLI style values and internal
# XML API values, we need to set the CLI value on a copy of the
# object we are checking, read back the result, and compare with that
xmlval = xmlutil.get_prop_path(inst, self.propname)
setter = inst.__class__(inst.conn, parsexml=inst.get_xml())
xmlutil.set_prop_path(setter, self.propname, self.val)
clival = xmlutil.get_prop_path(setter, self.propname)
return xmlval == clival
def parse_optstr_tuples(optstr):