test_kernelpkg.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # -*- coding: utf-8 -*-
  2. """
  3. :synopsis: Unit Tests for 'module.aptkernelpkg'
  4. :platform: Linux
  5. :maturity: develop
  6. versionadded:: 2018.3.0
  7. """
  8. # pylint: disable=invalid-name,no-member
  9. # Import Python libs
  10. from __future__ import absolute_import, print_function, unicode_literals
  11. # Import Salt libs
  12. from salt.ext import six
  13. try:
  14. # Import Salt Testing Libs
  15. from tests.support.mixins import LoaderModuleMockMixin
  16. from tests.support.unit import skipIf, TestCase
  17. from tests.support.mock import MagicMock, patch
  18. # Import Salt Libs
  19. import salt.states.kernelpkg as kernelpkg
  20. HAS_MODULES = True
  21. except ImportError:
  22. HAS_MODULES = False
  23. KERNEL_LIST = ["4.4.0-70-generic", "4.4.0-71-generic", "4.5.1-14-generic"]
  24. STATE_NAME = "kernelpkg-test"
  25. @skipIf(not HAS_MODULES, "Salt modules could not be loaded")
  26. class KernelPkgTestCase(TestCase, LoaderModuleMockMixin):
  27. """
  28. Test cases for salt.states.aptpkg
  29. """
  30. def setup_loader_modules(self):
  31. return {
  32. kernelpkg: {
  33. "__salt__": {
  34. "system.reboot": MagicMock(return_value=None),
  35. "kernelpkg.upgrade": MagicMock(
  36. return_value={
  37. "upgrades": {"kernel": {"old": "1.0.0", "new": "2.0.0"}}
  38. }
  39. ),
  40. "kernelpkg.active": MagicMock(return_value=0),
  41. "kernelpkg.latest_installed": MagicMock(return_value=0),
  42. }
  43. }
  44. }
  45. def test_latest_installed_with_changes(self):
  46. """
  47. Test - latest_installed when an upgrade is available
  48. """
  49. installed = MagicMock(return_value=KERNEL_LIST[:-1])
  50. upgrade = MagicMock(return_value=KERNEL_LIST[-1])
  51. with patch.dict(kernelpkg.__salt__, {"kernelpkg.list_installed": installed}):
  52. with patch.dict(
  53. kernelpkg.__salt__, {"kernelpkg.latest_available": upgrade}
  54. ):
  55. with patch.dict(kernelpkg.__opts__, {"test": False}):
  56. kernelpkg.__salt__["kernelpkg.upgrade"].reset_mock()
  57. ret = kernelpkg.latest_installed(name=STATE_NAME)
  58. self.assertEqual(ret["name"], STATE_NAME)
  59. self.assertTrue(ret["result"])
  60. self.assertIsInstance(ret["changes"], dict)
  61. self.assertIsInstance(ret["comment"], six.text_type)
  62. self.assert_called_once(kernelpkg.__salt__["kernelpkg.upgrade"])
  63. with patch.dict(kernelpkg.__opts__, {"test": True}):
  64. kernelpkg.__salt__["kernelpkg.upgrade"].reset_mock()
  65. ret = kernelpkg.latest_installed(name=STATE_NAME)
  66. self.assertEqual(ret["name"], STATE_NAME)
  67. self.assertIsNone(ret["result"])
  68. self.assertDictEqual(ret["changes"], {})
  69. self.assertIsInstance(ret["comment"], six.text_type)
  70. kernelpkg.__salt__["kernelpkg.upgrade"].assert_not_called()
  71. def test_latest_installed_at_latest(self):
  72. """
  73. Test - latest_installed when no upgrade is available
  74. """
  75. installed = MagicMock(return_value=KERNEL_LIST)
  76. upgrade = MagicMock(return_value=KERNEL_LIST[-1])
  77. with patch.dict(kernelpkg.__salt__, {"kernelpkg.list_installed": installed}):
  78. with patch.dict(
  79. kernelpkg.__salt__, {"kernelpkg.latest_available": upgrade}
  80. ):
  81. with patch.dict(kernelpkg.__opts__, {"test": False}):
  82. ret = kernelpkg.latest_installed(name=STATE_NAME)
  83. self.assertEqual(ret["name"], STATE_NAME)
  84. self.assertTrue(ret["result"])
  85. self.assertDictEqual(ret["changes"], {})
  86. self.assertIsInstance(ret["comment"], six.text_type)
  87. kernelpkg.__salt__["kernelpkg.upgrade"].assert_not_called()
  88. with patch.dict(kernelpkg.__opts__, {"test": True}):
  89. ret = kernelpkg.latest_installed(name=STATE_NAME)
  90. self.assertEqual(ret["name"], STATE_NAME)
  91. self.assertTrue(ret["result"])
  92. self.assertDictEqual(ret["changes"], {})
  93. self.assertIsInstance(ret["comment"], six.text_type)
  94. kernelpkg.__salt__["kernelpkg.upgrade"].assert_not_called()
  95. def test_latest_active_with_changes(self):
  96. """
  97. Test - latest_active when a new kernel is available
  98. """
  99. reboot = MagicMock(return_value=True)
  100. latest = MagicMock(return_value=1)
  101. with patch.dict(
  102. kernelpkg.__salt__,
  103. {"kernelpkg.needs_reboot": reboot, "kernelpkg.latest_installed": latest},
  104. ), patch.dict(kernelpkg.__opts__, {"test": False}):
  105. kernelpkg.__salt__["system.reboot"].reset_mock()
  106. ret = kernelpkg.latest_active(name=STATE_NAME)
  107. self.assertEqual(ret["name"], STATE_NAME)
  108. self.assertTrue(ret["result"])
  109. self.assertIsInstance(ret["changes"], dict)
  110. self.assertIsInstance(ret["comment"], six.text_type)
  111. self.assert_called_once(kernelpkg.__salt__["system.reboot"])
  112. with patch.dict(kernelpkg.__opts__, {"test": True}):
  113. kernelpkg.__salt__["system.reboot"].reset_mock()
  114. ret = kernelpkg.latest_active(name=STATE_NAME)
  115. self.assertEqual(ret["name"], STATE_NAME)
  116. self.assertIsNone(ret["result"])
  117. self.assertDictEqual(ret["changes"], {"kernel": {"new": 1, "old": 0}})
  118. self.assertIsInstance(ret["comment"], six.text_type)
  119. kernelpkg.__salt__["system.reboot"].assert_not_called()
  120. def test_latest_active_at_latest(self):
  121. """
  122. Test - latest_active when the newest kernel is already active
  123. """
  124. reboot = MagicMock(return_value=False)
  125. with patch.dict(kernelpkg.__salt__, {"kernelpkg.needs_reboot": reboot}):
  126. with patch.dict(kernelpkg.__opts__, {"test": False}):
  127. kernelpkg.__salt__["system.reboot"].reset_mock()
  128. ret = kernelpkg.latest_active(name=STATE_NAME)
  129. self.assertEqual(ret["name"], STATE_NAME)
  130. self.assertTrue(ret["result"])
  131. self.assertDictEqual(ret["changes"], {})
  132. self.assertIsInstance(ret["comment"], six.text_type)
  133. kernelpkg.__salt__["system.reboot"].assert_not_called()
  134. with patch.dict(kernelpkg.__opts__, {"test": True}):
  135. kernelpkg.__salt__["system.reboot"].reset_mock()
  136. ret = kernelpkg.latest_active(name=STATE_NAME)
  137. self.assertEqual(ret["name"], STATE_NAME)
  138. self.assertTrue(ret["result"])
  139. self.assertDictEqual(ret["changes"], {})
  140. self.assertIsInstance(ret["comment"], six.text_type)
  141. kernelpkg.__salt__["system.reboot"].assert_not_called()
  142. def test_latest_wait(self):
  143. """
  144. Test - latest_wait static results
  145. """
  146. ret = kernelpkg.latest_wait(name=STATE_NAME)
  147. self.assertEqual(ret["name"], STATE_NAME)
  148. self.assertTrue(ret["result"])
  149. self.assertDictEqual(ret["changes"], {})
  150. self.assertIsInstance(ret["comment"], six.text_type)