Skip to content

Commit 0e18ab1

Browse files
committed
Update unittests
1 parent 9b05d1f commit 0e18ab1

6 files changed

Lines changed: 648 additions & 32 deletions

File tree

cloudify_libvirt/tests/test_common_base.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,29 @@ def _check_no_such_object_pool(self, libvirt_open, func, args, kwargs,
8080
connect.lookupByName.assert_not_called()
8181
connect.storagePoolLookupByName.assert_called_with(resource_id)
8282

83+
def _check_no_such_object_volume(self, libvirt_open, func, args, kwargs,
84+
resource_id):
85+
# check that we correctly raise exception without such object
86+
# no such
87+
connect = self._create_fake_connection()
88+
fake_pool = mock.Mock()
89+
fake_pool.storageVolLookupByName = mock.Mock(
90+
side_effect=libvirt.libvirtError("storageVolLookupByName"))
91+
connect.storagePoolLookupByName = mock.Mock(return_value=fake_pool)
92+
with mock.patch(
93+
libvirt_open,
94+
mock.Mock(return_value=connect)
95+
):
96+
with self.assertRaisesRegexp(
97+
NonRecoverableError,
98+
'Failed to find the volume'
99+
):
100+
func(*args, **kwargs)
101+
102+
connect.networkLookupByName.assert_not_called()
103+
connect.lookupByName.assert_not_called()
104+
connect.storagePoolLookupByName.assert_called_with('pool_name')
105+
83106
def _check_no_such_object_domain(self, libvirt_open, func, args, kwargs,
84107
resource_id):
85108
# check that we correctly raise exception without such object
@@ -194,3 +217,12 @@ def _test_no_snapshot_name(self, _ctx, libvirt_open, func):
194217
connect.storagePoolLookupByName = mock.Mock()
195218
with mock.patch(libvirt_open, mock.Mock(return_value=connect)):
196219
func(ctx=_ctx)
220+
221+
def _test_check_correct_connect_action(self, libvirt_open, func):
222+
# check correct handle exception with empty connection
223+
# for start/stop/delete
224+
_ctx = self._create_ctx()
225+
_ctx.instance.runtime_properties['resource_id'] = 'resource'
226+
self._check_correct_connect(
227+
libvirt_open,
228+
func, [], {'ctx': _ctx})

cloudify_libvirt/tests/test_domain.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def test_configure(self):
156156
def test_update(self):
157157
self._test_no_resource_id(domain_tasks.update,
158158
"No servers for update")
159-
self._test_check_correct_connect_action(domain_tasks.update)
159+
self._test_check_correct_connect_action(
160+
"cloudify_libvirt.domain_tasks.libvirt.open",
161+
domain_tasks.update)
160162
self._test_check_correct_connect_no_object(domain_tasks.update)
161163
# check memory
162164
self._test_action_states(
@@ -180,7 +182,9 @@ def test_update(self):
180182
def test_reboot(self):
181183
self._test_no_resource_id(domain_tasks.reboot,
182184
"No servers for reboot")
183-
self._test_check_correct_connect_action(domain_tasks.reboot)
185+
self._test_check_correct_connect_action(
186+
"cloudify_libvirt.domain_tasks.libvirt.open",
187+
domain_tasks.reboot)
184188
self._test_check_correct_connect_no_object(domain_tasks.reboot)
185189
self._test_action_states(
186190
domain_tasks.reboot,
@@ -190,7 +194,9 @@ def test_reboot(self):
190194
def test_start(self):
191195
self._test_no_resource_id(domain_tasks.start,
192196
"No servers for start")
193-
self._test_check_correct_connect_action(domain_tasks.start)
197+
self._test_check_correct_connect_action(
198+
"cloudify_libvirt.domain_tasks.libvirt.open",
199+
domain_tasks.start)
194200
self._test_check_correct_connect_no_object(domain_tasks.start)
195201
self._test_action_states(
196202
domain_tasks.start,
@@ -202,7 +208,9 @@ def test_stop(self):
202208
self._test_reused_object(
203209
"cloudify_libvirt.domain_tasks.libvirt.open",
204210
domain_tasks.stop)
205-
self._test_check_correct_connect_action(domain_tasks.stop)
211+
self._test_check_correct_connect_action(
212+
"cloudify_libvirt.domain_tasks.libvirt.open",
213+
domain_tasks.stop)
206214
self._test_check_correct_connect_no_object(domain_tasks.stop)
207215
self._test_action_states(
208216
domain_tasks.stop,
@@ -212,7 +220,9 @@ def test_stop(self):
212220
def test_resume(self):
213221
self._test_no_resource_id(domain_tasks.resume,
214222
"No servers for resume")
215-
self._test_check_correct_connect_action(domain_tasks.resume)
223+
self._test_check_correct_connect_action(
224+
"cloudify_libvirt.domain_tasks.libvirt.open",
225+
domain_tasks.resume)
216226
self._test_check_correct_connect_no_object(domain_tasks.resume)
217227
self._test_action_states(
218228
domain_tasks.resume,
@@ -222,7 +232,9 @@ def test_resume(self):
222232
def test_suspend(self):
223233
self._test_no_resource_id(domain_tasks.suspend,
224234
"No servers for suspend")
225-
self._test_check_correct_connect_action(domain_tasks.suspend)
235+
self._test_check_correct_connect_action(
236+
"cloudify_libvirt.domain_tasks.libvirt.open",
237+
domain_tasks.suspend)
226238
self._test_check_correct_connect_no_object(domain_tasks.suspend)
227239
self._test_action_states(
228240
domain_tasks.suspend,
@@ -234,7 +246,9 @@ def test_delete(self):
234246
self._test_reused_object(
235247
"cloudify_libvirt.domain_tasks.libvirt.open",
236248
domain_tasks.delete)
237-
self._test_check_correct_connect_action(domain_tasks.delete)
249+
self._test_check_correct_connect_action(
250+
"cloudify_libvirt.domain_tasks.libvirt.open",
251+
domain_tasks.delete)
238252
self._test_check_correct_connect_no_object(domain_tasks.delete)
239253

240254
# delete snapshot with error
@@ -262,7 +276,7 @@ def test_delete(self):
262276
):
263277
with self.assertRaisesRegexp(
264278
RecoverableError,
265-
"Still have several snapshots: \['snapshot!'\]."
279+
"Still have several snapshots: \\['snapshot!'\\]."
266280
):
267281
domain_tasks.delete(ctx=_ctx,
268282
snapshot_name='snapshot_name',
@@ -326,7 +340,9 @@ def _snapshot_list():
326340
def test_perfomance(self):
327341
self._test_no_resource_id(domain_tasks.perfomance,
328342
"No servers for statistics.")
329-
self._test_check_correct_connect_action(domain_tasks.perfomance)
343+
self._test_check_correct_connect_action(
344+
"cloudify_libvirt.domain_tasks.libvirt.open",
345+
domain_tasks.perfomance)
330346
self._test_check_correct_connect_no_object(domain_tasks.perfomance)
331347

332348
# some fake statistics
@@ -532,14 +548,6 @@ def _test_check_correct_connect_no_object(self, func):
532548
"cloudify_libvirt.domain_tasks.libvirt.open", func, [],
533549
{'ctx': _ctx}, 'resource')
534550

535-
def _test_check_correct_connect_action(self, func):
536-
# check correct handle exception with empty connection
537-
_ctx = self._create_ctx()
538-
_ctx.instance.runtime_properties['resource_id'] = 'resource'
539-
self._check_correct_connect(
540-
"cloudify_libvirt.domain_tasks.libvirt.open",
541-
func, [], {'ctx': _ctx})
542-
543551
def _check_create_backups(self, _ctx, connect, domain, snapshot, raw_case):
544552
# raw_case - dump xml without real raw dump
545553
_ctx.instance.runtime_properties['params']['full_dump'] = raw_case
@@ -555,7 +563,7 @@ def _check_create_backups(self, _ctx, connect, domain, snapshot, raw_case):
555563
):
556564
with self.assertRaisesRegexp(
557565
NonRecoverableError,
558-
"Snapshot snapshot\! already exists."
566+
"Snapshot snapshot\\! already exists."
559567
):
560568
domain_tasks.snapshot_create(
561569
ctx=_ctx,
@@ -847,7 +855,7 @@ def test_snapshot_delete(self):
847855
):
848856
with self.assertRaisesRegexp(
849857
NonRecoverableError,
850-
"Sub snapshots \['snapshot-'\] found for "
858+
"Sub snapshots \\['snapshot-'\\] found for "
851859
"node_name-snapshot_name. You should remove subsnaphots before"
852860
" remove current."
853861
):
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
########
2+
# Copyright (c) 2016-2018 GigaSpaces Technologies Ltd. All rights reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
import unittest
16+
import mock
17+
18+
from cloudify_libvirt.tests.test_common_base import LibVirtCommonTest
19+
import cloudify_libvirt.iso9660_tasks as iso9660_tasks
20+
21+
22+
class TestIso9660Task(LibVirtCommonTest):
23+
24+
def test_joliet_name(self):
25+
self.assertEqual("/abc", iso9660_tasks._joliet_name("abc"))
26+
self.assertEqual("/" + "*" * 64, iso9660_tasks._joliet_name("*" * 128))
27+
self.assertEqual("/" + "*" * 64,
28+
iso9660_tasks._joliet_name("/" + "*" * 128))
29+
30+
def test_iso_name(self):
31+
self.assertEqual("/ABC.;1", iso9660_tasks._iso_name("abc"))
32+
self.assertEqual("/12345678.;1",
33+
iso9660_tasks._iso_name("1234567890.abcdef"))
34+
self.assertEqual("/" + "_" * 8 + ".;1",
35+
iso9660_tasks._iso_name("*" * 128))
36+
self.assertEqual("/" + "_" * 8 + ".;1",
37+
iso9660_tasks._iso_name("/" + "*" * 128))
38+
self.assertEqual("/12345678.123;1",
39+
iso9660_tasks._iso_name("12345678.123"))
40+
41+
def test_create(self):
42+
# check correct handle exception with empty connection
43+
self._check_correct_connect(
44+
"cloudify_libvirt.iso9660_tasks.libvirt.open",
45+
iso9660_tasks.create, [], {'ctx': self._create_ctx()})
46+
47+
# check error with create iso image
48+
self._check_create_object(
49+
'Failed to find the volume',
50+
"cloudify_libvirt.iso9660_tasks.libvirt.open",
51+
iso9660_tasks.create, [], {'ctx': self._create_ctx(),
52+
'params': {'pool': 'empty'}})
53+
54+
volume = mock.Mock()
55+
pool = mock.Mock()
56+
pool.storageVolLookupByName = mock.Mock(return_value=volume)
57+
58+
connect = self._create_fake_connection()
59+
connect.storagePoolLookupByName = mock.Mock(return_value=pool)
60+
61+
_ctx = self._create_ctx()
62+
_ctx.instance.runtime_properties['params'] = {}
63+
_ctx.node.properties['params'] = {}
64+
with mock.patch(
65+
"cloudify_libvirt.iso9660_tasks.libvirt.open",
66+
mock.Mock(return_value=connect)
67+
):
68+
iso9660_tasks.create(ctx=_ctx, params={
69+
"pool": "_+pool",
70+
"volume": "_+volume",
71+
"files": {
72+
"meta-data": "instance-id: localhost"
73+
}
74+
})
75+
connect.storagePoolLookupByName.assert_called_with("_+pool")
76+
pool.storageVolLookupByName.assert_called_with("_+volume")
77+
78+
79+
if __name__ == '__main__':
80+
unittest.main()

cloudify_libvirt/tests/test_network.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -497,19 +497,6 @@ def test_delete(self):
497497
self.assertFalse(_ctx.instance.runtime_properties.get('resource_id'))
498498
self.assertFalse(_ctx.instance.runtime_properties.get("backup"))
499499

500-
def _create_ctx(self):
501-
_ctx = MockCloudifyContext(
502-
'node_name',
503-
properties={
504-
'libvirt_auth': {'a': 'c'}
505-
},
506-
runtime_properties={
507-
'libvirt_auth': {'a': 'd'}
508-
}
509-
)
510-
current_ctx.set(_ctx)
511-
return _ctx
512-
513500
def test_create(self):
514501
# check correct handle exception with empty connection
515502
self._check_correct_connect(

cloudify_libvirt/tests/test_pool.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ def _test_empty_connection_backup(self, func):
4646
"cloudify_libvirt.pool_tasks.libvirt.open",
4747
func, [], {'ctx': _ctx, "snapshot_name": "backup"})
4848

49+
def _test_empty_pool(self, func):
50+
# check correct handle exception with empty volume
51+
_ctx = self._create_ctx()
52+
_ctx.instance.runtime_properties['resource_id'] = 'resource'
53+
_ctx.instance.runtime_properties['params'] = {'pool': 'pool_name'}
54+
self._check_no_such_object_pool(
55+
"cloudify_libvirt.pool_tasks.libvirt.open",
56+
func, [], {'ctx': _ctx}, 'resource')
57+
4958
def _test_empty_pool_backup(self, func):
5059
# check correct handle exception with empty pool
5160
_ctx = self._create_ctx()
@@ -293,6 +302,100 @@ def test_snapshot_delete(self):
293302
remove_mock.assert_called_with('./backup!/resource.xml')
294303
fake_file.assert_called_with('./backup!/resource.xml', 'r')
295304

305+
def test_create(self):
306+
# check correct handle exception with empty connection
307+
self._check_correct_connect(
308+
"cloudify_libvirt.pool_tasks.libvirt.open",
309+
pool_tasks.create, [], {'ctx': self._create_ctx()})
310+
311+
# check error with create pool
312+
self._check_create_object(
313+
'Failed to create a virtual pool',
314+
"cloudify_libvirt.pool_tasks.libvirt.open",
315+
pool_tasks.create, [], {'ctx': self._create_ctx()})
316+
317+
def test_reuse_pool_create_not_exist(self):
318+
# check correct handle exception with empty network
319+
_ctx = self._create_ctx()
320+
self._check_no_such_object_pool(
321+
"cloudify_libvirt.pool_tasks.libvirt.open",
322+
pool_tasks.create, [], {
323+
'ctx': _ctx,
324+
"resource_id": 'resource',
325+
"use_external_resource": True,
326+
}, 'resource')
327+
328+
def test_reuse_pool_create_exist(self):
329+
# check that we can use network
330+
_ctx = self._create_ctx()
331+
332+
pool = mock.Mock()
333+
pool.name = mock.Mock(return_value="resource")
334+
335+
connect = self._create_fake_connection()
336+
connect.storagePoolLookupByName = mock.Mock(return_value=pool)
337+
with mock.patch(
338+
"cloudify_libvirt.pool_tasks.libvirt.open",
339+
mock.Mock(return_value=connect)
340+
):
341+
pool_tasks.create(ctx=_ctx,
342+
resource_id='resource',
343+
use_external_resource=True)
344+
connect.storagePoolLookupByName.assert_called_with('resource')
345+
self.assertEqual(
346+
_ctx.instance.runtime_properties['resource_id'], 'resource'
347+
)
348+
self.assertTrue(
349+
_ctx.instance.runtime_properties['use_external_resource']
350+
)
351+
352+
def test_start(self):
353+
# check correct handle exception with empty connection
354+
self._test_check_correct_connect_action(
355+
"cloudify_libvirt.pool_tasks.libvirt.open",
356+
pool_tasks.start)
357+
self._test_reused_object(
358+
"cloudify_libvirt.pool_tasks.libvirt.open",
359+
pool_tasks.start)
360+
self._test_no_resource_id(pool_tasks.start, "No pool for start")
361+
362+
def test_configure(self):
363+
# check correct handle exception with empty connection
364+
self._test_check_correct_connect_action(
365+
"cloudify_libvirt.pool_tasks.libvirt.open",
366+
pool_tasks.configure)
367+
368+
self._test_empty_pool(pool_tasks.configure)
369+
self._test_reused_object(
370+
"cloudify_libvirt.pool_tasks.libvirt.open",
371+
pool_tasks.configure)
372+
self._test_no_resource_id(pool_tasks.configure,
373+
"No pool for configure")
374+
375+
def test_stop(self):
376+
# check correct handle exception with empty connection
377+
self._test_check_correct_connect_action(
378+
"cloudify_libvirt.pool_tasks.libvirt.open",
379+
pool_tasks.stop)
380+
381+
self._test_empty_pool(pool_tasks.stop)
382+
self._test_reused_object(
383+
"cloudify_libvirt.pool_tasks.libvirt.open",
384+
pool_tasks.stop)
385+
self._test_no_resource_id(pool_tasks.stop)
386+
387+
def test_delete(self):
388+
# check correct handle exception with empty connection
389+
self._test_check_correct_connect_action(
390+
"cloudify_libvirt.pool_tasks.libvirt.open",
391+
pool_tasks.delete)
392+
393+
self._test_empty_pool(pool_tasks.delete)
394+
self._test_reused_object(
395+
"cloudify_libvirt.pool_tasks.libvirt.open",
396+
pool_tasks.delete)
397+
self._test_no_resource_id(pool_tasks.delete)
398+
296399

297400
if __name__ == '__main__':
298401
unittest.main()

0 commit comments

Comments
 (0)