Merge "LXC: Image device should be reset in mount() and teardown()"

This commit is contained in:
Jenkins 2013-12-19 02:57:41 +00:00 committed by Gerrit Code Review
commit ed53a102e2
2 changed files with 88 additions and 8 deletions

View File

@ -20,6 +20,7 @@ import os
from nova import test
from nova import utils
from nova.virt.disk import api as disk_api
from nova.virt.disk.mount import api as mount
from nova.virt import driver
@ -82,6 +83,87 @@ class TestVirtDriver(test.NoDBTestCase):
'swap_size': 1}))
class FakeMount(object):
def __init__(self, image, mount_dir, partition=None, device=None):
self.image = image
self.partition = partition
self.mount_dir = mount_dir
self.linked = self.mapped = self.mounted = False
self.device = device
def do_mount(self):
self.linked = True
self.mapped = True
self.mounted = True
self.device = '/dev/fake'
return True
def do_umount(self):
self.linked = True
self.mounted = False
def do_teardown(self):
self.linked = False
self.mapped = False
self.mounted = False
self.device = None
class TestDiskImage(test.NoDBTestCase):
def setUp(self):
super(TestDiskImage, self).setUp()
def test_mount(self):
image = '/tmp/fake-image'
mountdir = '/mnt/fake_rootfs'
fakemount = FakeMount(image, mountdir, None)
def fake_instance_for_format(imgfile, mountdir, partition, imgfmt):
return fakemount
self.stubs.Set(mount.Mount, 'instance_for_format',
staticmethod(fake_instance_for_format))
diskimage = disk_api._DiskImage(image=image, mount_dir=mountdir)
dev = diskimage.mount()
self.assertEqual(diskimage._mounter, fakemount)
self.assertEqual(dev, '/dev/fake')
def test_umount(self):
image = '/tmp/fake-image'
mountdir = '/mnt/fake_rootfs'
fakemount = FakeMount(image, mountdir, None)
def fake_instance_for_format(imgfile, mountdir, partition, imgfmt):
return fakemount
self.stubs.Set(mount.Mount, 'instance_for_format',
staticmethod(fake_instance_for_format))
diskimage = disk_api._DiskImage(image=image, mount_dir=mountdir)
dev = diskimage.mount()
self.assertEqual(diskimage._mounter, fakemount)
self.assertEqual(dev, '/dev/fake')
diskimage.umount()
self.assertEqual(diskimage._mounter, None)
def test_teardown(self):
image = '/tmp/fake-image'
mountdir = '/mnt/fake_rootfs'
fakemount = FakeMount(image, mountdir, None)
def fake_instance_for_format(imgfile, mountdir, partition, imgfmt):
return fakemount
self.stubs.Set(mount.Mount, 'instance_for_format',
staticmethod(fake_instance_for_format))
diskimage = disk_api._DiskImage(image=image, mount_dir=mountdir)
dev = diskimage.mount()
self.assertEqual(diskimage._mounter, fakemount)
self.assertEqual(dev, '/dev/fake')
diskimage.teardown()
self.assertEqual(diskimage._mounter, None)
class TestVirtDisk(test.NoDBTestCase):
def setUp(self):
super(TestVirtDisk, self).setUp()

View File

@ -220,8 +220,6 @@ class _DiskImage(object):
self.mount_dir = mount_dir
self.use_cow = use_cow
self.device = None
# Internal
self._mkdir = False
self._mounter = None
@ -253,7 +251,6 @@ class _DiskImage(object):
mount_name = os.path.basename(self.mount_dir or '')
self._mkdir = mount_name.startswith(self.tmp_prefix)
self.device = self._mounter.device
@property
def errors(self):
@ -285,11 +282,11 @@ class _DiskImage(object):
imgfmt)
if mounter.do_mount():
self._mounter = mounter
return self._mounter.device
else:
LOG.debug(mounter.error)
self._errors.append(mounter.error)
return bool(self._mounter)
return None
def umount(self):
"""Umount a mount point from the filesystem."""
@ -365,14 +362,15 @@ def setup_container(image, container_dir, use_cow=False):
Returns path of image device which is mounted to the container directory.
"""
img = _DiskImage(image=image, use_cow=use_cow, mount_dir=container_dir)
if not img.mount():
dev = img.mount()
if dev is None:
LOG.error(_("Failed to mount container filesystem '%(image)s' "
"on '%(target)s': %(errors)s"),
{"image": img, "target": container_dir,
"errors": img.errors})
raise exception.NovaException(img.errors)
else:
return img.device
return dev
def teardown_container(container_dir, container_root_device=None):