test_proxy_minion.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """
  2. :codeauthor: Gareth J. Greenaway <gareth@saltstack.com>
  3. """
  4. import copy
  5. import logging
  6. import pathlib
  7. import shutil
  8. import tempfile
  9. import textwrap
  10. import salt.config
  11. import salt.ext.tornado
  12. import salt.ext.tornado.testing
  13. import salt.metaproxy.proxy
  14. import salt.minion
  15. import salt.syspaths
  16. from saltfactories.utils import random_string
  17. from tests.support.helpers import slowTest
  18. from tests.support.mock import MagicMock, patch
  19. from tests.support.runtests import RUNTIME_VARS
  20. from tests.support.unit import TestCase
  21. log = logging.getLogger(__name__)
  22. class ProxyMinionTestCase(TestCase):
  23. @slowTest
  24. def test_post_master_init_metaproxy_called(self):
  25. """
  26. Tests that when the _post_master_ini function is called, _metaproxy_call is also called.
  27. """
  28. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  29. mock_opts.update(salt.config.DEFAULT_PROXY_MINION_OPTS)
  30. mock_jid_queue = [123]
  31. proxy_minion = salt.minion.ProxyMinion(
  32. mock_opts,
  33. jid_queue=copy.copy(mock_jid_queue),
  34. io_loop=salt.ext.tornado.ioloop.IOLoop(),
  35. )
  36. mock_metaproxy_call = MagicMock()
  37. with patch(
  38. "salt.minion._metaproxy_call",
  39. return_value=mock_metaproxy_call,
  40. autospec=True,
  41. ):
  42. try:
  43. ret = proxy_minion._post_master_init("dummy_master")
  44. self.assert_called_once(salt.minion._metaproxy_call)
  45. finally:
  46. proxy_minion.destroy()
  47. @slowTest
  48. def test_handle_decoded_payload_metaproxy_called(self):
  49. """
  50. Tests that when the _handle_decoded_payload function is called, _metaproxy_call is also called.
  51. """
  52. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  53. mock_opts.update(salt.config.DEFAULT_PROXY_MINION_OPTS)
  54. mock_data = {"fun": "foo.bar", "jid": 123}
  55. mock_jid_queue = [123]
  56. proxy_minion = salt.minion.ProxyMinion(
  57. mock_opts,
  58. jid_queue=copy.copy(mock_jid_queue),
  59. io_loop=salt.ext.tornado.ioloop.IOLoop(),
  60. )
  61. mock_metaproxy_call = MagicMock()
  62. with patch(
  63. "salt.minion._metaproxy_call",
  64. return_value=mock_metaproxy_call,
  65. autospec=True,
  66. ):
  67. try:
  68. ret = proxy_minion._handle_decoded_payload(mock_data).result()
  69. self.assertEqual(proxy_minion.jid_queue, mock_jid_queue)
  70. self.assert_called_once(salt.minion._metaproxy_call)
  71. finally:
  72. proxy_minion.destroy()
  73. @slowTest
  74. def test_handle_payload_metaproxy_called(self):
  75. """
  76. Tests that when the _handle_payload function is called, _metaproxy_call is also called.
  77. """
  78. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  79. mock_opts.update(salt.config.DEFAULT_PROXY_MINION_OPTS)
  80. mock_data = {"fun": "foo.bar", "jid": 123}
  81. mock_jid_queue = [123]
  82. proxy_minion = salt.minion.ProxyMinion(
  83. mock_opts,
  84. jid_queue=copy.copy(mock_jid_queue),
  85. io_loop=salt.ext.tornado.ioloop.IOLoop(),
  86. )
  87. mock_metaproxy_call = MagicMock()
  88. with patch(
  89. "salt.minion._metaproxy_call",
  90. return_value=mock_metaproxy_call,
  91. autospec=True,
  92. ):
  93. try:
  94. ret = proxy_minion._handle_decoded_payload(mock_data).result()
  95. self.assertEqual(proxy_minion.jid_queue, mock_jid_queue)
  96. self.assert_called_once(mock_metaproxy_call)
  97. finally:
  98. proxy_minion.destroy()
  99. def test_proxy_config_default_include(self):
  100. """
  101. Tests that when the proxy_config function is called,
  102. for the proxy minion, eg. /etc/salt/proxy.d/<The-Proxy-ID>/*.conf
  103. """
  104. proxyid = random_string("proxy-")
  105. root_dir = pathlib.Path(tempfile.mkdtemp(dir=RUNTIME_VARS.TMP))
  106. self.addCleanup(shutil.rmtree, str(root_dir), ignore_errors=True)
  107. conf_dir = root_dir / "conf"
  108. conf_file = conf_dir / "proxy"
  109. conf_d_dir = conf_dir / "proxy.d"
  110. proxy_conf_d = conf_d_dir / proxyid
  111. proxy_conf_d.mkdir(parents=True)
  112. with salt.utils.files.fopen(str(conf_file), "w") as wfh:
  113. wfh.write(
  114. textwrap.dedent(
  115. """\
  116. id: {id}
  117. root_dir: {root_dir}
  118. pidfile: run/proxy.pid
  119. pki_dir: pki
  120. cachedir: cache
  121. sock_dir: run/proxy
  122. log_file: logs/proxy.log
  123. """.format(
  124. id=proxyid, root_dir=root_dir
  125. )
  126. )
  127. )
  128. with salt.utils.files.fopen(str(proxy_conf_d / "_schedule.conf"), "w") as wfh:
  129. wfh.write(
  130. textwrap.dedent(
  131. """\
  132. schedule:
  133. test_job:
  134. args: [arg1, arg2]
  135. enabled: true
  136. function: test.arg
  137. jid_include: true
  138. kwargs: {key1: value1, key2: value2}
  139. maxrunning: 1
  140. name: test_job
  141. return_job: false
  142. """
  143. )
  144. )
  145. opts = salt.config.proxy_config(
  146. str(conf_file), minion_id=proxyid, cache_minion_id=False,
  147. )
  148. self.assertIn("schedule", opts)
  149. self.assertIn("test_job", opts["schedule"])