1
0

test_restartcheck.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import io
  2. import pytest
  3. import salt.modules.cmdmod as cmdmod
  4. import salt.modules.restartcheck as restartcheck
  5. import salt.modules.system as system
  6. import salt.modules.systemd_service as service
  7. from tests.support.mock import create_autospec, patch
  8. @pytest.fixture(autouse=True)
  9. def setup_loader():
  10. setup_loader_modules = {restartcheck: {}}
  11. with pytest.helpers.loader_mock(setup_loader_modules) as loader_mock:
  12. yield loader_mock
  13. def test_when_timestamp_file_does_not_exist_then_file_changed_nilrt_should_be_True():
  14. expected_changed = True
  15. def timestamp_not_exists(filename):
  16. if filename.endswith(".timestamp"):
  17. return False
  18. return True
  19. with patch("os.path.exists", side_effect=timestamp_not_exists, autospec=True):
  20. actual_changed = restartcheck._file_changed_nilrt(full_filepath="fnord")
  21. assert actual_changed == expected_changed
  22. def test_when_timestamp_file_exists_but_not_md5sum_file_then_file_changed_nilrt_should_be_True():
  23. expected_changed = True
  24. def timestamp_not_exists(filename):
  25. if filename.endswith(".md5sum"):
  26. return False
  27. return True
  28. with patch("os.path.exists", side_effect=timestamp_not_exists, autospec=True):
  29. actual_changed = restartcheck._file_changed_nilrt(full_filepath="fnord")
  30. assert actual_changed == expected_changed
  31. def test_when_nisysapi_path_exists_and_nilrt_files_changed_then_sysapi_changed_nilrt_should_be_True():
  32. expected_change = True
  33. patch_os_path = patch("os.path.exists", return_value=True, autospec=True)
  34. patch_file_changed = patch(
  35. "salt.modules.restartcheck._file_changed_nilrt",
  36. autospec=True,
  37. return_value=True,
  38. )
  39. with patch_os_path, patch_file_changed:
  40. actual_change = restartcheck._sysapi_changed_nilrt()
  41. assert actual_change == expected_change
  42. # TODO: can I parametrize a fixture? And use mocked_grain or something? -W. Werner, 2020-08-18
  43. @pytest.mark.parametrize("cpuarch_grain", ["arm", "x86_64"])
  44. def test_when_nisysapi_conf_d_path_does_not_exist_then_sysapi_changed_should_be_False(
  45. cpuarch_grain,
  46. ):
  47. expected_change = False
  48. def conf_d_not_exists(filename):
  49. return "nisysapi/conf.d" not in filename
  50. patch_os_path = patch(
  51. "os.path.exists", side_effect=conf_d_not_exists, autospec=True
  52. )
  53. patch_file_changed = patch(
  54. "salt.modules.restartcheck._file_changed_nilrt",
  55. autospec=True,
  56. return_value=False,
  57. )
  58. patch_grain = patch.dict(restartcheck.__grains__, {"cpuarch": cpuarch_grain})
  59. with patch_os_path, patch_file_changed, patch_grain:
  60. actual_change = restartcheck._sysapi_changed_nilrt()
  61. assert actual_change == expected_change
  62. @pytest.mark.parametrize("cpuarch_grain", ["arm", "x86_64"])
  63. def test_when_nisysapi_conf_d_path_does_exist_and_no_restart_check_file_exists_then_sysapi_changed_should_be_True(
  64. cpuarch_grain,
  65. ):
  66. expected_change = True
  67. def conf_d_not_exists(filename):
  68. return not filename.endswith("/sysapi.conf.d.count")
  69. patch_os_path = patch(
  70. "os.path.exists", side_effect=conf_d_not_exists, autospec=True
  71. )
  72. patch_file_changed = patch(
  73. "salt.modules.restartcheck._file_changed_nilrt",
  74. autospec=True,
  75. return_value=False,
  76. )
  77. patch_grain = patch.dict(restartcheck.__grains__, {"cpuarch": cpuarch_grain})
  78. with patch_os_path, patch_file_changed, patch_grain:
  79. actual_change = restartcheck._sysapi_changed_nilrt()
  80. assert actual_change == expected_change
  81. @pytest.mark.parametrize("cpuarch_grain", ["arm", "x86_64"])
  82. def test_when_nisysapi_conf_d_path_does_exist_and_count_file_exists_and_count_is_different_than_files_in_conf_d_path_then_sysapi_changed_should_be_True(
  83. cpuarch_grain,
  84. ):
  85. expected_change = True
  86. # Fake count and listdir should be different values
  87. fake_count = io.StringIO("42")
  88. patch_listdir = patch("os.listdir", autospec=True, return_value=["boop"])
  89. patch_os_path = patch("os.path.exists", return_value=True, autospec=True)
  90. patch_file_changed = patch(
  91. "salt.modules.restartcheck._file_changed_nilrt",
  92. autospec=True,
  93. return_value=False,
  94. )
  95. patch_grain = patch.dict(restartcheck.__grains__, {"cpuarch": cpuarch_grain})
  96. patch_fopen = patch(
  97. "salt.utils.files.fopen", autospec=True, return_value=fake_count
  98. )
  99. with patch_os_path, patch_file_changed, patch_grain, patch_listdir, patch_fopen:
  100. actual_change = restartcheck._sysapi_changed_nilrt()
  101. assert actual_change == expected_change
  102. @pytest.mark.parametrize("cpuarch_grain", ["arm", "x86_64"])
  103. def test_when_nisysapi_conf_d_path_does_exist_and_count_file_exists_and_count_is_same_as_files_in_conf_d_path_but_no_nilrt_files_changed_then_sysapi_changed_should_be_False(
  104. cpuarch_grain,
  105. ):
  106. expected_change = False
  107. # listdir should return the same number of values as count
  108. fake_count = io.StringIO("42")
  109. patch_listdir = patch("os.listdir", autospec=True, return_value=["boop"] * 42)
  110. patch_os_path = patch("os.path.exists", return_value=True, autospec=True)
  111. patch_file_changed = patch(
  112. "salt.modules.restartcheck._file_changed_nilrt",
  113. autospec=True,
  114. return_value=False,
  115. )
  116. patch_grain = patch.dict(restartcheck.__grains__, {"cpuarch": cpuarch_grain})
  117. patch_fopen = patch(
  118. "salt.utils.files.fopen", autospec=True, return_value=fake_count
  119. )
  120. with patch_os_path, patch_file_changed, patch_grain, patch_listdir, patch_fopen:
  121. actual_change = restartcheck._sysapi_changed_nilrt()
  122. assert actual_change == expected_change
  123. @pytest.mark.parametrize("cpuarch_grain", ["arm", "x86_64"])
  124. def test_when_nisysapi_conf_d_path_does_exist_and_count_file_exists_and_count_is_same_as_files_in_conf_d_path_and_file_changed_nilrt_then_sysapi_changed_should_be_True(
  125. cpuarch_grain,
  126. ):
  127. expected_change = True
  128. def fake_file_changed(filename):
  129. return filename != "/usr/local/natinst/share/nisysapi.ini"
  130. # listdir should return the same number of values as count
  131. fake_count = io.StringIO("42")
  132. patch_listdir = patch("os.listdir", autospec=True, return_value=["boop"] * 42)
  133. patch_os_path = patch("os.path.exists", return_value=True, autospec=True)
  134. patch_file_changed = patch(
  135. "salt.modules.restartcheck._file_changed_nilrt",
  136. autospec=True,
  137. side_effect=fake_file_changed,
  138. )
  139. patch_grain = patch.dict(restartcheck.__grains__, {"cpuarch": cpuarch_grain})
  140. patch_fopen = patch(
  141. "salt.utils.files.fopen", autospec=True, return_value=fake_count
  142. )
  143. with patch_os_path, patch_file_changed, patch_grain, patch_listdir, patch_fopen:
  144. actual_change = restartcheck._sysapi_changed_nilrt()
  145. assert actual_change == expected_change
  146. def test_when_nilinuxrt_and_not_kernel_modules_changed_or_sysapi_files_changed_and_not_reboot_required_witnessed_then_no_reboot_should_be_required():
  147. expected_result = "No packages seem to need to be restarted."
  148. restart_required = False
  149. current_kernel = "fnord"
  150. patch_grains = patch.dict(restartcheck.__grains__, {"os_family": "NILinuxRT"})
  151. patch_kernel_versions = patch(
  152. "salt.modules.restartcheck._kernel_versions_nilrt",
  153. autospec=True,
  154. return_value=[current_kernel],
  155. )
  156. patch_salt = patch.dict(
  157. restartcheck.__salt__,
  158. {
  159. "cmd.run": create_autospec(cmdmod.run, return_value=current_kernel),
  160. "system.get_reboot_required_witnessed": create_autospec(
  161. system.get_reboot_required_witnessed, return_value=restart_required,
  162. ),
  163. "service.get_running": create_autospec(
  164. service.get_running, return_value=[]
  165. ),
  166. },
  167. )
  168. patch_kernel_mod_changed = patch(
  169. "salt.modules.restartcheck._kernel_modules_changed_nilrt",
  170. autospec=True,
  171. return_value=False,
  172. )
  173. patch_sysapi_changed = patch(
  174. "salt.modules.restartcheck._sysapi_changed_nilrt",
  175. autospec=True,
  176. return_value=False,
  177. )
  178. patch_del_files = patch(
  179. "salt.modules.restartcheck._deleted_files", autospec=True, return_value=[],
  180. )
  181. with patch_grains, patch_kernel_versions, patch_salt, patch_sysapi_changed, patch_kernel_mod_changed, patch_del_files:
  182. actual_result = restartcheck.restartcheck()
  183. assert actual_result == expected_result
  184. def test_when_nilinuxrt_and_not_kernel_modules_changed_or_sysapi_files_changed_and_reboot_required_witnessed_then_reboot_should_be_required():
  185. expected_result = "System restart required.\n\n"
  186. restart_required = True
  187. current_kernel = "fnord"
  188. patch_grains = patch.dict(restartcheck.__grains__, {"os_family": "NILinuxRT"})
  189. patch_kernel_versions = patch(
  190. "salt.modules.restartcheck._kernel_versions_nilrt",
  191. autospec=True,
  192. return_value=[current_kernel],
  193. )
  194. patch_salt = patch.dict(
  195. restartcheck.__salt__,
  196. {
  197. "cmd.run": create_autospec(cmdmod.run, return_value=current_kernel),
  198. "system.get_reboot_required_witnessed": create_autospec(
  199. system.get_reboot_required_witnessed, return_value=restart_required,
  200. ),
  201. "service.get_running": create_autospec(
  202. service.get_running, return_value=[]
  203. ),
  204. },
  205. )
  206. patch_kernel_mod_changed = patch(
  207. "salt.modules.restartcheck._kernel_modules_changed_nilrt",
  208. autospec=True,
  209. return_value=False,
  210. )
  211. patch_sysapi_changed = patch(
  212. "salt.modules.restartcheck._sysapi_changed_nilrt",
  213. autospec=True,
  214. return_value=False,
  215. )
  216. patch_del_files = patch(
  217. "salt.modules.restartcheck._deleted_files", autospec=True, return_value=[],
  218. )
  219. with patch_grains, patch_kernel_versions, patch_salt, patch_sysapi_changed, patch_kernel_mod_changed, patch_del_files:
  220. actual_result = restartcheck.restartcheck()
  221. assert actual_result == expected_result