Handle different attribute and element ordering (#1592)

Normalise the XML to ensure the attributes for both documents have the
same ordering to prevent excessive noise when differences are detected.
Additionally sort various elements based on attributes that make
ordering irrelevant to allow for simpler comparison using xmlsimple.

Closes: #1583
This commit is contained in:
Darragh Bailey
2022-09-22 17:14:49 +01:00
committed by GitHub
parent 5e0b169dff
commit f111842dbe
4 changed files with 98 additions and 1 deletions

View File

@@ -429,9 +429,20 @@ module VagrantPlugins
raise Errors::UpdateServerError, error_message: e.message
end
# this normalises the attribute order to be the same as what was sent in the above
# request to update the domain XML. Without this, if the XML documents are not
# equivalent, many more differences will be reported than there actually are.
applied_xml = String.new
REXML::Document.new(libvirt_domain.xml_desc(1)).write(applied_xml)
# need to check whether the updated XML contains all the changes requested
proposed = VagrantPlugins::ProviderLibvirt::Util::Xml.new(new_xml)
applied = VagrantPlugins::ProviderLibvirt::Util::Xml.new(libvirt_domain.xml_desc(1))
applied = VagrantPlugins::ProviderLibvirt::Util::Xml.new(applied_xml)
# perform some sorting to allow comparison otherwise order of devices differing
# even if they are equivalent will be reported as being different.
proposed.xml['devices'][0].each { |_, v| next unless v.is_a?(Array); v.sort_by! { |e| [e['type'], e['index']]} }
applied.xml['devices'][0].each { |_, v| next unless v.is_a?(Array); v.sort_by! { |e| [e['type'], e['index']]} }
if proposed != applied
require 'diffy'