Skip to content

Commit 8825f0d

Browse files
author
ting.wang
committed
Add unit tests for 'hypervisor stats' command
'hypervisor stats show' command isn't covered by unit tests, so add unit tests to test it. Change-Id: Ic355230cbdd596e848191b599803dca7f27c2ffb
1 parent ba08683 commit 8825f0d

2 files changed

Lines changed: 140 additions & 0 deletions

File tree

openstackclient/tests/compute/v2/fakes.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ def __init__(self, **kwargs):
121121
self.hypervisors = mock.Mock()
122122
self.hypervisors.resource_class = fakes.FakeResource(None, {})
123123

124+
self.hypervisors_stats = mock.Mock()
125+
self.hypervisors_stats.resource_class = fakes.FakeResource(None, {})
126+
124127
self.security_groups = mock.Mock()
125128
self.security_groups.resource_class = fakes.FakeResource(None, {})
126129

@@ -235,6 +238,64 @@ def create_hypervisors(attrs={}, count=2):
235238
return hypervisors
236239

237240

241+
class FakehypervisorStats(object):
242+
"""Fake one or more hypervisor stats."""
243+
244+
@staticmethod
245+
def create_one_hypervisor_stats(attrs={}, methods={}):
246+
"""Create a fake hypervisor stats.
247+
248+
:param Dictionary attrs:
249+
A dictionary with all attributes
250+
:return:
251+
A FakeResource object, with id, hypervisor_hostname, and so on
252+
"""
253+
# Set default attributes.
254+
stats_info = {
255+
'count': 2,
256+
'current_workload': 0,
257+
'disk_available_least': 50,
258+
'free_disk_gb': 100,
259+
'free_ram_mb': 23000,
260+
'local_gb': 100,
261+
'local_gb_used': 0,
262+
'memory_mb': 23800,
263+
'memory_mb_used': 1400,
264+
'running_vms': 3,
265+
'vcpus': 8,
266+
'vcpus_used': 3,
267+
}
268+
stats_info.update(attrs)
269+
270+
# Set default method.
271+
hypervisor_stats_method = {'to_dict': stats_info}
272+
hypervisor_stats_method.update(methods)
273+
274+
hypervisor_stats = fakes.FakeResource(
275+
info=copy.deepcopy(stats_info),
276+
methods=copy.deepcopy(hypervisor_stats_method),
277+
loaded=True)
278+
return hypervisor_stats
279+
280+
@staticmethod
281+
def create_hypervisors_stats(attrs={}, count=2):
282+
"""Create multiple fake hypervisors stats.
283+
284+
:param Dictionary attrs:
285+
A dictionary with all attributes
286+
:param int count:
287+
The number of hypervisors to fake
288+
:return:
289+
A list of FakeResource objects faking the hypervisors
290+
"""
291+
hypervisors = []
292+
for i in range(0, count):
293+
hypervisors.append(
294+
FakehypervisorStats.create_one_hypervisor_stats(attrs))
295+
296+
return hypervisors
297+
298+
238299
class FakeSecurityGroupRule(object):
239300
"""Fake one or more security group rules."""
240301

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2016 EasyStack Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
#
15+
16+
from openstackclient.compute.v2 import hypervisor_stats
17+
from openstackclient.tests.compute.v2 import fakes as compute_fakes
18+
19+
20+
class TestHypervisorStats(compute_fakes.TestComputev2):
21+
22+
def setUp(self):
23+
super(TestHypervisorStats, self).setUp()
24+
25+
# Get a shortcut to the compute client hypervisors mock
26+
self.hypervisors_mock = self.app.client_manager.compute.hypervisors
27+
self.hypervisors_mock.reset_mock()
28+
29+
30+
class TestHypervisorStatsShow(TestHypervisorStats):
31+
32+
def setUp(self):
33+
super(TestHypervisorStatsShow, self).setUp()
34+
35+
self.hypervisor_stats = \
36+
compute_fakes.FakehypervisorStats.create_one_hypervisor_stats()
37+
38+
self.hypervisors_mock.statistics.return_value =\
39+
self.hypervisor_stats
40+
41+
self.cmd = hypervisor_stats.ShowHypervisorStats(self.app, None)
42+
43+
self.columns = (
44+
'count',
45+
'current_workload',
46+
'disk_available_least',
47+
'free_disk_gb',
48+
'free_ram_mb',
49+
'local_gb',
50+
'local_gb_used',
51+
'memory_mb',
52+
'memory_mb_used',
53+
'running_vms',
54+
'vcpus',
55+
'vcpus_used',
56+
)
57+
58+
self.data = (
59+
2,
60+
0,
61+
50,
62+
100,
63+
23000,
64+
100,
65+
0,
66+
23800,
67+
1400,
68+
3,
69+
8,
70+
3,
71+
)
72+
73+
def test_hypervisor_show_stats(self):
74+
arglist = []
75+
verifylist = []
76+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
77+
columns, data = self.cmd.take_action(parsed_args)
78+
self.assertEqual(self.columns, columns)
79+
self.assertEqual(self.data, data)

0 commit comments

Comments
 (0)