Skip to content

Commit 860af54

Browse files
committed
get_node_info: Avoid parsing XML in a loop
This refactors the method so it only parses the XML once instead of 4 times. It adds basic tests to avoid regressions.
1 parent ad67094 commit 860af54

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/fog/libvirt/requests/compute/get_node_info.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,25 @@ def get_node_info
1212
node_hash[param] = client.send(param) rescue nil
1313
end
1414
node_hash[:uri] = client.uri
15-
xml = client.sys_info rescue nil
16-
[:uuid, :manufacturer, :product, :serial].each do |attr|
17-
node_hash[attr] = node_attr(attr, xml) rescue nil
18-
end if xml
15+
if (xml = sys_info)
16+
[:uuid, :manufacturer, :product, :serial].each do |attr|
17+
element = xml / "sysinfo/system/entry[@name=#{attr}]"
18+
node_hash[attr] = element&.text&.strip
19+
end
20+
end
1921

2022
node_hash[:hostname] = client.hostname
2123
[node_hash]
2224
end
2325

2426
private
2527

26-
def node_attr attr, xml
27-
xml_element(xml, "sysinfo/system/entry[@name='#{attr}']").strip
28+
def sys_info
29+
Nokogiri::XML(client.sys_info)
30+
rescue LibvirtError
31+
# qemu:///session typically doesn't have permission to retrieve this
32+
rescue StandardError
33+
# TODO: log this?
2834
end
2935
end
3036

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Shindo.tests("Fog::Compute[:libvirt] | get_node_info", 'libvirt') do
2+
3+
compute = Fog::Compute[:libvirt]
4+
5+
tests("get_node_info response") do
6+
response = compute.get_node_info
7+
info = response[0]
8+
tests("sys_info attributes") do
9+
[:uuid, :manufacturer, :product, :serial].each do |attr|
10+
test("attribute #{attr} is set") { info[attr].is_a?(String) }
11+
end
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)