kernelpkg.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # -*- coding: utf-8 -*-
  2. """
  3. :synopsis: Base class for kernelpkg modules
  4. :platform: Linux
  5. :maturity: develop
  6. .. versionadded:: 2018.3.0
  7. """
  8. # pylint: disable=invalid-name,no-member
  9. from __future__ import absolute_import, print_function, unicode_literals
  10. # Salt testing libs
  11. try:
  12. from tests.support.mock import MagicMock, patch
  13. from salt.exceptions import CommandExecutionError
  14. except ImportError:
  15. pass
  16. class KernelPkgTestCase(object):
  17. """
  18. Test cases shared by all kernelpkg virtual modules
  19. """
  20. def test_active(self):
  21. """
  22. Test - Return return the active kernel version
  23. """
  24. self.assertEqual(self._kernelpkg.active(), self.KERNEL_LIST[0])
  25. def test_latest_available_no_results(self):
  26. """
  27. Test - Return the latest available kernel version
  28. """
  29. mock = MagicMock(return_value="")
  30. with patch.dict(self._kernelpkg.__salt__, {"pkg.latest_version": mock}):
  31. with patch.object(
  32. self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]
  33. ):
  34. self.assertEqual(
  35. self._kernelpkg.latest_available(), self.KERNEL_LIST[-1]
  36. )
  37. def test_latest_available_at_latest(self):
  38. """
  39. Test - Return the latest available kernel version
  40. """
  41. mock = MagicMock(return_value=self.LATEST)
  42. with patch.dict(self._kernelpkg.__salt__, {"pkg.latest_version": mock}):
  43. with patch.object(
  44. self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]
  45. ):
  46. self.assertEqual(
  47. self._kernelpkg.latest_available(), self.KERNEL_LIST[-1]
  48. )
  49. def test_latest_available_with_updates(self):
  50. """
  51. Test - Return the latest available kernel version
  52. """
  53. mock = MagicMock(return_value=self.LATEST)
  54. with patch.dict(self._kernelpkg.__salt__, {"pkg.latest_version": mock}):
  55. with patch.object(
  56. self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]
  57. ):
  58. self.assertEqual(
  59. self._kernelpkg.latest_available(), self.KERNEL_LIST[-1]
  60. )
  61. def test_latest_installed_with_updates(self):
  62. """
  63. Test - Return the latest installed kernel version
  64. """
  65. with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]):
  66. with patch.object(
  67. self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
  68. ):
  69. self.assertEqual(
  70. self._kernelpkg.latest_installed(), self.KERNEL_LIST[-1]
  71. )
  72. def test_latest_installed_at_latest(self):
  73. """
  74. Test - Return the latest installed kernel version
  75. """
  76. with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]):
  77. with patch.object(
  78. self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
  79. ):
  80. self.assertEqual(
  81. self._kernelpkg.latest_installed(), self.KERNEL_LIST[-1]
  82. )
  83. def test_needs_reboot_with_update(self):
  84. """
  85. Test - Return True if a new kernel is ready to be booted
  86. """
  87. with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]):
  88. with patch.object(
  89. self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[1]
  90. ):
  91. self.assertTrue(self._kernelpkg.needs_reboot())
  92. def test_needs_reboot_at_latest(self):
  93. """
  94. Test - Return True if a new kernel is ready to be booted
  95. """
  96. with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[1]):
  97. with patch.object(
  98. self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[1]
  99. ):
  100. self.assertFalse(self._kernelpkg.needs_reboot())
  101. def test_needs_reboot_order_inverted(self):
  102. """
  103. Test - Return True if a new kernel is ready to be booted
  104. """
  105. with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[1]):
  106. with patch.object(
  107. self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[0]
  108. ):
  109. self.assertFalse(self._kernelpkg.needs_reboot())
  110. def test_upgrade_not_needed_with_reboot(self):
  111. """
  112. Test - Upgrade function when no upgrade is available and reboot has been requested
  113. """
  114. with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]):
  115. with patch.object(
  116. self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
  117. ):
  118. result = self._kernelpkg.upgrade(reboot=True)
  119. self.assertIn("upgrades", result)
  120. self.assertEqual(result["active"], self.KERNEL_LIST[-1])
  121. self.assertEqual(result["latest_installed"], self.KERNEL_LIST[-1])
  122. self.assertEqual(result["reboot_requested"], True)
  123. self.assertEqual(result["reboot_required"], False)
  124. self._kernelpkg.__salt__["system.reboot"].assert_not_called()
  125. def test_upgrade_not_needed_without_reboot(self):
  126. """
  127. Test - Upgrade function when no upgrade is available and no reboot has been requested
  128. """
  129. with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]):
  130. with patch.object(
  131. self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
  132. ):
  133. result = self._kernelpkg.upgrade(reboot=False)
  134. self.assertIn("upgrades", result)
  135. self.assertEqual(result["active"], self.KERNEL_LIST[-1])
  136. self.assertEqual(result["latest_installed"], self.KERNEL_LIST[-1])
  137. self.assertEqual(result["reboot_requested"], False)
  138. self.assertEqual(result["reboot_required"], False)
  139. self._kernelpkg.__salt__["system.reboot"].assert_not_called()
  140. def test_upgrade_needed_with_reboot(self):
  141. """
  142. Test - Upgrade function when an upgrade is available and reboot has been requested
  143. """
  144. with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]):
  145. with patch.object(
  146. self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
  147. ):
  148. result = self._kernelpkg.upgrade(reboot=True)
  149. self.assertIn("upgrades", result)
  150. self.assertEqual(result["active"], self.KERNEL_LIST[0])
  151. self.assertEqual(result["latest_installed"], self.KERNEL_LIST[-1])
  152. self.assertEqual(result["reboot_requested"], True)
  153. self.assertEqual(result["reboot_required"], True)
  154. self.assert_called_once(self._kernelpkg.__salt__["system.reboot"])
  155. def test_upgrade_needed_without_reboot(self):
  156. """
  157. Test - Upgrade function when an upgrade is available and no reboot has been requested
  158. """
  159. with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]):
  160. with patch.object(
  161. self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
  162. ):
  163. result = self._kernelpkg.upgrade(reboot=False)
  164. self.assertIn("upgrades", result)
  165. self.assertEqual(result["active"], self.KERNEL_LIST[0])
  166. self.assertEqual(result["latest_installed"], self.KERNEL_LIST[-1])
  167. self.assertEqual(result["reboot_requested"], False)
  168. self.assertEqual(result["reboot_required"], True)
  169. self._kernelpkg.__salt__["system.reboot"].assert_not_called()
  170. def test_upgrade_available_true(self):
  171. """
  172. Test - upgrade_available
  173. """
  174. with patch.object(
  175. self._kernelpkg, "latest_available", return_value=self.KERNEL_LIST[-1]
  176. ):
  177. with patch.object(
  178. self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[0]
  179. ):
  180. self.assertTrue(self._kernelpkg.upgrade_available())
  181. def test_upgrade_available_false(self):
  182. """
  183. Test - upgrade_available
  184. """
  185. with patch.object(
  186. self._kernelpkg, "latest_available", return_value=self.KERNEL_LIST[-1]
  187. ):
  188. with patch.object(
  189. self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[-1]
  190. ):
  191. self.assertFalse(self._kernelpkg.upgrade_available())
  192. def test_upgrade_available_inverted(self):
  193. """
  194. Test - upgrade_available
  195. """
  196. with patch.object(
  197. self._kernelpkg, "latest_available", return_value=self.KERNEL_LIST[0]
  198. ):
  199. with patch.object(
  200. self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[-1]
  201. ):
  202. self.assertFalse(self._kernelpkg.upgrade_available())
  203. def test_remove_active(self):
  204. """
  205. Test - remove kernel package
  206. """
  207. mock = MagicMock(return_value={"retcode": 0, "stderr": []})
  208. with patch.dict(self._kernelpkg.__salt__, {"cmd.run_all": mock}):
  209. with patch.object(
  210. self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]
  211. ):
  212. with patch.object(
  213. self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
  214. ):
  215. self.assertRaises(
  216. CommandExecutionError,
  217. self._kernelpkg.remove,
  218. release=self.KERNEL_LIST[-1],
  219. )
  220. self._kernelpkg.__salt__["cmd.run_all"].assert_not_called()
  221. def test_remove_invalid(self):
  222. """
  223. Test - remove kernel package
  224. """
  225. mock = MagicMock(return_value={"retcode": 0, "stderr": []})
  226. with patch.dict(self._kernelpkg.__salt__, {"cmd.run_all": mock}):
  227. with patch.object(
  228. self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]
  229. ):
  230. with patch.object(
  231. self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
  232. ):
  233. self.assertRaises(
  234. CommandExecutionError, self._kernelpkg.remove, release="invalid"
  235. )
  236. self._kernelpkg.__salt__["cmd.run_all"].assert_not_called()