16
16
17
17
import copy
18
18
import datetime
19
+ import io
19
20
from io import StringIO
20
21
import urllib .parse as urlparse
21
22
22
23
import cryptography
23
24
from cursive import exception as cursive_exception
24
25
import ddt
26
+ import glanceclient .common .utils
25
27
import glanceclient .exc
26
28
from glanceclient .v1 import images
27
29
from glanceclient .v2 import schemas
@@ -701,16 +703,15 @@ def test_download_no_returned_image_data_v2(
701
703
with testtools .ExpectedException (exception .ImageUnacceptable ):
702
704
service .download (ctx , mock .sentinel .image_id )
703
705
704
- @mock .patch ('glanceclient.common.utils.IterableWithLength' )
705
- @mock .patch ('os.path.getsize' , return_value = 1 )
706
+ @mock .patch ('os.path.getsize' )
706
707
@mock .patch ('builtins.open' )
707
708
@mock .patch ('nova.image.glance.LOG' )
708
709
@mock .patch ('nova.image.glance.GlanceImageServiceV2._get_verifier' )
709
710
@mock .patch ('nova.image.glance.GlanceImageServiceV2._get_transfer_method' )
710
711
@mock .patch ('nova.image.glance.GlanceImageServiceV2.show' )
711
712
def test_download_direct_rbd_uri_v2 (
712
713
self , show_mock , get_tran_mock , get_verifier_mock , log_mock ,
713
- open_mock , getsize_mock , iterable_with_length_mock ):
714
+ open_mock , getsize_mock ):
714
715
self .flags (enable_rbd_download = True , group = 'glance' )
715
716
show_mock .return_value = {
716
717
'locations' : [
@@ -724,9 +725,13 @@ def test_download_direct_rbd_uri_v2(
724
725
get_tran_mock .return_value = tran_mod
725
726
client = mock .MagicMock ()
726
727
ctx = mock .sentinel .ctx
728
+ image_content = ["rbd1" , "rbd2" ]
729
+ getsize_mock .return_value = len (image_content )
730
+ file_object = mock .MagicMock (spec = io .BytesIO )
731
+ file_object .__iter__ .return_value = image_content
727
732
writer = mock .MagicMock ()
733
+ writer .__enter__ .return_value = file_object
728
734
open_mock .return_value = writer
729
- iterable_with_length_mock .return_value = ["rbd1" , "rbd2" ]
730
735
service = glance .GlanceImageServiceV2 (client )
731
736
732
737
verifier = mock .MagicMock ()
@@ -994,7 +999,7 @@ def test_download_with_get_verifier_failure_v2(self,
994
999
service .download ,
995
1000
context = None , image_id = None ,
996
1001
data = None , dst_path = None )
997
- mock_log .error .assert_called_once_with ( mock . ANY , mock . ANY )
1002
+ self . assertEqual ( mock_log .error .call_count , 2 )
998
1003
999
1004
@mock .patch ('nova.image.glance.LOG' )
1000
1005
@mock .patch ('nova.image.glance.GlanceImageServiceV2.show' )
@@ -1052,6 +1057,33 @@ def test_download_dst_path_signature_fail_v2(self, mock_fsync,
1052
1057
mock_dest .truncate .assert_called_once_with (0 )
1053
1058
self .assertTrue (mock_dest .close .called )
1054
1059
1060
+ @mock .patch ('builtins.open' )
1061
+ @mock .patch ('cursive.signature_utils.get_verifier' )
1062
+ @mock .patch ('nova.image.glance.GlanceImageServiceV2.show' )
1063
+ @mock .patch ('nova.image.glance.GlanceImageServiceV2._safe_fsync' )
1064
+ def test_download_dst_path_with_invalid_signature_v2 (
1065
+ self , mock_fsync , mock_show , mock_get_verifier , mock_open ):
1066
+ glance_iterable = mock .MagicMock (spec = io .BytesIO )
1067
+ glance_iterable .__iter__ .return_value = self .fake_img_data
1068
+ self .client .call .return_value = fake_glance_response (
1069
+ glanceclient .common .utils .IterableWithLength (
1070
+ iterable = glance_iterable , length = len (self .fake_img_data )))
1071
+ service = glance .GlanceImageServiceV2 (self .client )
1072
+ mock_get_verifier .side_effect = \
1073
+ cursive_exception .SignatureVerificationError
1074
+ mock_dest = mock .MagicMock ()
1075
+ mock_open .return_value = mock_dest
1076
+ mock_show .return_value = self .fake_img_props
1077
+ fake_path = 'FAKE_PATH'
1078
+ self .assertRaises (cursive_exception .SignatureVerificationError ,
1079
+ service .download ,
1080
+ context = None , image_id = None ,
1081
+ data = None , dst_path = fake_path )
1082
+ mock_open .assert_called_once_with (fake_path , 'wb' )
1083
+ mock_fsync .assert_called_once_with (mock_dest )
1084
+ mock_dest .close .assert_called ()
1085
+ glance_iterable .close .assert_called ()
1086
+
1055
1087
1056
1088
class TestDownloadCertificateValidation (test .NoDBTestCase ):
1057
1089
"""Tests the download method of the GlanceImageServiceV2 when
0 commit comments