Skip to content

Commit 3079340

Browse files
authored
Merge pull request #17 from cloudify-incubator/for_review
Update unittest coverage
2 parents 70289a5 + d7f2b2b commit 3079340

File tree

9 files changed

+191
-10
lines changed

9 files changed

+191
-10
lines changed

CHANGELOG.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ releases:
3131
* Support storage pool creation.
3232
* Rename `params.resource_id` to `params.name`.
3333
* Support creation snapshot on external resources.
34+
35+
v0.8.0:
36+
* Support storage volume creation.
37+
38+
v0.8.1:
39+
* Fix volume wipe code.

cloudify_libvirt/tests/test_common_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def _create_fake_connection(self):
167167
connect.defineXML = mock.Mock(return_value=None)
168168
connect.storagePoolDefineXML = mock.Mock(return_value=None)
169169
connect.close = mock.Mock(return_value=None)
170+
connect.newStream = mock.Mock(return_value=mock.Mock())
170171
return connect
171172

172173
def _create_ctx(self):

cloudify_libvirt/tests/test_pool.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,30 @@ def test_create(self):
314314
"cloudify_libvirt.pool_tasks.libvirt.open",
315315
pool_tasks.create, [], {'ctx': self._create_ctx()})
316316

317+
# successful create
318+
_ctx = self._create_ctx()
319+
_ctx.get_resource = mock.Mock(return_value='<somexml/>')
320+
321+
pool = mock.Mock()
322+
pool.name = mock.Mock(return_value="pool_name")
323+
324+
connect = self._create_fake_connection()
325+
connect.storagePoolDefineXML = mock.Mock(return_value=pool)
326+
327+
# without params
328+
_ctx.instance.runtime_properties['params'] = {}
329+
_ctx.node.properties['params'] = {}
330+
with mock.patch(
331+
"cloudify_libvirt.pool_tasks.libvirt.open",
332+
mock.Mock(return_value=connect)
333+
):
334+
pool_tasks.create(ctx=_ctx,
335+
template_resource="template_resource")
336+
connect.storagePoolDefineXML.assert_called_with('<somexml/>')
337+
self.assertEqual(
338+
_ctx.instance.runtime_properties['resource_id'], "pool_name"
339+
)
340+
317341
def test_reuse_pool_create_not_exist(self):
318342
# check correct handle exception with empty network
319343
_ctx = self._create_ctx()

cloudify_libvirt/tests/test_volume.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,48 @@ def test_create(self):
324324
volume_tasks.create, [], {'ctx': self._create_ctx(),
325325
'params': {'pool': 'empty'}})
326326

327+
# successful create
328+
_ctx = self._create_ctx()
329+
_ctx.get_resource = mock.Mock(return_value='<somexml/>')
330+
331+
volume = mock.Mock()
332+
volume.name = mock.Mock(return_value="volume_name")
333+
334+
pool = mock.Mock()
335+
pool.createXML = mock.Mock(return_value=volume)
336+
337+
connect = self._create_fake_connection()
338+
connect.storagePoolLookupByName = mock.Mock(return_value=pool)
339+
340+
# without params
341+
_ctx.instance.runtime_properties['params'] = {}
342+
_ctx.node.properties['params'] = {}
343+
with mock.patch(
344+
"cloudify_libvirt.volume_tasks.libvirt.open",
345+
mock.Mock(return_value=connect)
346+
):
347+
volume_tasks.create(ctx=_ctx,
348+
template_resource="template_resource",
349+
params={'pool': 'empty'})
350+
pool.createXML.assert_called_with('<somexml/>')
351+
self.assertEqual(
352+
_ctx.instance.runtime_properties['resource_id'], "volume_name"
353+
)
354+
355+
# failed on create
356+
pool.createXML = mock.Mock(return_value=None)
357+
with mock.patch(
358+
"cloudify_libvirt.volume_tasks.libvirt.open",
359+
mock.Mock(return_value=connect)
360+
):
361+
with self.assertRaisesRegexp(
362+
NonRecoverableError,
363+
'Failed to create a virtual volume'
364+
):
365+
volume_tasks.create(ctx=_ctx,
366+
template_resource="template_resource",
367+
params={'pool': 'empty'})
368+
327369
def test_reuse_volume_create_not_exist(self):
328370
# check correct handle exception with empty network
329371
_ctx = self._create_ctx()
@@ -377,6 +419,32 @@ def test_start(self):
377419
volume_tasks.start)
378420
self._test_no_resource_id(volume_tasks.start)
379421

