Skip to content

Commit

Permalink
Merge pull request #29398 from cachedout/lint_29288
Browse files Browse the repository at this point in the history
Lint 29288
  • Loading branch information
Mike Place committed Dec 3, 2015
2 parents 5b8e782 + 3b0033e commit d2c0fcb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
29 changes: 26 additions & 3 deletions salt/modules/glusterfs.py
Expand Up @@ -29,6 +29,18 @@ def __virtual__():
return (False, 'glusterfs server is not installed')


def _get_minor_version():
# Set default version to 6 for tests
version = 6
cmd = 'gluster --version'
result = __salt__['cmd.run'](cmd).splitlines()
for line_number in range(len(result)):
line = result[line_number]
if line.startswith('glusterfs'):
version = int(line.split()[1].split('.')[1])
return version


def list_peers():
'''
Return a list of gluster peers
Expand Down Expand Up @@ -225,6 +237,8 @@ def status(name):
salt '*' glusterfs.status myvolume
'''
# Get minor version
minor_version = _get_minor_version()
# Get volume status
cmd = 'gluster volume status {0}'.format(name)
result = __salt__['cmd.run'](cmd).splitlines()
Expand All @@ -244,7 +258,10 @@ def status(name):
line = line.rstrip() + result[line_number]

# Parse Brick data
brick, port, online, pid = line.split()[1:]
if minor_version >= 7:
brick, port, port_rdma, online, pid = line.split()[1:]
else:
brick, port, online, pid = line.split()[1:]
host, path = brick.split(':')
data = {'port': port, 'pid': pid, 'host': host, 'path': path}
if online == 'Y':
Expand All @@ -260,7 +277,10 @@ def status(name):
line = line.rstrip() + result[line_number]

# Parse NFS Server data
host, port, online, pid = line.split()[3:]
if minor_version >= 7:
host, port, port_rdma, online, pid = line.split()[3:]
else:
host, port, online, pid = line.split()[3:]
data = {'port': port, 'pid': pid}
if online == 'Y':
data['online'] = True
Expand All @@ -275,7 +295,10 @@ def status(name):
line = line.rstrip() + result[line_number]

# Parse NFS Server data
host, port, online, pid = line.split()[3:]
if minor_version >= 7:
host, port, port_rdma, online, pid = line.split()[3:]
else:
host, port, online, pid = line.split()[3:]
data = {'port': port, 'pid': pid}
if online == 'Y':
data['online'] = True
Expand Down
41 changes: 22 additions & 19 deletions tests/unit/modules/glusterfs_test.py
Expand Up @@ -24,7 +24,7 @@


@skipIf(NO_MOCK, NO_MOCK_REASON)
class GitTestCase(TestCase):
class GlusterfsTestCase(TestCase):
'''
Test cases for salt.modules.glusterfs
'''
Expand Down Expand Up @@ -130,26 +130,29 @@ def test_start_volume(self):
'''
Test if it start a gluster volume.
'''
mock = MagicMock(return_value=['Newvolume1', 'Newvolume2'])
with patch.object(glusterfs, 'list_volumes', mock):
mock = MagicMock(return_value='creation success')
with patch.dict(glusterfs.__salt__, {'cmd.run': mock}):
self.assertEqual(glusterfs.start_volume('Newvolume1'),
'Volume already started')
mock_list = MagicMock(return_value=['Newvolume1', 'Newvolume2'])
with patch.object(glusterfs, 'list_volumes', mock_list):
mock_status = MagicMock(return_value={})
with patch.object(glusterfs, 'status', mock_status):
mock = MagicMock(return_value='creation success')
with patch.dict(glusterfs.__salt__, {'cmd.run': mock}):
self.assertEqual(glusterfs.start_volume('Newvolume1'),
'Volume already started')

mock = MagicMock(side_effect=['does not exist',
'creation success'])
with patch.dict(glusterfs.__salt__, {'cmd.run': mock}):
self.assertEqual(glusterfs.start_volume('Newvolume1'),
'Volume Newvolume1 started')
mock_status = MagicMock(return_value='')
with patch.object(glusterfs, 'status', mock_status):
mock_run = MagicMock(return_value='creation success')
with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}):
self.assertEqual(glusterfs.start_volume('Newvolume1'),
'Volume Newvolume1 started')

mock = MagicMock(return_value='does not exist')
with patch.dict(glusterfs.__salt__, {'cmd.run': mock}):
self.assertEqual(glusterfs.start_volume('Newvolume1'),
'does not exist')
mock = MagicMock(return_value='does not exist')
with patch.dict(glusterfs.__salt__, {'cmd.run': mock}):
self.assertEqual(glusterfs.start_volume('Newvolume1'),
'does not exist')

mock = MagicMock(return_value='No volumes present in cluster')
with patch.dict(glusterfs.__salt__, {'cmd.run': mock}):
mock_run = MagicMock(return_value='No volumes present in cluster')
with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}):
self.assertEqual(glusterfs.start_volume('mycluster'),
'Volume does not exist')

Expand Down Expand Up @@ -245,4 +248,4 @@ def test_add_volume_bricks(self):

if __name__ == '__main__':
from integration import run_tests
run_tests(GitTestCase, needs_daemon=False)
run_tests(GlusterfsTestCase, needs_daemon=False)

0 comments on commit d2c0fcb

Please sign in to comment.