Skip to content

Commit

Permalink
Merge pull request #46713 from DSRCorporation/bugs/43941_grains_refre…
Browse files Browse the repository at this point in the history
…sh_twice

Don't refresh modules twice per sync or refresh ops
  • Loading branch information
dwoz authored Dec 3, 2019
2 parents 07c9426 + 94f490b commit a77b1b3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion salt/minion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,8 @@ def pillar_refresh(self, force_refresh=False):
'''
Refresh the pillar
'''
self.module_refresh(force_refresh)

if self.connected:
log.debug('Refreshing pillar')
async_pillar = salt.pillar.get_async_pillar(
Expand All @@ -2205,7 +2207,6 @@ def pillar_refresh(self, force_refresh=False):
'One or more masters may be down!')
finally:
async_pillar.destroy()
self.module_refresh(force_refresh)
self.matchers_refresh()
self.beacons_refresh()
evt = salt.utils.event.get_event('minion', opts=self.opts)
Expand Down
10 changes: 6 additions & 4 deletions salt/modules/saltutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,11 @@ def refresh_grains(**kwargs):
# Modules and pillar need to be refreshed in case grains changes affected
# them, and the module refresh process reloads the grains and assigns the
# newly-reloaded grains to each execution module's __grains__ dunder.
refresh_modules()
if _refresh_pillar:
# we don't need to call refresh_modules here because it's done by refresh_pillar
refresh_pillar()
else:
refresh_modules()
return True


Expand Down Expand Up @@ -425,7 +427,7 @@ def sync_grains(saltenv=None, refresh=True, extmod_whitelist=None, extmod_blackl
'''
ret = _sync('grains', saltenv, extmod_whitelist, extmod_blacklist)
if refresh:
refresh_modules()
# we don't need to call refresh_modules here because it's done by refresh_pillar
refresh_pillar()
return ret

Expand Down Expand Up @@ -913,7 +915,7 @@ def sync_pillar(saltenv=None, refresh=True, extmod_whitelist=None, extmod_blackl
)
ret = _sync('pillar', saltenv, extmod_whitelist, extmod_blacklist)
if refresh:
refresh_modules()
# we don't need to call refresh_modules here because it's done by refresh_pillar
refresh_pillar()
return ret

Expand Down Expand Up @@ -1026,7 +1028,7 @@ def sync_all(saltenv=None, refresh=True, extmod_whitelist=None, extmod_blacklist
if __opts__['file_client'] == 'local':
ret['pillar'] = sync_pillar(saltenv, False, extmod_whitelist, extmod_blacklist)
if refresh:
refresh_modules()
# we don't need to call refresh_modules here because it's done by refresh_pillar
refresh_pillar()
return ret

Expand Down
22 changes: 20 additions & 2 deletions tests/integration/modules/test_grains.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import time
import pprint

# Import Salt libs
from salt.ext.six.moves import range

# Import Salt Testing libs
from tests.support.case import ModuleCase
from tests.support.unit import skipIf
from tests.support.helpers import flaky
from tests.support.unit import skipIf

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -174,7 +177,8 @@ def test_grains_append_val_already_present(self):
'salttesting-grain-key'.format(self.GRAIN_VAL)

# First, make sure the test grain is present
self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
ret = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])

# Now try to append again
ret = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
Expand Down Expand Up @@ -226,3 +230,17 @@ def test_grains_append_call_twice(self):
pprint.pformat(append_2)
)
)

def test_grains_remove_add(self):
second_grain = self.GRAIN_VAL + '-2'
ret = self.run_function('grains.get', [self.GRAIN_KEY])
self.assertEqual(ret, [])

for i in range(10):
self.run_function('grains.setval', [self.GRAIN_KEY, []])
ret = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])

self.run_function('grains.setval', [self.GRAIN_KEY, []])
ret = self.run_function('grains.append', [self.GRAIN_KEY, [self.GRAIN_VAL, second_grain]])
self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL, second_grain])
6 changes: 6 additions & 0 deletions tests/integration/modules/test_saltutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def test_wheel_with_kwarg(self):
self.assertIn('priv', ret['return'])


class SyncGrainsTest(ModuleCase):
def test_sync_grains(self):
ret = self.run_function('saltutil.sync_grains')
self.assertEqual(ret, [])


class SaltUtilSyncModuleTest(ModuleCase):
'''
Testcase for the saltutil sync execution module
Expand Down

0 comments on commit a77b1b3

Please sign in to comment.