1
0

test_ext_nodes.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.tops.ext_nodes as ext_nodes
  10. # Import Salt libs
  11. import salt.utils.stringutils
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, patch
  14. # Import Salt Testing libs
  15. from tests.support.unit import TestCase
  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(
  37. textwrap.dedent(
  38. """\
  39. classes:
  40. - one
  41. - two"""
  42. )
  43. )
  44. communicate_mock = MagicMock(return_value=(stdout, None))
  45. with patch.object(subprocess.Popen, "communicate", communicate_mock):
  46. ret = ext_nodes.top(opts={"id": "foo"})
  47. self.assertEqual(ret, {"base": ["one", "two"]})
  48. def test_ext_nodes_with_environment(self):
  49. """
  50. Same as above, but also tests that the matches are assigned to the proper
  51. environment if one is returned by the ext_nodes command.
  52. """
  53. stdout = salt.utils.stringutils.to_bytes(
  54. textwrap.dedent(
  55. """\
  56. classes:
  57. - one
  58. - two
  59. environment: dev"""
  60. )
  61. )
  62. communicate_mock = MagicMock(return_value=(stdout, None))
  63. with patch.object(subprocess.Popen, "communicate", communicate_mock):
  64. ret = ext_nodes.top(opts={"id": "foo"})
  65. self.assertEqual(ret, {"dev": ["one", "two"]})