Skip to content

Commit

Permalink
Fix up unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam Young committed Oct 2, 2015
1 parent 563e7f2 commit 54dab00
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/swift_storage_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,14 @@ def get_mount_point(device):
if line and not line.startswith('TARGET'):
mnt_points.append(line.split()[0])
if len(mnt_points) > 1:
log('Device {} mounted in multiple places, ignoring'.format(device))
log('Device {} mounted in multiple times, ignoring'.format(device))
else:
mnt_point = mnt_points[0]
except CalledProcessError:
pass
return mnt_point


def find_block_devices(include_mounted=False):
found = []
incl = ['sd[a-z]', 'vd[a-z]', 'cciss\/c[0-9]d[0-9]']
Expand All @@ -198,18 +199,25 @@ def find_block_devices(include_mounted=False):
devs = [f for f in found if _is_storage_ready(f)]
return devs


def guess_block_devices():
bdevs = find_block_devices(include_mounted=True)
print bdevs
gdevs = []
for dev in bdevs:
print dev
if is_device_mounted(dev):
print " .. is mounted"
mnt_point = get_mount_point(dev)
if mnt_point and mnt_point.startswith('/srv/node'):
gdevs.append(dev)
else:
print " .. is not mounted"
gdevs.append(dev)
print gdevs
return gdevs


def determine_block_devices():
block_device = config('block-device')

Expand Down
34 changes: 33 additions & 1 deletion unit_tests/test_swift_storage_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
8 16 119454720 sdb1
"""

FINDMNT_FOUND_TEMPLATE = """
TARGET SOURCE FSTYPE OPTIONS
{} /dev/{} xfs rw,relatime,attr2,inode64,noquota
"""


class SwiftStorageUtilsTests(CharmTestCase):

Expand Down Expand Up @@ -153,16 +158,43 @@ def test_determine_block_device_with_missing(self, _ensure):
ex = ['/dev/vdb', '/srv/swift.img']
self.assertEqual(ex, result)

@patch.object(swift_utils, 'check_output')
@patch.object(swift_utils, 'find_block_devices')
@patch.object(swift_utils, 'ensure_block_device')
def test_determine_block_device_guess_dev(self, _ensure, _find):
def test_determine_block_device_guess_dev(self, _ensure, _find,
_check_output):
"Devices already mounted under /srv/node/ should be returned"
def _findmnt(cmd):
dev = cmd[1].split('/')[-1]
mnt_point = '/srv/node/' + dev
return FINDMNT_FOUND_TEMPLATE.format(mnt_point, dev)
_check_output.side_effect = _findmnt
_ensure.side_effect = self._fake_ensure
self.test_config.set('block-device', 'guess')
_find.return_value = ['/dev/vdb', '/dev/sdb']
result = swift_utils.determine_block_devices()
self.assertTrue(_find.called)
self.assertEquals(result, ['/dev/vdb', '/dev/sdb'])

@patch.object(swift_utils, 'check_output')
@patch.object(swift_utils, 'find_block_devices')
@patch.object(swift_utils, 'ensure_block_device')
def test_determine_block_device_guess_dev_not_eligable(self, _ensure,
_find,
_check_output):
"Devices not mounted under /srv/node/ should not be returned"
def _findmnt(cmd):
dev = cmd[1].split('/')[-1]
mnt_point = '/'
return FINDMNT_FOUND_TEMPLATE.format(mnt_point, dev)
_check_output.side_effect = _findmnt
_ensure.side_effect = self._fake_ensure
self.test_config.set('block-device', 'guess')
_find.return_value = ['/dev/vdb']
result = swift_utils.determine_block_devices()
self.assertTrue(_find.called)
self.assertEquals(result, [])

def test_mkfs_xfs(self):
swift_utils.mkfs_xfs('/dev/sdb')
self.check_call.assert_called_with(
Expand Down

0 comments on commit 54dab00

Please sign in to comment.