test_win_system.py 18 KB

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