@@ -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
405556if __name__ == '__main__' :
406557 unittest .main ()
0 commit comments