~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/fix-ubuntu-tests.patch/nova/tests/test_imagebackend.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Adam Gandelman
  • Date: 2013-02-22 09:27:29 UTC
  • mfrom: (1.1.68)
  • Revision ID: package-import@ubuntu.com-20130222092729-nn3gt8rf97uvts77
Tags: 2013.1.g3-0ubuntu1
[ Chuck Short ]
* New usptream release. 
* debian/patches/debian/patches/fix-ubuntu-tests.patch: Refreshed.
* debian/nova-baremetal.logrotate: Fix logfile path.
* debian/control, debian/nova-spiceproxy.{install, logrotate, upstart}:
  Add spice html5 proxy support.
* debian/nova-novncproxy.upstart: Start on runlevel [2345]
* debian/rules: Call testr directly since run_tests.sh -N gives weird return
  value when tests pass.
* debian/pyddist-overrides: Add websockify.
* debian/nova-common.postinst: Removed config file conversion, since
  the option is no longer available. (LP: #1110567)
* debian/control: Add python-pyasn1 as a dependency.
* debian/control: Add python-oslo-config as a dependency.
* debian/control: Suggest sysfsutils, sg3-utils, multipath-tools for fibre
  channel support.

[ Adam Gandelman ]
* debian/control: Fix typo (websocikfy -> websockify).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2012 Grid Dynamics
4
 
# All Rights Reserved.
5
 
#
6
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
7
 
#    not use this file except in compliance with the License. You may obtain
8
 
#    a copy of the License at
9
 
#
10
 
#         http://www.apache.org/licenses/LICENSE-2.0
11
 
#
12
 
#    Unless required by applicable law or agreed to in writing, software
13
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 
#    License for the specific language governing permissions and limitations
16
 
#    under the License.
17
 
 
18
 
import fixtures
19
 
import os
20
 
 
21
 
from nova.openstack.common import cfg
22
 
from nova import test
23
 
from nova.tests import fake_libvirt_utils
24
 
from nova.virt.libvirt import imagebackend
25
 
 
26
 
CONF = cfg.CONF
27
 
 
28
 
 
29
 
class _ImageTestCase(object):
30
 
    INSTANCES_PATH = '/fake'
31
 
 
32
 
    def mock_create_image(self, image):
33
 
        def create_image(fn, base, size, *args, **kwargs):
34
 
            fn(target=base, *args, **kwargs)
35
 
        image.create_image = create_image
36
 
 
37
 
    def setUp(self):
38
 
        super(_ImageTestCase, self).setUp()
39
 
        self.flags(disable_process_locking=True,
40
 
                   instances_path=self.INSTANCES_PATH)
41
 
        self.INSTANCE = 'instance'
42
 
        self.NAME = 'fake.vm'
43
 
        self.TEMPLATE = 'template'
44
 
 
45
 
        self.PATH = os.path.join(CONF.instances_path, self.INSTANCE,
46
 
                                 self.NAME)
47
 
        self.TEMPLATE_DIR = os.path.join(CONF.instances_path,
48
 
                                         '_base')
49
 
        self.TEMPLATE_PATH = os.path.join(self.TEMPLATE_DIR, 'template')
50
 
 
51
 
        self.useFixture(fixtures.MonkeyPatch(
52
 
            'nova.virt.libvirt.imagebackend.libvirt_utils',
53
 
            fake_libvirt_utils))
54
 
 
55
 
    def test_cache(self):
56
 
        self.mox.StubOutWithMock(os.path, 'exists')
57
 
        os.path.exists(self.PATH).AndReturn(False)
58
 
        os.path.exists(self.TEMPLATE_DIR).AndReturn(False)
59
 
        os.path.exists(self.TEMPLATE_PATH).AndReturn(False)
60
 
        fn = self.mox.CreateMockAnything()
61
 
        fn(target=self.TEMPLATE_PATH)
62
 
        self.mox.StubOutWithMock(imagebackend.fileutils, 'ensure_tree')
63
 
        imagebackend.fileutils.ensure_tree(self.TEMPLATE_DIR)
64
 
        self.mox.ReplayAll()
65
 
 
66
 
        image = self.image_class(self.INSTANCE, self.NAME)
67
 
        self.mock_create_image(image)
68
 
        image.cache(fn, self.TEMPLATE)
69
 
 
70
 
        self.mox.VerifyAll()
71
 
 
72
 
    def test_cache_image_exists(self):
73
 
        self.mox.StubOutWithMock(os.path, 'exists')
74
 
        os.path.exists(self.PATH).AndReturn(True)
75
 
        self.mox.ReplayAll()
76
 
 
77
 
        image = self.image_class(self.INSTANCE, self.NAME)
78
 
        image.cache(None, self.TEMPLATE)
79
 
 
80
 
        self.mox.VerifyAll()
81
 
 
82
 
    def test_cache_base_dir_exists(self):
83
 
        self.mox.StubOutWithMock(os.path, 'exists')
84
 
        os.path.exists(self.PATH).AndReturn(False)
85
 
        os.path.exists(self.TEMPLATE_DIR).AndReturn(True)
86
 
        os.path.exists(self.TEMPLATE_PATH).AndReturn(False)
87
 
        fn = self.mox.CreateMockAnything()
88
 
        fn(target=self.TEMPLATE_PATH)
89
 
        self.mox.StubOutWithMock(imagebackend.fileutils, 'ensure_tree')
90
 
        self.mox.ReplayAll()
91
 
 
92
 
        image = self.image_class(self.INSTANCE, self.NAME)
93
 
        self.mock_create_image(image)
94
 
        image.cache(fn, self.TEMPLATE)
95
 
 
96
 
        self.mox.VerifyAll()
97
 
 
98
 
    def test_cache_template_exists(self):
99
 
        self.mox.StubOutWithMock(os.path, 'exists')
100
 
        os.path.exists(self.PATH).AndReturn(False)
101
 
        os.path.exists(self.TEMPLATE_DIR).AndReturn(True)
102
 
        os.path.exists(self.TEMPLATE_PATH).AndReturn(True)
103
 
        fn = self.mox.CreateMockAnything()
104
 
        self.mox.ReplayAll()
105
 
 
106
 
        image = self.image_class(self.INSTANCE, self.NAME)
107
 
        self.mock_create_image(image)
108
 
        image.cache(fn, self.TEMPLATE)
109
 
 
110
 
        self.mox.VerifyAll()
111
 
 
112
 
 
113
 
class RawTestCase(_ImageTestCase, test.TestCase):
114
 
 
115
 
    SIZE = 1024
116
 
 
117
 
    def setUp(self):
118
 
        self.image_class = imagebackend.Raw
119
 
        super(RawTestCase, self).setUp()
120
 
 
121
 
    def prepare_mocks(self):
122
 
        fn = self.mox.CreateMockAnything()
123
 
        self.mox.StubOutWithMock(imagebackend.lockutils.synchronized,
124
 
                                 '__call__')
125
 
        self.mox.StubOutWithMock(imagebackend.libvirt_utils, 'copy_image')
126
 
        self.mox.StubOutWithMock(imagebackend.disk, 'extend')
127
 
        return fn
128
 
 
129
 
    def test_create_image(self):
130
 
        fn = self.prepare_mocks()
131
 
        fn(target=self.TEMPLATE_PATH, image_id=None)
132
 
        imagebackend.libvirt_utils.copy_image(self.TEMPLATE_PATH, self.PATH)
133
 
        self.mox.ReplayAll()
134
 
 
135
 
        image = self.image_class(self.INSTANCE, self.NAME)
136
 
        image.create_image(fn, self.TEMPLATE_PATH, None, image_id=None)
137
 
 
138
 
        self.mox.VerifyAll()
139
 
 
140
 
    def test_create_image_generated(self):
141
 
        fn = self.prepare_mocks()
142
 
        fn(target=self.PATH)
143
 
        self.mox.ReplayAll()
144
 
 
145
 
        image = self.image_class(self.INSTANCE, self.NAME)
146
 
        image.create_image(fn, self.TEMPLATE_PATH, None)
147
 
 
148
 
        self.mox.VerifyAll()
149
 
 
150
 
    def test_create_image_extend(self):
151
 
        fn = self.prepare_mocks()
152
 
        fn(target=self.TEMPLATE_PATH, image_id=None)
153
 
        imagebackend.libvirt_utils.copy_image(self.TEMPLATE_PATH, self.PATH)
154
 
        imagebackend.disk.extend(self.PATH, self.SIZE)
155
 
        self.mox.ReplayAll()
156
 
 
157
 
        image = self.image_class(self.INSTANCE, self.NAME)
158
 
        image.create_image(fn, self.TEMPLATE_PATH, self.SIZE, image_id=None)
159
 
 
160
 
        self.mox.VerifyAll()
161
 
 
162
 
 
163
 
class Qcow2TestCase(_ImageTestCase, test.TestCase):
164
 
    SIZE = 1024 * 1024 * 1024
165
 
 
166
 
    def setUp(self):
167
 
        self.image_class = imagebackend.Qcow2
168
 
        super(Qcow2TestCase, self).setUp()
169
 
        self.QCOW2_BASE = (self.TEMPLATE_PATH +
170
 
                           '_%d' % (self.SIZE / (1024 * 1024 * 1024)))
171
 
 
172
 
    def prepare_mocks(self):
173
 
        fn = self.mox.CreateMockAnything()
174
 
        self.mox.StubOutWithMock(imagebackend.lockutils.synchronized,
175
 
                                 '__call__')
176
 
        self.mox.StubOutWithMock(imagebackend.libvirt_utils,
177
 
                                 'create_cow_image')
178
 
        self.mox.StubOutWithMock(imagebackend.libvirt_utils, 'copy_image')
179
 
        self.mox.StubOutWithMock(imagebackend.disk, 'extend')
180
 
        return fn
181
 
 
182
 
    def test_create_image(self):
183
 
        fn = self.prepare_mocks()
184
 
        fn(target=self.TEMPLATE_PATH)
185
 
        imagebackend.libvirt_utils.create_cow_image(self.TEMPLATE_PATH,
186
 
                                                    self.PATH)
187
 
        self.mox.ReplayAll()
188
 
 
189
 
        image = self.image_class(self.INSTANCE, self.NAME)
190
 
        image.create_image(fn, self.TEMPLATE_PATH, None)
191
 
 
192
 
        self.mox.VerifyAll()
193
 
 
194
 
    def test_create_image_with_size(self):
195
 
        fn = self.prepare_mocks()
196
 
        fn(target=self.TEMPLATE_PATH)
197
 
        self.mox.StubOutWithMock(os.path, 'exists')
198
 
        imagebackend.libvirt_utils.create_cow_image(self.TEMPLATE_PATH,
199
 
                                                    self.PATH)
200
 
        imagebackend.disk.extend(self.PATH, self.SIZE)
201
 
        self.mox.ReplayAll()
202
 
 
203
 
        image = self.image_class(self.INSTANCE, self.NAME)
204
 
        image.create_image(fn, self.TEMPLATE_PATH, self.SIZE)
205
 
 
206
 
        self.mox.VerifyAll()
207
 
 
208
 
 
209
 
class LvmTestCase(_ImageTestCase, test.TestCase):
210
 
    VG = 'FakeVG'
211
 
    TEMPLATE_SIZE = 512
212
 
    SIZE = 1024
213
 
 
214
 
    def setUp(self):
215
 
        self.image_class = imagebackend.Lvm
216
 
        super(LvmTestCase, self).setUp()
217
 
        self.flags(libvirt_images_volume_group=self.VG)
218
 
        self.LV = '%s_%s' % (self.INSTANCE, self.NAME)
219
 
        self.PATH = os.path.join('/dev', self.VG, self.LV)
220
 
 
221
 
        self.disk = imagebackend.disk
222
 
        self.utils = imagebackend.utils
223
 
        self.libvirt_utils = imagebackend.libvirt_utils
224
 
 
225
 
    def prepare_mocks(self):
226
 
        fn = self.mox.CreateMockAnything()
227
 
        self.mox.StubOutWithMock(self.disk, 'resize2fs')
228
 
        self.mox.StubOutWithMock(self.libvirt_utils, 'create_lvm_image')
229
 
        self.mox.StubOutWithMock(self.disk, 'get_disk_size')
230
 
        self.mox.StubOutWithMock(self.utils, 'execute')
231
 
        return fn
232
 
 
233
 
    def _create_image(self, sparse):
234
 
        fn = self.prepare_mocks()
235
 
        fn(target=self.TEMPLATE_PATH)
236
 
        self.libvirt_utils.create_lvm_image(self.VG,
237
 
                                            self.LV,
238
 
                                            self.TEMPLATE_SIZE,
239
 
                                            sparse=sparse)
240
 
        self.disk.get_disk_size(self.TEMPLATE_PATH
241
 
                                         ).AndReturn(self.TEMPLATE_SIZE)
242
 
        cmd = ('dd', 'if=%s' % self.TEMPLATE_PATH,
243
 
               'of=%s' % self.PATH, 'bs=4M')
244
 
        self.utils.execute(*cmd, run_as_root=True)
245
 
        self.mox.ReplayAll()
246
 
 
247
 
        image = self.image_class(self.INSTANCE, self.NAME)
248
 
        image.create_image(fn, self.TEMPLATE_PATH, None)
249
 
 
250
 
        self.mox.VerifyAll()
251
 
 
252
 
    def _create_image_generated(self, sparse):
253
 
        fn = self.prepare_mocks()
254
 
        self.libvirt_utils.create_lvm_image(self.VG, self.LV,
255
 
                                            self.SIZE, sparse=sparse)
256
 
        fn(target=self.PATH, ephemeral_size=None)
257
 
        self.mox.ReplayAll()
258
 
 
259
 
        image = self.image_class(self.INSTANCE, self.NAME)
260
 
        image.create_image(fn, self.TEMPLATE_PATH,
261
 
                self.SIZE, ephemeral_size=None)
262
 
 
263
 
        self.mox.VerifyAll()
264
 
 
265
 
    def _create_image_resize(self, sparse):
266
 
        fn = self.prepare_mocks()
267
 
        fn(target=self.TEMPLATE_PATH)
268
 
        self.libvirt_utils.create_lvm_image(self.VG, self.LV,
269
 
                                            self.SIZE, sparse=sparse)
270
 
        self.disk.get_disk_size(self.TEMPLATE_PATH
271
 
                                         ).AndReturn(self.TEMPLATE_SIZE)
272
 
        cmd = ('dd', 'if=%s' % self.TEMPLATE_PATH,
273
 
               'of=%s' % self.PATH, 'bs=4M')
274
 
        self.utils.execute(*cmd, run_as_root=True)
275
 
        self.disk.resize2fs(self.PATH)
276
 
        self.mox.ReplayAll()
277
 
 
278
 
        image = self.image_class(self.INSTANCE, self.NAME)
279
 
        image.create_image(fn, self.TEMPLATE_PATH, self.SIZE)
280
 
 
281
 
        self.mox.VerifyAll()
282
 
 
283
 
    def test_create_image(self):
284
 
        self._create_image(False)
285
 
 
286
 
    def test_create_image_sparsed(self):
287
 
        self.flags(libvirt_sparse_logical_volumes=True)
288
 
        self._create_image(True)
289
 
 
290
 
    def test_create_image_generated(self):
291
 
        self._create_image_generated(False)
292
 
 
293
 
    def test_create_image_generated_sparsed(self):
294
 
        self.flags(libvirt_sparse_logical_volumes=True)
295
 
        self._create_image_generated(True)
296
 
 
297
 
    def test_create_image_resize(self):
298
 
        self._create_image_resize(False)
299
 
 
300
 
    def test_create_image_resize_sparsed(self):
301
 
        self.flags(libvirt_sparse_logical_volumes=True)
302
 
        self._create_image_resize(True)
303
 
 
304
 
    def test_create_image_negative(self):
305
 
        fn = self.prepare_mocks()
306
 
        fn(target=self.TEMPLATE_PATH)
307
 
        self.libvirt_utils.create_lvm_image(self.VG,
308
 
                                            self.LV,
309
 
                                            self.SIZE,
310
 
                                            sparse=False
311
 
                                            ).AndRaise(RuntimeError())
312
 
        self.disk.get_disk_size(self.TEMPLATE_PATH
313
 
                                         ).AndReturn(self.TEMPLATE_SIZE)
314
 
        self.mox.StubOutWithMock(self.libvirt_utils, 'remove_logical_volumes')
315
 
        self.libvirt_utils.remove_logical_volumes(self.PATH)
316
 
        self.mox.ReplayAll()
317
 
 
318
 
        image = self.image_class(self.INSTANCE, self.NAME)
319
 
 
320
 
        self.assertRaises(RuntimeError, image.create_image, fn,
321
 
                          self.TEMPLATE_PATH, self.SIZE)
322
 
        self.mox.VerifyAll()
323
 
 
324
 
    def test_create_image_generated_negative(self):
325
 
        fn = self.prepare_mocks()
326
 
        fn(target=self.PATH,
327
 
           ephemeral_size=None).AndRaise(RuntimeError())
328
 
        self.libvirt_utils.create_lvm_image(self.VG,
329
 
                                            self.LV,
330
 
                                            self.SIZE,
331
 
                                            sparse=False)
332
 
        self.mox.StubOutWithMock(self.libvirt_utils, 'remove_logical_volumes')
333
 
        self.libvirt_utils.remove_logical_volumes(self.PATH)
334
 
        self.mox.ReplayAll()
335
 
 
336
 
        image = self.image_class(self.INSTANCE, self.NAME)
337
 
 
338
 
        self.assertRaises(RuntimeError, image.create_image, fn,
339
 
                          self.TEMPLATE_PATH, self.SIZE,
340
 
                          ephemeral_size=None)
341
 
        self.mox.VerifyAll()
342
 
 
343
 
 
344
 
class BackendTestCase(test.TestCase):
345
 
    INSTANCE = 'fake-instance'
346
 
    NAME = 'fake-name.suffix'
347
 
 
348
 
    def get_image(self, use_cow, image_type):
349
 
        return imagebackend.Backend(use_cow).image(self.INSTANCE,
350
 
                                                   self.NAME,
351
 
                                                   image_type)
352
 
 
353
 
    def _test_image(self, image_type, image_not_cow, image_cow):
354
 
        image1 = self.get_image(False, image_type)
355
 
        image2 = self.get_image(True, image_type)
356
 
 
357
 
        def assertIsInstance(instance, class_object):
358
 
            failure = ('Expected %s,' +
359
 
                       ' but got %s.') % (class_object.__name__,
360
 
                                          instance.__class__.__name__)
361
 
            self.assertTrue(isinstance(instance, class_object), failure)
362
 
 
363
 
        assertIsInstance(image1, image_not_cow)
364
 
        assertIsInstance(image2, image_cow)
365
 
 
366
 
    def test_image_raw(self):
367
 
        self._test_image('raw', imagebackend.Raw, imagebackend.Raw)
368
 
 
369
 
    def test_image_qcow2(self):
370
 
        self._test_image('qcow2', imagebackend.Qcow2, imagebackend.Qcow2)
371
 
 
372
 
    def test_image_lvm(self):
373
 
        self.flags(libvirt_images_volume_group='FakeVG')
374
 
        self._test_image('lvm', imagebackend.Lvm, imagebackend.Lvm)
375
 
 
376
 
    def test_image_default(self):
377
 
        self._test_image('default', imagebackend.Raw, imagebackend.Qcow2)