test_ext_nodes.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Test ext_nodes master_tops module
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import subprocess
  8. import textwrap
  9. # Import Salt Testing libs
  10. from tests.support.unit import TestCase
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import patch, MagicMock
  13. # Import Salt libs
  14. import salt.utils.stringutils
  15. import salt.tops.ext_nodes as ext_nodes
  16. class ExtNodesTestCase(TestCase, LoaderModuleMockMixin):
  17. def setup_loader_modules(self):
  18. return {
  19. ext_nodes: {
  20. '__opts__': {
  21. 'master_tops': {
  22. # Since ext_nodes runs the command with shell=True,
  23. # this will keep "command not found" errors from
  24. # showing up on the console. We'll be mocking the
  25. # communicate results anyway.
  26. 'ext_nodes': 'echo',
  27. }
  28. }
  29. }
  30. }
  31. def test_ext_nodes(self):
  32. '''
  33. Confirm that subprocess.Popen works as expected and does not raise an
  34. exception (see https://github.com/saltstack/salt/pull/46863).
  35. '''
  36. stdout = salt.utils.stringutils.to_bytes(textwrap.dedent('''\
  37. classes:
  38. - one
  39. - two'''))
  40. communicate_mock = MagicMock(return_value=(stdout, None))
  41. with patch.object(subprocess.Popen, 'communicate', communicate_mock):
  42. ret = ext_nodes.top(opts={'id': 'foo'})
  43. self.assertEqual(ret, {'base': ['one', 'two']})
  44. def test_ext_nodes_with_environment(self):
  45. '''
  46. Same as above, but also tests that the matches are assigned to the proper
  47. environment if one is returned by the ext_nodes command.
  48. '''
  49. stdout = salt.utils.stringutils.to_bytes(textwrap.dedent('''\
  50. classes:
  51. - one
  52. - two
  53. environment: dev'''))
  54. communicate_mock = MagicMock(return_value=(stdout, None))
  55. with patch.object(subprocess.Popen, 'communicate', communicate_mock):
  56. ret = ext_nodes.top(opts={'id': 'foo'})
  57. self.assertEqual(ret, {'dev': ['one', 'two']})