test_nodegroups.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import TestCase
  7. from tests.support.mock import patch, MagicMock
  8. # Import Salt Libs
  9. import salt.pillar.nodegroups as nodegroups
  10. fake_minion_id = 'fake_id'
  11. fake_pillar = {}
  12. fake_nodegroups = {
  13. 'groupA': fake_minion_id,
  14. 'groupB': 'another_minion_id',
  15. }
  16. fake_opts = {
  17. 'cache': 'localfs',
  18. 'nodegroups': fake_nodegroups,
  19. 'id': fake_minion_id
  20. }
  21. fake_pillar_name = 'fake_pillar_name'
  22. def side_effect(group_sel, t):
  23. if group_sel.find(fake_minion_id) != -1:
  24. return {'minions': [fake_minion_id, ],
  25. 'missing': []}
  26. return {'minions': ['another_minion_id', ],
  27. 'missing': []}
  28. class NodegroupsPillarTestCase(TestCase, LoaderModuleMockMixin):
  29. '''
  30. Tests for salt.pillar.nodegroups
  31. '''
  32. def setup_loader_modules(self):
  33. return {nodegroups: {'__opts__': fake_opts}}
  34. def _runner(self, expected_ret, pillar_name=None):
  35. with patch('salt.utils.minions.CkMinions.check_minions',
  36. MagicMock(side_effect=side_effect)):
  37. pillar_name = pillar_name or fake_pillar_name
  38. actual_ret = nodegroups.ext_pillar(fake_minion_id, fake_pillar, pillar_name=pillar_name)
  39. self.assertDictEqual(actual_ret, expected_ret)
  40. def test_succeeds(self):
  41. ret = {fake_pillar_name: ['groupA', ]}
  42. self._runner(ret)