glusterfs: Implement methods to update share stats

Implement get_share_stats, and _update_share_stats methods.

Change-Id: Id6e9aa130c40561c7fe24e86019751c67580c9e1
This commit is contained in:
Csaba Henk 2014-06-05 15:43:24 +05:30 committed by Ramana Raja
parent 99857a4983
commit ee54dd906c
2 changed files with 85 additions and 0 deletions

View File

@ -90,6 +90,8 @@ class GlusterfsShareDriver(driver.ExecuteMixin, driver.ShareDriver):
self._helpers = None
self.gluster_address = None
self.configuration.append_config_values(GlusterfsManilaShare_opts)
self.backend_name = self.configuration.safe_get(
'share_backend_name') or 'GlusterFS'
def do_setup(self, context):
"""Native mount the Gluster volume."""
@ -228,6 +230,45 @@ class GlusterfsShareDriver(driver.ExecuteMixin, driver.ShareDriver):
local_vol_path)
return os.path.join(local_vol_path, share['name'])
def get_share_stats(self, refresh=False):
"""Get share stats.
If 'refresh' is True, run update the stats first.
"""
if refresh:
self._update_share_stats()
return self._stats
def _update_share_stats(self):
"""Retrieve stats info from the GlusterFS volume."""
# sanity check for gluster ctl mount
smpb = os.stat(self.configuration.glusterfs_mount_point_base)
smp = os.stat(self._get_mount_point_for_gluster_vol())
if smpb.st_dev == smp.st_dev:
raise exception.GlusterfsException(
_("GlusterFS control mount is not available")
)
smpv = os.statvfs(self._get_mount_point_for_gluster_vol())
LOG.debug("Updating share stats")
data = {}
data["share_backend_name"] = self.backend_name
data["vendor_name"] = 'Red Hat'
data["driver_version"] = '1.0'
data["storage_protocol"] = 'NFS'
data['reserved_percentage'] = \
self.configuration.reserved_share_percentage
data['QoS_support'] = False
data['total_capacity_gb'] = (smpv.f_blocks * smpv.f_frsize) >> 30
data['free_capacity_gb'] = (smpv.f_bavail * smpv.f_frsize) >> 30
self._stats = data
def create_share(self, ctx, share):
"""Create a directory that'd serve as a share in a Gluster volume."""
local_share_path = self._get_local_share_path(share)

View File

@ -117,6 +117,7 @@ class GlusterfsShareDriverTestCase(test.TestCase):
self._context = context.get_admin_context()
CONF.set_default('glusterfs_mount_point_base', '/mnt/nfs')
CONF.set_default('reserved_share_percentage', 50)
self.fake_conf = config.Configuration(None)
self._db = Mock()
@ -350,6 +351,49 @@ class GlusterfsShareDriverTestCase(test.TestCase):
self._driver._get_local_share_path,
self.share)
def test_get_share_stats_refresh_false(self):
self._driver._stats = Mock()
ret = self._driver.get_share_stats()
self.assertEqual(ret, self._driver._stats)
def test_get_share_stats_refresh_true(self):
def foo():
self._driver._stats = {'key': 'value'}
self._driver._update_share_stats = Mock(side_effect=foo)
ret = self._driver.get_share_stats(refresh=True)
self.assertEqual(ret, {'key': 'value'})
def test_update_share_stats(self):
test_data = {
'share_backend_name': 'GlusterFS',
'vendor_name': 'Red Hat',
'driver_version': '1.0',
'storage_protocol': 'NFS',
'reserved_percentage': 50,
'QoS_support': False,
'total_capacity_gb': 2,
'free_capacity_gb': 2,
}
test_statvfs = Mock(f_frsize=4096, f_blocks=524288, f_bavail=524288)
self._driver._get_mount_point_for_gluster_vol = \
Mock(return_value='/mnt/nfs/testvol')
some_no = 42
not_some_no = some_no + 1
os_stat = lambda path: Mock(st_dev=some_no) if path == '/mnt/nfs' \
else Mock(st_dev=not_some_no)
with patch.object(os, 'statvfs', return_value=test_statvfs):
with patch.object(os, 'stat', os_stat):
ret = self._driver._update_share_stats()
self.assertEqual(self._driver._stats, test_data)
def test_update_share_stats_gluster_mnt_unavailable(self):
self._driver._get_mount_point_for_gluster_vol = \
Mock(return_value='/mnt/nfs/testvol')
some_no = 42
with patch.object(os, 'stat', return_value=Mock(st_dev=some_no)):
self.assertRaises(exception.GlusterfsException,
self._driver._update_share_stats)
def test_create_share(self):
self._driver._get_local_share_path =\
Mock(return_value='/mnt/nfs/testvol/fakename')