Refactoring driver interfaces

Added method create_share_form_snapshot to
manila.share.driver.ShareDriver.

Removed methods allocate_container, allocate_container_from_snapshot,
create_export, deallocate_container, remove_export

Implements bp refacto-driver-interfaces

Change-Id: I9f1c26b189f50aedc8e19864a172557700adf849
This commit is contained in:
Yulia Portnova 2013-12-13 14:17:49 +02:00
parent 488b11bdd1
commit 7b1f1a3b38
5 changed files with 188 additions and 338 deletions

View File

@ -51,9 +51,6 @@ CONF = cfg.CONF
CONF.register_opts(share_opts)
#TODO(rushiagr): keep the configuration option in only one class and not two
#NOTE(rushiagr): The right place for this class is manila.driver or
# manila.utils.
class ExecuteMixin(object):
"""Provides an executable functionality to a driver class."""
@ -77,7 +74,7 @@ class ExecuteMixin(object):
self._execute(*command, **kwargs)
return True
except exception.ProcessExecutionError:
tries = tries + 1
tries += 1
if tries >= self.configuration.num_shell_tries:
raise
LOG.exception(_("Recovering from a failed execute. "
@ -94,22 +91,14 @@ class ShareDriver(object):
if self.configuration:
self.configuration.append_config_values(share_opts)
def allocate_container(self, context, share):
"""Is called to allocate container for share."""
raise NotImplementedError()
def allocate_container_from_snapshot(self, context, share, snapshot):
"""Is called to create share from snapshot."""
raise NotImplementedError()
def deallocate_container(self, context, share):
"""Is called to deallocate container of share."""
raise NotImplementedError()
def create_share(self, context, share):
"""Is called to create share."""
raise NotImplementedError()
def create_share_from_snapshot(self, context, share, snapshot):
"""Is called to create share from snapshot."""
raise NotImplementedError()
def create_snapshot(self, context, snapshot):
"""Is called to create snapshot."""
raise NotImplementedError()
@ -122,14 +111,6 @@ class ShareDriver(object):
"""Is called to remove snapshot."""
raise NotImplementedError()
def create_export(self, context, share):
"""Is called to export share."""
raise NotImplementedError()
def remove_export(self, context, share):
"""Is called to stop exporting share."""
raise NotImplementedError()
def ensure_share(self, context, share):
"""Invoked to sure that share is exported."""
raise NotImplementedError()

View File

@ -71,8 +71,8 @@ class LVMShareDriver(driver.ExecuteMixin, driver.ShareDriver):
"""Do initialization."""
super(LVMShareDriver, self).__init__(*args, **kwargs)
self.db = db
self._helpers = None
self.configuration.append_config_values(share_opts)
self._helpers = None
def check_for_setup_error(self):
"""Returns an error if prerequisites aren't met."""
@ -112,8 +112,9 @@ class LVMShareDriver(driver.ExecuteMixin, driver.ShareDriver):
escaped_name = share['name'].replace('-', '--')
return "/dev/mapper/%s-%s" % (escaped_group, escaped_name)
def _allocate_container(self, share_name, sizestr):
cmd = ['lvcreate', '-L', sizestr, '-n', share_name,
def _allocate_container(self, share):
sizestr = '%sG' % share['size']
cmd = ['lvcreate', '-L', sizestr, '-n', share['name'],
self.configuration.share_volume_group]
if self.configuration.share_lvm_mirrors:
cmd += ['-m', self.configuration.share_lvm_mirrors, '--nosync']
@ -125,6 +126,8 @@ class LVMShareDriver(driver.ExecuteMixin, driver.ShareDriver):
cmd += ['-R', str(rsize)]
self._try_execute(*cmd, run_as_root=True)
device_name = self._local_path(share)
self._execute('mkfs.ext4', device_name, run_as_root=True)
def _deallocate_container(self, share_name):
"""Deletes a logical volume for share."""
@ -187,32 +190,36 @@ class LVMShareDriver(driver.ExecuteMixin, driver.ShareDriver):
self._stats = data
def deallocate_container(self, ctx, share):
"""Remove LVM volume that will be represented as share."""
self._deallocate_container(share['name'])
def allocate_container(self, ctx, share):
"""Create LVM volume that will be represented as share."""
self._allocate_container(share['name'], '%sG' % share['size'])
def create_share(self, context, share):
self._allocate_container(share)
#create file system
device_name = self._local_path(share)
self._execute('mkfs.ext4', device_name, run_as_root=True)
def allocate_container_from_snapshot(self, context, share, snapshot):
"""Is called to create share from snapshot."""
self._allocate_container(share['name'], '%sG' % share['size'])
self._copy_volume(self._local_path(snapshot), self._local_path(share),
snapshot['share_size'])
def create_export(self, ctx, share):
"""Exports the volume. Can optionally return a Dictionary of changes
to the share object to be persisted."""
device_name = self._local_path(share)
location = self._mount_device(share, device_name)
mount_path = self._get_mount_path(share)
location = self._get_helper(share).create_export(mount_path,
share['name'])
self._mount_device(share, device_name)
#TODO(rushiagr): what is the provider_location? realy needed?
return {'provider_location': location}
return location
def remove_export(self, ctx, share):
def create_share_from_snapshot(self, context, share, snapshot):
"""Is called to create share from snapshot."""
self._allocate_container(share)
device_name = self._local_path(snapshot)
self._copy_volume(device_name, self._local_path(share),
snapshot['share_size'])
mount_path = self._get_mount_path(share)
location = self._get_helper(share).create_export(mount_path,
share['name'])
self._mount_device(share, device_name)
#TODO(rushiagr): what is the provider_location? realy needed?
return location
def delete_share(self, context, share):
self._remove_export(context, share)
self._delete_share(context, share)
self._deallocate_container(share['name'])
def _remove_export(self, ctx, share):
"""Removes an access rules for a share."""
mount_path = self._get_mount_path(share)
if os.path.exists(mount_path):
@ -230,13 +237,6 @@ class LVMShareDriver(driver.ExecuteMixin, driver.ShareDriver):
except OSError:
LOG.info('Unable to delete %s', mount_path)
def create_share(self, ctx, share):
"""Is called after allocate_space to create share on the volume."""
location = self._get_mount_path(share)
location = self._get_helper(share).create_export(location,
share['name'])
return location
def create_snapshot(self, context, snapshot):
"""Creates a snapshot."""
orig_lv_name = "%s/%s" % (self.configuration.share_volume_group,
@ -252,7 +252,7 @@ class LVMShareDriver(driver.ExecuteMixin, driver.ShareDriver):
self._get_helper(share).create_export(location, share['name'],
recreate=True)
def delete_share(self, ctx, share):
def _delete_share(self, ctx, share):
"""Delete a share."""
try:
location = self._get_mount_path(share)
@ -558,6 +558,7 @@ class CIFSNetConfHelper(NASHelperBase):
raise exception.ShareBackendException(msg=msg)
else:
raise
parameters = {
'browseable': 'yes',
'create mask': '0755',

View File

@ -107,15 +107,12 @@ class ShareManager(manager.SchedulerDependentManager):
try:
if snapshot_ref:
self.driver.allocate_container_from_snapshot(context,
share_ref,
snapshot_ref)
export_location = self.driver.create_share_from_snapshot(
context, share_ref, snapshot_ref)
else:
self.driver.allocate_container(context, share_ref)
export_location = self.driver.create_share(context, share_ref)
export_location = self.driver.create_share(context, share_ref)
self.db.share_update(context, share_id,
{'export_location': export_location})
self.driver.create_export(context, share_ref)
except Exception:
with excutils.save_and_reraise_exception():
self.db.share_update(context, share_id, {'status': 'error'})
@ -137,9 +134,7 @@ class ShareManager(manager.SchedulerDependentManager):
try:
for access_ref in rules:
self._deny_access(context, access_ref, share_ref)
self.driver.remove_export(context, share_ref)
self.driver.delete_share(context, share_ref)
self.driver.deallocate_container(context, share_ref)
except Exception:
with excutils.save_and_reraise_exception():
self.db.share_update(context, share_id,

View File

@ -18,27 +18,16 @@
Tests for Share Code.
"""
import datetime
import os
import mox
import shutil
import tempfile
import mock
from manila import context
from manila import db
from manila import exception
from manila.image import image_utils
from manila.openstack.common import importutils
from manila.openstack.common.notifier import api as notifier_api
from manila.openstack.common.notifier import test_notifier
from manila.openstack.common import rpc
import manila.policy
from manila.share import manager
from manila import test
from manila.tests import conf_fixture
from oslo.config import cfg
CONF = cfg.CONF
@ -48,33 +37,21 @@ class FakeShareDriver(object):
def __init__(self, db, **kwargs):
self.db = db
def allocate_container(self, context, share):
pass
def allocate_container_from_snapshot(self, context, share, snapshot):
pass
def create_snapshot(self, context, snapshot):
pass
def delete_snapshot(self, context, snapshot):
pass
def deallocate_container(self, context, share):
def create_share(self, context, share):
pass
def create_share(self, context, share):
return 'fake_location'
def create_share_from_snapshot(self, context, share, snapshot):
pass
def delete_share(self, context, share):
pass
def create_export(self, context, share):
pass
def remove_export(self, context, share):
pass
def ensure_share(self, context, share):
pass
@ -151,24 +128,17 @@ class ShareTestCase(test.TestCase):
access = self._create_access(share_id=share_id, state='active')
self.mox.StubOutWithMock(context, 'get_admin_context')
context.get_admin_context().AndReturn(self.context)
self.mox.StubOutWithMock(db, 'share_get_all_by_host')
db.share_get_all_by_host(self.context, mox.IgnoreArg())\
.AndReturn([share, another_share])
driver = self.mox.CreateMockAnything(FakeShareDriver)
driver.do_setup(self.context)
driver.check_for_setup_error()
driver.ensure_share(self.context, share)
driver.allow_access(self.context, share, mox.IgnoreArg())
driver.get_share_stats(refresh=True)
context.get_admin_context = mock.Mock(return_value=self.context)
db.share_get_all_by_host = mock.Mock(
return_value=[share, another_share])
driver = mock.Mock()
driver.get_share_stats.return_value = {}
self.share.driver = driver
self.mox.ReplayAll()
self.share.init_host()
driver.ensure_share.assert_called_once_with(self.context, share)
driver.allow_access.assert_called_once_with(
self.context, share, mock.ANY)
driver.get_share_stats.assert_called_once_with(refresh=True)
def test_create_share_from_snapshot(self):
"""Test share can be created from snapshot."""
@ -209,9 +179,6 @@ class ShareTestCase(test.TestCase):
self.assertEquals(snap['status'], 'available')
self.share.delete_snapshot(self.context, snapshot_id)
self.assertEquals('deleted', db.share_snapshot_get(
context.get_admin_context(read_deleted='yes'), snapshot_id).status)
self.assertRaises(exception.NotFound,
db.share_snapshot_get,
self.context,
@ -276,10 +243,6 @@ class ShareTestCase(test.TestCase):
self.assertEquals(shr['status'], 'available')
self.share.delete_share(self.context, share_id)
shr = db.share_get(context.get_admin_context(read_deleted='yes'),
share_id)
self.assertEquals(shr['status'], 'deleted')
self.assertRaises(exception.NotFound,
db.share_get,
self.context,
@ -288,15 +251,15 @@ class ShareTestCase(test.TestCase):
def test_create_delete_share_error(self):
"""Test share can be created and deleted with error."""
def _fake_create_export(self, context, share):
def _fake_create_share(self, context, share):
raise exception.NotFound()
def _fake_deallocate_container(self, context, share):
def _fake_delete_share(self, context, share):
raise exception.NotFound()
self.stubs.Set(FakeShareDriver, "create_export", _fake_create_export)
self.stubs.Set(FakeShareDriver, "deallocate_container",
_fake_deallocate_container)
self.stubs.Set(FakeShareDriver, "create_share", _fake_create_share)
self.stubs.Set(FakeShareDriver, "delete_share",
_fake_delete_share)
share = self._create_share()
share_id = share['id']
@ -317,7 +280,6 @@ class ShareTestCase(test.TestCase):
def test_allow_deny_access(self):
"""Test access rules to share can be created and deleted."""
share = self._create_share()
share_id = share['id']
access = self._create_access(share_id=share_id)
@ -327,10 +289,6 @@ class ShareTestCase(test.TestCase):
access_id).state)
self.share.deny_access(self.context, access_id)
acs = db.share_access_get(
context.get_admin_context(read_deleted='yes'),
access_id)
self.assertEquals(acs['state'], 'deleted')
self.assertRaises(exception.NotFound,
db.share_access_get,
self.context,

View File

@ -15,7 +15,7 @@
# under the License.
"""Unit tests for the NFS driver module."""
import mox
import mock
import os
from manila import context
@ -84,10 +84,12 @@ class LVMShareDriverTestCase(test.TestCase):
CONF.set_default('share_volume_group', 'fakevg')
CONF.set_default('share_export_ip', '10.0.0.1')
self._helper_cifs = self.mox.CreateMock(lvm.CIFSNetConfHelper)
self._helper_nfs = self.mox.CreateMock(lvm.NFSHelper)
self._helper_cifs = mock.Mock()
self._helper_nfs = mock.Mock()
self.fake_conf = Configuration(None)
self._db = self.mox.CreateMockAnything()
self._db = mock.Mock()
self._os = lvm.os = mock.Mock()
self._os.path.join = os.path.join
self._driver = lvm.LVMShareDriver(self._db,
execute=self._execute,
configuration=self.fake_conf)
@ -106,24 +108,13 @@ class LVMShareDriverTestCase(test.TestCase):
fake_utils.fake_execute_clear_log()
def test_do_setup(self):
self.mox.StubOutWithMock(importutils, 'import_class')
helpers = [
(self._helper_cifs, 'manila.share.drivers.lvm.CIFSNetConfHelper'),
(self._helper_nfs, 'manila.share.drivers.lvm.NFSHelper'),
]
for helper, path in helpers:
importutils.import_class(path).AndReturn(helper)
helper.__call__(fake_utils.fake_execute,
self.fake_conf).\
AndReturn(helper)
helper.init()
self.mox.ReplayAll()
CONF.set_default('share_lvm_helpers', ['NFS=fakenfs'])
lvm.importutils = mock.Mock()
lvm.importutils.import_class.return_value = self._helper_nfs
self._driver.do_setup(self._context)
expected_helpers = {
'CIFS': self._helper_cifs,
'NFS': self._helper_nfs,
}
self.assertEqual(self._driver._helpers, expected_helpers)
lvm.importutils.import_class.assert_has_calls([
mock.call('fakenfs')
])
def test_check_for_setup_error(self):
def exec_runner(*ignore_args, **ignore_kwargs):
@ -133,7 +124,6 @@ class LVMShareDriverTestCase(test.TestCase):
'vgs --noheadings -o name',
]
fake_utils.fake_execute_set_repliers([(expected_exec[0], exec_runner)])
self.mox.ReplayAll()
ret = self._driver.check_for_setup_error()
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
@ -143,7 +133,6 @@ class LVMShareDriverTestCase(test.TestCase):
fake_utils.fake_execute_set_repliers([('vgs --noheadings -o name',
exec_runner)])
self.mox.ReplayAll()
self.assertRaises(exception.InvalidParameterValue,
self._driver.check_for_setup_error)
@ -154,44 +143,52 @@ class LVMShareDriverTestCase(test.TestCase):
fake_utils.fake_execute_set_repliers([('vgs --noheadings -o name',
exec_runner)])
CONF.set_default('share_export_ip', None)
self.mox.ReplayAll()
self.assertRaises(exception.InvalidParameterValue,
self._driver.check_for_setup_error)
def test_local_path_normal(self):
share = fake_share(name='fake_sharename')
CONF.set_default('share_volume_group', 'fake_vg')
self.mox.ReplayAll()
ret = self._driver._local_path(share)
self.assertEqual(ret, '/dev/mapper/fake_vg-fake_sharename')
def test_local_path_escapes(self):
share = fake_share(name='fake-sharename')
CONF.set_default('share_volume_group', 'fake-vg')
self.mox.ReplayAll()
ret = self._driver._local_path(share)
self.assertEqual(ret, '/dev/mapper/fake--vg-fake--sharename')
def test_allocate_container_normal(self):
def test_create_share(self):
self._helper_nfs.create_export.return_value = 'fakelocation'
self._driver._mount_device = mock.Mock()
ret = self._driver.create_share(self._context, self.share)
CONF.set_default('share_lvm_mirrors', 0)
self.mox.ReplayAll()
ret = self._driver.allocate_container(self._context, self.share)
self._driver._mount_device.assert_called_with(
self.share, '/dev/mapper/fakevg-fakename')
expected_exec = [
'lvcreate -L 1G -n fakename fakevg',
'mkfs.ext4 /dev/mapper/fakevg-fakename',
]
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
self.assertEqual(ret, 'fakelocation')
def test_allocate_container_from_snapshot(self):
def test_create_share_from_snapshot(self):
CONF.set_default('share_lvm_mirrors', 0)
self._driver._mount_device = mock.Mock()
mount_share = '/dev/mapper/fakevg-fakename'
mount_snapshot = '/dev/mapper/fakevg-fakesnapshotname'
self.mox.ReplayAll()
ret = self._driver.allocate_container_from_snapshot(self._context,
self._helper_nfs.create_export.return_value = 'fakelocation'
mount_path = self._get_mount_path(self.share)
ret = self._driver.create_share_from_snapshot(self._context,
self.share,
self.snapshot)
self._driver._mount_device.assert_called_with(self.share,
mount_snapshot)
expected_exec = [
'lvcreate -L 1G -n fakename fakevg',
'mkfs.ext4 /dev/mapper/fakevg-fakename',
("dd count=0 if=%s of=%s iflag=direct oflag=direct" %
(mount_snapshot, mount_share)),
("dd if=%s of=%s count=1024 bs=1M iflag=direct oflag=direct" %
@ -199,41 +196,25 @@ class LVMShareDriverTestCase(test.TestCase):
]
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
def test_allocate_container_from_snapshot_without_extra(self):
def exec_runner(*ignore_args, **ignore_kwargs):
raise exception.ProcessExecutionError()
def test_create_share_mirrors(self):
CONF.set_default('share_lvm_mirrors', 0)
mount_share = '/dev/mapper/fakevg-fakename'
mount_snapshot = '/dev/mapper/fakevg-fakesnapshotname'
expected_exec = [
'lvcreate -L 1G -n fakename fakevg',
("dd count=0 if=%s of=%s iflag=direct oflag=direct" %
(mount_snapshot, mount_share)),
"dd if=%s of=%s count=1024 bs=1M" % (mount_snapshot, mount_share),
]
fake_utils.fake_execute_set_repliers([(expected_exec[1], exec_runner)])
self.mox.ReplayAll()
ret = self._driver.allocate_container_from_snapshot(self._context,
self.share,
self.snapshot)
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
def test_allocate_container_mirrors(self):
share = fake_share(size='2048')
CONF.set_default('share_lvm_mirrors', 2)
self.mox.ReplayAll()
ret = self._driver.allocate_container(self._context, share)
self._helper_nfs.create_export.return_value = 'fakelocation'
self._driver._mount_device = mock.Mock()
ret = self._driver.create_share(self._context, share)
self._driver._mount_device.assert_called_with(
share, '/dev/mapper/fakevg-fakename')
expected_exec = [
'lvcreate -L 2048G -n fakename fakevg -m 2 --nosync -R 2',
'mkfs.ext4 /dev/mapper/fakevg-fakename',
]
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
self.assertEqual(ret, 'fakelocation')
def test_deallocate_container(self):
self.mox.ReplayAll()
expected_exec = ['lvremove -f fakevg/fakename']
ret = self._driver.deallocate_container(self._context, self.share)
ret = self._driver._deallocate_container(self.share['name'])
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
def test_get_share_stats(self):
@ -245,7 +226,6 @@ class LVMShareDriverTestCase(test.TestCase):
]
fake_utils.fake_execute_set_repliers([(expected_exec[0], exec_runner)])
CONF.set_default('reserved_share_percentage', 1)
self.mox.ReplayAll()
ret = self._driver.get_share_stats(refresh=True)
expected_ret = {
'share_backend_name': 'LVM',
@ -269,7 +249,6 @@ class LVMShareDriverTestCase(test.TestCase):
]
fake_utils.fake_execute_set_repliers([(expected_exec[0], exec_runner)])
CONF.set_default('reserved_share_percentage', 1)
self.mox.ReplayAll()
ret = self._driver.get_share_stats(refresh=True)
expected_ret = {
'share_backend_name': 'LVM',
@ -284,46 +263,31 @@ class LVMShareDriverTestCase(test.TestCase):
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
self.assertEqual(ret, expected_ret)
def test_create_export(self):
self.mox.StubOutWithMock(self._driver, '_mount_device')
self._driver._mount_device(self.share, '/dev/mapper/fakevg-fakename').\
AndReturn('fakelocation')
self.mox.ReplayAll()
ret = self._driver.create_export(self._context, self.share)
expected_ret = {
'provider_location': 'fakelocation',
}
self.assertEqual(ret, expected_ret)
def test_remove_export(self):
mount_path = self._get_mount_path(self.share)
self._os.path.exists.return_value = True
self.mox.StubOutWithMock(os.path, 'exists')
os.path.exists(mount_path).AndReturn(True)
self._driver._remove_export(self._context, self.share)
self.mox.StubOutWithMock(os, 'rmdir')
os.rmdir(mount_path)
self.mox.ReplayAll()
self._driver.remove_export(self._context, self.share)
expected_exec = [
"umount -f %s" % (mount_path,),
]
self._os.path.exists.assert_called_with(mount_path)
self._os.rmdir.assert_called_with(mount_path)
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
def test_remove_export_is_busy_error(self):
def exec_runner(*ignore_args, **ignore_kwargs):
raise exception.ProcessExecutionError(stderr='device is busy')
self._os.path.exists.return_value = True
mount_path = self._get_mount_path(self.share)
expected_exec = [
"umount -f %s" % (mount_path),
]
fake_utils.fake_execute_set_repliers([(expected_exec[0], exec_runner)])
self.mox.StubOutWithMock(os.path, 'exists')
os.path.exists(mount_path).AndReturn(True)
self.mox.ReplayAll()
self.assertRaises(exception.ShareIsBusy, self._driver.remove_export,
self.assertRaises(exception.ShareIsBusy, self._driver._remove_export,
self._context, self.share)
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
@ -336,22 +300,11 @@ class LVMShareDriverTestCase(test.TestCase):
"umount -f %s" % (mount_path),
]
fake_utils.fake_execute_set_repliers([(expected_exec[0], exec_runner)])
self.mox.StubOutWithMock(os.path, 'exists')
os.path.exists(mount_path).AndReturn(True)
self.mox.ReplayAll()
self._driver.remove_export(self._context, self.share)
self._os.path.exists.return_value = True
self._driver._remove_export(self._context, self.share)
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
def test_create_share(self):
mount_path = self._get_mount_path(self.share)
self._helper_nfs.create_export(mount_path, self.share['name']).\
AndReturn('fakelocation')
self.mox.ReplayAll()
ret = self._driver.create_share(self._context, self.share)
self.assertEqual(ret, 'fakelocation')
def test_create_snapshot(self):
self.mox.ReplayAll()
self._driver.create_snapshot(self._context, self.snapshot)
expected_exec = [
("lvcreate -L 1G --name fakesnapshotname --snapshot %s/fakename" %
@ -372,27 +325,16 @@ class LVMShareDriverTestCase(test.TestCase):
def test_delete_share(self):
mount_path = self._get_mount_path(self.share)
self._helper_nfs.remove_export(mount_path, self.share['name'])
self.mox.ReplayAll()
self._driver.delete_share(self._context, self.share)
self._driver._delete_share(self._context, self.share)
def test_delete_snapshot(self):
self.mox.ReplayAll()
expected_exec = ['lvremove -f fakevg/fakesnapshotname']
self._driver.delete_snapshot(self._context, self.snapshot)
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
def test_delete_share_process_error(self):
self.mox.StubOutWithMock(self._driver, '_get_mount_path')
self._driver._get_mount_path(self.share).AndRaise(
exception.ProcessExecutionError())
self.mox.ReplayAll()
self._driver.delete_share(self._context, self.share)
def test_delete_share_invalid_share(self):
self.mox.StubOutWithMock(self._driver, '_get_helper')
self._driver._get_helper(self.share).AndRaise(
exception.InvalidShare(reason='fake'))
self.mox.ReplayAll()
self._driver._get_helper = mock.Mock(
side_effect=exception.InvalidShare(reason='fake'))
self._driver.delete_share(self._context, self.share)
def test_allow_access(self):
@ -401,7 +343,6 @@ class LVMShareDriverTestCase(test.TestCase):
self.share['name'],
self.access['access_type'],
self.access['access_to'])
self.mox.ReplayAll()
self._driver.allow_access(self._context, self.share, self.access)
def test_deny_access(self):
@ -410,12 +351,10 @@ class LVMShareDriverTestCase(test.TestCase):
self.share['name'],
self.access['access_type'],
self.access['access_to'])
self.mox.ReplayAll()
self._driver.deny_access(self._context, self.share, self.access)
def test_mount_device(self):
mount_path = self._get_mount_path(self.share)
self.mox.ReplayAll()
ret = self._driver._mount_device(self.share, 'fakedevice')
expected_exec = [
"mkdir -p %s" % (mount_path,),
@ -435,7 +374,6 @@ class LVMShareDriverTestCase(test.TestCase):
"mount fakedevice %s" % (mount_path,),
]
fake_utils.fake_execute_set_repliers([(expected_exec[1], exec_runner)])
self.mox.ReplayAll()
ret = self._driver._mount_device(self.share, 'fakedevice')
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
self.assertEqual(ret, mount_path)
@ -450,7 +388,6 @@ class LVMShareDriverTestCase(test.TestCase):
"mount fakedevice %s" % (mount_path,),
]
fake_utils.fake_execute_set_repliers([(expected_exec[1], exec_runner)])
self.mox.ReplayAll()
self.assertRaises(exception.ProcessExecutionError,
self._driver._mount_device, self.share, 'fakedevice')
@ -458,7 +395,6 @@ class LVMShareDriverTestCase(test.TestCase):
share_cifs = fake_share(share_proto='CIFS')
share_nfs = fake_share(share_proto='NFS')
share_fake = fake_share(share_proto='FAKE')
self.mox.ReplayAll()
self.assertEqual(self._driver._get_helper(share_cifs),
self._helper_cifs)
self.assertEqual(self._driver._get_helper(share_nfs),
@ -488,25 +424,19 @@ class NFSHelperTestCase(test.TestCase):
fake_utils.fake_execute_clear_log()
def test_failed_init(self):
self.mox.StubOutWithMock(self, '_execute')
self._execute('exportfs', check_exit_code=True, run_as_root=True).\
AndRaise(exception.ProcessExecutionError)
self.mox.ReplayAll()
self._execute = mock.Mock(side_effect=exception.ProcessExecutionError)
self.assertRaises(exception.Error, lvm.NFSHelper.__init__,
self._helper, self._execute, self.fake_conf)
def test_create_export(self):
self.mox.ReplayAll()
ret = self._helper.create_export('/opt/nfs', 'volume-00001')
expected_location = '%s:/opt/nfs' % CONF.share_export_ip
self.assertEqual(ret, expected_location)
def test_remove_export(self):
self.mox.ReplayAll()
self._helper.remove_export('/opt/nfs', 'volume-00001')
def test_allow_access(self):
self.mox.ReplayAll()
self._helper.allow_access('/opt/nfs', 'volume-00001', 'ip', '10.0.0.*')
export_string = '10.0.0.*:/opt/nfs'
@ -517,7 +447,6 @@ class NFSHelperTestCase(test.TestCase):
self.assertEqual(fake_utils.fake_execute_get_log(), expected_exec)
def test_allow_access_no_ip(self):
self.mox.ReplayAll()
self.assertRaises(exception.InvalidShareAccess,
self._helper.allow_access, '/opt/nfs', 'share0',
'fake', 'fakerule')
@ -527,13 +456,11 @@ class NFSHelperTestCase(test.TestCase):
return '\n/opt/nfs\t\t10.0.0.*\n', ''
fake_utils.fake_execute_set_repliers([('exportfs', exec_runner)])
self.mox.ReplayAll()
self.assertRaises(exception.ShareAccessExists,
self._helper.allow_access,
'/opt/nfs', 'volume-00001', 'ip', '10.0.0.*')
def test_deny_access(self):
self.mox.ReplayAll()
self._helper.deny_access('/opt/nfs', 'volume-00001', 'ip', '10.0.0.*')
export_string = '10.0.0.*:/opt/nfs'
expected_exec = ['exportfs -u %s' % export_string]
@ -560,21 +487,21 @@ class CIFSNetConfHelperTestCase(test.TestCase):
def test_create_export(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_execute')
self._helper._execute('net', 'conf', 'addshare', share_name,
'fakelocalpath', 'writeable=y', 'guest_ok=y',
run_as_root=True)
self._helper._execute = mock.Mock()
parameters = {
'browseable': 'yes',
'create mask': '0755',
'hosts deny': '0.0.0.0/0',
'hosts allow': '127.0.0.1',
}
for name, value in parameters.items():
self._helper._execute('net', 'conf', 'setparm', share_name, name,
value, run_as_root=True)
self.mox.ReplayAll()
}
ret = self._helper.create_export('fakelocalpath', share_name)
calls = [mock.call('net', 'conf', 'addshare', share_name,
'fakelocalpath', 'writeable=y', 'guest_ok=y',
run_as_root=True)]
for name, value in parameters.items():
calls.append(mock.call('net', 'conf', 'setparm', share_name, name,
value, run_as_root=True))
self._helper._execute.assert_has_calls(calls)
expected_ret = "//127.0.0.1/%s" % (share_name,)
self.assertEqual(ret, expected_ret)
@ -589,167 +516,155 @@ class CIFSNetConfHelperTestCase(test.TestCase):
),
]
fake_utils.fake_execute_set_repliers([(expected_exec[0], exec_runner)])
self.mox.ReplayAll()
self.assertRaises(exception.ShareBackendException,
self._helper.create_export, 'fakelocalpath',
self.share['name'])
def test_create_export_recreate(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_execute')
self._helper._execute('net', 'conf', 'addshare', share_name,
'fakelocalpath', 'writeable=y', 'guest_ok=y',
run_as_root=True).\
AndRaise(exception.ProcessExecutionError(stderr='already exists'))
self._helper._execute('net', 'conf', 'delshare', share_name,
run_as_root=True)
self._helper._execute('net', 'conf', 'addshare', share_name,
'fakelocalpath', 'writeable=y', 'guest_ok=y',
run_as_root=True)
def raise_exec_error():
raise exception.ProcessExecutionError(stderr="already exists")
execute_return_values = [raise_exec_error, '']
parameters = {
'browseable': 'yes',
'create mask': '0755',
'hosts deny': '0.0.0.0/0',
'hosts allow': '127.0.0.1',
}
for name, value in parameters.items():
self._helper._execute('net', 'conf', 'setparm', share_name, name,
value, run_as_root=True)
self.mox.ReplayAll()
execute_return_values.extend([''] * len(parameters))
self._helper._execute = mock.Mock(side_effect=execute_return_values)
ret = self._helper.create_export('fakelocalpath', share_name,
recreate=True)
expected_ret = "//127.0.0.1/%s" % (share_name,)
calls = [mock.call('net', 'conf', 'setparm', share_name, name,
value, run_as_root=True) for
name, value in parameters.items()]
self._helper._execute.assert_has_calls(calls)
self.assertEqual(ret, expected_ret)
def test_create_export_error(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_execute')
self._helper._execute('net', 'conf', 'addshare', share_name,
'fakelocalpath', 'writeable=y', 'guest_ok=y',
run_as_root=True).\
AndRaise(exception.ProcessExecutionError(stderr='fake error'))
self.mox.ReplayAll()
def raise_exec_error(*args, **kwargs):
raise exception.ProcessExecutionError(stderr="fake_stderr")
self._helper._execute = mock.Mock(
side_effect=raise_exec_error)
self.assertRaises(exception.ProcessExecutionError,
self._helper.create_export, 'fakelocalpath',
share_name)
def test_remove_export(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_execute')
self._helper._execute('net', 'conf', 'delshare', share_name,
run_as_root=True)
self._helper._execute('smbcontrol', 'all', 'close-share', share_name,
run_as_root=True)
self.mox.ReplayAll()
self._helper._execute = mock.Mock()
self._helper.remove_export('fakelocalpath', share_name)
self._helper._execute.assert_called_with('smbcontrol', 'all',
'close-share', share_name,
run_as_root=True)
def test_remove_export_no_such_service(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_execute')
self._helper._execute('net', 'conf', 'delshare', share_name,
run_as_root=True).\
AndRaise(exception.ProcessExecutionError(
stderr='SBC_ERR_NO_SUCH_SERVICE'))
self._helper._execute('smbcontrol', 'all', 'close-share', share_name,
run_as_root=True)
self.mox.ReplayAll()
def exec_return(*args, **kwargs):
if 'net' in args:
raise exception.ProcessExecutionError(
stderr='SBC_ERR_NO_SUCH_SERVICE')
self._helper._execute = mock.Mock(side_effect=exec_return)
self._helper.remove_export('fakelocalpath', share_name)
self._helper._execute.assert_called_with(
'smbcontrol', 'all', 'close-share', share_name, run_as_root=True)
def test_remove_export_error(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_execute')
self._helper._execute('net', 'conf', 'delshare', share_name,
run_as_root=True).\
AndRaise(exception.ProcessExecutionError(stderr='fake error'))
self.mox.ReplayAll()
def raise_exec_error(*args, **kwargs):
raise exception.ProcessExecutionError(stderr="fake_stderr")
self._helper._execute = mock.Mock(
side_effect=raise_exec_error)
self.assertRaises(exception.ProcessExecutionError,
self._helper.remove_export, 'fakelocalpath',
share_name)
def test_allow_access(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_get_allow_hosts')
self.mox.StubOutWithMock(self._helper, '_set_allow_hosts')
self._helper._get_allow_hosts(share_name).AndReturn(['127.0.0.1',
'10.0.0.1'])
self._helper._set_allow_hosts(['127.0.0.1', '10.0.0.1', '10.0.0.2'],
share_name)
self.mox.ReplayAll()
self._helper._get_allow_hosts = mock.Mock(return_value=['127.0.0.1',
'10.0.0.1'])
self._helper._set_allow_hosts = mock.Mock()
self._helper.allow_access('fakelocalpath', share_name, 'ip',
'10.0.0.2')
self._helper._set_allow_hosts.assert_called_with(
['127.0.0.1', '10.0.0.1', '10.0.0.2'], share_name)
def test_allow_access_exists(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_get_allow_hosts')
self._helper._get_allow_hosts(share_name).AndReturn(['127.0.0.1',
'10.0.0.1'])
self.mox.ReplayAll()
self._helper._get_allow_hosts = mock.Mock(return_value=['127.0.0.1',
'10.0.0.1'])
self.assertRaises(exception.ShareAccessExists,
self._helper.allow_access, 'fakelocalpath',
share_name, 'ip', '10.0.0.1')
def test_allow_access_wrong_type(self):
share_name = self.share['name']
self.mox.ReplayAll()
self.assertRaises(exception.InvalidShareAccess,
self._helper.allow_access, 'fakelocalpath',
share_name, 'fake', 'fake access')
def test_deny_access(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_get_allow_hosts')
self.mox.StubOutWithMock(self._helper, '_set_allow_hosts')
self._helper._get_allow_hosts(share_name).AndReturn(['127.0.0.1',
'10.0.0.1'])
self._helper._set_allow_hosts(['127.0.0.1'], share_name)
self.mox.ReplayAll()
self._helper._get_allow_hosts = mock.Mock(return_value=['127.0.0.1',
'10.0.0.1'])
self._helper._set_allow_hosts = mock.Mock()
self._helper.deny_access('fakelocalpath', share_name, 'ip',
'10.0.0.1')
self._helper._set_allow_hosts.assert_called_with(
['127.0.0.1'], share_name)
def test_deny_access_not_exists(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_get_allow_hosts')
self._helper._get_allow_hosts(share_name).\
AndRaise(exception.ProcessExecutionError(stdout='does not exist'))
self.mox.ReplayAll()
def raise_exec_error(*args, **kwargs):
raise exception.ProcessExecutionError(stdout="does not exist")
self._helper._get_allow_hosts = mock.Mock(side_effect=raise_exec_error)
self.assertRaises(exception.ProcessExecutionError,
self._helper.deny_access, 'fakelocalpath',
share_name, 'ip', '10.0.0.1')
def test_deny_access_not_exists_force(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_get_allow_hosts')
self._helper._get_allow_hosts(share_name).\
AndRaise(exception.ProcessExecutionError(stdout='does not exist'))
self.mox.ReplayAll()
def raise_exec_error(*args, **kwargs):
raise exception.ProcessExecutionError(stdout="does not exist")
self._helper._get_allow_hosts = mock.Mock(side_effect=raise_exec_error)
self._helper.deny_access('fakelocalpath', share_name, 'ip', '10.0.0.1',
force=True)
def test_deny_access_error(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_get_allow_hosts')
self._helper._get_allow_hosts(share_name).\
AndRaise(exception.ProcessExecutionError(stdout='fake out'))
self.mox.ReplayAll()
def raise_exec_error(*args, **kwargs):
raise exception.ProcessExecutionError(stdout="fake out")
self._helper._get_allow_hosts = mock.Mock(side_effect=raise_exec_error)
self.assertRaises(exception.ProcessExecutionError,
self._helper.deny_access, 'fakelocalpath',
share_name, 'ip', '10.0.0.1')
def test_get_allow_hosts(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_execute')
self._helper._execute('net', 'conf', 'getparm', share_name,
'hosts allow', run_as_root=True).\
AndReturn(('127.0.0.1 10.0.0.1', ''))
self.mox.ReplayAll()
self._helper._execute = mock.Mock(return_value=(
'127.0.0.1 10.0.0.1', ''))
ret = self._helper._get_allow_hosts(share_name)
self.assertEqual(ret, ['127.0.0.1', '10.0.0.1'])
def test_set_allow_hosts(self):
share_name = self.share['name']
self.mox.StubOutWithMock(self._helper, '_execute')
self._helper._execute('net', 'conf', 'setparm', share_name,
'hosts allow', '127.0.0.1 10.0.0.1',
run_as_root=True)
self.mox.ReplayAll()
self._helper._execute = mock.Mock()
self._helper._set_allow_hosts(['127.0.0.1', '10.0.0.1'], share_name)
self._helper._execute.assert_called_with(
'net', 'conf', 'setparm', share_name, 'hosts allow',
'127.0.0.1 10.0.0.1', run_as_root=True)