kernelpkg.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[0]):
  32. self.assertEqual(self._kernelpkg.latest_available(), self.KERNEL_LIST[-1])
  33. def test_latest_available_at_latest(self):
  34. '''
  35. Test - Return the latest available kernel version
  36. '''
  37. mock = MagicMock(return_value=self.LATEST)
  38. with patch.dict(self._kernelpkg.__salt__, {'pkg.latest_version': mock}):
  39. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[-1]):
  40. self.assertEqual(self._kernelpkg.latest_available(), self.KERNEL_LIST[-1])
  41. def test_latest_available_with_updates(self):
  42. '''
  43. Test - Return the latest available kernel version
  44. '''
  45. mock = MagicMock(return_value=self.LATEST)
  46. with patch.dict(self._kernelpkg.__salt__, {'pkg.latest_version': mock}):
  47. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[0]):
  48. self.assertEqual(self._kernelpkg.latest_available(), self.KERNEL_LIST[-1])
  49. def test_latest_installed_with_updates(self):
  50. '''
  51. Test - Return the latest installed kernel version
  52. '''
  53. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[0]):
  54. with patch.object(self._kernelpkg, 'list_installed', return_value=self.KERNEL_LIST):
  55. self.assertEqual(self._kernelpkg.latest_installed(), self.KERNEL_LIST[-1])
  56. def test_latest_installed_at_latest(self):
  57. '''
  58. Test - Return the latest installed kernel version
  59. '''
  60. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[-1]):
  61. with patch.object(self._kernelpkg, 'list_installed', return_value=self.KERNEL_LIST):
  62. self.assertEqual(self._kernelpkg.latest_installed(), self.KERNEL_LIST[-1])
  63. def test_needs_reboot_with_update(self):
  64. '''
  65. Test - Return True if a new kernel is ready to be booted
  66. '''
  67. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[0]):
  68. with patch.object(self._kernelpkg, 'latest_installed', return_value=self.KERNEL_LIST[1]):
  69. self.assertTrue(self._kernelpkg.needs_reboot())
  70. def test_needs_reboot_at_latest(self):
  71. '''
  72. Test - Return True if a new kernel is ready to be booted
  73. '''
  74. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[1]):
  75. with patch.object(self._kernelpkg, 'latest_installed', return_value=self.KERNEL_LIST[1]):
  76. self.assertFalse(self._kernelpkg.needs_reboot())
  77. def test_needs_reboot_order_inverted(self):
  78. '''
  79. Test - Return True if a new kernel is ready to be booted
  80. '''
  81. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[1]):
  82. with patch.object(self._kernelpkg, 'latest_installed', return_value=self.KERNEL_LIST[0]):
  83. self.assertFalse(self._kernelpkg.needs_reboot())
  84. def test_upgrade_not_needed_with_reboot(self):
  85. '''
  86. Test - Upgrade function when no upgrade is available and reboot has been requested
  87. '''
  88. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[-1]):
  89. with patch.object(self._kernelpkg, 'list_installed', return_value=self.KERNEL_LIST):
  90. result = self._kernelpkg.upgrade(reboot=True)
  91. self.assertIn('upgrades', result)
  92. self.assertEqual(result['active'], self.KERNEL_LIST[-1])
  93. self.assertEqual(result['latest_installed'], self.KERNEL_LIST[-1])
  94. self.assertEqual(result['reboot_requested'], True)
  95. self.assertEqual(result['reboot_required'], False)
  96. self._kernelpkg.__salt__['system.reboot'].assert_not_called()
  97. def test_upgrade_not_needed_without_reboot(self):
  98. '''
  99. Test - Upgrade function when no upgrade is available and no reboot has been requested
  100. '''
  101. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[-1]):
  102. with patch.object(self._kernelpkg, 'list_installed', return_value=self.KERNEL_LIST):
  103. result = self._kernelpkg.upgrade(reboot=False)
  104. self.assertIn('upgrades', result)
  105. self.assertEqual(result['active'], self.KERNEL_LIST[-1])
  106. self.assertEqual(result['latest_installed'], self.KERNEL_LIST[-1])
  107. self.assertEqual(result['reboot_requested'], False)
  108. self.assertEqual(result['reboot_required'], False)
  109. self._kernelpkg.__salt__['system.reboot'].assert_not_called()
  110. def test_upgrade_needed_with_reboot(self):
  111. '''
  112. Test - Upgrade function when an upgrade is available and reboot has been requested
  113. '''
  114. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[0]):
  115. with patch.object(self._kernelpkg, 'list_installed', return_value=self.KERNEL_LIST):
  116. result = self._kernelpkg.upgrade(reboot=True)
  117. self.assertIn('upgrades', result)
  118. self.assertEqual(result['active'], self.KERNEL_LIST[0])
  119. self.assertEqual(result['latest_installed'], self.KERNEL_LIST[-1])
  120. self.assertEqual(result['reboot_requested'], True)
  121. self.assertEqual(result['reboot_required'], True)
  122. self.assert_called_once(self._kernelpkg.__salt__['system.reboot'])
  123. def test_upgrade_needed_without_reboot(self):
  124. '''
  125. Test - Upgrade function when an upgrade is available and no reboot has been requested
  126. '''
  127. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[0]):
  128. with patch.object(self._kernelpkg, 'list_installed', return_value=self.KERNEL_LIST):
  129. result = self._kernelpkg.upgrade(reboot=False)
  130. self.assertIn('upgrades', result)
  131. self.assertEqual(result['active'], self.KERNEL_LIST[0])
  132. self.assertEqual(result['latest_installed'], self.KERNEL_LIST[-1])
  133. self.assertEqual(result['reboot_requested'], False)
  134. self.assertEqual(result['reboot_required'], True)
  135. self._kernelpkg.__salt__['system.reboot'].assert_not_called()
  136. def test_upgrade_available_true(self):
  137. '''
  138. Test - upgrade_available
  139. '''
  140. with patch.object(self._kernelpkg, 'latest_available', return_value=self.KERNEL_LIST[-1]):
  141. with patch.object(self._kernelpkg, 'latest_installed', return_value=self.KERNEL_LIST[0]):
  142. self.assertTrue(self._kernelpkg.upgrade_available())
  143. def test_upgrade_available_false(self):
  144. '''
  145. Test - upgrade_available
  146. '''
  147. with patch.object(self._kernelpkg, 'latest_available', return_value=self.KERNEL_LIST[-1]):
  148. with patch.object(self._kernelpkg, 'latest_installed', return_value=self.KERNEL_LIST[-1]):
  149. self.assertFalse(self._kernelpkg.upgrade_available())
  150. def test_upgrade_available_inverted(self):
  151. '''
  152. Test - upgrade_available
  153. '''
  154. with patch.object(self._kernelpkg, 'latest_available', return_value=self.KERNEL_LIST[0]):
  155. with patch.object(self._kernelpkg, 'latest_installed', return_value=self.KERNEL_LIST[-1]):
  156. self.assertFalse(self._kernelpkg.upgrade_available())
  157. def test_remove_active(self):
  158. '''
  159. Test - remove kernel package
  160. '''
  161. mock = MagicMock(return_value={'retcode': 0, 'stderr': []})
  162. with patch.dict(self._kernelpkg.__salt__, {'cmd.run_all': mock}):
  163. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[-1]):
  164. with patch.object(self._kernelpkg, 'list_installed', return_value=self.KERNEL_LIST):
  165. self.assertRaises(CommandExecutionError, self._kernelpkg.remove, release=self.KERNEL_LIST[-1])
  166. self._kernelpkg.__salt__['cmd.run_all'].assert_not_called()
  167. def test_remove_invalid(self):
  168. '''
  169. Test - remove kernel package
  170. '''
  171. mock = MagicMock(return_value={'retcode': 0, 'stderr': []})
  172. with patch.dict(self._kernelpkg.__salt__, {'cmd.run_all': mock}):
  173. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[-1]):
  174. with patch.object(self._kernelpkg, 'list_installed', return_value=self.KERNEL_LIST):
  175. self.assertRaises(CommandExecutionError, self._kernelpkg.remove, release='invalid')
  176. self._kernelpkg.__salt__['cmd.run_all'].assert_not_called()