File catching-error-when-pidfile-cannot-be-deleted.patch of Package salt

From 945c905b355e278fd81af6b6ee02badb9761d27b Mon Sep 17 00:00:00 2001
From: Jochen Breuer <jbreuer@suse.de>
Date: Wed, 6 Sep 2017 10:16:51 +0200
Subject: [PATCH] Catching error when PIDfile cannot be deleted

Usually the PIDfile is locate in /run. If Salt is not started with root
permissions, it is not able to delete the PIDfile in /run. It should
be safe to just log and ignore this error, since Salt overwrites the
PIDfile on the next start.
---
 salt/utils/parsers.py            |  8 +++++++-
 tests/unit/utils/parsers_test.py | 30 ++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/salt/utils/parsers.py b/salt/utils/parsers.py
index c38506f3c5..5e26475a1a 100644
--- a/salt/utils/parsers.py
+++ b/salt/utils/parsers.py
@@ -881,7 +881,13 @@ class DaemonMixIn(six.with_metaclass(MixInMeta, object)):
             # We've loaded and merged options into the configuration, it's safe
             # to query about the pidfile
             if self.check_pidfile():
-                os.unlink(self.config['pidfile'])
+                try:
+                    os.unlink(self.config['pidfile'])
+                except OSError as err:
+                    # This happens when running salt-master as a non-root user
+                    # and can be ignored, since salt-master is able to
+                    # overwrite the PIDfile on the next start.
+                    pass
 
     def set_pidfile(self):
         from salt.utils.process import set_pidfile
diff --git a/tests/unit/utils/parsers_test.py b/tests/unit/utils/parsers_test.py
index 3968898c88..c2708206e9 100644
--- a/tests/unit/utils/parsers_test.py
+++ b/tests/unit/utils/parsers_test.py
@@ -5,6 +5,7 @@
 
 # Import python libs
 from __future__ import absolute_import
+import os
 
 # Import Salt Testing Libs
 from salttesting import skipIf, TestCase
@@ -21,6 +22,7 @@ import salt.utils.parsers
 import salt.log.setup as log
 import salt.config
 import salt.syspaths
+from salt.utils.parsers import DaemonMixIn
 
 ensure_in_syspath('../../')
 
@@ -926,6 +928,33 @@ 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.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)):
+                self.mixin._mixin_before_exit()
+                assert os_unlink.call_count == 1
+
 # Hide the class from unittest framework when it searches for TestCase classes in the module
 del LogSettingsParserTests
 
@@ -944,4 +973,5 @@ if __name__ == '__main__':
               SaltCloudParserTestCase,
               SPMParserTestCase,
               SaltAPIParserTestCase,
+              DaemonMixInTestCase,
               needs_daemon=False)
-- 
2.13.6


openSUSE Build Service is sponsored by