Skip to content

Commit b2ad9dc

Browse files
committed
Add unit tests for coriolisclient.v1.licensing.py module
1 parent b14cf98 commit b2ad9dc

File tree

1 file changed

+371
-0
lines changed

1 file changed

+371
-0
lines changed
Lines changed: 371 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,371 @@
1+
# Copyright 2024 Cloudbase Solutions Srl
2+
# All Rights Reserved.
3+
4+
from unittest import mock
5+
6+
import requests
7+
8+
from coriolisclient import exceptions
9+
from coriolisclient.tests import test_base
10+
from coriolisclient.v1 import licensing
11+
12+
13+
class LicensingClientTestCase(
14+
test_base.CoriolisBaseTestCase):
15+
"""Test suite for the Coriolis v1 Licensing Client."""
16+
17+
def setUp(self):
18+
mock_client = mock.Mock()
19+
super(LicensingClientTestCase, self).setUp()
20+
self.licence = licensing.LicensingClient(
21+
mock_client, "endpoint_name")
22+
mock_client.verify = True
23+
self.licence._cli = mock_client
24+
25+
def test_get_licensing_endpoint_url(self):
26+
self.licence._cli.get_endpoint.return_value = "url/endpoint_url/"
27+
28+
result = self.licence._get_licensing_endpoint_url()
29+
30+
self.assertEqual(
31+
"url/endpoint_url",
32+
result
33+
)
34+
self.licence._cli.get_endpoint.assert_called_once_with(
35+
service_type="endpoint_name")
36+
37+
def test_get_licensing_endpoint_url_raises(self):
38+
self.licence._cli.get_endpoint.side_effect = Exception()
39+
40+
with self.assertLogs(level="WARN"):
41+
self.assertRaises(
42+
exceptions.LicensingEndpointNotFound,
43+
self.licence._get_licensing_endpoint_url
44+
)
45+
self.licence._cli.get_endpoint.assert_called_once_with(
46+
service_type="endpoint_name")
47+
48+
@mock.patch.object(licensing.LicensingClient,
49+
"_get_licensing_endpoint_url")
50+
def test_do_req_raw(
51+
self,
52+
mock_get_licensing_endpoint_url
53+
):
54+
mock_method = mock.Mock()
55+
mock_resp = mock.Mock()
56+
mock_resp.ok = True
57+
mock_method.return_value = mock_resp
58+
setattr(requests, "mock_method", mock_method)
59+
mock_get_licensing_endpoint_url.return_value = 'url/endpoint_url/'
60+
result = self.licence._do_req(
61+
method_name="mock_method",
62+
resource='url/resource_url/',
63+
body=None,
64+
response_key=None,
65+
raw_response=True
66+
)
67+
68+
self.assertEqual(
69+
mock_resp,
70+
result
71+
)
72+
mock_method.assert_called_once_with(
73+
'url/endpoint_url/url/resource_url/',
74+
verify=self.licence._cli.verify
75+
)
76+
77+
@mock.patch.object(licensing.LicensingClient,
78+
"_get_licensing_endpoint_url")
79+
def test_do_req_json(
80+
self,
81+
mock_get_licensing_endpoint_url
82+
):
83+
mock_method = mock.Mock()
84+
mock_resp = mock.Mock()
85+
mock_resp.ok = True
86+
mock_resp.json.return_value = {"response_key": mock.sentinel.data}
87+
mock_method.return_value = mock_resp
88+
setattr(requests, "mock_method", mock_method)
89+
mock_get_licensing_endpoint_url.return_value = 'url/endpoint_url/'
90+
result = self.licence._do_req(
91+
method_name="mock_method",
92+
resource='url/resource_url/',
93+
body={"mock_body": "value"},
94+
response_key='response_key',
95+
raw_response=False
96+
)
97+
98+
self.assertEqual(
99+
mock.sentinel.data,
100+
result
101+
)
102+
mock_method.assert_called_once_with(
103+
'url/endpoint_url/url/resource_url/',
104+
verify=self.licence._cli.verify,
105+
data='{"mock_body": "value"}'
106+
)
107+
108+
@mock.patch.object(licensing.LicensingClient,
109+
"_get_licensing_endpoint_url")
110+
def test_do_req_error(
111+
self,
112+
mock_get_licensing_endpoint_url
113+
):
114+
mock_method = mock.Mock()
115+
mock_resp = mock.Mock()
116+
mock_resp.ok = False
117+
mock_resp.json.side_effect = Exception
118+
mock_resp.raise_for_status.side_effect = exceptions.CoriolisException
119+
mock_method.return_value = mock_resp
120+
setattr(requests, "mock_method", mock_method)
121+
mock_get_licensing_endpoint_url.return_value = 'url/endpoint_url/'
122+
123+
with self.assertLogs(level="DEBUG"):
124+
self.assertRaises(
125+
exceptions.CoriolisException,
126+
self.licence._do_req,
127+
method_name="mock_method",
128+
resource='url/resource_url/',
129+
body=None,
130+
response_key='response_key',
131+
raw_response=False
132+
)
133+
mock_method.assert_called_once_with(
134+
'url/endpoint_url/url/resource_url/',
135+
verify=self.licence._cli.verify
136+
)
137+
138+
@mock.patch.object(licensing.LicensingClient,
139+
"_get_licensing_endpoint_url")
140+
def test_do_req_http_error(
141+
self,
142+
mock_get_licensing_endpoint_url
143+
):
144+
mock_method = mock.Mock()
145+
mock_resp = mock.Mock()
146+
mock_resp.ok = False
147+
mock_resp.json.return_value = {"error": {"code": 123, "message": ""}}
148+
mock_method.return_value = mock_resp
149+
setattr(requests, "mock_method", mock_method)
150+
mock_get_licensing_endpoint_url.return_value = 'url/endpoint_url/'
151+
152+
self.assertRaises(
153+
exceptions.HTTPError,
154+
self.licence._do_req,
155+
method_name="mock_method",
156+
resource='url/resource_url/',
157+
body=None,
158+
response_key='response_key',
159+
raw_response=False
160+
)
161+
mock_method.assert_called_once_with(
162+
'url/endpoint_url/url/resource_url/',
163+
verify=self.licence._cli.verify
164+
)
165+
166+
@mock.patch.object(licensing.LicensingClient,
167+
"_get_licensing_endpoint_url")
168+
def test_do_req_response_key_error(
169+
self,
170+
mock_get_licensing_endpoint_url
171+
):
172+
mock_method = mock.Mock()
173+
mock_resp = mock.Mock()
174+
mock_resp.ok = False
175+
mock_resp.json.return_value = {"response_key": mock.sentinel.data}
176+
mock_method.return_value = mock_resp
177+
setattr(requests, "mock_method", mock_method)
178+
mock_get_licensing_endpoint_url.return_value = 'url/endpoint_url/'
179+
180+
self.assertRaises(
181+
ValueError,
182+
self.licence._do_req,
183+
method_name="mock_method",
184+
resource='url/resource_url/',
185+
body=None,
186+
response_key='invalid',
187+
raw_response=False
188+
)
189+
mock_method.assert_called_once_with(
190+
'url/endpoint_url/url/resource_url/',
191+
verify=self.licence._cli.verify
192+
)
193+
194+
def test_do_req_method_error(self):
195+
setattr(requests, "mock_method", None)
196+
197+
self.assertRaises(
198+
ValueError,
199+
self.licence._do_req,
200+
method_name="mock_method",
201+
resource='url/resource_url/',
202+
body=None,
203+
response_key='invalid',
204+
raw_response=False
205+
)
206+
207+
@mock.patch.object(licensing.LicensingClient, '_do_req')
208+
def test_get(self, mock_do_req):
209+
result = self.licence.get(
210+
resource=mock.sentinel.resource,
211+
body=mock.sentinel.body,
212+
response_key=mock.sentinel.response_key,
213+
raw_response=False
214+
)
215+
self.assertEqual(
216+
mock_do_req.return_value,
217+
result
218+
)
219+
mock_do_req.assert_called_once_with(
220+
'GET',
221+
mock.sentinel.resource,
222+
response_key=mock.sentinel.response_key,
223+
body=mock.sentinel.body,
224+
raw_response=False
225+
)
226+
227+
@mock.patch.object(licensing.LicensingClient, '_do_req')
228+
def test_post(self, mock_do_req):
229+
result = self.licence.post(
230+
resource=mock.sentinel.resource,
231+
body=mock.sentinel.body,
232+
response_key=mock.sentinel.response_key,
233+
raw_response=False
234+
)
235+
self.assertEqual(
236+
mock_do_req.return_value,
237+
result
238+
)
239+
mock_do_req.assert_called_once_with(
240+
'POST',
241+
mock.sentinel.resource,
242+
response_key=mock.sentinel.response_key,
243+
body=mock.sentinel.body,
244+
raw_response=False
245+
)
246+
247+
@mock.patch.object(licensing.LicensingClient, '_do_req')
248+
def test_delete(self, mock_do_req):
249+
result = self.licence.delete(
250+
resource=mock.sentinel.resource,
251+
body=mock.sentinel.body,
252+
response_key=mock.sentinel.response_key,
253+
raw_response=False
254+
)
255+
self.assertEqual(
256+
mock_do_req.return_value,
257+
result
258+
)
259+
mock_do_req.assert_called_once_with(
260+
'DELETE',
261+
mock.sentinel.resource,
262+
response_key=mock.sentinel.response_key,
263+
body=mock.sentinel.body,
264+
raw_response=False
265+
)
266+
267+
268+
class LicensingManagerTestCase(
269+
test_base.CoriolisBaseTestCase):
270+
"""Test suite for the Coriolis v1 Licensing Client."""
271+
272+
@mock.patch.object(licensing, 'LicensingClient')
273+
def setUp(self, mock_LicensingClient):
274+
mock_client = mock.Mock()
275+
super(LicensingManagerTestCase, self).setUp()
276+
self.licence = licensing.LicensingManager(mock_client)
277+
self.licence._licensing_cli = mock_LicensingClient
278+
279+
def test_status(self):
280+
mock_resource_class = mock.Mock()
281+
self.licence.resource_class = mock_resource_class
282+
283+
result = self.licence.status(mock.sentinel.appliance_id)
284+
285+
self.assertEqual(
286+
mock_resource_class.return_value,
287+
result
288+
)
289+
self.licence._licensing_cli.get.assert_called_once_with(
290+
'/appliances/%s/status' % mock.sentinel.appliance_id,
291+
response_key='appliance_licence_status')
292+
mock_resource_class.assert_called_once_with(
293+
self.licence, self.licence._licensing_cli.get.return_value,
294+
loaded=True)
295+
296+
def test_list(self):
297+
mock_resource_class = mock.Mock()
298+
self.licence.resource_class = mock_resource_class
299+
self.licence._licensing_cli.get.return_value = {
300+
"licence1": "mock_licence1",
301+
"licence2": "mock_licence2"
302+
}
303+
304+
result = self.licence.list(mock.sentinel.appliance_id)
305+
306+
self.assertEqual(
307+
[mock_resource_class.return_value,
308+
mock_resource_class.return_value],
309+
result
310+
)
311+
self.licence._licensing_cli.get.assert_called_once_with(
312+
'/appliances/%s/licences' % mock.sentinel.appliance_id,
313+
response_key='licences')
314+
mock_resource_class.assert_has_calls([
315+
mock.call(self.licence, "licence1", loaded=True),
316+
mock.call(self.licence, "licence2", loaded=True)
317+
])
318+
319+
def test_register(self):
320+
mock_resource_class = mock.Mock()
321+
self.licence.resource_class = mock_resource_class
322+
323+
result = self.licence.register(
324+
mock.sentinel.appliance_id, mock.sentinel.licence)
325+
326+
self.assertEqual(
327+
mock_resource_class.return_value,
328+
result
329+
)
330+
self.licence._licensing_cli.post.assert_called_once_with(
331+
'/appliances/%s/licences' % mock.sentinel.appliance_id,
332+
body=mock.sentinel.licence,
333+
response_key='licence')
334+
mock_resource_class.assert_called_once_with(
335+
self.licence, self.licence._licensing_cli.post.return_value,
336+
loaded=True)
337+
338+
def test_show(self):
339+
mock_resource_class = mock.Mock()
340+
self.licence.resource_class = mock_resource_class
341+
342+
result = self.licence.show(
343+
mock.sentinel.appliance_id, mock.sentinel.licence_id)
344+
345+
self.assertEqual(
346+
mock_resource_class.return_value,
347+
result
348+
)
349+
self.licence._licensing_cli.get.assert_called_once_with(
350+
'/appliances/%s/licences/%s' % (mock.sentinel.appliance_id,
351+
mock.sentinel.licence_id),
352+
response_key='licence')
353+
mock_resource_class.assert_called_once_with(
354+
self.licence, self.licence._licensing_cli.get.return_value,
355+
loaded=True)
356+
357+
def test_delete(self):
358+
mock_resource_class = mock.Mock()
359+
self.licence.resource_class = mock_resource_class
360+
361+
result = self.licence.delete(
362+
mock.sentinel.appliance_id, mock.sentinel.licence_id)
363+
364+
self.assertEqual(
365+
self.licence._licensing_cli.delete.return_value,
366+
result
367+
)
368+
self.licence._licensing_cli.delete.assert_called_once_with(
369+
'/appliances/%s/licences/%s' % (mock.sentinel.appliance_id,
370+
mock.sentinel.licence_id),
371+
raw_response=True)

0 commit comments

Comments
 (0)