422+
def test_start_wipe(self):
423+
# zero wipe
424+
_ctx = self._create_ctx()
425+
_ctx.instance.runtime_properties['resource_id'] = 'volume'
426+
_ctx.instance.runtime_properties['params'] = {'pool': 'pool_name'}
427+
428+
volume = mock.Mock()
429+
volume.name = mock.Mock(return_value="volume")
430+
volume.upload = mock.Mock()
431+
pool = mock.Mock()
432+
pool.name = mock.Mock(return_value="pool")
433+
pool.storageVolLookupByName = mock.Mock(return_value=volume)
434+
435+
connect = self._create_fake_connection()
436+
437+
connect.storagePoolLookupByName = mock.Mock(return_value=pool)
438+
with mock.patch(
439+
"cloudify_libvirt.volume_tasks.libvirt.open",
440+
mock.Mock(return_value=connect)
441+
):
442+
volume_tasks.start(ctx=_ctx,
443+
params={
444+
'zero_wipe': True,
445+
'allocation': 1
446+
})
447+
380448
def test_stop(self):
381449
# check correct handle exception with empty connection
382450
self._test_check_correct_connect_action(
@@ -389,6 +457,47 @@ def test_stop(self):
389457
volume_tasks.stop)
390458
self._test_no_resource_id(volume_tasks.stop)
391459

460+
def test_stop_wipe(self):
461+
# failed to wipe/error ignored
462+
_ctx = self._create_ctx()
463+
_ctx.instance.runtime_properties['resource_id'] = 'volume'
464+
_ctx.instance.runtime_properties['params'] = {'pool': 'pool_name'}
465+
466+
volume = mock.Mock()
467+
volume.name = mock.Mock(return_value="volume")
468+
volume.wipe = mock.Mock(
469+
side_effect=volume_tasks.libvirt.libvirtError("e"))
470+
pool = mock.Mock()
471+
pool.name = mock.Mock(return_value="pool")
472+
pool.storageVolLookupByName = mock.Mock(return_value=volume)
473+
474+
connect = self._create_fake_connection()
475+
476+
connect.storagePoolLookupByName = mock.Mock(return_value=pool)
477+
with mock.patch(
478+
"cloudify_libvirt.volume_tasks.libvirt.open",
479+
mock.Mock(return_value=connect)
480+
):
481+
volume_tasks.stop(ctx=_ctx)
482+
# failed to wipe/wrong response
483+
volume.wipe = mock.Mock(return_value=-1)
484+
with mock.patch(
485+
"cloudify_libvirt.volume_tasks.libvirt.open",
486+
mock.Mock(return_value=connect)
487+
):
488+
with mock.patch(
489+
"cloudify_libvirt.volume_tasks.time.sleep",
490+
mock.Mock(return_value=mock.Mock())
491+
):
492+
volume_tasks.stop(ctx=_ctx)
493+
# correctly wiped
494+
volume.wipe = mock.Mock(return_value=0)
495+
with mock.patch(
496+
"cloudify_libvirt.volume_tasks.libvirt.open",
497+
mock.Mock(return_value=connect)
498+
):
499+
volume_tasks.stop(ctx=_ctx)
500+
392501
def test_delete(self):
393502
# check correct handle exception with empty connection
394503
self._test_check_correct_connect_action(
@@ -401,6 +510,48 @@ def test_delete(self):
401510
volume_tasks.delete)
402511
self._test_no_resource_id(volume_tasks.delete)
403512

