@@ -524,13 +524,15 @@ def test_cache_template_exists(self, mock_exists):
524524
525525 mock_exists .assert_has_calls (exist_calls )
526526
527+ @mock .patch ('nova.image.format_inspector.detect_file_format' )
527528 @mock .patch .object (imagebackend .utils , 'synchronized' )
528529 @mock .patch ('nova.virt.libvirt.utils.create_image' )
529530 @mock .patch .object (os .path , 'exists' , side_effect = [])
530531 @mock .patch .object (imagebackend .Image , 'verify_base_size' )
531532 @mock .patch ('nova.privsep.path.utime' )
532533 def test_create_image (
533- self , mock_utime , mock_verify , mock_exist , mock_create , mock_sync
534+ self , mock_utime , mock_verify , mock_exist , mock_create , mock_sync ,
535+ mock_detect_format
534536 ):
535537 mock_sync .side_effect = lambda * a , ** kw : self ._fake_deco
536538 fn = mock .MagicMock ()
@@ -551,15 +553,19 @@ def test_create_image(
551553 mock_exist .assert_has_calls (exist_calls )
552554 self .assertTrue (mock_sync .called )
553555 mock_utime .assert_called ()
556+ mock_detect_format .assert_called_once ()
557+ mock_detect_format .return_value .safety_check .assert_called_once_with ()
554558
559+ @mock .patch ('nova.image.format_inspector.detect_file_format' )
555560 @mock .patch .object (imagebackend .utils , 'synchronized' )
556561 @mock .patch ('nova.virt.libvirt.utils.create_image' )
557562 @mock .patch .object (imagebackend .disk , 'extend' )
558563 @mock .patch .object (os .path , 'exists' , side_effect = [])
559564 @mock .patch .object (imagebackend .Qcow2 , 'get_disk_size' )
560565 @mock .patch ('nova.privsep.path.utime' )
561566 def test_create_image_too_small (self , mock_utime , mock_get , mock_exist ,
562- mock_extend , mock_create , mock_sync ):
567+ mock_extend , mock_create , mock_sync ,
568+ mock_detect_format ):
563569 mock_sync .side_effect = lambda * a , ** kw : self ._fake_deco
564570 mock_get .return_value = self .SIZE
565571 fn = mock .MagicMock ()
@@ -576,7 +582,9 @@ def test_create_image_too_small(self, mock_utime, mock_get, mock_exist,
576582 self .assertTrue (mock_sync .called )
577583 self .assertFalse (mock_create .called )
578584 self .assertFalse (mock_extend .called )
585+ mock_detect_format .assert_called_once ()
579586
587+ @mock .patch ('nova.image.format_inspector.detect_file_format' )
580588 @mock .patch .object (imagebackend .utils , 'synchronized' )
581589 @mock .patch ('nova.virt.libvirt.utils.create_image' )
582590 @mock .patch ('nova.virt.libvirt.utils.get_disk_backing_file' )
@@ -588,7 +596,8 @@ def test_create_image_too_small(self, mock_utime, mock_get, mock_exist,
588596 def test_generate_resized_backing_files (self , mock_utime , mock_copy ,
589597 mock_verify , mock_exist ,
590598 mock_extend , mock_get ,
591- mock_create , mock_sync ):
599+ mock_create , mock_sync ,
600+ mock_detect_format ):
592601 mock_sync .side_effect = lambda * a , ** kw : self ._fake_deco
593602 mock_get .return_value = self .QCOW2_BASE
594603 fn = mock .MagicMock ()
@@ -615,7 +624,9 @@ def test_generate_resized_backing_files(self, mock_utime, mock_copy,
615624 self .assertTrue (mock_sync .called )
616625 self .assertFalse (mock_create .called )
617626 mock_utime .assert_called ()
627+ mock_detect_format .assert_called_once ()
618628
629+ @mock .patch ('nova.image.format_inspector.detect_file_format' )
619630 @mock .patch .object (imagebackend .utils , 'synchronized' )
620631 @mock .patch ('nova.virt.libvirt.utils.create_image' )
621632 @mock .patch ('nova.virt.libvirt.utils.get_disk_backing_file' )
@@ -626,7 +637,8 @@ def test_generate_resized_backing_files(self, mock_utime, mock_copy,
626637 def test_qcow2_exists_and_has_no_backing_file (self , mock_utime ,
627638 mock_verify , mock_exist ,
628639 mock_extend , mock_get ,
629- mock_create , mock_sync ):
640+ mock_create , mock_sync ,
641+ mock_detect_format ):
630642 mock_sync .side_effect = lambda * a , ** kw : self ._fake_deco
631643 mock_get .return_value = None
632644 fn = mock .MagicMock ()
@@ -647,6 +659,31 @@ def test_qcow2_exists_and_has_no_backing_file(self, mock_utime,
647659 self .assertTrue (mock_sync .called )
648660 self .assertFalse (mock_create .called )
649661 self .assertFalse (mock_extend .called )
662+ mock_detect_format .assert_called_once ()
663+
664+ @mock .patch ('nova.image.format_inspector.detect_file_format' )
665+ @mock .patch .object (imagebackend .utils , 'synchronized' )
666+ @mock .patch ('nova.virt.libvirt.utils.create_image' )
667+ @mock .patch ('nova.virt.libvirt.utils.get_disk_backing_file' )
668+ @mock .patch .object (imagebackend .disk , 'extend' )
669+ @mock .patch .object (os .path , 'exists' , side_effect = [])
670+ @mock .patch .object (imagebackend .Image , 'verify_base_size' )
671+ def test_qcow2_exists_and_fails_safety_check (self ,
672+ mock_verify , mock_exist ,
673+ mock_extend , mock_get ,
674+ mock_create , mock_sync ,
675+ mock_detect_format ):
676+ mock_detect_format .return_value .safety_check .return_value = False
677+ mock_sync .side_effect = lambda * a , ** kw : self ._fake_deco
678+ mock_get .return_value = None
679+ fn = mock .MagicMock ()
680+ mock_exist .side_effect = [False , True , False , True , True ]
681+ image = self .image_class (self .INSTANCE , self .NAME )
682+
683+ self .assertRaises (exception .InvalidDiskInfo ,
684+ image .create_image , fn , self .TEMPLATE_PATH ,
685+ self .SIZE )
686+ mock_verify .assert_not_called ()
650687
651688 def test_resolve_driver_format (self ):
652689 image = self .image_class (self .INSTANCE , self .NAME )
0 commit comments