Add a check for negative values in the resize method. Add the QemuImgException and tests for the new goodies
Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/e5b23b87 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/e5b23b87 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/e5b23b87 Branch: refs/heads/qemu-img Commit: e5b23b87a50256db89967e2b27b3af551a036e5e Parents: 1fa7b04 Author: Wido den Hollander <[email protected]> Authored: Mon Feb 25 14:31:08 2013 +0100 Committer: Wido den Hollander <[email protected]> Committed: Mon Feb 25 14:31:08 2013 +0100 ---------------------------------------------------------------------- .../org/apache/cloudstack/utils/qemu/QemuImg.java | 8 +++- .../apache/cloudstack/utils/qemu/QemuImgTest.java | 31 +++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e5b23b87/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java ---------------------------------------------------------------------- diff --git a/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java b/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java index b838522..fe2af6c 100644 --- a/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java +++ b/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java @@ -17,6 +17,7 @@ package org.apache.cloudstack.utils.qemu; import org.apache.cloudstack.utils.qemu.QemuImgFile; +import org.apache.cloudstack.utils.qemu.QemuImgException; import com.cloud.utils.script.Script; import com.cloud.utils.script.OutputInterpreter; @@ -295,7 +296,7 @@ public class QemuImg { * @param delta * Flag if the new size is a delta */ - public void resize(QemuImgFile file, long size, boolean delta) { + public void resize(QemuImgFile file, long size, boolean delta) throws QemuImgException { String newSize = null; if (delta) { @@ -305,6 +306,9 @@ public class QemuImg { newSize = "-" + Long.toString(size); } } else { + if (size <= 0) { + throw new QemuImgException("size should not be negative if 'delta' is false!"); + } newSize = Long.toString(size); } @@ -328,7 +332,7 @@ public class QemuImg { * @param size * The new size */ - public void resize(QemuImgFile file, long size) { + public void resize(QemuImgFile file, long size) throws QemuImgException { this.resize(file, size, false); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e5b23b87/utils/test/org/apache/cloudstack/utils/qemu/QemuImgTest.java ---------------------------------------------------------------------- diff --git a/utils/test/org/apache/cloudstack/utils/qemu/QemuImgTest.java b/utils/test/org/apache/cloudstack/utils/qemu/QemuImgTest.java index 2f0ca68..2d63564 100644 --- a/utils/test/org/apache/cloudstack/utils/qemu/QemuImgTest.java +++ b/utils/test/org/apache/cloudstack/utils/qemu/QemuImgTest.java @@ -91,6 +91,37 @@ public class QemuImgTest { long endSize = 40960; QemuImgFile file = new QemuImgFile(filename, startSize, PhysicalDiskFormat.QCOW2); + try { + QemuImg qemu = new QemuImg(); + qemu.create(file); + qemu.resize(file, endSize); + Map<String, String> info = qemu.info(file); + + if (info == null) { + fail("We didn't get any information back from qemu-img"); + } + + Long infoSize = Long.parseLong(info.get(new String("virtual_size"))); + assertEquals(Long.valueOf(endSize), Long.valueOf(infoSize)); + } catch (QemuImgException e) { + fail(e.getMessage()); + } + + File f = new File(filename); + f.delete(); + + } + + @Test(expected = QemuImgException.class) + public void testCreateAndResizeFail() throws QemuImgException { + String filename = "/tmp/test-resize-image.qcow2"; + + long startSize = 20480; + + /* Negative new size, expect failure */ + long endSize = -1; + QemuImgFile file = new QemuImgFile(filename, startSize, PhysicalDiskFormat.QCOW2); + QemuImg qemu = new QemuImg(); qemu.create(file); qemu.resize(file, endSize);
