test_nodegroups.py 1.5 KB

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