Skip to content

Commit aaadbb6

Browse files
committed
fix tests
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 2c9471e commit aaadbb6

File tree

2 files changed

+134
-16
lines changed

2 files changed

+134
-16
lines changed

plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImg.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,17 @@ public void convert(final QemuImgFile srcFile, final QemuImgFile destFile,
395395
convert(srcFile, destFile, options, qemuObjects, srcImageOpts, snapshotName, forceSourceFormat, false);
396396
}
397397

398+
protected Map<String, String> getResizeOptionsFromConvertOptions(final Map<String, String> options) {
399+
if (MapUtils.isEmpty(options)) {
400+
return null;
401+
}
402+
Map<String, String> resizeOpts = new HashMap<>();
403+
if (options.containsKey(PREALLOCATION)) {
404+
resizeOpts.put(PREALLOCATION, options.get(PREALLOCATION));
405+
}
406+
return resizeOpts;
407+
}
408+
398409
/**
399410
* Converts an image from source to destination.
400411
*
@@ -485,11 +496,7 @@ public void convert(final QemuImgFile srcFile, final QemuImgFile destFile,
485496
}
486497

487498
if (srcFile.getSize() < destFile.getSize()) {
488-
Map<String, String> resizeOpts = new HashMap<>();
489-
if (options.containsKey(PREALLOCATION)) {
490-
resizeOpts.put(PREALLOCATION, options.get(PREALLOCATION));
491-
}
492-
this.resize(destFile, destFile.getSize(), resizeOpts);
499+
this.resize(destFile, destFile.getSize(), getResizeOptionsFromConvertOptions(options));
493500
}
494501
}
495502

@@ -696,7 +703,7 @@ public void deleteSnapshot(final QemuImageOptions srcImageOpts, final String sna
696703
}
697704
}
698705

699-
private void addScriptOptionsFromMap(Map<String, String> options, Script s) {
706+
protected void addScriptOptionsFromMap(Map<String, String> options, Script s) {
700707
if (MapUtils.isEmpty(options)) {
701708
return;
702709
}

plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImgTest.java

Lines changed: 121 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,28 @@
2222
import static org.junit.Assert.fail;
2323

2424
import java.io.File;
25-
import com.cloud.utils.script.Script;
26-
2725
import java.nio.file.Path;
2826
import java.nio.file.Paths;
2927
import java.util.ArrayList;
28+
import java.util.Collections;
3029
import java.util.HashMap;
3130
import java.util.List;
3231
import java.util.Map;
3332
import java.util.UUID;
3433

34+
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
35+
import org.apache.commons.collections.MapUtils;
3536
import org.junit.Assert;
3637
import org.junit.Ignore;
3738
import org.junit.Test;
38-
39-
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
39+
import org.junit.runner.RunWith;
4040
import 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)
4347
public 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

Comments
 (0)