test_ext_nodes.py 2.3 KB

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