summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2012-10-23 13:14:22 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2012-10-23 17:03:25 -0700
commitb4b897e7acebea1ccff336a5fad9313fd3f51326 (patch)
treeee340c17c7a17800514aa391f557fa97de7fc5e3
parent6ea4de1e27fc1fe4b980acc7cfdbcf621ed45afb (diff)
Handle the case where we encounter a snap shot correctly.
Fix the list in the qemu output and break out of the loop when we encounter that instead of failing to parse. Fixes bug 1070088. Change-Id: I5f501c599b94e0ac13892c97d60ec740ac7e641d
-rw-r--r--nova/tests/test_image_utils.py69
-rw-r--r--nova/virt/images.py16
2 files changed, 82 insertions, 3 deletions
diff --git a/nova/tests/test_image_utils.py b/nova/tests/test_image_utils.py
new file mode 100644
index 000000000..711f1c202
--- /dev/null
+++ b/nova/tests/test_image_utils.py
@@ -0,0 +1,69 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from nova import test
+from nova import utils
+
+from nova.virt import images
+
+
+class ImageUtilsTestCase(test.TestCase):
+ def test_qemu_info(self):
+ path = "disk.config"
+ example_output = """image: disk.config
+file format: raw
+virtual size: 64M (67108864 bytes)
+cluster_size: 65536
+disk size: 96K
+blah BLAH: bb
+"""
+ self.mox.StubOutWithMock(utils, 'execute')
+ utils.execute('env', 'LC_ALL=C', 'LANG=C',
+ 'qemu-img', 'info', path).AndReturn((example_output, ''))
+ self.mox.ReplayAll()
+ image_info = images.qemu_img_info(path)
+ self.assertEquals('disk.config', image_info['image'])
+ self.assertEquals('raw', image_info['file format'])
+ self.assertEquals('64M (67108864 bytes)', image_info['virtual size'])
+ self.assertEquals('96K', image_info['disk size'])
+ self.assertEquals('bb', image_info['blah blah'])
+ self.assertEquals("65536", image_info['cluster_size'])
+
+ def test_qemu_info_snap(self):
+ path = "disk.config"
+ example_output = """image: disk.config
+file format: raw
+virtual size: 64M (67108864 bytes)
+cluster_size: 65536
+disk size: 96K
+Snapshot list:
+ID TAG VM SIZE DATE VM CLOCK
+1 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
+"""
+ self.mox.StubOutWithMock(utils, 'execute')
+ utils.execute('env', 'LC_ALL=C', 'LANG=C',
+ 'qemu-img', 'info', path).AndReturn((example_output, ''))
+ self.mox.ReplayAll()
+ image_info = images.qemu_img_info(path)
+ self.assertEquals('disk.config', image_info['image'])
+ self.assertEquals('raw', image_info['file format'])
+ self.assertEquals('64M (67108864 bytes)', image_info['virtual size'])
+ self.assertEquals('96K', image_info['disk size'])
+ self.assertEquals("65536", image_info['cluster_size'])
+ # This would be triggered if the split encountered this section
+ self.assertNotIn('snapshot list', image_info)
+ bad_cap = '1 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10'
+ self.assertNotIn(bad_cap, image_info)
diff --git a/nova/virt/images.py b/nova/virt/images.py
index d94a1aeba..133f5e25b 100644
--- a/nova/virt/images.py
+++ b/nova/virt/images.py
@@ -50,11 +50,21 @@ def qemu_img_info(path):
'qemu-img', 'info', path)
# output of qemu-img is 'field: value'
+ # except when its in the snapshot listing mode
data = {}
for line in out.splitlines():
- field, val = line.split(':', 1)
- if val[0] == " ":
- val = val[1:]
+ pieces = line.split(':', 1)
+ if len(pieces) != 2:
+ continue
+ (field, val) = pieces
+ field = field.strip().lower()
+ val = val.strip()
+ if field == 'snapshot list':
+ # Skip everything after the snapshot list
+ # which is safe to do since the code prints
+ # these out at the end and nobody currently
+ # uses this information in openstack as-is.
+ break
data[field] = val
return data