test_win_system.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. # Import Python Libs
  2. import os
  3. # Import Salt Libs
  4. import salt.utils.platform
  5. # Import Salt Testing Libs
  6. from tests.support.mock import patch
  7. from tests.support.unit import TestCase, skipIf
  8. try:
  9. import salt.utils.win_system as win_system
  10. except Exception as exc: # pylint: disable=broad-except
  11. win_system = exc
  12. class WinSystemImportTestCase(TestCase):
  13. """
  14. Simply importing should not raise an error, especially on Linux
  15. """
  16. def test_import(self):
  17. if isinstance(win_system, Exception):
  18. raise Exception(
  19. "Importing win_system caused traceback: {}".format(win_system)
  20. )
  21. @skipIf(not salt.utils.platform.is_windows(), "Only test on Windows systems")
  22. class WinSystemTestCase(TestCase):
  23. """
  24. Test cases for salt.utils.win_system
  25. """
  26. def test_get_computer_name(self):
  27. """
  28. Should return the computer name
  29. """
  30. with patch("win32api.GetComputerNameEx", return_value="FAKENAME"):
  31. self.assertEqual(win_system.get_computer_name(), "FAKENAME")
  32. def test_get_computer_name_fail(self):
  33. """
  34. If it fails, it returns False
  35. """
  36. with patch("win32api.GetComputerNameEx", return_value=None):
  37. self.assertFalse(win_system.get_computer_name())
  38. def test_get_pending_computer_name(self):
  39. """
  40. Will return the pending computer name if one is pending
  41. """
  42. expected = "PendingName"
  43. patch_value = {"vdata": expected}
  44. with patch("salt.utils.win_reg.read_value", return_value=patch_value):
  45. result = win_system.get_pending_computer_name()
  46. self.assertEqual(expected, result)
  47. def test_get_pending_computer_name_none(self):
  48. """
  49. Will return the None if the pending computer is the current name
  50. """
  51. patch_value = {"vdata": os.environ.get("COMPUTERNAME")}
  52. with patch("salt.utils.win_reg.read_value", return_value=patch_value):
  53. self.assertIsNone(win_system.get_pending_computer_name())
  54. def test_get_pending_computer_name_false(self):
  55. """
  56. Will return False if there is no pending computer name
  57. """
  58. with patch("salt.utils.win_reg.read_value", return_value=False):
  59. self.assertIsNone(win_system.get_pending_computer_name())
  60. def test_get_pending_component_servicing(self):
  61. """
  62. If none of the keys exist, should return False
  63. """
  64. with patch("salt.utils.win_reg.key_exists", return_value=False):
  65. self.assertFalse(win_system.get_pending_component_servicing())
  66. def test_get_pending_component_servicing_true_1(self):
  67. """
  68. If the RebootPending key exists, should return True
  69. """
  70. with patch("salt.utils.win_reg.key_exists", side_effect=[True]):
  71. self.assertTrue(win_system.get_pending_component_servicing())
  72. def test_get_pending_component_servicing_true_2(self):
  73. """
  74. If the RebootInProgress key exists, should return True
  75. """
  76. with patch("salt.utils.win_reg.key_exists", side_effect=[False, True]):
  77. self.assertTrue(win_system.get_pending_component_servicing())
  78. def test_get_pending_component_servicing_true_3(self):
  79. """
  80. If the PackagesPending key exists, should return True
  81. """
  82. with patch("salt.utils.win_reg.key_exists", side_effect=[False, False, True]):
  83. self.assertTrue(win_system.get_pending_component_servicing())
  84. def test_get_pending_domain_join(self):
  85. """
  86. If none of the keys exist, should return False
  87. """
  88. with patch("salt.utils.win_reg.key_exists", return_value=False):
  89. self.assertFalse(win_system.get_pending_domain_join())
  90. def test_get_pending_domain_join_true_1(self):
  91. """
  92. If the AvoidSpnSet key exists, should return True
  93. """
  94. with patch("salt.utils.win_reg.key_exists", side_effect=[True]):
  95. self.assertTrue(win_system.get_pending_domain_join())
  96. def test_get_pending_domain_join_true_2(self):
  97. """
  98. If the JoinDomain key exists, should return True
  99. """
  100. with patch("salt.utils.win_reg.key_exists", side_effect=[False, True]):
  101. self.assertTrue(win_system.get_pending_domain_join())
  102. def test_get_pending_file_rename_false_1(self):
  103. """
  104. If none of the value names exist, should return False
  105. """
  106. patched_return = {"success": False}
  107. with patch("salt.utils.win_reg.read_value", return_value=patched_return):
  108. self.assertFalse(win_system.get_pending_file_rename())
  109. def test_get_pending_file_rename_false_2(self):
  110. """
  111. If one of the value names exists but is not set, should return False
  112. """
  113. patched_return = {"success": True, "vdata": "(value not set)"}
  114. with patch("salt.utils.win_reg.read_value", return_value=patched_return):
  115. self.assertFalse(win_system.get_pending_file_rename())
  116. def test_get_pending_file_rename_true_1(self):
  117. """
  118. If one of the value names exists and is set, should return True
  119. """
  120. patched_return = {"success": True, "vdata": "some value"}
  121. with patch("salt.utils.win_reg.read_value", return_value=patched_return):
  122. self.assertTrue(win_system.get_pending_file_rename())
  123. def test_get_pending_servermanager_false_1(self):
  124. """
  125. If the CurrentRebootAttempts value name does not exist, should return
  126. False
  127. """
  128. patched_return = {"success": False}
  129. with patch("salt.utils.win_reg.read_value", return_value=patched_return):
  130. self.assertFalse(win_system.get_pending_servermanager())
  131. def test_get_pending_servermanager_false_2(self):
  132. """
  133. If the CurrentRebootAttempts value name exists but is not an integer,
  134. should return False
  135. """
  136. patched_return = {"success": True, "vdata": "(value not set)"}
  137. with patch("salt.utils.win_reg.read_value", return_value=patched_return):
  138. self.assertFalse(win_system.get_pending_file_rename())
  139. def test_get_pending_servermanager_true(self):
  140. """
  141. If the CurrentRebootAttempts value name exists and is an integer,
  142. should return True
  143. """
  144. patched_return = {"success": True, "vdata": 1}
  145. with patch("salt.utils.win_reg.read_value", return_value=patched_return):
  146. self.assertTrue(win_system.get_pending_file_rename())
  147. def test_get_pending_dvd_reboot(self):
  148. """
  149. If the DVDRebootSignal value name does not exist, should return False
  150. """
  151. with patch("salt.utils.win_reg.value_exists", return_value=False):
  152. self.assertFalse(win_system.get_pending_dvd_reboot())
  153. def test_get_pending_dvd_reboot_true(self):
  154. """
  155. If the DVDRebootSignal value name exists, should return True
  156. """
  157. with patch("salt.utils.win_reg.value_exists", return_value=True):
  158. self.assertTrue(win_system.get_pending_dvd_reboot())
  159. def test_get_pending_update(self):
  160. """
  161. If none of the keys exist and there are not subkeys, should return False
  162. """
  163. with patch("salt.utils.win_reg.key_exists", return_value=False), patch(
  164. "salt.utils.win_reg.list_keys", return_value=[]
  165. ):
  166. self.assertFalse(win_system.get_pending_update())
  167. def test_get_pending_update_true_1(self):
  168. """
  169. If the RebootRequired key exists, should return True
  170. """
  171. with patch("salt.utils.win_reg.key_exists", side_effect=[True]):
  172. self.assertTrue(win_system.get_pending_update())
  173. def test_get_pending_update_true_2(self):
  174. """
  175. If the PostRebootReporting key exists, should return True
  176. """
  177. with patch("salt.utils.win_reg.key_exists", side_effect=[False, True]):
  178. self.assertTrue(win_system.get_pending_update())
  179. def test_get_reboot_required_witnessed_false_1(self):
  180. """
  181. The ``Reboot Required`` value name does not exist, should return False
  182. """
  183. patched_data = {"vdata": None}
  184. with patch("salt.utils.win_reg.read_value", return_value=patched_data):
  185. self.assertFalse(win_system.get_reboot_required_witnessed())
  186. def test_get_reboot_required_witnessed_false_2(self):
  187. """
  188. The ``Reboot required`` value name is set to 0, should return False
  189. """
  190. patched_data = {"vdata": 0}
  191. with patch("salt.utils.win_reg.read_value", return_value=patched_data):
  192. self.assertFalse(win_system.get_reboot_required_witnessed())
  193. def test_get_reboot_required_witnessed_true(self):
  194. """
  195. The ``Reboot required`` value name is set to 1, should return True
  196. """
  197. patched_data = {"vdata": 1}
  198. with patch("salt.utils.win_reg.read_value", return_value=patched_data):
  199. self.assertTrue(win_system.get_reboot_required_witnessed())
  200. def test_set_reboot_required_witnessed(self):
  201. """
  202. The call to ``set_value`` should return True and should be called with
  203. the specified parameters
  204. """
  205. with patch("salt.utils.win_reg.set_value", return_value=True) as sv:
  206. self.assertTrue(win_system.set_reboot_required_witnessed())
  207. sv.assert_called_once_with(
  208. hive="HKLM",
  209. key=win_system.MINION_VOLATILE_KEY,
  210. volatile=True,
  211. vname=win_system.REBOOT_REQUIRED_NAME,
  212. vdata=1,
  213. vtype="REG_DWORD",
  214. )
  215. def test_get_pending_update_exe_volatile_false_1(self):
  216. """
  217. If UpdateExeVolatile value name is 0, should return False
  218. """
  219. patched_data = {"success": True, "vdata": 0}
  220. with patch("salt.utils.win_reg.read_value", return_value=patched_data):
  221. self.assertFalse(win_system.get_pending_update_exe_volatile())
  222. def test_get_pending_update_exe_volatile_false_2(self):
  223. """
  224. If UpdateExeVolatile value name is not present, should return False
  225. """
  226. patched_data = {"success": False}
  227. with patch("salt.utils.win_reg.read_value", return_value=patched_data):
  228. self.assertFalse(win_system.get_pending_update_exe_volatile())
  229. def test_get_pending_update_exe_volatile_true_1(self):
  230. """
  231. If UpdateExeVolatile value name is not 0, should return True
  232. """
  233. patched_data = {"success": True, "vdata": 1}
  234. with patch("salt.utils.win_reg.read_value", return_value=patched_data):
  235. self.assertTrue(win_system.get_pending_update_exe_volatile())
  236. def test_get_pending_reboot(self):
  237. """
  238. If all functions return Falsy data, should return False
  239. """
  240. with patch(
  241. "salt.utils.win_system.get_pending_update", return_value=False
  242. ), patch("salt.utils.win_update.needs_reboot", return_value=False), patch(
  243. "salt.utils.win_system.get_pending_update_exe_volatile", return_value=False
  244. ), patch(
  245. "salt.utils.win_system.get_pending_file_rename", return_value=False
  246. ), patch(
  247. "salt.utils.win_system.get_pending_servermanager", return_value=False
  248. ), patch(
  249. "salt.utils.win_system.get_pending_component_servicing", return_value=False
  250. ), patch(
  251. "salt.utils.win_system.get_pending_dvd_reboot", return_value=False
  252. ), patch(
  253. "salt.utils.win_system.get_reboot_required_witnessed", return_value=False
  254. ), patch(
  255. "salt.utils.win_system.get_pending_computer_name", return_value=None
  256. ), patch(
  257. "salt.utils.win_system.get_pending_domain_join", return_value=False
  258. ):
  259. self.assertFalse(win_system.get_pending_reboot())
  260. def test_get_pending_reboot_true_1(self):
  261. """
  262. If any boolean returning functions return True, should return True
  263. """
  264. with patch(
  265. "salt.utils.win_system.get_pending_update", return_value=False
  266. ), patch("salt.utils.win_update.needs_reboot", return_value=False), patch(
  267. "salt.utils.win_system.get_pending_update_exe_volatile", return_value=False
  268. ), patch(
  269. "salt.utils.win_system.get_pending_file_rename", return_value=False
  270. ), patch(
  271. "salt.utils.win_system.get_pending_servermanager", return_value=False
  272. ), patch(
  273. "salt.utils.win_system.get_pending_component_servicing", return_value=False
  274. ), patch(
  275. "salt.utils.win_system.get_pending_dvd_reboot", return_value=False
  276. ), patch(
  277. "salt.utils.win_system.get_reboot_required_witnessed", return_value=False
  278. ), patch(
  279. "salt.utils.win_system.get_pending_computer_name", return_value=None
  280. ), patch(
  281. "salt.utils.win_system.get_pending_domain_join", return_value=True
  282. ):
  283. self.assertTrue(win_system.get_pending_reboot())
  284. def test_get_pending_reboot_true_2(self):
  285. """
  286. If a computer name is returned, should return True
  287. """
  288. with patch(
  289. "salt.utils.win_system.get_pending_update", return_value=False
  290. ), patch("salt.utils.win_update.needs_reboot", return_value=False), patch(
  291. "salt.utils.win_system.get_pending_update_exe_volatile", return_value=False
  292. ), patch(
  293. "salt.utils.win_system.get_pending_file_rename", return_value=False
  294. ), patch(
  295. "salt.utils.win_system.get_pending_servermanager", return_value=False
  296. ), patch(
  297. "salt.utils.win_system.get_pending_component_servicing", return_value=False
  298. ), patch(
  299. "salt.utils.win_system.get_pending_dvd_reboot", return_value=False
  300. ), patch(
  301. "salt.utils.win_system.get_reboot_required_witnessed", return_value=False
  302. ), patch(
  303. "salt.utils.win_system.get_pending_computer_name",
  304. return_value="pending name",
  305. ):
  306. self.assertTrue(win_system.get_pending_reboot())
  307. def test_get_pending_reboot_details(self):
  308. """
  309. All items False should return a dictionary with all items False
  310. """
  311. with patch(
  312. "salt.utils.win_system.get_pending_update", return_value=False
  313. ), patch("salt.utils.win_update.needs_reboot", return_value=False), patch(
  314. "salt.utils.win_system.get_pending_update_exe_volatile", return_value=False
  315. ), patch(
  316. "salt.utils.win_system.get_pending_file_rename", return_value=False
  317. ), patch(
  318. "salt.utils.win_system.get_pending_servermanager", return_value=False
  319. ), patch(
  320. "salt.utils.win_system.get_pending_component_servicing", return_value=False
  321. ), patch(
  322. "salt.utils.win_system.get_pending_dvd_reboot", return_value=False
  323. ), patch(
  324. "salt.utils.win_system.get_reboot_required_witnessed", return_value=False
  325. ), patch(
  326. "salt.utils.win_system.get_pending_computer_name", return_value=None
  327. ), patch(
  328. "salt.utils.win_system.get_pending_domain_join", return_value=False
  329. ):
  330. expected = {
  331. "Pending Component Servicing": False,
  332. "Pending Computer Rename": False,
  333. "Pending DVD Reboot": False,
  334. "Pending File Rename": False,
  335. "Pending Join Domain": False,
  336. "Pending ServerManager": False,
  337. "Pending Update": False,
  338. "Pending Windows Update": False,
  339. "Reboot Required Witnessed": False,
  340. "Volatile Update Exe": False,
  341. }
  342. result = win_system.get_pending_reboot_details()
  343. self.assertDictEqual(expected, result)
  344. def test_get_pending_reboot_details_true(self):
  345. """
  346. All items True should return a dictionary with all items True
  347. """
  348. with patch(
  349. "salt.utils.win_system.get_pending_update", return_value=True
  350. ), patch("salt.utils.win_update.needs_reboot", return_value=True), patch(
  351. "salt.utils.win_system.get_pending_update_exe_volatile", return_value=True
  352. ), patch(
  353. "salt.utils.win_system.get_pending_file_rename", return_value=True
  354. ), patch(
  355. "salt.utils.win_system.get_pending_servermanager", return_value=True
  356. ), patch(
  357. "salt.utils.win_system.get_pending_component_servicing", return_value=True
  358. ), patch(
  359. "salt.utils.win_system.get_pending_dvd_reboot", return_value=True
  360. ), patch(
  361. "salt.utils.win_system.get_reboot_required_witnessed", return_value=True
  362. ), patch(
  363. "salt.utils.win_system.get_pending_computer_name",
  364. return_value="pending name",
  365. ), patch(
  366. "salt.utils.win_system.get_pending_domain_join", return_value=True
  367. ):
  368. expected = {
  369. "Pending Component Servicing": True,
  370. "Pending Computer Rename": True,
  371. "Pending DVD Reboot": True,
  372. "Pending File Rename": True,
  373. "Pending Join Domain": True,
  374. "Pending ServerManager": True,
  375. "Pending Update": True,
  376. "Pending Windows Update": True,
  377. "Reboot Required Witnessed": True,
  378. "Volatile Update Exe": True,
  379. }
  380. result = win_system.get_pending_reboot_details()
  381. self.assertDictEqual(expected, result)