Skip to content

Commit 5951026

Browse files
author
Huanxuan Ao
committed
Add unit tests for "host list" and "host show" commands
Missing unit tests for "host list" and "host show" commands in computev2, this patch add them. Change-Id: Ib157920fa2267ec96d206cdf46213563a105501b
1 parent 21ac923 commit 5951026

2 files changed

Lines changed: 111 additions & 1 deletion

File tree

openstackclient/tests/compute/v2/fakes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,13 @@ def create_one_host(attrs=None):
10881088
"stats": "",
10891089
"numa_topology": "",
10901090
"ram_allocation_ratio": 1.0,
1091-
"cpu_allocation_ratio": 1.0
1091+
"cpu_allocation_ratio": 1.0,
1092+
"zone": 'zone-' + uuid.uuid4().hex,
1093+
"host_name": 'name-' + uuid.uuid4().hex,
1094+
"service": 'service-' + uuid.uuid4().hex,
1095+
"cpu": 4,
1096+
"disk_gb": 100,
1097+
'project': 'project-' + uuid.uuid4().hex,
10921098
}
10931099
host_info.update(attrs)
10941100
host = fakes.FakeResource(

openstackclient/tests/compute/v2/test_host.py

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

1616
from openstackclient.compute.v2 import host
1717
from openstackclient.tests.compute.v2 import fakes as compute_fakes
18+
from openstackclient.tests import utils as tests_utils
1819

1920

2021
class TestHost(compute_fakes.TestComputev2):
@@ -27,6 +28,58 @@ def setUp(self):
2728
self.host_mock.reset_mock()
2829

2930

31+
class TestHostList(TestHost):
32+
33+
host = compute_fakes.FakeHost.create_one_host()
34+
35+
columns = (
36+
'Host Name',
37+
'Service',
38+
'Zone',
39+
)
40+
41+
data = [(
42+
host.host_name,
43+
host.service,
44+
host.zone,
45+
)]
46+
47+
def setUp(self):
48+
super(TestHostList, self).setUp()
49+
50+
self.host_mock.list_all.return_value = [self.host]
51+
52+
self.cmd = host.ListHost(self.app, None)
53+
54+
def test_host_list_no_option(self):
55+
arglist = []
56+
verifylist = []
57+
58+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
59+
60+
columns, data = self.cmd.take_action(parsed_args)
61+
62+
self.host_mock.list_all.assert_called_with(None)
63+
self.assertEqual(self.columns, columns)
64+
self.assertEqual(self.data, list(data))
65+
66+
def test_host_list_with_option(self):
67+
arglist = [
68+
'--zone', self.host.zone,
69+
]
70+
verifylist = [
71+
('zone', self.host.zone),
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.host_mock.list_all.assert_called_with(self.host.zone)
79+
self.assertEqual(self.columns, columns)
80+
self.assertEqual(self.data, list(data))
81+
82+
3083
class TestHostSet(TestHost):
3184

3285
def setUp(self):
@@ -73,3 +126,54 @@ def test_host_set(self):
73126

74127
body = {'status': 'enable', 'maintenance_mode': 'disable'}
75128
self.host_mock.update.assert_called_with(self.host.host, body)
129+
130+
131+
class TestHostShow(TestHost):
132+
133+
host = compute_fakes.FakeHost.create_one_host()
134+
135+
columns = (
136+
'Host',
137+
'Project',
138+
'CPU',
139+
'Memory MB',
140+
'Disk GB',
141+
)
142+
data = [(
143+
host.host,
144+
host.project,
145+
host.cpu,
146+
host.memory_mb,
147+
host.disk_gb,
148+
)]
149+
150+
def setUp(self):
151+
super(TestHostShow, self).setUp()
152+
153+
self.host_mock.get.return_value = [self.host]
154+
155+
self.cmd = host.ShowHost(self.app, None)
156+
157+
def test_host_show_no_option(self):
158+
arglist = []
159+
verifylist = []
160+
161+
# Missing required args should bail here
162+
self.assertRaises(tests_utils.ParserException, self.check_parser,
163+
self.cmd, arglist, verifylist)
164+
165+
def test_host_show_with_option(self):
166+
arglist = [
167+
self.host.host_name,
168+
]
169+
verifylist = [
170+
('host', self.host.host_name),
171+
]
172+
173+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
174+
175+
columns, data = self.cmd.take_action(parsed_args)
176+
177+
self.host_mock.get.assert_called_with(self.host.host_name)
178+
self.assertEqual(self.columns, columns)
179+
self.assertEqual(self.data, list(data))

0 commit comments

Comments
 (0)