File restrict-the-start_event_grains-only-to-the-start-ev.patch of Package salt.14915

From e819518ec3ef48bc93f7514710ebfebe59dfd228 Mon Sep 17 00:00:00 2001
From: Abid Mehmood <amehmood@suse.de>
Date: Thu, 16 Jan 2020 11:28:04 +0100
Subject: [PATCH] Restrict the 'start_event_grains' only to the start
 events

add test for custom events
---
 salt/minion.py            | 11 ++++++++---
 tests/unit/test_minion.py | 18 +++++++++++++++++-
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/salt/minion.py b/salt/minion.py
index 4c7ea0491c..8f60195cb3 100644
--- a/salt/minion.py
+++ b/salt/minion.py
@@ -1424,7 +1424,7 @@ class Minion(MinionBase):
         finally:
             channel.close()
 
-    def _fire_master(self, data=None, tag=None, events=None, pretag=None, timeout=60, sync=True, timeout_handler=None):
+    def _fire_master(self, data=None, tag=None, events=None, pretag=None, timeout=60, sync=True, timeout_handler=None, include_startup_grains=False):
         '''
         Fire an event on the master, or drop message if unable to send.
         '''
@@ -1443,7 +1443,7 @@ class Minion(MinionBase):
         else:
             return
 
-        if self.opts['start_event_grains']:
+        if include_startup_grains:
             grains_to_add = dict(
                 [(k, v) for k, v in six.iteritems(self.opts.get('grains', {})) if k in self.opts['start_event_grains']])
             load['grains'] = grains_to_add
@@ -2157,6 +2157,9 @@ class Minion(MinionBase):
             })
 
     def _fire_master_minion_start(self):
+        include_grains = False
+        if self.opts['start_event_grains']:
+            include_grains = True
         # Send an event to the master that the minion is live
         if self.opts['enable_legacy_startup_events']:
             # Old style event. Defaults to False in Sodium release.
@@ -2165,7 +2168,8 @@ class Minion(MinionBase):
                 self.opts['id'],
                 time.asctime()
                 ),
-                'minion_start'
+                'minion_start',
+                include_startup_grains=include_grains
             )
         # send name spaced event
         self._fire_master(
@@ -2174,6 +2178,7 @@ class Minion(MinionBase):
             time.asctime()
             ),
             tagify([self.opts['id'], 'start'], 'minion'),
+            include_startup_grains=include_grains
         )
 
     def module_refresh(self, force_refresh=False, notify=False):
diff --git a/tests/unit/test_minion.py b/tests/unit/test_minion.py
index 7913b9cd01..4c57ab8f3f 100644
--- a/tests/unit/test_minion.py
+++ b/tests/unit/test_minion.py
@@ -314,7 +314,7 @@ class MinionTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
         try:
             minion.tok = MagicMock()
             minion._send_req_sync = MagicMock()
-            minion._fire_master('Minion has started', 'minion_start')
+            minion._fire_master('Minion has started', 'minion_start', include_startup_grains=True)
             load = minion._send_req_sync.call_args[0][0]
 
             self.assertTrue('grains' in load)
@@ -337,6 +337,22 @@ class MinionTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
         finally:
             minion.destroy()
 
+    def test_when_other_events_fired_and_start_event_grains_are_set(self):
+        mock_opts = self.get_config('minion', from_scratch=True)
+        mock_opts['start_event_grains'] = ["os"]
+        io_loop = tornado.ioloop.IOLoop()
+        io_loop.make_current()
+        minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
+        try:
+            minion.tok = MagicMock()
+            minion._send_req_sync = MagicMock()
+            minion._fire_master('Custm_event_fired', 'custom_event')
+            load = minion._send_req_sync.call_args[0][0]
+
+            self.assertTrue('grains' not in load)
+        finally:
+            minion.destroy()
+
     def test_minion_retry_dns_count(self):
         '''
         Tests that the resolve_dns will retry dns look ups for a maximum of
-- 
2.16.4