Skip to content

Commit 98bafda

Browse files
author
Hironori Shiina
committed
Display hypervisor information without uptime
Some virt drivers such as ironic virt driver doesn't implement a method to get host uptime. For such drivers, hypervisor show command displays no information although these drivers provides other host information. This patch fixes the command to display hypervisor information in case where a virt driver doesn't provide host uptime by ignoring a HTTPNotImplemented exception. Change-Id: I7bcca5862cd9c05aadaf6192cb80aa651cd77cad Closes-Bug: 1612065
1 parent d6f99b7 commit 98bafda

2 files changed

Lines changed: 87 additions & 12 deletions

File tree

openstackclient/compute/v2/hypervisor.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import re
1919

20+
from novaclient import exceptions as nova_exceptions
2021
from osc_lib.command import command
2122
from osc_lib import utils
2223
import six
@@ -94,18 +95,22 @@ def take_action(self, parsed_args):
9495
if service_host in aggregate.hosts]
9596
hypervisor["aggregates"] = member_of
9697

97-
uptime = compute_client.hypervisors.uptime(hypervisor['id'])._info
98-
# Extract data from uptime value
99-
# format: 0 up 0, 0 users, load average: 0, 0, 0
100-
# example: 17:37:14 up 2:33, 3 users, load average: 0.33, 0.36, 0.34
101-
m = re.match(
102-
"\s*(.+)\sup\s+(.+),\s+(.+)\susers?,\s+load average:\s(.+)",
103-
uptime['uptime'])
104-
if m:
105-
hypervisor["host_time"] = m.group(1)
106-
hypervisor["uptime"] = m.group(2)
107-
hypervisor["users"] = m.group(3)
108-
hypervisor["load_average"] = m.group(4)
98+
try:
99+
uptime = compute_client.hypervisors.uptime(hypervisor['id'])._info
100+
# Extract data from uptime value
101+
# format: 0 up 0, 0 users, load average: 0, 0, 0
102+
# example: 17:37:14 up 2:33, 3 users,
103+
# load average: 0.33, 0.36, 0.34
104+
m = re.match(
105+
"\s*(.+)\sup\s+(.+),\s+(.+)\susers?,\s+load average:\s(.+)",
106+
uptime['uptime'])
107+
if m:
108+
hypervisor["host_time"] = m.group(1)
109+
hypervisor["uptime"] = m.group(2)
110+
hypervisor["users"] = m.group(3)
111+
hypervisor["load_average"] = m.group(4)
112+
except nova_exceptions.HTTPNotImplemented:
113+
pass
109114

110115
hypervisor["service_id"] = hypervisor["service"]["id"]
111116
hypervisor["service_host"] = hypervisor["service"]["host"]

openstackclient/tests/unit/compute/v2/test_hypervisor.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import copy
1717

18+
from novaclient import exceptions as nova_exceptions
1819
from osc_lib import exceptions
1920

2021
from openstackclient.compute.v2 import hypervisor
@@ -227,3 +228,72 @@ def test_hypervisor_show(self):
227228

228229
self.assertEqual(self.columns, columns)
229230
self.assertEqual(self.data, data)
231+
232+
def test_hyprvisor_show_uptime_not_implemented(self):
233+
arglist = [
234+
self.hypervisor.hypervisor_hostname,
235+
]
236+
verifylist = [
237+
('hypervisor', self.hypervisor.hypervisor_hostname),
238+
]
239+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
240+
241+
self.hypervisors_mock.uptime.side_effect = (
242+
nova_exceptions.HTTPNotImplemented(501))
243+
244+
# In base command class ShowOne in cliff, abstract method take_action()
245+
# returns a two-part tuple with a tuple of column names and a tuple of
246+
# data to be shown.
247+
columns, data = self.cmd.take_action(parsed_args)
248+
249+
expected_columns = (
250+
'aggregates',
251+
'cpu_info',
252+
'current_workload',
253+
'disk_available_least',
254+
'free_disk_gb',
255+
'free_ram_mb',
256+
'host_ip',
257+
'hypervisor_hostname',
258+
'hypervisor_type',
259+
'hypervisor_version',
260+
'id',
261+
'local_gb',
262+
'local_gb_used',
263+
'memory_mb',
264+
'memory_mb_used',
265+
'running_vms',
266+
'service_host',
267+
'service_id',
268+
'state',
269+
'status',
270+
'vcpus',
271+
'vcpus_used',
272+
)
273+
expected_data = (
274+
[],
275+
{'aaa': 'aaa'},
276+
0,
277+
50,
278+
50,
279+
1024,
280+
'192.168.0.10',
281+
self.hypervisor.hypervisor_hostname,
282+
'QEMU',
283+
2004001,
284+
self.hypervisor.id,
285+
50,
286+
0,
287+
1024,
288+
512,
289+
0,
290+
'aaa',
291+
1,
292+
'up',
293+
'enabled',
294+
4,
295+
0,
296+
)
297+
298+
self.assertEqual(expected_columns, columns)
299+
self.assertEqual(expected_data, data)

0 commit comments

Comments
 (0)