@@ -351,7 +351,58 @@ def test_create(self):
351351 _ctx .instance .runtime_properties ['resource_id' ], "volume_name"
352352 )
353353
354+ # failed check size of download
355+ _ctx .instance .runtime_properties ['resource_id' ] = None
356+ _ctx .instance .runtime_properties ['params' ] = {}
357+ _ctx .node .properties ['params' ] = {}
358+ with mock .patch (
359+ "cloudify_libvirt.volume_tasks.libvirt.open" ,
360+ mock .Mock (return_value = connect )
361+ ):
362+ # empty
363+ head_response = mock .Mock ()
364+ head_response .headers = {'Content-Length' : 0 }
365+ with mock .patch (
366+ "cloudify_libvirt.volume_tasks.requests.head" ,
367+ mock .Mock (return_value = head_response )
368+ ):
369+ with self .assertRaisesRegexp (
370+ NonRecoverableError ,
371+ "Failed to download volume."
372+ ):
373+ volume_tasks .create (
374+ ctx = _ctx ,
375+ template_resource = "template_resource" ,
376+ params = {
377+ 'pool' : 'empty' ,
378+ 'url' : "https://fake.org/centos.iso" })
379+
380+ # sucessful check size of download
381+ _ctx .instance .runtime_properties ['resource_id' ] = None
382+ _ctx .instance .runtime_properties ['params' ] = {}
383+ _ctx .node .properties ['params' ] = {}
384+ with mock .patch (
385+ "cloudify_libvirt.volume_tasks.libvirt.open" ,
386+ mock .Mock (return_value = connect )
387+ ):
388+ head_response = mock .Mock ()
389+ head_response .headers = {'Content-Length' : 512 ,
390+ 'Accept-Ranges' : 'bytes' }
391+ with mock .patch (
392+ "cloudify_libvirt.volume_tasks.requests.head" ,
393+ mock .Mock (return_value = head_response )
394+ ):
395+ volume_tasks .create (
396+ ctx = _ctx ,
397+ template_resource = "template_resource" ,
398+ params = {
399+ 'pool' : 'empty' ,
400+ 'url' : "https://fake.org/centos.iso" })
401+
354402 # failed on create
403+ _ctx .instance .runtime_properties ['resource_id' ] = None
404+ _ctx .instance .runtime_properties ['params' ] = {}
405+ _ctx .node .properties ['params' ] = {}
355406 pool .createXML = mock .Mock (return_value = None )
356407 with mock .patch (
357408 "cloudify_libvirt.volume_tasks.libvirt.open" ,
@@ -444,6 +495,60 @@ def test_start_wipe(self):
444495 'allocation' : 1
445496 })
446497
498+ def test_start_download (self ):
499+ # download
500+ _ctx = self ._create_ctx ()
501+ _ctx .instance .runtime_properties ['resource_id' ] = 'volume'
502+ _ctx .instance .runtime_properties ['params' ] = {'pool' : 'pool_name' }
503+
504+ volume = mock .Mock ()
505+ volume .name = mock .Mock (return_value = "volume" )
506+ volume .upload = mock .Mock ()
507+ pool = mock .Mock ()
508+ pool .name = mock .Mock (return_value = "pool" )
509+ pool .storageVolLookupByName = mock .Mock (return_value = volume )
510+
511+ connect = self ._create_fake_connection ()
512+
513+ connect .storagePoolLookupByName = mock .Mock (return_value = pool )
514+ with mock .patch (
515+ "cloudify_libvirt.volume_tasks.libvirt.open" ,
516+ mock .Mock (return_value = connect )
517+ ):
518+ # empty
519+ head_response = mock .Mock ()
520+ head_response .headers = {'Content-Length' : 0 }
521+ with mock .patch (
522+ "cloudify_libvirt.volume_tasks.requests.head" ,
523+ mock .Mock (return_value = head_response )
524+ ):
525+ with self .assertRaisesRegexp (
526+ NonRecoverableError ,
527+ "Failed to download volume."
528+ ):
529+ volume_tasks .start (
530+ ctx = _ctx ,
531+ params = {
532+ 'url' : "https://fake.org/centos.iso" })
533+
534+ # 512 for download
535+ head_response = mock .Mock ()
536+ head_response .headers = {'Content-Length' : 512 ,
537+ 'Accept-Ranges' : 'bytes' }
538+ head_response .iter_content = mock .Mock (return_value = ["\0 " * 256 ])
539+ with mock .patch (
540+ "cloudify_libvirt.volume_tasks.requests.head" ,
541+ mock .Mock (return_value = head_response )
542+ ):
543+ with mock .patch (
544+ "cloudify_libvirt.volume_tasks.requests.get" ,
545+ mock .Mock (return_value = head_response )
546+ ):
547+ volume_tasks .start (
548+ ctx = _ctx ,
549+ params = {
550+ 'url' : "https://fake.org/centos.iso" })
551+
447552 def test_stop (self ):
448553 # check correct handle exception with empty connection
449554 self ._test_check_correct_connect_action (
0 commit comments