Skip to content

Commit 3d04c78

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add unit tests for "hypervisor show" command"
2 parents 8a250f3 + fc708c4 commit 3d04c78

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

openstackclient/tests/compute/v2/fakes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888

8989
class FakeComputev2Client(object):
9090
def __init__(self, **kwargs):
91+
self.aggregates = mock.Mock()
92+
self.aggregates.resource_class = fakes.FakeResource(None, {})
9193
self.availability_zones = mock.Mock()
9294
self.availability_zones.resource_class = fakes.FakeResource(None, {})
9395

@@ -175,6 +177,30 @@ def create_one_hypervisor(attrs={}):
175177
hypervisor_info = {
176178
'id': 'hypervisor-id-' + uuid.uuid4().hex,
177179
'hypervisor_hostname': 'hypervisor-hostname-' + uuid.uuid4().hex,
180+
'status': 'enabled',
181+
'host_ip': '192.168.0.10',
182+
'cpu_info': {
183+
'aaa': 'aaa',
184+
},
185+
'free_disk_gb': 50,
186+
'hypervisor_version': 2004001,
187+
'disk_available_least': 50,
188+
'local_gb': 50,
189+
'free_ram_mb': 1024,
190+
'service': {
191+
'host': 'aaa',
192+
'disabled_reason': None,
193+
'id': 1,
194+
},
195+
'vcpus_used': 0,
196+
'hypervisor_type': 'QEMU',
197+
'local_gb_used': 0,
198+
'vcpus': 4,
199+
'memory_mb_used': 512,
200+
'memory_mb': 1024,
201+
'current_workload': 0,
202+
'state': 'up',
203+
'running_vms': 0,
178204
}
179205

180206
# Overwrite default attributes.

openstackclient/tests/compute/v2/test_hypervisor.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
# under the License.
1414
#
1515

16+
import copy
17+
1618
from openstackclient.common import exceptions
1719
from openstackclient.compute.v2 import hypervisor
1820
from openstackclient.tests.compute.v2 import fakes as compute_fakes
21+
from openstackclient.tests import fakes
1922

2023

2124
class TestHypervisor(compute_fakes.TestComputev2):
@@ -27,6 +30,10 @@ def setUp(self):
2730
self.hypervisors_mock = self.app.client_manager.compute.hypervisors
2831
self.hypervisors_mock.reset_mock()
2932

33+
# Get a shortcut to the compute client aggregates mock
34+
self.aggregates_mock = self.app.client_manager.compute.aggregates
35+
self.aggregates_mock.reset_mock()
36+
3037

3138
class TestHypervisorList(TestHypervisor):
3239

@@ -113,3 +120,101 @@ def test_hypervisor_list_matching_option_not_found(self):
113120
self.assertRaises(exceptions.NotFound,
114121
self.cmd.take_action,
115122
parsed_args)
123+
124+
125+
class TestHypervisorShow(TestHypervisor):
126+
127+
def setUp(self):
128+
super(TestHypervisorShow, self).setUp()
129+
130+
# Fake hypervisors to be listed up
131+
self.hypervisor = compute_fakes.FakeHypervisor.create_one_hypervisor()
132+
133+
# Return value of utils.find_resource()
134+
self.hypervisors_mock.get.return_value = self.hypervisor
135+
136+
# Return value of compute_client.aggregates.list()
137+
self.aggregates_mock.list.return_value = []
138+
139+
# Return value of compute_client.hypervisors.uptime()
140+
uptime_info = {
141+
'status': self.hypervisor.status,
142+
'state': self.hypervisor.state,
143+
'id': self.hypervisor.id,
144+
'hypervisor_hostname': self.hypervisor.hypervisor_hostname,
145+
'uptime': ' 01:28:24 up 3 days, 11:15, 1 user, '
146+
' load average: 0.94, 0.62, 0.50\n',
147+
}
148+
self.hypervisors_mock.uptime.return_value = fakes.FakeResource(
149+
info=copy.deepcopy(uptime_info),
150+
loaded=True
151+
)
152+
153+
self.columns = (
154+
'aggregates',
155+
'cpu_info',
156+
'current_workload',
157+
'disk_available_least',
158+
'free_disk_gb',
159+
'free_ram_mb',
160+
'host_ip',
161+
'hypervisor_hostname',
162+
'hypervisor_type',
163+
'hypervisor_version',
164+
'id',
165+
'local_gb',
166+
'local_gb_used',
167+
'memory_mb',
168+
'memory_mb_used',
169+
'running_vms',
170+
'service_host',
171+
'service_id',
172+
'state',
173+
'status',
174+
'vcpus',
175+
'vcpus_used',
176+
)
177+
self.data = (
178+
[],
179+
{'aaa': 'aaa'},
180+
0,
181+
50,
182+
50,
183+
1024,
184+
'192.168.0.10',
185+
self.hypervisor.hypervisor_hostname,
186+
'QEMU',
187+
2004001,
188+
self.hypervisor.id,
189+
50,
190+
0,
191+
1024,
192+
512,
193+
0,
194+
'aaa',
195+
1,
196+
'up',
197+
'enabled',
198+
4,
199+
0,
200+
)
201+
202+
# Get the command object to test
203+
self.cmd = hypervisor.ShowHypervisor(self.app, None)
204+
205+
def test_hypervisor_show(self):
206+
arglist = [
207+
self.hypervisor.hypervisor_hostname,
208+
]
209+
verifylist = [
210+
('hypervisor', self.hypervisor.hypervisor_hostname),
211+
]
212+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
213+
214+
# In base command class ShowOne in cliff, abstractmethod take_action()
215+
# returns a two-part tuple with a tuple of column names and a tuple of
216+
# data to be shown.
217+
columns, data = self.cmd.take_action(parsed_args)
218+
219+
self.assertEqual(self.columns, columns)
220+
self.assertEqual(self.data, data)

0 commit comments

Comments
 (0)