Skip to content

Commit 05b1874

Browse files
author
Tang Chen
committed
Add unit tests for "hypervisor list" command
There is no unit tests for "hypervisor" command. This patch introudces a new class FakeHypervisor to fake one or more hypervisors, and a base class TestHypervisor. Also adds hypervisors mock to fake compute client. And also, this patch adds unit tests for "hypervisor list" command. Change-Id: I18733eae1a8f4fff72e830d9a060fb8f0f58fbf5
1 parent 42b607e commit 05b1874

2 files changed

Lines changed: 160 additions & 0 deletions

File tree

openstackclient/tests/compute/v2/fakes.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def __init__(self, **kwargs):
106106
self.quota_classes.resource_class = fakes.FakeResource(None, {})
107107
self.volumes = mock.Mock()
108108
self.volumes.resource_class = fakes.FakeResource(None, {})
109+
self.hypervisors = mock.Mock()
110+
self.hypervisors.resource_class = fakes.FakeResource(None, {})
109111
self.auth_token = kwargs['token']
110112
self.management_url = kwargs['endpoint']
111113

@@ -140,6 +142,49 @@ def setUp(self):
140142
)
141143

142144

145+
class FakeHypervisor(object):
146+
"""Fake one or more hypervisor."""
147+
148+
@staticmethod
149+
def create_one_hypervisor(attrs={}):
150+
"""Create a fake hypervisor.
151+
152+
:param Dictionary attrs:
153+
A dictionary with all attributes
154+
:return:
155+
A FakeResource object, with id, hypervisor_hostname, and so on
156+
"""
157+
# Set default attributes.
158+
hypervisor_info = {
159+
'id': 'hypervisor-id-' + uuid.uuid4().hex,
160+
'hypervisor_hostname': 'hypervisor-hostname-' + uuid.uuid4().hex,
161+
}
162+
163+
# Overwrite default attributes.
164+
hypervisor_info.update(attrs)
165+
166+
hypervisor = fakes.FakeResource(info=copy.deepcopy(hypervisor_info),
167+
loaded=True)
168+
return hypervisor
169+
170+
@staticmethod
171+
def create_hypervisors(attrs={}, count=2):
172+
"""Create multiple fake hypervisors.
173+
174+
:param Dictionary attrs:
175+
A dictionary with all attributes
176+
:param int count:
177+
The number of hypervisors to fake
178+
:return:
179+
A list of FakeResource objects faking the hypervisors
180+
"""
181+
hypervisors = []
182+
for i in range(0, count):
183+
hypervisors.append(FakeHypervisor.create_one_hypervisor(attrs))
184+
185+
return hypervisors
186+
187+
143188
class FakeServer(object):
144189
"""Fake one or more compute servers."""
145190

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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.common import exceptions
17+
from openstackclient.compute.v2 import hypervisor
18+
from openstackclient.tests.compute.v2 import fakes as compute_fakes
19+
20+
21+
class TestHypervisor(compute_fakes.TestComputev2):
22+
23+
def setUp(self):
24+
super(TestHypervisor, self).setUp()
25+
26+
# Get a shortcut to the compute client hypervisors mock
27+
self.hypervisors_mock = self.app.client_manager.compute.hypervisors
28+
self.hypervisors_mock.reset_mock()
29+
30+
31+
class TestHypervisorList(TestHypervisor):
32+
33+
def setUp(self):
34+
super(TestHypervisorList, self).setUp()
35+
36+
# Fake hypervisors to be listed up
37+
self.hypervisors = compute_fakes.FakeHypervisor.create_hypervisors()
38+
self.hypervisors_mock.list.return_value = self.hypervisors
39+
40+
self.columns = (
41+
"ID",
42+
"Hypervisor Hostname"
43+
)
44+
self.data = (
45+
(
46+
self.hypervisors[0].id,
47+
self.hypervisors[0].hypervisor_hostname,
48+
),
49+
(
50+
self.hypervisors[1].id,
51+
self.hypervisors[1].hypervisor_hostname,
52+
),
53+
)
54+
55+
# Get the command object to test
56+
self.cmd = hypervisor.ListHypervisor(self.app, None)
57+
58+
def test_hypervisor_list_no_option(self):
59+
arglist = []
60+
verifylist = []
61+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
62+
63+
# In base command class Lister in cliff, abstractmethod take_action()
64+
# returns a tuple containing the column names and an iterable
65+
# containing the data to be listed.
66+
columns, data = self.cmd.take_action(parsed_args)
67+
68+
self.hypervisors_mock.list.assert_called_with()
69+
self.assertEqual(self.columns, columns)
70+
self.assertEqual(self.data, tuple(data))
71+
72+
def test_hypervisor_list_matching_option_found(self):
73+
arglist = [
74+
'--matching', self.hypervisors[0].hypervisor_hostname,
75+
]
76+
verifylist = [
77+
('matching', self.hypervisors[0].hypervisor_hostname),
78+
]
79+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
80+
81+
# Fake the return value of search()
82+
self.hypervisors_mock.search.return_value = [self.hypervisors[0]]
83+
self.data = (
84+
(
85+
self.hypervisors[0].id,
86+
self.hypervisors[0].hypervisor_hostname,
87+
),
88+
)
89+
90+
# In base command class Lister in cliff, abstractmethod take_action()
91+
# returns a tuple containing the column names and an iterable
92+
# containing the data to be listed.
93+
columns, data = self.cmd.take_action(parsed_args)
94+
95+
self.hypervisors_mock.search.assert_called_with(
96+
self.hypervisors[0].hypervisor_hostname
97+
)
98+
self.assertEqual(self.columns, columns)
99+
self.assertEqual(self.data, tuple(data))
100+
101+
def test_hypervisor_list_matching_option_not_found(self):
102+
arglist = [
103+
'--matching', 'xxx',
104+
]
105+
verifylist = [
106+
('matching', 'xxx'),
107+
]
108+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
109+
110+
# Fake exception raised from search()
111+
self.hypervisors_mock.search.side_effect = exceptions.NotFound(None)
112+
113+
self.assertRaises(exceptions.NotFound,
114+
self.cmd.take_action,
115+
parsed_args)

0 commit comments

Comments
 (0)