Merge pull request #48077 from twangboy/fix_parsers

Fix issue with `salt.utils.parsers` on Windows
This commit is contained in:
Mike Place 2018-06-14 18:22:56 +02:00 committed by GitHub
commit 4b6f1c7f75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 14 deletions

View file

@ -968,9 +968,17 @@ class DaemonMixIn(six.with_metaclass(MixInMeta, object)):
# Log error only when running salt-master as a root user.
# Otherwise this can be ignored, since salt-master is able to
# overwrite the PIDfile on the next start.
if not os.getuid():
logger.info('PIDfile could not be deleted: %s', six.text_type(self.config['pidfile']))
logger.debug(six.text_type(err))
err_msg = ('PIDfile could not be deleted: %s',
six.text_type(self.config['pidfile']))
if salt.utils.platform.is_windows():
user = salt.utils.win_functions.get_current_user()
if salt.utils.win_functions.is_admin(user):
logger.info(*err_msg)
logger.debug(six.text_type(err))
else:
if not os.getuid():
logger.info(*err_msg)
logger.debug(six.text_type(err))
def set_pidfile(self):
from salt.utils.process import set_pidfile

View file

@ -1036,30 +1036,44 @@ class DaemonMixInTestCase(TestCase):
@patch('os.unlink', MagicMock(side_effect=OSError()))
@patch('os.path.isfile', MagicMock(return_value=True))
@patch('os.getuid', MagicMock(return_value=0))
@patch('salt.utils.parsers.logger', MagicMock())
def test_pid_deleted_oserror_as_root(self):
'''
PIDfile deletion with exception, running as root.
'''
self.daemon_mixin._mixin_before_exit()
assert salt.utils.parsers.os.unlink.call_count == 1
salt.utils.parsers.logger.info.assert_called_with('PIDfile could not be deleted: %s',
format(self.daemon_mixin.config['pidfile']))
salt.utils.parsers.logger.debug.assert_called()
if salt.utils.platform.is_windows():
patch_args = ('salt.utils.win_functions.is_admin',
MagicMock(return_value=True))
else:
patch_args = ('os.getuid', MagicMock(return_value=0))
with patch(*patch_args):
self.daemon_mixin._mixin_before_exit()
assert salt.utils.parsers.os.unlink.call_count == 1
salt.utils.parsers.logger.info.assert_called_with(
'PIDfile could not be deleted: %s',
format(self.daemon_mixin.config['pidfile'])
)
salt.utils.parsers.logger.debug.assert_called()
@patch('os.unlink', MagicMock(side_effect=OSError()))
@patch('os.path.isfile', MagicMock(return_value=True))
@patch('os.getuid', MagicMock(return_value=1000))
@patch('salt.utils.parsers.logger', MagicMock())
def test_pid_deleted_oserror_as_non_root(self):
'''
PIDfile deletion with exception, running as non-root.
'''
self.daemon_mixin._mixin_before_exit()
assert salt.utils.parsers.os.unlink.call_count == 1
salt.utils.parsers.logger.info.assert_not_called()
salt.utils.parsers.logger.debug.assert_not_called()
if salt.utils.platform.is_windows():
patch_args = ('salt.utils.win_functions.is_admin',
MagicMock(return_value=False))
else:
patch_args = ('os.getuid', MagicMock(return_value=1000))
with patch(*patch_args):
self.daemon_mixin._mixin_before_exit()
assert salt.utils.parsers.os.unlink.call_count == 1
salt.utils.parsers.logger.info.assert_not_called()
salt.utils.parsers.logger.debug.assert_not_called()
# Hide the class from unittest framework when it searches for TestCase classes in the module