Skip to content

Commit

Permalink
get_node_info: Avoid parsing XML in a loop
Browse files Browse the repository at this point in the history
This refactors the method so it only parses the XML once instead of 4
times. It adds basic tests to avoid regressions.
  • Loading branch information
ekohl committed Dec 29, 2024
1 parent ad67094 commit 860af54
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/fog/libvirt/requests/compute/get_node_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ def get_node_info
node_hash[param] = client.send(param) rescue nil
end
node_hash[:uri] = client.uri
xml = client.sys_info rescue nil
[:uuid, :manufacturer, :product, :serial].each do |attr|
node_hash[attr] = node_attr(attr, xml) rescue nil
end if xml
if (xml = sys_info)
[:uuid, :manufacturer, :product, :serial].each do |attr|
element = xml / "sysinfo/system/entry[@name=#{attr}]"
node_hash[attr] = element&.text&.strip
end
end

node_hash[:hostname] = client.hostname
[node_hash]
end

private

def node_attr attr, xml
xml_element(xml, "sysinfo/system/entry[@name='#{attr}']").strip
def sys_info
Nokogiri::XML(client.sys_info)
rescue LibvirtError
# qemu:///session typically doesn't have permission to retrieve this
rescue StandardError
# TODO: log this?
end
end

Expand Down
14 changes: 14 additions & 0 deletions tests/libvirt/requests/compute/get_node_info_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Shindo.tests("Fog::Compute[:libvirt] | get_node_info", 'libvirt') do

compute = Fog::Compute[:libvirt]

tests("get_node_info response") do
response = compute.get_node_info
info = response[0]
tests("sys_info attributes") do
[:uuid, :manufacturer, :product, :serial].each do |attr|
test("attribute #{attr} is set") { info[attr].is_a?(String) }
end
end
end
end

0 comments on commit 860af54

Please sign in to comment.