test_minion.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Mike Place <mp@saltstack.com>
  4. """
  5. from __future__ import absolute_import
  6. import copy
  7. import os
  8. import salt.ext.tornado
  9. import salt.ext.tornado.testing
  10. import salt.minion
  11. import salt.syspaths
  12. import salt.utils.crypt
  13. import salt.utils.event as event
  14. import salt.utils.process
  15. from salt.exceptions import SaltMasterUnresolvableError, SaltSystemExit
  16. from salt.ext.six.moves import range
  17. from tests.support.helpers import skip_if_not_root, slowTest
  18. from tests.support.mixins import AdaptedConfigurationTestCaseMixin
  19. from tests.support.mock import MagicMock, patch
  20. from tests.support.unit import TestCase
  21. class MinionTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
  22. def setUp(self):
  23. self.opts = {}
  24. self.addCleanup(delattr, self, "opts")
  25. def test_invalid_master_address(self):
  26. with patch.dict(
  27. self.opts,
  28. {
  29. "ipv6": False,
  30. "master": float("127.0"),
  31. "master_port": "4555",
  32. "retry_dns": False,
  33. },
  34. ):
  35. self.assertRaises(SaltSystemExit, salt.minion.resolve_dns, self.opts)
  36. def test_source_int_name_local(self):
  37. """
  38. test when file_client local and
  39. source_interface_name is set
  40. """
  41. interfaces = {
  42. "bond0.1234": {
  43. "hwaddr": "01:01:01:d0:d0:d0",
  44. "up": True,
  45. "inet": [
  46. {
  47. "broadcast": "111.1.111.255",
  48. "netmask": "111.1.0.0",
  49. "label": "bond0",
  50. "address": "111.1.0.1",
  51. }
  52. ],
  53. }
  54. }
  55. with patch.dict(
  56. self.opts,
  57. {
  58. "ipv6": False,
  59. "master": "127.0.0.1",
  60. "master_port": "4555",
  61. "file_client": "local",
  62. "source_interface_name": "bond0.1234",
  63. "source_ret_port": 49017,
  64. "source_publish_port": 49018,
  65. },
  66. ), patch("salt.utils.network.interfaces", MagicMock(return_value=interfaces)):
  67. assert salt.minion.resolve_dns(self.opts) == {
  68. "master_ip": "127.0.0.1",
  69. "source_ip": "111.1.0.1",
  70. "source_ret_port": 49017,
  71. "source_publish_port": 49018,
  72. "master_uri": "tcp://127.0.0.1:4555",
  73. }
  74. @slowTest
  75. def test_source_int_name_remote(self):
  76. """
  77. test when file_client remote and
  78. source_interface_name is set and
  79. interface is down
  80. """
  81. interfaces = {
  82. "bond0.1234": {
  83. "hwaddr": "01:01:01:d0:d0:d0",
  84. "up": False,
  85. "inet": [
  86. {
  87. "broadcast": "111.1.111.255",
  88. "netmask": "111.1.0.0",
  89. "label": "bond0",
  90. "address": "111.1.0.1",
  91. }
  92. ],
  93. }
  94. }
  95. with patch.dict(
  96. self.opts,
  97. {
  98. "ipv6": False,
  99. "master": "127.0.0.1",
  100. "master_port": "4555",
  101. "file_client": "remote",
  102. "source_interface_name": "bond0.1234",
  103. "source_ret_port": 49017,
  104. "source_publish_port": 49018,
  105. },
  106. ), patch("salt.utils.network.interfaces", MagicMock(return_value=interfaces)):
  107. assert salt.minion.resolve_dns(self.opts) == {
  108. "master_ip": "127.0.0.1",
  109. "source_ret_port": 49017,
  110. "source_publish_port": 49018,
  111. "master_uri": "tcp://127.0.0.1:4555",
  112. }
  113. @slowTest
  114. def test_source_address(self):
  115. """
  116. test when source_address is set
  117. """
  118. interfaces = {
  119. "bond0.1234": {
  120. "hwaddr": "01:01:01:d0:d0:d0",
  121. "up": False,
  122. "inet": [
  123. {
  124. "broadcast": "111.1.111.255",
  125. "netmask": "111.1.0.0",
  126. "label": "bond0",
  127. "address": "111.1.0.1",
  128. }
  129. ],
  130. }
  131. }
  132. with patch.dict(
  133. self.opts,
  134. {
  135. "ipv6": False,
  136. "master": "127.0.0.1",
  137. "master_port": "4555",
  138. "file_client": "local",
  139. "source_interface_name": "",
  140. "source_address": "111.1.0.1",
  141. "source_ret_port": 49017,
  142. "source_publish_port": 49018,
  143. },
  144. ), patch("salt.utils.network.interfaces", MagicMock(return_value=interfaces)):
  145. assert salt.minion.resolve_dns(self.opts) == {
  146. "source_publish_port": 49018,
  147. "source_ret_port": 49017,
  148. "master_uri": "tcp://127.0.0.1:4555",
  149. "source_ip": "111.1.0.1",
  150. "master_ip": "127.0.0.1",
  151. }
  152. # Tests for _handle_decoded_payload in the salt.minion.Minion() class: 3
  153. @slowTest
  154. def test_handle_decoded_payload_jid_match_in_jid_queue(self):
  155. """
  156. Tests that the _handle_decoded_payload function returns when a jid is given that is already present
  157. in the jid_queue.
  158. Note: This test doesn't contain all of the patch decorators above the function like the other tests
  159. for _handle_decoded_payload below. This is essential to this test as the call to the function must
  160. return None BEFORE any of the processes are spun up because we should be avoiding firing duplicate
  161. jobs.
  162. """
  163. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  164. mock_data = {"fun": "foo.bar", "jid": 123}
  165. mock_jid_queue = [123]
  166. minion = salt.minion.Minion(
  167. mock_opts,
  168. jid_queue=copy.copy(mock_jid_queue),
  169. io_loop=salt.ext.tornado.ioloop.IOLoop(),
  170. )
  171. try:
  172. ret = minion._handle_decoded_payload(mock_data).result()
  173. self.assertEqual(minion.jid_queue, mock_jid_queue)
  174. self.assertIsNone(ret)
  175. finally:
  176. minion.destroy()
  177. @slowTest
  178. def test_handle_decoded_payload_jid_queue_addition(self):
  179. """
  180. Tests that the _handle_decoded_payload function adds a jid to the minion's jid_queue when the new
  181. jid isn't already present in the jid_queue.
  182. """
  183. with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
  184. "salt.utils.process.SignalHandlingProcess.start",
  185. MagicMock(return_value=True),
  186. ), patch(
  187. "salt.utils.process.SignalHandlingProcess.join",
  188. MagicMock(return_value=True),
  189. ):
  190. mock_jid = 11111
  191. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  192. mock_data = {"fun": "foo.bar", "jid": mock_jid}
  193. mock_jid_queue = [123, 456]
  194. minion = salt.minion.Minion(
  195. mock_opts,
  196. jid_queue=copy.copy(mock_jid_queue),
  197. io_loop=salt.ext.tornado.ioloop.IOLoop(),
  198. )
  199. try:
  200. # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
  201. # This can help debug any test failures if the _handle_decoded_payload call fails.
  202. self.assertEqual(minion.jid_queue, mock_jid_queue)
  203. # Call the _handle_decoded_payload function and update the mock_jid_queue to include the new
  204. # mock_jid. The mock_jid should have been added to the jid_queue since the mock_jid wasn't
  205. # previously included. The minion's jid_queue attribute and the mock_jid_queue should be equal.
  206. minion._handle_decoded_payload(mock_data).result()
  207. mock_jid_queue.append(mock_jid)
  208. self.assertEqual(minion.jid_queue, mock_jid_queue)
  209. finally:
  210. minion.destroy()
  211. @slowTest
  212. def test_handle_decoded_payload_jid_queue_reduced_minion_jid_queue_hwm(self):
  213. """
  214. Tests that the _handle_decoded_payload function removes a jid from the minion's jid_queue when the
  215. minion's jid_queue high water mark (minion_jid_queue_hwm) is hit.
  216. """
  217. with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
  218. "salt.utils.process.SignalHandlingProcess.start",
  219. MagicMock(return_value=True),
  220. ), patch(
  221. "salt.utils.process.SignalHandlingProcess.join",
  222. MagicMock(return_value=True),
  223. ):
  224. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  225. mock_opts["minion_jid_queue_hwm"] = 2
  226. mock_data = {"fun": "foo.bar", "jid": 789}
  227. mock_jid_queue = [123, 456]
  228. minion = salt.minion.Minion(
  229. mock_opts,
  230. jid_queue=copy.copy(mock_jid_queue),
  231. io_loop=salt.ext.tornado.ioloop.IOLoop(),
  232. )
  233. try:
  234. # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
  235. # This can help debug any test failures if the _handle_decoded_payload call fails.
  236. self.assertEqual(minion.jid_queue, mock_jid_queue)
  237. # Call the _handle_decoded_payload function and check that the queue is smaller by one item
  238. # and contains the new jid
  239. minion._handle_decoded_payload(mock_data).result()
  240. self.assertEqual(len(minion.jid_queue), 2)
  241. self.assertEqual(minion.jid_queue, [456, 789])
  242. finally:
  243. minion.destroy()
  244. @slowTest
  245. def test_process_count_max(self):
  246. """
  247. Tests that the _handle_decoded_payload function does not spawn more than the configured amount of processes,
  248. as per process_count_max.
  249. """
  250. with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
  251. "salt.utils.process.SignalHandlingProcess.start",
  252. MagicMock(return_value=True),
  253. ), patch(
  254. "salt.utils.process.SignalHandlingProcess.join",
  255. MagicMock(return_value=True),
  256. ), patch(
  257. "salt.utils.minion.running", MagicMock(return_value=[])
  258. ), patch(
  259. "salt.ext.tornado.gen.sleep",
  260. MagicMock(return_value=salt.ext.tornado.concurrent.Future()),
  261. ):
  262. process_count_max = 10
  263. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  264. mock_opts["__role"] = "minion"
  265. mock_opts["minion_jid_queue_hwm"] = 100
  266. mock_opts["process_count_max"] = process_count_max
  267. io_loop = salt.ext.tornado.ioloop.IOLoop()
  268. minion = salt.minion.Minion(mock_opts, jid_queue=[], io_loop=io_loop)
  269. try:
  270. # mock gen.sleep to throw a special Exception when called, so that we detect it
  271. class SleepCalledException(Exception):
  272. """Thrown when sleep is called"""
  273. salt.ext.tornado.gen.sleep.return_value.set_exception(
  274. SleepCalledException()
  275. )
  276. # up until process_count_max: gen.sleep does not get called, processes are started normally
  277. for i in range(process_count_max):
  278. mock_data = {"fun": "foo.bar", "jid": i}
  279. io_loop.run_sync(
  280. lambda data=mock_data: minion._handle_decoded_payload(data)
  281. )
  282. self.assertEqual(
  283. salt.utils.process.SignalHandlingProcess.start.call_count, i + 1
  284. )
  285. self.assertEqual(len(minion.jid_queue), i + 1)
  286. salt.utils.minion.running.return_value += [i]
  287. # above process_count_max: gen.sleep does get called, JIDs are created but no new processes are started
  288. mock_data = {"fun": "foo.bar", "jid": process_count_max + 1}
  289. self.assertRaises(
  290. SleepCalledException,
  291. lambda: io_loop.run_sync(
  292. lambda: minion._handle_decoded_payload(mock_data)
  293. ),
  294. )
  295. self.assertEqual(
  296. salt.utils.process.SignalHandlingProcess.start.call_count,
  297. process_count_max,
  298. )
  299. self.assertEqual(len(minion.jid_queue), process_count_max + 1)
  300. finally:
  301. minion.destroy()
  302. @slowTest
  303. def test_beacons_before_connect(self):
  304. """
  305. Tests that the 'beacons_before_connect' option causes the beacons to be initialized before connect.
  306. """
  307. with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
  308. "salt.minion.Minion.sync_connect_master",
  309. MagicMock(side_effect=RuntimeError("stop execution")),
  310. ), patch(
  311. "salt.utils.process.SignalHandlingProcess.start",
  312. MagicMock(return_value=True),
  313. ), patch(
  314. "salt.utils.process.SignalHandlingProcess.join",
  315. MagicMock(return_value=True),
  316. ):
  317. mock_opts = self.get_config("minion", from_scratch=True)
  318. mock_opts["beacons_before_connect"] = True
  319. io_loop = salt.ext.tornado.ioloop.IOLoop()
  320. io_loop.make_current()
  321. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  322. try:
  323. try:
  324. minion.tune_in(start=True)
  325. except RuntimeError:
  326. pass
  327. # Make sure beacons are initialized but the sheduler is not
  328. self.assertTrue("beacons" in minion.periodic_callbacks)
  329. self.assertTrue("schedule" not in minion.periodic_callbacks)
  330. finally:
  331. minion.destroy()
  332. @slowTest
  333. def test_scheduler_before_connect(self):
  334. """
  335. Tests that the 'scheduler_before_connect' option causes the scheduler to be initialized before connect.
  336. """
  337. with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
  338. "salt.minion.Minion.sync_connect_master",
  339. MagicMock(side_effect=RuntimeError("stop execution")),
  340. ), patch(
  341. "salt.utils.process.SignalHandlingProcess.start",
  342. MagicMock(return_value=True),
  343. ), patch(
  344. "salt.utils.process.SignalHandlingProcess.join",
  345. MagicMock(return_value=True),
  346. ):
  347. mock_opts = self.get_config("minion", from_scratch=True)
  348. mock_opts["scheduler_before_connect"] = True
  349. io_loop = salt.ext.tornado.ioloop.IOLoop()
  350. io_loop.make_current()
  351. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  352. try:
  353. try:
  354. minion.tune_in(start=True)
  355. except RuntimeError:
  356. pass
  357. # Make sure the scheduler is initialized but the beacons are not
  358. self.assertTrue("schedule" in minion.periodic_callbacks)
  359. self.assertTrue("beacons" not in minion.periodic_callbacks)
  360. finally:
  361. minion.destroy()
  362. @slowTest
  363. def test_when_ping_interval_is_set_the_callback_should_be_added_to_periodic_callbacks(
  364. self,
  365. ):
  366. with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
  367. "salt.minion.Minion.sync_connect_master",
  368. MagicMock(side_effect=RuntimeError("stop execution")),
  369. ), patch(
  370. "salt.utils.process.SignalHandlingProcess.start",
  371. MagicMock(return_value=True),
  372. ), patch(
  373. "salt.utils.process.SignalHandlingProcess.join",
  374. MagicMock(return_value=True),
  375. ):
  376. mock_opts = self.get_config("minion", from_scratch=True)
  377. mock_opts["ping_interval"] = 10
  378. io_loop = salt.ext.tornado.ioloop.IOLoop()
  379. io_loop.make_current()
  380. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  381. try:
  382. try:
  383. minion.connected = MagicMock(side_effect=(False, True))
  384. minion._fire_master_minion_start = MagicMock()
  385. minion.tune_in(start=False)
  386. except RuntimeError:
  387. pass
  388. # Make sure the scheduler is initialized but the beacons are not
  389. self.assertTrue("ping" in minion.periodic_callbacks)
  390. finally:
  391. minion.destroy()
  392. @slowTest
  393. def test_when_passed_start_event_grains(self):
  394. mock_opts = self.get_config("minion", from_scratch=True)
  395. mock_opts["start_event_grains"] = ["os"]
  396. io_loop = salt.ext.tornado.ioloop.IOLoop()
  397. io_loop.make_current()
  398. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  399. try:
  400. minion.tok = MagicMock()
  401. minion._send_req_sync = MagicMock()
  402. minion._fire_master(
  403. "Minion has started", "minion_start", include_startup_grains=True
  404. )
  405. load = minion._send_req_sync.call_args[0][0]
  406. self.assertTrue("grains" in load)
  407. self.assertTrue("os" in load["grains"])
  408. finally:
  409. minion.destroy()
  410. @slowTest
  411. def test_when_not_passed_start_event_grains(self):
  412. mock_opts = self.get_config("minion", from_scratch=True)
  413. io_loop = salt.ext.tornado.ioloop.IOLoop()
  414. io_loop.make_current()
  415. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  416. try:
  417. minion.tok = MagicMock()
  418. minion._send_req_sync = MagicMock()
  419. minion._fire_master("Minion has started", "minion_start")
  420. load = minion._send_req_sync.call_args[0][0]
  421. self.assertTrue("grains" not in load)
  422. finally:
  423. minion.destroy()
  424. @slowTest
  425. def test_when_other_events_fired_and_start_event_grains_are_set(self):
  426. mock_opts = self.get_config("minion", from_scratch=True)
  427. mock_opts["start_event_grains"] = ["os"]
  428. io_loop = salt.ext.tornado.ioloop.IOLoop()
  429. io_loop.make_current()
  430. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  431. try:
  432. minion.tok = MagicMock()
  433. minion._send_req_sync = MagicMock()
  434. minion._fire_master("Custm_event_fired", "custom_event")
  435. load = minion._send_req_sync.call_args[0][0]
  436. self.assertTrue("grains" not in load)
  437. finally:
  438. minion.destroy()
  439. @slowTest
  440. def test_minion_retry_dns_count(self):
  441. """
  442. Tests that the resolve_dns will retry dns look ups for a maximum of
  443. 3 times before raising a SaltMasterUnresolvableError exception.
  444. """
  445. with patch.dict(
  446. self.opts,
  447. {
  448. "ipv6": False,
  449. "master": "dummy",
  450. "master_port": "4555",
  451. "retry_dns": 1,
  452. "retry_dns_count": 3,
  453. },
  454. ):
  455. self.assertRaises(
  456. SaltMasterUnresolvableError, salt.minion.resolve_dns, self.opts
  457. )
  458. @slowTest
  459. def test_gen_modules_executors(self):
  460. """
  461. Ensure gen_modules is called with the correct arguments #54429
  462. """
  463. mock_opts = self.get_config("minion", from_scratch=True)
  464. io_loop = salt.ext.tornado.ioloop.IOLoop()
  465. io_loop.make_current()
  466. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  467. class MockPillarCompiler(object):
  468. def compile_pillar(self):
  469. return {}
  470. try:
  471. with patch("salt.pillar.get_pillar", return_value=MockPillarCompiler()):
  472. with patch("salt.loader.executors") as execmock:
  473. minion.gen_modules()
  474. assert execmock.called_with(minion.opts, minion.functions)
  475. finally:
  476. minion.destroy()
  477. @patch("salt.utils.process.default_signals")
  478. @slowTest
  479. def test_reinit_crypto_on_fork(self, def_mock):
  480. """
  481. Ensure salt.utils.crypt.reinit_crypto() is executed when forking for new job
  482. """
  483. mock_opts = self.get_config("minion", from_scratch=True)
  484. mock_opts["multiprocessing"] = True
  485. io_loop = salt.ext.tornado.ioloop.IOLoop()
  486. io_loop.make_current()
  487. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  488. job_data = {"jid": "test-jid", "fun": "test.ping"}
  489. def mock_start(self):
  490. # pylint: disable=comparison-with-callable
  491. assert (
  492. len(
  493. [
  494. x
  495. for x in self._after_fork_methods
  496. if x[0] == salt.utils.crypt.reinit_crypto
  497. ]
  498. )
  499. == 1
  500. )
  501. # pylint: enable=comparison-with-callable
  502. with patch.object(
  503. salt.utils.process.SignalHandlingProcess, "start", mock_start
  504. ):
  505. io_loop.run_sync(lambda: minion._handle_decoded_payload(job_data))
  506. def test_minion_manage_schedule(self):
  507. """
  508. Tests that the manage_schedule will call the add function, adding
  509. schedule data into opts.
  510. """
  511. with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
  512. "salt.minion.Minion.sync_connect_master",
  513. MagicMock(side_effect=RuntimeError("stop execution")),
  514. ), patch(
  515. "salt.utils.process.SignalHandlingMultiprocessingProcess.start",
  516. MagicMock(return_value=True),
  517. ), patch(
  518. "salt.utils.process.SignalHandlingMultiprocessingProcess.join",
  519. MagicMock(return_value=True),
  520. ):
  521. mock_opts = self.get_config("minion", from_scratch=True)
  522. io_loop = salt.ext.tornado.ioloop.IOLoop()
  523. io_loop.make_current()
  524. with patch(
  525. "salt.utils.schedule.clean_proc_dir", MagicMock(return_value=None)
  526. ):
  527. mock_functions = {"test.ping": None}
  528. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  529. minion.schedule = salt.utils.schedule.Schedule(
  530. mock_opts, mock_functions, returners={}
  531. )
  532. schedule_data = {
  533. "test_job": {
  534. "function": "test.ping",
  535. "return_job": False,
  536. "jid_include": True,
  537. "maxrunning": 2,
  538. "seconds": 10,
  539. }
  540. }
  541. data = {"name": "test-item", "schedule": schedule_data, "func": "add"}
  542. tag = "manage_schedule"
  543. minion.manage_schedule(tag, data)
  544. self.assertIn("test_job", minion.opts["schedule"])
  545. def test_minion_manage_beacons(self):
  546. """
  547. Tests that the manage_beacons will call the add function, adding
  548. beacon data into opts.
  549. """
  550. with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
  551. "salt.minion.Minion.sync_connect_master",
  552. MagicMock(side_effect=RuntimeError("stop execution")),
  553. ), patch(
  554. "salt.utils.process.SignalHandlingMultiprocessingProcess.start",
  555. MagicMock(return_value=True),
  556. ), patch(
  557. "salt.utils.process.SignalHandlingMultiprocessingProcess.join",
  558. MagicMock(return_value=True),
  559. ):
  560. mock_opts = self.get_config("minion", from_scratch=True)
  561. io_loop = salt.ext.tornado.ioloop.IOLoop()
  562. io_loop.make_current()
  563. mock_functions = {"test.ping": None}
  564. minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
  565. minion.beacons = salt.beacons.Beacon(mock_opts, mock_functions)
  566. bdata = [{"salt-master": "stopped"}, {"apache2": "stopped"}]
  567. data = {"name": "ps", "beacon_data": bdata, "func": "add"}
  568. tag = "manage_beacons"
  569. minion.manage_beacons(tag, data)
  570. self.assertIn("ps", minion.opts["beacons"])
  571. self.assertEqual(minion.opts["beacons"]["ps"], bdata)
  572. class MinionAsyncTestCase(
  573. TestCase, AdaptedConfigurationTestCaseMixin, salt.ext.tornado.testing.AsyncTestCase
  574. ):
  575. def setUp(self):
  576. super(MinionAsyncTestCase, self).setUp()
  577. self.opts = {}
  578. self.addCleanup(delattr, self, "opts")
  579. @skip_if_not_root
  580. def test_sock_path_len(self):
  581. """
  582. This tests whether or not a larger hash causes the sock path to exceed
  583. the system's max sock path length. See the below link for more
  584. information.
  585. https://github.com/saltstack/salt/issues/12172#issuecomment-43903643
  586. """
  587. opts = {
  588. "id": "salt-testing",
  589. "hash_type": "sha512",
  590. "sock_dir": os.path.join(salt.syspaths.SOCK_DIR, "minion"),
  591. "extension_modules": "",
  592. }
  593. with patch.dict(self.opts, opts):
  594. try:
  595. event_publisher = event.AsyncEventPublisher(self.opts)
  596. result = True
  597. except ValueError:
  598. # There are rare cases where we operate a closed socket, especially in containers.
  599. # In this case, don't fail the test because we'll catch it down the road.
  600. result = True
  601. except SaltSystemExit:
  602. result = False
  603. self.assertTrue(result)