Skip to content

Commit 7b1febf

Browse files
jiahui.qiangstevemar
authored andcommitted
Add unit tests for usage commands in compute v2
Add unit tests and fakes for command below in compute v2: usage list usage show Change-Id: Ie533e23375ca6b8ba4cb7e865d39fac652cc0195
1 parent 5d62981 commit 7b1febf

2 files changed

Lines changed: 244 additions & 0 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ def __init__(self, **kwargs):
168168
self.quota_classes = mock.Mock()
169169
self.quota_classes.resource_class = fakes.FakeResource(None, {})
170170

171+
self.usage = mock.Mock()
172+
self.usage.resource_class = fakes.FakeResource(None, {})
173+
171174
self.volumes = mock.Mock()
172175
self.volumes.resource_class = fakes.FakeResource(None, {})
173176

@@ -1248,3 +1251,65 @@ def create_one_server_group(attrs=None):
12481251
info=copy.deepcopy(server_group_info),
12491252
loaded=True)
12501253
return server_group
1254+
1255+
1256+
class FakeUsage(object):
1257+
"""Fake one or more usage."""
1258+
1259+
@staticmethod
1260+
def create_one_usage(attrs=None):
1261+
"""Create a fake usage.
1262+
1263+
:param Dictionary attrs:
1264+
A dictionary with all attributes
1265+
:return:
1266+
A FakeResource object, with tenant_id and other attributes
1267+
"""
1268+
if attrs is None:
1269+
attrs = {}
1270+
1271+
# Set default attributes.
1272+
usage_info = {
1273+
'tenant_id': 'usage-tenant-id-' + uuid.uuid4().hex,
1274+
'total_memory_mb_usage': 512.0,
1275+
'total_vcpus_usage': 1.0,
1276+
'total_local_gb_usage': 1.0,
1277+
'server_usages': [
1278+
{
1279+
'ended_at': None,
1280+
'flavor': 'usage-flavor-' + uuid.uuid4().hex,
1281+
'hours': 1.0,
1282+
'local_gb': 1,
1283+
'memory_mb': 512,
1284+
'name': 'usage-name-' + uuid.uuid4().hex,
1285+
'state': 'active',
1286+
'uptime': 3600,
1287+
'vcpus': 1
1288+
}
1289+
]
1290+
}
1291+
1292+
# Overwrite default attributes.
1293+
usage_info.update(attrs)
1294+
1295+
usage = fakes.FakeResource(info=copy.deepcopy(usage_info),
1296+
loaded=True)
1297+
1298+
return usage
1299+
1300+
@staticmethod
1301+
def create_usages(attrs=None, count=2):
1302+
"""Create multiple fake services.
1303+
1304+
:param Dictionary attrs:
1305+
A dictionary with all attributes
1306+
:param int count:
1307+
The number of services to fake
1308+
:return:
1309+
A list of FakeResource objects faking the services
1310+
"""
1311+
usages = []
1312+
for i in range(0, count):
1313+
usages.append(FakeUsage.create_one_usage(attrs))
1314+
1315+
return usages
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
import datetime
15+
import mock
16+
17+
from openstackclient.compute.v2 import usage
18+
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
19+
from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes
20+
21+
22+
class TestUsage(compute_fakes.TestComputev2):
23+
24+
def setUp(self):
25+
super(TestUsage, self).setUp()
26+
27+
self.usage_mock = self.app.client_manager.compute.usage
28+
self.usage_mock.reset_mock()
29+
30+
self.projects_mock = self.app.client_manager.identity.projects
31+
self.projects_mock.reset_mock()
32+
33+
34+
class TestUsageList(TestUsage):
35+
36+
project = identity_fakes.FakeProject.create_one_project()
37+
# Return value of self.usage_mock.list().
38+
usages = compute_fakes.FakeUsage.create_usages(
39+
attrs={'tenant_id': project.name}, count=1)
40+
41+
columns = (
42+
"Project",
43+
"Servers",
44+
"RAM MB-Hours",
45+
"CPU Hours",
46+
"Disk GB-Hours"
47+
)
48+
49+
data = [(
50+
usages[0].tenant_id,
51+
len(usages[0].server_usages),
52+
float("%.2f" % usages[0].total_memory_mb_usage),
53+
float("%.2f" % usages[0].total_vcpus_usage),
54+
float("%.2f" % usages[0].total_local_gb_usage),
55+
)]
56+
57+
def setUp(self):
58+
super(TestUsageList, self).setUp()
59+
60+
self.usage_mock.list.return_value = self.usages
61+
62+
self.projects_mock.list.return_value = [self.project]
63+
# Get the command object to test
64+
self.cmd = usage.ListUsage(self.app, None)
65+
66+
def test_usage_list_no_options(self):
67+
68+
arglist = []
69+
verifylist = [
70+
('start', None),
71+
('end', None),
72+
]
73+
74+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
75+
76+
columns, data = self.cmd.take_action(parsed_args)
77+
78+
self.projects_mock.list.assert_called_with()
79+
80+
self.assertEqual(self.columns, columns)
81+
self.assertEqual(tuple(self.data), tuple(data))
82+
83+
def test_usage_list_with_options(self):
84+
arglist = [
85+
'--start', '2016-11-11',
86+
'--end', '2016-12-20',
87+
]
88+
verifylist = [
89+
('start', '2016-11-11'),
90+
('end', '2016-12-20'),
91+
]
92+
93+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
94+
95+
columns, data = self.cmd.take_action(parsed_args)
96+
97+
self.projects_mock.list.assert_called_with()
98+
self.usage_mock.list.assert_called_with(
99+
datetime.datetime(2016, 11, 11, 0, 0),
100+
datetime.datetime(2016, 12, 20, 0, 0),
101+
detailed=True)
102+
103+
self.assertEqual(self.columns, columns)
104+
self.assertEqual(tuple(self.data), tuple(data))
105+
106+
107+
class TestUsageShow(TestUsage):
108+
109+
project = identity_fakes.FakeProject.create_one_project()
110+
# Return value of self.usage_mock.list().
111+
usage = compute_fakes.FakeUsage.create_one_usage(
112+
attrs={'tenant_id': project.name})
113+
114+
columns = (
115+
'CPU Hours',
116+
'Disk GB-Hours',
117+
'RAM MB-Hours',
118+
'Servers',
119+
)
120+
121+
data = (
122+
float("%.2f" % usage.total_vcpus_usage),
123+
float("%.2f" % usage.total_local_gb_usage),
124+
float("%.2f" % usage.total_memory_mb_usage),
125+
len(usage.server_usages),
126+
)
127+
128+
def setUp(self):
129+
super(TestUsageShow, self).setUp()
130+
131+
self.usage_mock.get.return_value = self.usage
132+
133+
self.projects_mock.get.return_value = self.project
134+
# Get the command object to test
135+
self.cmd = usage.ShowUsage(self.app, None)
136+
137+
def test_usage_show_no_options(self):
138+
139+
self.app.client_manager.auth_ref = mock.Mock()
140+
self.app.client_manager.auth_ref.project_id = self.project.id
141+
142+
arglist = []
143+
verifylist = [
144+
('project', None),
145+
('start', None),
146+
('end', None),
147+
]
148+
149+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
150+
151+
columns, data = self.cmd.take_action(parsed_args)
152+
153+
self.assertEqual(self.columns, columns)
154+
self.assertEqual(self.data, data)
155+
156+
def test_usage_show_with_options(self):
157+
158+
arglist = [
159+
'--project', self.project.id,
160+
'--start', '2016-11-11',
161+
'--end', '2016-12-20',
162+
]
163+
verifylist = [
164+
('project', self.project.id),
165+
('start', '2016-11-11'),
166+
('end', '2016-12-20'),
167+
]
168+
169+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
170+
171+
columns, data = self.cmd.take_action(parsed_args)
172+
173+
self.usage_mock.get.assert_called_with(
174+
self.project.id,
175+
datetime.datetime(2016, 11, 11, 0, 0),
176+
datetime.datetime(2016, 12, 20, 0, 0))
177+
178+
self.assertEqual(self.columns, columns)
179+
self.assertEqual(self.data, data)

0 commit comments

Comments
 (0)