Skip to content

Commit 6cc2d4a

Browse files
committed
Add unit tests for coriolisclient.cli.shell module
1 parent b6f7075 commit 6cc2d4a

File tree

1 file changed

+356
-0
lines changed

1 file changed

+356
-0
lines changed
Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
1+
# Copyright 2024 Cloudbase Solutions Srl
2+
# All Rights Reserved.
3+
4+
import ddt
5+
from unittest import mock
6+
7+
from cliff import app
8+
from keystoneauth1 import loading
9+
from keystoneauth1 import session
10+
11+
from coriolisclient.cli import shell
12+
from coriolisclient import client
13+
from coriolisclient.tests import test_base
14+
15+
16+
class CustomMock(mock.MagicMock):
17+
def __getattr__(self, name):
18+
return None
19+
20+
21+
@ddt.ddt
22+
class CoriolisTestCase(test_base.CoriolisBaseTestCase):
23+
"""Test suite for the Coriolis Client Diagnostics."""
24+
25+
def setUp(self):
26+
super(CoriolisTestCase, self).setUp()
27+
self.coriolis = shell.Coriolis()
28+
29+
@ddt.data(
30+
{
31+
"args": {
32+
"os_project_id": "mock_os_project_id",
33+
"os_project_name": "mock_os_project_name",
34+
"os_project_domain_name": "mock_os_project_domain_name",
35+
"os_project_domain_id": "mock_os_project_domain_id",
36+
"os_tenant_id": "mock_os_tenant_id",
37+
"os_tenant_name": "mock_os_tenant_name"
38+
},
39+
"api_version": '3',
40+
"raise_exc": False,
41+
"expected_result": True
42+
},
43+
{
44+
"args": {
45+
"os_project_id": "mock_os_project_id",
46+
"os_project_name": "mock_os_project_name",
47+
"os_project_domain_name": "mock_os_project_domain_name",
48+
"os_project_domain_id": "mock_os_project_domain_id",
49+
},
50+
"api_version": '3',
51+
"raise_exc": False,
52+
"expected_result": True
53+
},
54+
{
55+
"args": {
56+
"os_tenant_id": "mock_os_tenant_id",
57+
"os_tenant_name": "mock_os_tenant_name"
58+
},
59+
"api_version": '2',
60+
"raise_exc": False,
61+
"expected_result": True
62+
},
63+
{
64+
"raise_exc": False,
65+
"expected_result": False
66+
},
67+
{
68+
"api_version": '2',
69+
"raise_exc": True,
70+
}
71+
)
72+
def test_check_auth_arguments(self, data):
73+
args = CustomMock()
74+
for key, value in data.get("args", {}).items():
75+
setattr(args, key, value)
76+
expected_result = data.get("expected_result")
77+
78+
if expected_result is not None:
79+
result = self.coriolis.check_auth_arguments(
80+
args,
81+
api_version=data.get("api_version", None),
82+
raise_exc=data.get("raise_exc", False)
83+
)
84+
85+
self.assertEqual(
86+
expected_result,
87+
result
88+
)
89+
else:
90+
self.assertRaises( # noqa: H202
91+
Exception,
92+
self.coriolis.check_auth_arguments,
93+
args,
94+
data.get("api_version", None),
95+
raise_exc=data.get("raise_exc", False)
96+
)
97+
98+
@ddt.data(
99+
{
100+
"args": {
101+
"os_tenant_id": "mock_os_tenant_id",
102+
"os_tenant_name": "mock_os_tenant_name"
103+
},
104+
"kwargs": {
105+
"os_tenant_name": "new_mock_os_tenant_name"
106+
},
107+
"api_version": '2',
108+
"auth_type": 'token',
109+
"auth_fun": 'keystoneauth1.identity.v2.Token'
110+
},
111+
{
112+
"args": {
113+
"os_tenant_id": "mock_os_tenant_id",
114+
"os_tenant_name": "mock_os_tenant_name"
115+
},
116+
"kwargs": {
117+
"os_tenant_name": "new_mock_os_tenant_name"
118+
},
119+
"api_version": '2',
120+
"auth_fun": 'keystoneauth1.identity.v2.Password'
121+
},
122+
{
123+
"args": {
124+
"os_project_id": "mock_os_project_id",
125+
"os_project_name": "mock_os_project_name",
126+
"os_project_domain_name": "mock_os_project_domain_name",
127+
"os_project_domain_id": "mock_os_project_domain_id",
128+
},
129+
"kwargs": {
130+
"os_project_domain_name": "new_mock_os_project_domain_name",
131+
"os_project_domain_id": "new_mock_os_project_domain_id",
132+
},
133+
"api_version": '3',
134+
"auth_type": 'token',
135+
"auth_fun": 'keystoneauth1.identity.v3.Token'
136+
},
137+
{
138+
"args": {
139+
"os_project_id": "mock_os_project_id",
140+
"os_project_name": "mock_os_project_name",
141+
"os_project_domain_name": "mock_os_project_domain_name",
142+
"os_project_domain_id": "mock_os_project_domain_id",
143+
},
144+
"kwargs": {
145+
"os_project_domain_name": "new_mock_os_project_domain_name",
146+
"os_project_domain_id": "new_mock_os_project_domain_id",
147+
},
148+
"auth_fun": 'keystoneauth1.identity.v3.Password',
149+
"err_msg": True
150+
}
151+
)
152+
@mock.patch.object(session, 'Session')
153+
@mock.patch.object(shell.Coriolis, 'build_kwargs_based_on_version')
154+
@mock.patch.object(shell.Coriolis, 'check_auth_arguments')
155+
def test_create_keystone_session(
156+
self,
157+
data,
158+
mock_check_auth_arguments,
159+
mock_build_kwargs_based_on_version,
160+
mock_Session
161+
):
162+
args = CustomMock()
163+
for key, value in data.get("args", {}).items():
164+
setattr(args, key, value)
165+
kwargs = data.get("kwargs", {})
166+
ret_args = data.get("args", {})
167+
mock_build_kwargs_based_on_version.return_value = ret_args.copy()
168+
expected_kwargs = ret_args.copy()
169+
expected_kwargs.update(kwargs.copy())
170+
mock_stderr = mock.Mock()
171+
self.coriolis.stderr = mock_stderr
172+
173+
auth_fun = data['auth_fun']
174+
with mock.patch(auth_fun) as mock_auth:
175+
result = self.coriolis.create_keystone_session(
176+
args,
177+
api_version=data.get("api_version", None),
178+
kwargs_dict=kwargs.copy(),
179+
auth_type=data.get("auth_type", None)
180+
)
181+
182+
self.assertEqual(
183+
mock_Session.return_value,
184+
result
185+
)
186+
mock_check_auth_arguments.assert_called_once_with(
187+
args,
188+
data.get("api_version", None),
189+
raise_exc=True
190+
)
191+
mock_build_kwargs_based_on_version.assert_called_once_with(
192+
args,
193+
data.get("api_version", None)
194+
)
195+
mock_auth.assert_called_once_with(**expected_kwargs)
196+
if data.get("err_msg", False):
197+
mock_stderr.write.assert_called_once()
198+
else:
199+
mock_stderr.write.assert_not_called()
200+
201+
@ddt.data(
202+
{
203+
"args": {
204+
"no_auth": "mock_no_auth",
205+
"os_auth_url": "mock_os_auth_url"
206+
}
207+
},
208+
{
209+
"args": {
210+
"no_auth": "mock_no_auth",
211+
"endpoint": "mock_endpoint"
212+
}
213+
},
214+
{
215+
"args": {
216+
"no_auth": "mock_no_auth",
217+
"endpoint": "mock_endpoint",
218+
"os_tenant_id": "mock_os_tenant_id"
219+
},
220+
"call_args": {
221+
"endpoint": "mock_endpoint",
222+
"project_id": "mock_os_tenant_id",
223+
"verify": True
224+
}
225+
},
226+
{
227+
"args": {
228+
"os_auth_token": "mock_os_auth_token"
229+
}
230+
},
231+
{
232+
"args": {
233+
"os_auth_token": "mock_os_auth_token",
234+
"os_auth_url": "mock_os_auth_url",
235+
"os_tenant_id": "mock_os_tenant_id",
236+
"endpoint": "mock_endpoint"
237+
},
238+
"call_args": {
239+
"endpoint": "mock_endpoint"
240+
},
241+
"create_keystone_session_args": {
242+
"auth_url": "mock_os_auth_url",
243+
"token": "mock_os_auth_token"
244+
},
245+
"auth_type": "token"
246+
},
247+
{
248+
"args": {
249+
"os_auth_url": "mock_os_auth_url",
250+
"os_password": "mock_os_password",
251+
"os_user_id": "mock_os_user_id",
252+
"os_username": "mock_os_username",
253+
"endpoint": "mock_endpoint"
254+
},
255+
"call_args": {
256+
"endpoint": "mock_endpoint"
257+
},
258+
"create_keystone_session_args": {
259+
"auth_url": "mock_os_auth_url",
260+
"password": "mock_os_password",
261+
"user_id": "mock_os_user_id",
262+
"username": "mock_os_username",
263+
},
264+
"auth_type": "password"
265+
},
266+
{}
267+
)
268+
@mock.patch.object(shell.Coriolis, 'create_keystone_session')
269+
@mock.patch.object(client, 'Client')
270+
@mock.patch.object(shell.Coriolis, '_get_endpoint_filter_kwargs')
271+
def test_create_client(
272+
self,
273+
data,
274+
mock_get_endpoint_filter_kwargs,
275+
mock_Client,
276+
mock_create_keystone_session
277+
):
278+
args = CustomMock()
279+
args.os_identity_api_version = mock.sentinel.os_identity_api_version
280+
for key, value in data.get("args", {}).items():
281+
setattr(args, key, value)
282+
endpoint_filter_kwargs = {"filter_arg": "mock_filter_arg"}
283+
mock_get_endpoint_filter_kwargs.return_value = endpoint_filter_kwargs
284+
call_args = data.get("call_args", None)
285+
create_keystone_session_args = data.get(
286+
"create_keystone_session_args", None)
287+
288+
if call_args is not None:
289+
call_args.update(endpoint_filter_kwargs)
290+
291+
result = self.coriolis.create_client(args)
292+
293+
self.assertEqual(
294+
mock_Client.return_value,
295+
result
296+
)
297+
298+
if create_keystone_session_args is None:
299+
mock_Client.assert_called_once_with(**call_args)
300+
else:
301+
mock_create_keystone_session.assert_called_once_with(
302+
args,
303+
mock.sentinel.os_identity_api_version,
304+
create_keystone_session_args,
305+
auth_type=data["auth_type"]
306+
)
307+
mock_Client.assert_called_once_with(
308+
session=mock_create_keystone_session.return_value,
309+
**call_args
310+
)
311+
else:
312+
self.assertRaises( # noqa: H202
313+
Exception,
314+
self.coriolis.create_client,
315+
args
316+
)
317+
318+
def test_build_kwargs_based_on_version(self):
319+
args = CustomMock()
320+
args.os_project_id = mock.sentinel.os_project_id
321+
args.os_tenant_name = mock.sentinel.os_tenant_name
322+
323+
result = self.coriolis.build_kwargs_based_on_version(args)
324+
325+
self.assertEqual(
326+
{'project_id': mock.sentinel.os_project_id},
327+
result
328+
)
329+
330+
api_version = '2'
331+
332+
result = self.coriolis.build_kwargs_based_on_version(args, api_version)
333+
334+
self.assertEqual(
335+
{'tenant_name': mock.sentinel.os_tenant_name},
336+
result
337+
)
338+
339+
@mock.patch.object(loading, 'register_session_argparse_arguments')
340+
@mock.patch.object(app.App, 'build_option_parser')
341+
def test_build_option_parser(
342+
self,
343+
mock_build_option_parser,
344+
mock_register_session_argparse_arguments
345+
):
346+
result = self.coriolis.build_option_parser(
347+
mock.sentinel.description, mock.sentinel.version)
348+
349+
self.assertEqual(
350+
mock_build_option_parser.return_value,
351+
result
352+
)
353+
mock_build_option_parser.assert_called_once_with(
354+
mock.sentinel.description, mock.sentinel.version, None)
355+
mock_register_session_argparse_arguments.assert_called_once_with(
356+
mock_build_option_parser.return_value)

0 commit comments

Comments
 (0)