1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
18
from nova import utils
20
from nova.virt import images
21
from nova.virt.libvirt import utils as libvirt_utils
24
class ImageUtilsTestCase(test.TestCase):
25
def test_disk_type(self):
26
# Seems like lvm detection
28
for p in ['/dev/b', '/dev/blah/blah']:
29
d_type = libvirt_utils.get_disk_type(p)
30
self.assertEquals('lvm', d_type)
32
template_output = """image: %(path)s
33
file format: %(format)s
34
virtual size: 64M (67108864 bytes)
38
path = '/myhome/disk.config'
39
for f in ['raw', 'qcow2']:
40
output = template_output % ({
44
self.mox.StubOutWithMock(utils, 'execute')
45
utils.execute('env', 'LC_ALL=C', 'LANG=C',
46
'qemu-img', 'info', path).AndReturn((output, ''))
48
d_type = libvirt_utils.get_disk_type(path)
49
self.assertEquals(f, d_type)
52
def test_disk_backing(self):
53
path = '/myhome/disk.config'
54
template_output = """image: %(path)s
56
virtual size: 2K (2048 bytes)
60
output = template_output % ({
63
self.mox.StubOutWithMock(utils, 'execute')
64
utils.execute('env', 'LC_ALL=C', 'LANG=C',
65
'qemu-img', 'info', path).AndReturn((output, ''))
67
d_backing = libvirt_utils.get_disk_backing_file(path)
68
self.assertEquals(None, d_backing)
70
def test_disk_size(self):
71
path = '/myhome/disk.config'
72
template_output = """image: %(path)s
74
virtual size: %(v_size)s (%(vsize_b)s bytes)
78
for i in range(0, 128):
81
mbytes = kbytes / 1024
82
output = template_output % ({
83
'v_size': "%sM" % (mbytes),
87
self.mox.StubOutWithMock(utils, 'execute')
88
utils.execute('env', 'LC_ALL=C', 'LANG=C',
89
'qemu-img', 'info', path).AndReturn((output, ''))
91
d_size = libvirt_utils.get_disk_size(path)
92
self.assertEquals(i, d_size)
94
output = template_output % ({
95
'v_size': "%sK" % (kbytes),
99
self.mox.StubOutWithMock(utils, 'execute')
100
utils.execute('env', 'LC_ALL=C', 'LANG=C',
101
'qemu-img', 'info', path).AndReturn((output, ''))
103
d_size = libvirt_utils.get_disk_size(path)
104
self.assertEquals(i, d_size)
105
self.mox.UnsetStubs()
107
def test_qemu_info_canon(self):
109
example_output = """image: disk.config
111
virtual size: 64M (67108864 bytes)
116
self.mox.StubOutWithMock(utils, 'execute')
117
utils.execute('env', 'LC_ALL=C', 'LANG=C',
118
'qemu-img', 'info', path).AndReturn((example_output, ''))
120
image_info = images.qemu_img_info(path)
121
self.assertEquals('disk.config', image_info.image)
122
self.assertEquals('raw', image_info.file_format)
123
self.assertEquals(67108864, image_info.virtual_size)
124
self.assertEquals(98304, image_info.disk_size)
125
self.assertEquals(65536, image_info.cluster_size)
127
def test_qemu_info_canon2(self):
129
example_output = """image: disk.config
131
virtual size: 67108844
134
backing file: /var/lib/nova/a328c7998805951a_2
136
self.mox.StubOutWithMock(utils, 'execute')
137
utils.execute('env', 'LC_ALL=C', 'LANG=C',
138
'qemu-img', 'info', path).AndReturn((example_output, ''))
140
image_info = images.qemu_img_info(path)
141
self.assertEquals('disk.config', image_info.image)
142
self.assertEquals('qcow2', image_info.file_format)
143
self.assertEquals(67108844, image_info.virtual_size)
144
self.assertEquals(963434, image_info.disk_size)
145
self.assertEquals(65536, image_info.cluster_size)
146
self.assertEquals('/var/lib/nova/a328c7998805951a_2',
147
image_info.backing_file)
149
def test_qemu_backing_file_actual(self):
151
example_output = """image: disk.config
153
virtual size: 64M (67108864 bytes)
157
ID TAG VM SIZE DATE VM CLOCK
158
1 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
159
backing file: /var/lib/nova/a328c7998805951a_2 (actual path: /b/3a988059e51a_2)
161
self.mox.StubOutWithMock(utils, 'execute')
162
utils.execute('env', 'LC_ALL=C', 'LANG=C',
163
'qemu-img', 'info', path).AndReturn((example_output, ''))
165
image_info = images.qemu_img_info(path)
166
self.assertEquals('disk.config', image_info.image)
167
self.assertEquals('raw', image_info.file_format)
168
self.assertEquals(67108864, image_info.virtual_size)
169
self.assertEquals(98304, image_info.disk_size)
170
self.assertEquals(1, len(image_info.snapshots))
171
self.assertEquals('/b/3a988059e51a_2',
172
image_info.backing_file)
174
def test_qemu_info_convert(self):
176
example_output = """image: disk.config
181
ID TAG VM SIZE DATE VM CLOCK
182
1 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
183
3 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
184
4 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
187
self.mox.StubOutWithMock(utils, 'execute')
188
utils.execute('env', 'LC_ALL=C', 'LANG=C',
189
'qemu-img', 'info', path).AndReturn((example_output, ''))
191
image_info = images.qemu_img_info(path)
192
self.assertEquals('disk.config', image_info.image)
193
self.assertEquals('raw', image_info.file_format)
194
self.assertEquals(67108864, image_info.virtual_size)
195
self.assertEquals(98304, image_info.disk_size)
197
def test_qemu_info_snaps(self):
199
example_output = """image: disk.config
201
virtual size: 64M (67108864 bytes)
204
ID TAG VM SIZE DATE VM CLOCK
205
1 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
206
3 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
207
4 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
209
self.mox.StubOutWithMock(utils, 'execute')
210
utils.execute('env', 'LC_ALL=C', 'LANG=C',
211
'qemu-img', 'info', path).AndReturn((example_output, ''))
213
image_info = images.qemu_img_info(path)
214
self.assertEquals('disk.config', image_info.image)
215
self.assertEquals('raw', image_info.file_format)
216
self.assertEquals(67108864, image_info.virtual_size)
217
self.assertEquals(98304, image_info.disk_size)
218
self.assertEquals(3, len(image_info.snapshots))