@@ -118,24 +118,86 @@ def test_create_image(self, mock_execute):
118118 @mock .patch ('oslo_concurrency.processutils.execute' )
119119 @mock .patch ('nova.virt.images.qemu_img_info' )
120120 @mock .patch ('nova.image.format_inspector.detect_file_format' )
121- def test_create_cow_image (self , mock_detect , mock_info , mock_execute ,
122- mock_exists ):
121+ def _test_create_cow_image (
122+ self , mock_detect , mock_info , mock_execute ,
123+ mock_exists , backing_file = None , safety_check = True
124+ ):
125+ if isinstance (backing_file , dict ):
126+ backing_info = backing_file
127+ backing_file = backing_info .pop ('file' , None )
128+ else :
129+ backing_info = {}
130+ backing_backing_file = backing_info .pop ('backing_file' , None )
131+ backing_fmt = backing_info .pop ('backing_fmt' ,
132+ mock .sentinel .backing_fmt )
133+
123134 mock_execute .return_value = ('stdout' , None )
124135 mock_info .return_value = mock .Mock (
125- file_format = mock . sentinel . backing_fmt ,
136+ file_format = backing_fmt ,
126137 cluster_size = mock .sentinel .cluster_size ,
127- backing_file = None )
128- mock_detect .return_value .safety_check .return_value = True
138+ backing_file = backing_backing_file ,
139+ format_specific = backing_info )
140+
141+ mock_detect .return_value .safety_check .return_value = safety_check
142+
129143 libvirt_utils .create_cow_image (mock .sentinel .backing_path ,
130144 mock .sentinel .new_path )
131145 mock_info .assert_called_once_with (mock .sentinel .backing_path )
132146 mock_execute .assert_has_calls ([mock .call (
133147 'qemu-img' , 'create' , '-f' , 'qcow2' , '-o' ,
134148 'backing_file=%s,backing_fmt=%s,cluster_size=%s' % (
135- mock .sentinel .backing_path , mock . sentinel . backing_fmt ,
149+ mock .sentinel .backing_path , backing_fmt ,
136150 mock .sentinel .cluster_size ),
137151 mock .sentinel .new_path )])
138- mock_detect .return_value .safety_check .assert_called_once_with ()
152+ if backing_file :
153+ mock_detect .return_value .safety_check .assert_called_once_with ()
154+
155+ def test_create_image_qcow2 (self ):
156+ self ._test_create_cow_image ()
157+
158+ def test_create_image_backing_file (self ):
159+ self ._test_create_cow_image (
160+ backing_file = mock .sentinel .backing_file
161+ )
162+
163+ def test_create_image_base_has_backing_file (self ):
164+ self .assertRaises (
165+ exception .InvalidDiskInfo ,
166+ self ._test_create_cow_image ,
167+ backing_file = {'file' : mock .sentinel .backing_file ,
168+ 'backing_file' : mock .sentinel .backing_backing_file },
169+ )
170+
171+ def test_create_image_base_has_data_file (self ):
172+ self .assertRaises (
173+ exception .InvalidDiskInfo ,
174+ self ._test_create_cow_image ,
175+ backing_file = {'file' : mock .sentinel .backing_file ,
176+ 'backing_file' : mock .sentinel .backing_backing_file ,
177+ 'data' : {'data-file' : mock .sentinel .data_file }},
178+ )
179+
180+ def test_create_image_size_none (self ):
181+ self ._test_create_cow_image (
182+ backing_file = mock .sentinel .backing_file ,
183+ )
184+
185+ def test_create_image_vmdk (self ):
186+ self ._test_create_cow_image (
187+ backing_file = {'file' : mock .sentinel .backing_file ,
188+ 'backing_fmt' : 'vmdk' ,
189+ 'backing_file' : None ,
190+ 'data' : {'create-type' : 'monolithicSparse' }}
191+ )
192+
193+ def test_create_image_vmdk_invalid_type (self ):
194+ self .assertRaises (exception .ImageUnacceptable ,
195+ self ._test_create_cow_image ,
196+ backing_file = {'file' : mock .sentinel .backing_file ,
197+ 'backing_fmt' : 'vmdk' ,
198+ 'backing_file' : None ,
199+ 'data' : {'create-type' : 'monolithicFlat' }}
200+ )
139201
140202 @ddt .unpack
141203 @ddt .data ({'fs_type' : 'some_fs_type' ,
0 commit comments