2222import static org .junit .Assert .fail ;
2323
2424import java .io .File ;
25- import com .cloud .utils .script .Script ;
26-
2725import java .nio .file .Path ;
2826import java .nio .file .Paths ;
2927import java .util .ArrayList ;
28+ import java .util .Collections ;
3029import java .util .HashMap ;
3130import java .util .List ;
3231import java .util .Map ;
3332import java .util .UUID ;
3433
34+ import org .apache .cloudstack .utils .qemu .QemuImg .PhysicalDiskFormat ;
35+ import org .apache .commons .collections .MapUtils ;
3536import org .junit .Assert ;
3637import org .junit .Ignore ;
3738import org .junit .Test ;
38-
39- import org .apache .cloudstack .utils .qemu .QemuImg .PhysicalDiskFormat ;
39+ import org .junit .runner .RunWith ;
4040import org .libvirt .LibvirtException ;
41+ import org .mockito .Mockito ;
42+ import org .mockito .junit .MockitoJUnitRunner ;
43+
44+ import com .cloud .utils .script .Script ;
4145
42- @ Ignore
46+ @ RunWith ( MockitoJUnitRunner . class )
4347public class QemuImgTest {
4448
4549 @ Test
@@ -130,8 +134,7 @@ public void testCreateWithSecretObject() throws QemuImgException, LibvirtExcepti
130134 public void testCreateSparseVolume () throws QemuImgException , LibvirtException {
131135 String filename = "/tmp/" + UUID .randomUUID () + ".qcow2" ;
132136
133- /* 10TB virtual_size */
134- long size = 10995116277760l ;
137+ long size = 10 * 1024 * 1024L ;
135138 QemuImgFile file = new QemuImgFile (filename , size , PhysicalDiskFormat .QCOW2 );
136139 String preallocation = "metadata" ;
137140 Map <String , String > options = new HashMap <String , String >();
@@ -141,8 +144,8 @@ public void testCreateSparseVolume() throws QemuImgException, LibvirtException {
141144 QemuImg qemu = new QemuImg (0 );
142145 qemu .create (file , options );
143146
144- String allocatedSize = Script .runSimpleBashScript (String .format ("ls -alhs %s | awk '{print $1}'" , file ));
145- String declaredSize = Script .runSimpleBashScript (String .format ("ls -alhs %s | awk '{print $6}'" , file ));
147+ String allocatedSize = Script .runSimpleBashScript (String .format ("ls -alhs %s | awk '{print $1}'" , filename ));
148+ String declaredSize = Script .runSimpleBashScript (String .format ("ls -alhs %s | awk '{print $6}'" , filename ));
146149
147150 assertFalse (allocatedSize .equals (declaredSize ));
148151
@@ -208,6 +211,9 @@ public void testCreateAndResizeDeltaPositive() throws QemuImgException, LibvirtE
208211 f .delete ();
209212 }
210213
214+ // This test is failing and needs changes in QemuImg.resize to support shrinking images with delta sizes.
215+ // Earlier whole test suite was ignored, now only this test is ignored to allow other tests to run.
216+ @ Ignore
211217 @ Test
212218 public void testCreateAndResizeDeltaNegative () throws QemuImgException , LibvirtException {
213219 String filename = "/tmp/" + UUID .randomUUID () + ".qcow2" ;
@@ -377,12 +383,117 @@ public void testCheckAndRepair() throws LibvirtException {
377383
378384 try {
379385 QemuImg qemu = new QemuImg (0 );
380- qemu .checkAndRepair (file , null , null , null );
386+ qemu .checkAndRepair (file , new QemuImageOptions ( Collections . emptyMap ()), Collections . emptyList () , null );
381387 } catch (QemuImgException e ) {
382388 fail (e .getMessage ());
383389 }
384390
385391 File f = new File (filename );
386392 f .delete ();
387393 }
394+
395+ @ Test
396+ public void addScriptOptionsFromMapAddsValidOptions () throws LibvirtException , QemuImgException {
397+ Script script = Mockito .mock (Script .class );
398+ Map <String , String > options = new HashMap <>();
399+ options .put ("key1" , "value1" );
400+ options .put ("key2" , "value2" );
401+
402+ QemuImg qemu = new QemuImg (0 );
403+ qemu .addScriptOptionsFromMap (options , script );
404+
405+ Mockito .verify (script , Mockito .times (1 )).add ("-o" );
406+ Mockito .verify (script , Mockito .times (1 )).add ("key1=value1,key2=value2" );
407+ }
408+
409+ @ Test
410+ public void addScriptOptionsFromMapHandlesEmptyOptions () throws LibvirtException , QemuImgException {
411+ Script script = Mockito .mock (Script .class );
412+ Map <String , String > options = new HashMap <>();
413+
414+ QemuImg qemu = new QemuImg (0 );
415+ qemu .addScriptOptionsFromMap (options , script );
416+
417+ Mockito .verify (script , Mockito .never ()).add (Mockito .anyString ());
418+ }
419+
420+ @ Test
421+ public void addScriptOptionsFromMapHandlesNullOptions () throws LibvirtException , QemuImgException {
422+ Script script = Mockito .mock (Script .class );
423+
424+ QemuImg qemu = new QemuImg (0 );
425+ qemu .addScriptOptionsFromMap (null , script );
426+
427+ Mockito .verify (script , Mockito .never ()).add (Mockito .anyString ());
428+ }
429+
430+ @ Test
431+ public void addScriptOptionsFromMapHandlesTrailingComma () throws LibvirtException , QemuImgException {
432+ Script script = Mockito .mock (Script .class );
433+ Map <String , String > options = new HashMap <>();
434+ options .put ("key1" , "value1" );
435+
436+ QemuImg qemu = new QemuImg (0 );
437+ qemu .addScriptOptionsFromMap (options , script );
438+
439+ Mockito .verify (script , Mockito .times (1 )).add ("-o" );
440+ Mockito .verify (script , Mockito .times (1 )).add ("key1=value1" );
441+ }
442+
443+ @ Test
444+ public void getResizeOptionsFromConvertOptionsReturnsNullForEmptyOptions () throws LibvirtException , QemuImgException {
445+ QemuImg qemuImg = new QemuImg (0 );
446+ Map <String , String > options = new HashMap <>();
447+
448+ Map <String , String > result = qemuImg .getResizeOptionsFromConvertOptions (options );
449+
450+ Assert .assertNull (result );
451+ }
452+
453+ @ Test
454+ public void getResizeOptionsFromConvertOptionsReturnsNullForNullOptions () throws LibvirtException , QemuImgException {
455+ QemuImg qemuImg = new QemuImg (0 );
456+
457+ Map <String , String > result = qemuImg .getResizeOptionsFromConvertOptions (null );
458+
459+ Assert .assertNull (result );
460+ }
461+
462+ @ Test
463+ public void getResizeOptionsFromConvertOptionsReturnsPreallocationOption () throws LibvirtException , QemuImgException {
464+ QemuImg qemuImg = new QemuImg (0 );
465+ Map <String , String > options = new HashMap <>();
466+ options .put (QemuImg .PREALLOCATION , "metadata" );
467+
468+ Map <String , String > result = qemuImg .getResizeOptionsFromConvertOptions (options );
469+
470+ Assert .assertNotNull (result );
471+ assertEquals (1 , result .size ());
472+ assertEquals ("metadata" , result .get (QemuImg .PREALLOCATION ));
473+ }
474+
475+ @ Test
476+ public void getResizeOptionsFromConvertOptionsIgnoresUnrelatedOptions () throws LibvirtException , QemuImgException {
477+ QemuImg qemuImg = new QemuImg (0 );
478+ Map <String , String > options = new HashMap <>();
479+ options .put ("unrelatedKey" , "unrelatedValue" );
480+
481+ Map <String , String > result = qemuImg .getResizeOptionsFromConvertOptions (options );
482+
483+ Assert .assertTrue (MapUtils .isEmpty (result ));
484+ }
485+
486+ @ Test
487+ public void getResizeOptionsFromConvertOptionsHandlesMixedOptions () throws LibvirtException , QemuImgException {
488+ QemuImg qemuImg = new QemuImg (0 );
489+ Map <String , String > options = new HashMap <>();
490+ options .put (QemuImg .PREALLOCATION , "full" );
491+ options .put ("unrelatedKey" , "unrelatedValue" );
492+
493+ Map <String , String > result = qemuImg .getResizeOptionsFromConvertOptions (options );
494+
495+ Assert .assertNotNull (result );
496+ assertEquals (1 , result .size ());
497+ assertEquals ("full" , result .get (QemuImg .PREALLOCATION ));
498+ }
388499}
0 commit comments