1
0

test_minion.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Mike Place <mp@saltstack.com>
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import
  7. import copy
  8. import os
  9. # Import Salt Testing libs
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import patch, MagicMock
  12. from tests.support.mixins import AdaptedConfigurationTestCaseMixin
  13. from tests.support.helpers import skip_if_not_root
  14. # Import salt libs
  15. import salt.minion
  16. import salt.utils.event as event
  17. from salt.exceptions import SaltSystemExit, SaltMasterUnresolvableError
  18. import salt.syspaths
  19. import salt.ext.tornado
  20. import salt.ext.tornado.testing
  21. from salt.ext.six.moves import range
  22. import salt.utils.crypt
  23. import salt.utils.process
  24. class MinionTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
  25. def setUp(self):
  26. self.opts = {}
  27. self.addCleanup(delattr, self, 'opts')
  28. def test_invalid_master_address(self):
  29. with patch.dict(self.opts, {'ipv6': False, 'master': float('127.0'), 'master_port': '4555', 'retry_dns': False}):
  30. self.assertRaises(SaltSystemExit, salt.minion.resolve_dns, self.opts)
  31. def test_source_int_name_local(self):
  32. '''
  33. test when file_client local and
  34. source_interface_name is set
  35. '''
  36. interfaces = {'bond0.1234': {'hwaddr': '01:01:01:d0:d0:d0',
  37. 'up': True, 'inet':
  38. [{'broadcast': '111.1.111.255',
  39. 'netmask': '111.1.0.0',
  40. 'label': 'bond0',
  41. 'address': '111.1.0.1'}]}}
  42. with patch.dict(self.opts, {'ipv6': False, 'master': '127.0.0.1',
  43. 'master_port': '4555', 'file_client': 'local',
  44. 'source_interface_name': 'bond0.1234',
  45. 'source_ret_port': 49017,
  46. 'source_publish_port': 49018}), \
  47. patch('salt.utils.network.interfaces',
  48. MagicMock(return_value=interfaces)):
  49. assert salt.minion.resolve_dns(self.opts) == {'master_ip': '127.0.0.1',
  50. 'source_ip': '111.1.0.1',
  51. 'source_ret_port': 49017,
  52. 'source_publish_port': 49018,
  53. 'master_uri': 'tcp://127.0.0.1:4555'}
  54. def test_source_int_name_remote(self):
  55. '''
  56. test when file_client remote and
  57. source_interface_name is set and
  58. interface is down
  59. '''
  60. interfaces = {'bond0.1234': {'hwaddr': '01:01:01:d0:d0:d0',
  61. 'up': False, 'inet':
  62. [{'broadcast': '111.1.111.255',
  63. 'netmask': '111.1.0.0',
  64. 'label': 'bond0',
  65. 'address': '111.1.0.1'}]}}
  66. with patch.dict(self.opts, {'ipv6': False, 'master': '127.0.0.1',
  67. 'master_port': '4555', 'file_client': 'remote',
  68. 'source_interface_name': 'bond0.1234',
  69. 'source_ret_port': 49017,
  70. 'source_publish_port': 49018}), \
  71. patch('salt.utils.network.interfaces',
  72. MagicMock(return_value=interfaces)):
  73. assert salt.minion.resolve_dns(self.opts) == {'master_ip': '127.0.0.1',
  74. 'source_ret_port': 49017,
  75. 'source_publish_port': 49018,
  76. 'master_uri': 'tcp://127.0.0.1:4555'}
  77. def test_source_address(self):
  78. '''
  79. test when source_address is set
  80. '''
  81. interfaces = {'bond0.1234': {'hwaddr': '01:01:01:d0:d0:d0',
  82. 'up': False, 'inet':
  83. [{'broadcast': '111.1.111.255',
  84. 'netmask': '111.1.0.0',
  85. 'label': 'bond0',
  86. 'address': '111.1.0.1'}]}}
  87. with patch.dict(self.opts, {'ipv6': False, 'master': '127.0.0.1',
  88. 'master_port': '4555', 'file_client': 'local',
  89. 'source_interface_name': '',
  90. 'source_address': '111.1.0.1',
  91. 'source_ret_port': 49017,
  92. 'source_publish_port': 49018}), \
  93. patch('salt.utils.network.interfaces',
  94. MagicMock(return_value=interfaces)):
  95. assert salt.minion.resolve_dns(self.opts) == {'source_publish_port': 49018,
  96. 'source_ret_port': 49017,
  97. 'master_uri': 'tcp://127.0.0.1:4555',
  98. 'source_ip': '111.1.0.1',
  99. 'master_ip': '127.0.0.1'}
  100. # Tests for _handle_decoded_payload in the salt.minion.Minion() class: 3
  101. def test_handle_decoded_payload_jid_match_in_jid_queue(self):
  102. '''
  103. Tests that the _handle_decoded_payload function returns when a jid is given that is already present
  104. in the jid_queue.
  105. Note: This test doesn't contain all of the patch decorators above the function like the other tests
  106. for _handle_decoded_payload below. This is essential to this test as the call to the function must
  107. return None BEFORE any of the processes are spun up because we should be avoiding firing duplicate
  108. jobs.
  109. '''
  110. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  111. mock_data = {'fun': 'foo.bar',
  112. 'jid': 123}
  113. mock_jid_queue = [123]
  114. minion = salt.minion.Minion(mock_opts, jid_queue=copy.copy(mock_jid_queue), io_loop=salt.ext.tornado.ioloop.IOLoop())
  115. try:
  116. ret = minion._handle_decoded_payload(mock_data).result()
  117. self.assertEqual(minion.jid_queue, mock_jid_queue)
  118. self.assertIsNone(ret)
  119. finally:
  120. minion.destroy()
  121. def test_handle_decoded_payload_jid_queue_addition(self):
  122. '''
  123. Tests that the _handle_decoded_payload function adds a jid to the minion's jid_queue when the new
  124. jid isn't already present in the jid_queue.
  125. '''
  126. with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \
  127. patch('salt.utils.process.SignalHandlingProcess.start', MagicMock(return_value=True)), \
  128. patch('salt.utils.process.SignalHandlingProcess.join', MagicMock(return_value=True)):
  129. mock_jid = 11111
  130. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  131. mock_data = {'fun': 'foo.bar',
  132. 'jid': mock_jid}
  133. mock_jid_queue = [123, 456]
  134. minion = salt.minion.Minion(mock_opts, jid_queue=copy.copy(mock_jid_queue), io_loop=salt.ext.tornado.ioloop.IOLoop())
  135. try:
  136. # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
  137. # This can help debug any test failures if the _handle_decoded_payload call fails.
  138. self.assertEqual(minion.jid_queue, mock_jid_queue)
  139. # Call the _handle_decoded_payload function and update the mock_jid_queue to include the new
  140. # mock_jid. The mock_jid should have been added to the jid_queue since the mock_jid wasn't
  141. # previously included. The minion's jid_queue attribute and the mock_jid_queue should be equal.
  142. minion._handle_decoded_payload(mock_data).result()
  143. mock_jid_queue.append(mock_jid)
  144. self.assertEqual(minion.jid_queue, mock_jid_queue)
  145. finally:
  146. minion.destroy()
  147. def test_handle_decoded_payload_jid_queue_reduced_minion_jid_queue_hwm(self):
  148. '''
  149. Tests that the _handle_decoded_payload function removes a jid from the minion's jid_queue when the
  150. minion's jid_queue high water mark (minion_jid_queue_hwm) is hit.
  151. '''
  152. with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \
  153. patch('salt.utils.process.SignalHandlingProcess.start', MagicMock(return_value=True)), \
  154. patch('salt.utils.process.SignalHandlingProcess.join', MagicMock(return_value=True)):
  155. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  156. mock_opts['minion_jid_queue_hwm'] = 2
  157. mock_data = {'fun': 'foo.bar',
  158. 'jid': 789}
  159. mock_jid_queue = [123, 456]
  160. minion = salt.minion.Minion(mock_opts, jid_queue=copy.copy(mock_jid_queue), io_loop=salt.ext.tornado.ioloop.IOLoop())
  161. try:
  162. # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
  163. # This can help debug any test failures if the _handle_decoded_payload call fails.
  164. self.assertEqual(minion.jid_queue, mock_jid_queue)
  165. # Call the _handle_decoded_payload function and check that the queue is smaller by one item
  166. # and contains the new jid
  167. minion._handle_decoded_payload(mock_data).result()
  168. self.assertEqual(len(minion.jid_queue), 2)
  169. self.assertEqual(minion.jid_queue, [456, 789])
  170. finally:
  171. minion.destroy()
  172. def test_process_count_max(self):
  173. '''
  174. Tests that the _handle_decoded_payload function does not spawn more than the configured amount of processes,
  175. as per process_count_max.
  176. '''
  177. with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \
  178. patch('salt.utils.process.SignalHandlingProcess.start', MagicMock(return_value=True)), \
  179. patch('salt.utils.process.SignalHandlingProcess.join', MagicMock(return_value=True)), \
  180. patch('salt.utils.minion.running', MagicMock(return_value=[])), \
  181. patch('salt.ext.tornado.gen.sleep', MagicMock(return_value=salt.ext.tornado.concurrent.Future())):
  182. process_count_max = 10
  183. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  184. mock_opts['__role'] = 'minion'
  185. mock_opts['minion_jid_queue_hwm'] = 100
  186. mock_opts["process_count_max"] = process_count_max
  187. io_loop = salt.ext.tornado.ioloop.IOLoop()
  188. minion = salt.minion.Minion(mock_opts, jid_queue=[], io_loop=io_loop)
  189. try:
  190. # mock gen.sleep to throw a special Exception when called, so that we detect it
  191. class SleepCalledException(Exception):
  192. """Thrown when sleep is called"""
  193. salt.ext.tornado.gen.sleep.return_value.set_exception(SleepCalledException())
  194. # up until process_count_max: gen.sleep does not get called, processes are started normally
  195. for i in range(process_count_max):
  196. mock_data = {'fun': 'foo.bar',
  197. 'jid': i}
  198. io_loop.run_sync(lambda data=mock_data: minion._handle_decoded_payload(data))
  199. self.assertEqual(salt.utils.process.SignalHandlingProcess.start.call_count, i + 1)
  200. self.assertEqual(len(minion.jid_queue), i + 1)
  201. salt.utils.minion.running.return_value += [i]
  202. # above process_count_max: gen.sleep does get called, JIDs are created but no new processes are started
  203. mock_data = {'fun': 'foo.bar',
  204. 'jid': process_count_max + 1}
  205. self.assertRaises(SleepCalledException,
  206. lambda: io_loop.run_sync(lambda: minion._handle_decoded_payload(mock_data)))
  207. self.assertEqual(salt.utils.process.SignalHandlingProcess.start.call_count,
  208. process_count_max)
  209. self.assertEqual(len(minion.jid_queue), process_count_max + 1)
  210. finally:
  211. minion.destroy()
  212. def test_beacons_before_connect(self):
  213. '''
  214. Tests that the 'beacons_before_connect' option causes the beacons to be initialized before connect.
  215. '''
  216. with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \
  217. patch('salt.minion.Minion.sync_connect_master', MagicMock(side_effect=RuntimeError('stop execution'))), \
  218. patch('salt.utils.process.SignalHandlingProcess.start', MagicMock(return_value=True)), \
  219. patch('salt.utils.process.SignalHandlingProcess.join', MagicMock(return_value=True)):
  220. mock_opts = self.get_config('minion', from_scratch=True)
  221. mock_opts['beacons_before_connect'] = True
  222. io_loop = salt.ext.tornado.ioloop.IOLoop()
  223. io_loop.make_current()
  224. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  225. try:
  226. try:
  227. minion.tune_in(start=True)
  228. except RuntimeError:
  229. pass
  230. # Make sure beacons are initialized but the sheduler is not
  231. self.assertTrue('beacons' in minion.periodic_callbacks)
  232. self.assertTrue('schedule' not in minion.periodic_callbacks)
  233. finally:
  234. minion.destroy()
  235. def test_scheduler_before_connect(self):
  236. '''
  237. Tests that the 'scheduler_before_connect' option causes the scheduler to be initialized before connect.
  238. '''
  239. with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \
  240. patch('salt.minion.Minion.sync_connect_master', MagicMock(side_effect=RuntimeError('stop execution'))), \
  241. patch('salt.utils.process.SignalHandlingProcess.start', MagicMock(return_value=True)), \
  242. patch('salt.utils.process.SignalHandlingProcess.join', MagicMock(return_value=True)):
  243. mock_opts = self.get_config('minion', from_scratch=True)
  244. mock_opts['scheduler_before_connect'] = True
  245. io_loop = salt.ext.tornado.ioloop.IOLoop()
  246. io_loop.make_current()
  247. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  248. try:
  249. try:
  250. minion.tune_in(start=True)
  251. except RuntimeError:
  252. pass
  253. # Make sure the scheduler is initialized but the beacons are not
  254. self.assertTrue('schedule' in minion.periodic_callbacks)
  255. self.assertTrue('beacons' not in minion.periodic_callbacks)
  256. finally:
  257. minion.destroy()
  258. def test_when_ping_interval_is_set_the_callback_should_be_added_to_periodic_callbacks(self):
  259. with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \
  260. patch('salt.minion.Minion.sync_connect_master', MagicMock(side_effect=RuntimeError('stop execution'))), \
  261. patch('salt.utils.process.SignalHandlingProcess.start', MagicMock(return_value=True)), \
  262. patch('salt.utils.process.SignalHandlingProcess.join', MagicMock(return_value=True)):
  263. mock_opts = self.get_config('minion', from_scratch=True)
  264. mock_opts['ping_interval'] = 10
  265. io_loop = salt.ext.tornado.ioloop.IOLoop()
  266. io_loop.make_current()
  267. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  268. try:
  269. try:
  270. minion.connected = MagicMock(side_effect=(False, True))
  271. minion._fire_master_minion_start = MagicMock()
  272. minion.tune_in(start=False)
  273. except RuntimeError:
  274. pass
  275. # Make sure the scheduler is initialized but the beacons are not
  276. self.assertTrue('ping' in minion.periodic_callbacks)
  277. finally:
  278. minion.destroy()
  279. def test_when_passed_start_event_grains(self):
  280. mock_opts = self.get_config('minion', from_scratch=True)
  281. mock_opts['start_event_grains'] = ["os"]
  282. io_loop = salt.ext.tornado.ioloop.IOLoop()
  283. io_loop.make_current()
  284. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  285. try:
  286. minion.tok = MagicMock()
  287. minion._send_req_sync = MagicMock()
  288. minion._fire_master('Minion has started', 'minion_start', include_startup_grains=True)
  289. load = minion._send_req_sync.call_args[0][0]
  290. self.assertTrue('grains' in load)
  291. self.assertTrue('os' in load['grains'])
  292. finally:
  293. minion.destroy()
  294. def test_when_not_passed_start_event_grains(self):
  295. mock_opts = self.get_config('minion', from_scratch=True)
  296. io_loop = salt.ext.tornado.ioloop.IOLoop()
  297. io_loop.make_current()
  298. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  299. try:
  300. minion.tok = MagicMock()
  301. minion._send_req_sync = MagicMock()
  302. minion._fire_master('Minion has started', 'minion_start')
  303. load = minion._send_req_sync.call_args[0][0]
  304. self.assertTrue('grains' not in load)
  305. finally:
  306. minion.destroy()
  307. def test_when_other_events_fired_and_start_event_grains_are_set(self):
  308. mock_opts = self.get_config('minion', from_scratch=True)
  309. mock_opts['start_event_grains'] = ["os"]
  310. io_loop = salt.ext.tornado.ioloop.IOLoop()
  311. io_loop.make_current()
  312. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  313. try:
  314. minion.tok = MagicMock()
  315. minion._send_req_sync = MagicMock()
  316. minion._fire_master('Custm_event_fired', 'custom_event')
  317. load = minion._send_req_sync.call_args[0][0]
  318. self.assertTrue('grains' not in load)
  319. finally:
  320. minion.destroy()
  321. def test_minion_retry_dns_count(self):
  322. '''
  323. Tests that the resolve_dns will retry dns look ups for a maximum of
  324. 3 times before raising a SaltMasterUnresolvableError exception.
  325. '''
  326. with patch.dict(self.opts, {'ipv6': False, 'master': 'dummy',
  327. 'master_port': '4555',
  328. 'retry_dns': 1, 'retry_dns_count': 3}):
  329. self.assertRaises(SaltMasterUnresolvableError,
  330. salt.minion.resolve_dns, self.opts)
  331. def test_gen_modules_executors(self):
  332. '''
  333. Ensure gen_modules is called with the correct arguments #54429
  334. '''
  335. mock_opts = self.get_config('minion', from_scratch=True)
  336. io_loop = salt.ext.tornado.ioloop.IOLoop()
  337. io_loop.make_current()
  338. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  339. class MockPillarCompiler(object):
  340. def compile_pillar(self):
  341. return {}
  342. try:
  343. with patch('salt.pillar.get_pillar', return_value=MockPillarCompiler()):
  344. with patch('salt.loader.executors') as execmock:
  345. minion.gen_modules()
  346. assert execmock.called_with(minion.opts, minion.functions)
  347. finally:
  348. minion.destroy()
  349. @patch('salt.utils.process.default_signals')
  350. def test_reinit_crypto_on_fork(self, def_mock):
  351. '''
  352. Ensure salt.utils.crypt.reinit_crypto() is executed when forking for new job
  353. '''
  354. mock_opts = self.get_config('minion', from_scratch=True)
  355. mock_opts["multiprocessing"] = True
  356. io_loop = salt.ext.tornado.ioloop.IOLoop()
  357. io_loop.make_current()
  358. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  359. job_data = {"jid": "test-jid", "fun": "test.ping"}
  360. def mock_start(self):
  361. assert len([x for x in self._after_fork_methods if x[0] == salt.utils.crypt.reinit_crypto]) == 1 # pylint: disable=comparison-with-callable
  362. with patch.object(salt.utils.process.SignalHandlingProcess, 'start', mock_start):
  363. io_loop.run_sync(lambda: minion._handle_decoded_payload(job_data))
  364. class MinionAsyncTestCase(TestCase, AdaptedConfigurationTestCaseMixin, salt.ext.tornado.testing.AsyncTestCase):
  365. def setUp(self):
  366. super(MinionAsyncTestCase, self).setUp()
  367. self.opts = {}
  368. self.addCleanup(delattr, self, 'opts')
  369. @skip_if_not_root
  370. def test_sock_path_len(self):
  371. '''
  372. This tests whether or not a larger hash causes the sock path to exceed
  373. the system's max sock path length. See the below link for more
  374. information.
  375. https://github.com/saltstack/salt/issues/12172#issuecomment-43903643
  376. '''
  377. opts = {
  378. 'id': 'salt-testing',
  379. 'hash_type': 'sha512',
  380. 'sock_dir': os.path.join(salt.syspaths.SOCK_DIR, 'minion'),
  381. 'extension_modules': ''
  382. }
  383. with patch.dict(self.opts, opts):
  384. try:
  385. event_publisher = event.AsyncEventPublisher(self.opts)
  386. result = True
  387. except ValueError:
  388. # There are rare cases where we operate a closed socket, especially in containers.
  389. # In this case, don't fail the test because we'll catch it down the road.
  390. result = True
  391. except SaltSystemExit:
  392. result = False
  393. self.assertTrue(result)