Merge pull request #43646 from brejoc/2016.11.4-pidfile-tests

Added tests for pid-file deletion in DaemonMixIn
This commit is contained in:
Mike Place 2017-09-20 13:21:53 -06:00 committed by GitHub
commit 6b4516c025

View file

@ -926,6 +926,48 @@ class SaltAPIParserTestCase(LogSettingsParserTests):
self.parser = salt.utils.parsers.SaltAPIParser
@skipIf(NO_MOCK, NO_MOCK_REASON)
class DaemonMixInTestCase(TestCase):
'''
Tests the PIDfile deletion in the DaemonMixIn.
'''
def setUp(self):
'''
Setting up
'''
# Set PID
self.pid = '/some/fake.pid'
# Setup mixin
self.mixin = salt.utils.parsers.DaemonMixIn()
self.mixin.info = None
self.mixin.config = {}
self.mixin.config['pidfile'] = self.pid
def test_pid_file_deletion(self):
'''
PIDfile deletion without exception.
'''
with patch('os.unlink', MagicMock()) as os_unlink:
with patch('os.path.isfile', MagicMock(return_value=True)):
with patch.object(self.mixin, 'info', MagicMock()):
self.mixin._mixin_before_exit()
assert self.mixin.info.call_count == 0
assert os_unlink.call_count == 1
def test_pid_file_deletion_with_oserror(self):
'''
PIDfile deletion with exception
'''
with patch('os.unlink', MagicMock(side_effect=OSError())) as os_unlink:
with patch('os.path.isfile', MagicMock(return_value=True)):
with patch.object(self.mixin, 'info', MagicMock()):
self.mixin._mixin_before_exit()
assert os_unlink.call_count == 1
self.mixin.info.assert_called_with(
'PIDfile could not be deleted: {0}'.format(self.pid))
# Hide the class from unittest framework when it searches for TestCase classes in the module
del LogSettingsParserTests
@ -944,4 +986,5 @@ if __name__ == '__main__':
SaltCloudParserTestCase,
SPMParserTestCase,
SaltAPIParserTestCase,
DaemonMixInTestCase,
needs_daemon=False)