2014-05-12 00:07:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Ted Strzalkowski (tedski@gmail.com)`
|
|
|
|
|
|
|
|
|
|
|
|
tests.unit.modules.mdadm_test
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
'''
|
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-05-12 00:07:48 +00:00
|
|
|
# Import Salt Testing libs
|
2017-03-21 17:49:55 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
|
2014-05-12 00:07:48 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.mdadm as mdadm
|
2014-05-12 00:07:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-03-21 17:49:55 +00:00
|
|
|
class MdadmTestCase(TestCase, LoaderModuleMockMixin):
|
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {mdadm: {}}
|
2014-05-12 00:07:48 +00:00
|
|
|
|
|
|
|
def test_create(self):
|
|
|
|
mock = MagicMock(return_value='salt')
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(mdadm.__salt__, {'cmd.run': mock}), \
|
|
|
|
patch('salt.utils.which', lambda exe: exe):
|
2014-05-12 00:07:48 +00:00
|
|
|
ret = mdadm.create(
|
|
|
|
'/dev/md0', 5,
|
2014-12-17 19:27:51 +00:00
|
|
|
devices=['/dev/sdb1', '/dev/sdc1', '/dev/sdd1'],
|
2014-05-12 00:07:48 +00:00
|
|
|
test_mode=False,
|
|
|
|
force=True,
|
|
|
|
chunk=256
|
|
|
|
)
|
|
|
|
self.assertEqual('salt', ret)
|
2017-06-21 12:45:55 +00:00
|
|
|
mock.assert_called_once()
|
|
|
|
args, kwargs = mock.call_args
|
|
|
|
# expected cmd is
|
|
|
|
# mdadm -C /dev/md0 -R -v --chunk 256 --force -l 5 -e default -n 3 /dev/sdb1 /dev/sdc1 /dev/sdd1
|
|
|
|
# where args between -v and -l could be in any order
|
|
|
|
self.assertEqual(len(args), 1)
|
|
|
|
self.assertEqual(len(args[0]), 17)
|
|
|
|
self.assertEqual(args[0][:5], [
|
|
|
|
'mdadm',
|
|
|
|
'-C', '/dev/md0',
|
|
|
|
'-R',
|
|
|
|
'-v'])
|
|
|
|
self.assertEqual(args[0][8:], [
|
|
|
|
'-l', '5',
|
|
|
|
'-e', 'default',
|
|
|
|
'-n', '3',
|
|
|
|
'/dev/sdb1', '/dev/sdc1', '/dev/sdd1'])
|
|
|
|
self.assertEqual(sorted(args[0][5:8]), sorted(['--chunk', '256', '--force']))
|
|
|
|
self.assertIn('--chunk 256', ' '.join(args[0][5:8]))
|
|
|
|
self.assertEqual(kwargs, {'python_shell': False})
|
2014-05-12 00:07:48 +00:00
|
|
|
|
|
|
|
def test_create_test_mode(self):
|
|
|
|
mock = MagicMock()
|
|
|
|
with patch.dict(mdadm.__salt__, {'cmd.run': mock}):
|
|
|
|
ret = mdadm.create(
|
|
|
|
'/dev/md0', 5,
|
2014-12-17 19:27:51 +00:00
|
|
|
devices=['/dev/sdb1', '/dev/sdc1', '/dev/sdd1'],
|
2014-05-12 00:07:48 +00:00
|
|
|
force=True,
|
|
|
|
chunk=256,
|
|
|
|
test_mode=True
|
|
|
|
)
|
2017-02-15 19:13:59 +00:00
|
|
|
self.assertEqual(sorted('mdadm -C /dev/md0 -R -v --chunk 256 '
|
2014-12-17 18:46:34 +00:00
|
|
|
'--force -l 5 -e default -n 3 '
|
2017-02-15 19:13:59 +00:00
|
|
|
'/dev/sdb1 /dev/sdc1 /dev/sdd1'.split()), sorted(ret.split()))
|
2014-05-12 00:07:48 +00:00
|
|
|
assert not mock.called, 'test mode failed, cmd.run called'
|