513+
# failed to remove
514+
_ctx = self._create_ctx()
515+
_ctx.instance.runtime_properties['resource_id'] = 'volume'
516+
_ctx.instance.runtime_properties['params'] = {'pool': 'pool_name'}
517+
518+
volume = mock.Mock()
519+
volume.name = mock.Mock(return_value="volume")
520+
volume.delete = mock.Mock(return_value=-1)
521+
pool = mock.Mock()
522+
pool.name = mock.Mock(return_value="pool")
523+
pool.storageVolLookupByName = mock.Mock(return_value=volume)
524+
525+
connect = self._create_fake_connection()
526+
527+
connect.storagePoolLookupByName = mock.Mock(return_value=pool)
528+
with mock.patch(
529+
"cloudify_libvirt.volume_tasks.libvirt.open",
530+
mock.Mock(return_value=connect)
531+
):
532+
with self.assertRaisesRegexp(
533+
NonRecoverableError,
534+
'Can not undefine volume.'
535+
):
536+
volume_tasks.delete(ctx=_ctx)
537+
538+
# sucessful remove
539+
volume.delete = mock.Mock(return_value=0)
540+
with mock.patch(
541+
"cloudify_libvirt.volume_tasks.libvirt.open",
542+
mock.Mock(return_value=connect)
543+
):
544+
volume_tasks.delete(ctx=_ctx)
545+
self.assertEqual(
546+
_ctx.instance.runtime_properties,
547+
{
548+
'backups': {},
549+
'libvirt_auth': {'a': 'd'},
550+
'params': {},
551+
'resource_id': None
552+
}
553+
)
554+
404555

405556
if __name__ == '__main__':
406557
unittest.main()

cloudify_libvirt/volume_tasks.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,12 @@ def stop(**kwargs):
156156
)
157157

158158
for i in xrange(10):
159-
ctx.logger.info("Tring to wipe vm {}/10".format(i))
159+
ctx.logger.info("Tring to wipe volume {}/10".format(i))
160160
if volume.wipe(0) == 0:
161161
break
162162
time.sleep(30)
163-
163+
except libvirt.libvirtError as e:
164+
ctx.logger.info('Failed to wipe the volume: {}'.format(repr(e)))
164165
finally:
165166
conn.close()
166167

dev-requirements.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
https://github.com/cloudify-cosmo/cloudify-dsl-parser/archive/master.zip
2-
https://github.com/cloudify-cosmo/cloudify-rest-client/archive/master.zip
3-
https://github.com/cloudify-cosmo/cloudify-plugins-common/archive/master.zip
1+
cloudify-plugins-common==3.4.2
2+
cloudify-rest-client==4.0

plugin.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ plugins:
22
libvirt:
33
executor: central_deployment_agent
44
package_name: cloudify-libvirt-plugin
5-
package_version: '0.8.0'
6-
source: https://github.com/cloudify-incubator/cloudify-libvirt-plugin/archive/0.8.0.zip
5+
package_version: '0.8.1'
6+
source: https://github.com/cloudify-incubator/cloudify-libvirt-plugin/archive/0.8.1.zip
77

88
data_types:
99

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
setup(
1717
name='cloudify-libvirt-plugin',
18-
version='0.8.0',
18+
version='0.8.1',
1919
description='support libvirt',
2020
author='Cloudify',
2121
author_email='hello@getcloudify.org',

test-requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
mock>=1.0
22
nose>=1.3
33
coverage
4-
testtools>=0.9.36,!=1.2.0
54
tox
65
nose-cov
76
flake8
87
pylint
98
# for blueprints validate
10-
cloudify==4.3.2
9+
cloudify==4.5.5

0 commit comments

Comments
 (0)