From 090778c449132dd8ed34c418ddc0ff36300e6288 Mon Sep 17 00:00:00 2001 From: Alberto Planas Date: Tue, 12 Mar 2019 10:36:09 +0100 Subject: [PATCH] mdadm_raid: Optionally ignore errors on examine. During the RAID creation, the code examine each device to decide if belongs to an already present array or not. The first time that the RAID is created, is expected that examine will fail. This patch add an optional parameter to examine, to ignore the fail in the logs. Also calls examine with this parameter during the RAID creation. (cherry picked from commit bfd8b1157f2de4db3453dd7545fcb6f3332ce8e2) --- salt/modules/mdadm_raid.py | 12 ++++++++++-- salt/states/mdadm_raid.py | 2 +- tests/unit/modules/test_mdadm_raid.py | 23 +++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/salt/modules/mdadm_raid.py b/salt/modules/mdadm_raid.py index 829f4cdd245e..93dd0a1e3336 100644 --- a/salt/modules/mdadm_raid.py +++ b/salt/modules/mdadm_raid.py @@ -360,17 +360,25 @@ def assemble(name, return __salt__['cmd.run'](cmd, python_shell=False) -def examine(device): +def examine(device, quiet=False): ''' Show detail for a specified RAID component device + device + Device to examine, that is part of the RAID + + quiet + If the device is not part of the RAID, do not show any error + CLI Example: .. code-block:: bash salt '*' raid.examine '/dev/sda1' ''' - res = __salt__['cmd.run_stdout']('mdadm -Y -E {0}'.format(device), output_loglevel='trace', python_shell=False) + res = __salt__['cmd.run_stdout']('mdadm -Y -E {0}'.format(device), + python_shell=False, + ignore_retcode=quiet) ret = {} for line in res.splitlines(): diff --git a/salt/states/mdadm_raid.py b/salt/states/mdadm_raid.py index fd285b6acee2..d634522c334e 100644 --- a/salt/states/mdadm_raid.py +++ b/salt/states/mdadm_raid.py @@ -98,7 +98,7 @@ def present(name, if dev == 'missing' or not __salt__['file.access'](dev, 'f'): missing.append(dev) continue - superblock = __salt__['raid.examine'](dev) + superblock = __salt__['raid.examine'](dev, quiet=True) if 'MD_UUID' in superblock: uuid = superblock['MD_UUID'] diff --git a/tests/unit/modules/test_mdadm_raid.py b/tests/unit/modules/test_mdadm_raid.py index bfca3af8762e..c9b09ed94eb8 100644 --- a/tests/unit/modules/test_mdadm_raid.py +++ b/tests/unit/modules/test_mdadm_raid.py @@ -75,3 +75,26 @@ def test_create_test_mode(self): '--force -l 5 -e default -n 3 ' '/dev/sdb1 /dev/sdc1 /dev/sdd1'.split()), sorted(ret.split())) assert not mock.called, 'test mode failed, cmd.run called' + + def test_examine(self): + ''' + Test for mdadm_raid.examine + ''' + mock = MagicMock(return_value='ARRAY /dev/md/pool metadata=1.2 UUID=567da122:fb8e445e:55b853e0:81bd0a3e name=positron:pool') + with patch.dict(mdadm.__salt__, {'cmd.run_stdout': mock}): + self.assertEqual(mdadm.examine('/dev/md0'), + { + 'ARRAY /dev/md/pool metadata': '1.2 UUID=567da122:fb8e445e:55b853e0:81bd0a3e name=positron:pool' + }) + mock.assert_called_with('mdadm -Y -E /dev/md0', ignore_retcode=False, + python_shell=False) + + def test_examine_quiet(self): + ''' + Test for mdadm_raid.examine + ''' + mock = MagicMock(return_value='') + with patch.dict(mdadm.__salt__, {'cmd.run_stdout': mock}): + self.assertEqual(mdadm.examine('/dev/md0', quiet=True), {}) + mock.assert_called_with('mdadm -Y -E /dev/md0', ignore_retcode=True, + python_shell=False)