1
0

test_minion.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Import python libs
  2. import logging
  3. # Import Salt Libs
  4. import salt.utils.minion
  5. from tests.support.mock import MagicMock, mock_open, patch
  6. # Import Salt Testing Libs
  7. from tests.support.unit import TestCase
  8. log = logging.getLogger(__name__)
  9. class FakeThreadingClass:
  10. name = "thread-name"
  11. class MinionUtilTestCase(TestCase):
  12. """
  13. TestCase for salt.utils.minion
  14. """
  15. def test__read_proc_file_multiprocessing_false(self):
  16. """
  17. test get_minion_pillar when
  18. target exists
  19. """
  20. opts = {"multiprocessing": False}
  21. proc_data = {
  22. "tgt_type": "glob",
  23. "jid": "20200310230030623022",
  24. "tgt": "minion",
  25. "pid": 12345,
  26. "ret": "",
  27. "user": "root",
  28. "arg": [10],
  29. "fun": "test.sleep",
  30. }
  31. fake_thread = FakeThreadingClass()
  32. fake_thread.name = "20200310230030623022-Job-20200310230030623022"
  33. with patch("os.getpid", MagicMock(return_value=12345)):
  34. with patch("salt.utils.files.fopen", mock_open(read_data=b"msgpack")):
  35. with patch(
  36. "salt.payload.Serial.loads", MagicMock(return_value=proc_data)
  37. ):
  38. with patch(
  39. "salt.utils.process.os_is_running", MagicMock(return_value=True)
  40. ):
  41. with patch(
  42. "threading.enumerate", MagicMock(return_value=[fake_thread])
  43. ):
  44. with patch(
  45. "salt.utils.minion._check_cmdline",
  46. MagicMock(return_value=True),
  47. ):
  48. data = salt.utils.minion._read_proc_file(
  49. "/var/cache/salt/minion/proc/20200310230030623022",
  50. opts,
  51. )
  52. self.assertEqual(data, proc_data)
  53. opts = {"multiprocessing": False}
  54. proc_data = {
  55. "tgt_type": "glob",
  56. "jid": "20200310230030623022",
  57. "tgt": "minion",
  58. "pid": 12345,
  59. "ret": "",
  60. "user": "root",
  61. "arg": [10],
  62. "fun": "test.sleep",
  63. }
  64. fake_thread = FakeThreadingClass()
  65. fake_thread.name = "20200310230030623022"
  66. with patch("os.getpid", MagicMock(return_value=12345)):
  67. with patch("salt.utils.files.fopen", mock_open(read_data=b"msgpack")):
  68. with patch(
  69. "salt.payload.Serial.loads", MagicMock(return_value=proc_data)
  70. ):
  71. with patch(
  72. "salt.utils.process.os_is_running", MagicMock(return_value=True)
  73. ):
  74. with patch(
  75. "threading.enumerate", MagicMock(return_value=[fake_thread])
  76. ):
  77. with patch(
  78. "salt.utils.minion._check_cmdline",
  79. MagicMock(return_value=True),
  80. ):
  81. data = salt.utils.minion._read_proc_file(
  82. "/var/cache/salt/minion/proc/20200310230030623022",
  83. opts,
  84. )
  85. self.assertEqual(data, proc_data)
  86. opts = {"multiprocessing": False}
  87. proc_data = {
  88. "tgt_type": "glob",
  89. "jid": "20200310230030623022",
  90. "tgt": "minion",
  91. "pid": 12345,
  92. "ret": "",
  93. "user": "root",
  94. "arg": [10],
  95. "fun": "test.sleep",
  96. }
  97. fake_thread = FakeThreadingClass()
  98. fake_thread.name = "20200310230030623022"
  99. with patch("os.getpid", MagicMock(return_value=12345)):
  100. with patch("salt.utils.files.fopen", mock_open(read_data=b"msgpack")):
  101. with patch(
  102. "salt.payload.Serial.loads", MagicMock(return_value=proc_data)
  103. ):
  104. with patch(
  105. "salt.utils.process.os_is_running", MagicMock(return_value=True)
  106. ):
  107. with patch(
  108. "threading.enumerate", MagicMock(return_value=[fake_thread])
  109. ):
  110. with patch(
  111. "salt.utils.minion._check_cmdline",
  112. MagicMock(return_value=False),
  113. ):
  114. with patch("os.remove", MagicMock(return_value=True)):
  115. data = salt.utils.minion._read_proc_file(
  116. "/var/cache/salt/minion/proc/20200310230030623022",
  117. opts,
  118. )
  119. self.assertEqual(data, None)
  120. def test__read_proc_file_multiprocessing_true(self):
  121. """
  122. test get_minion_pillar when
  123. target exists
  124. """
  125. opts = {"multiprocessing": True}
  126. proc_data = {
  127. "tgt_type": "glob",
  128. "jid": "20200310230030623022",
  129. "tgt": "minion",
  130. "pid": 12345,
  131. "ret": "",
  132. "user": "root",
  133. "arg": [10],
  134. "fun": "test.sleep",
  135. }
  136. with patch("os.getpid", MagicMock(return_value=12345)):
  137. with patch("salt.utils.files.fopen", mock_open(read_data=b"msgpack")):
  138. with patch(
  139. "salt.payload.Serial.loads", MagicMock(return_value=proc_data)
  140. ):
  141. with patch(
  142. "salt.utils.process.os_is_running", MagicMock(return_value=True)
  143. ):
  144. with patch(
  145. "salt.utils.minion._check_cmdline",
  146. MagicMock(return_value=True),
  147. ):
  148. data = salt.utils.minion._read_proc_file(
  149. "/var/cache/salt/minion/proc/20200310230030623022", opts
  150. )
  151. self.assertEqual(data, None)