123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- # -*- coding: utf-8 -*-
- """
- :synopsis: Base class for kernelpkg modules
- :platform: Linux
- :maturity: develop
- .. versionadded:: 2018.3.0
- """
- # pylint: disable=invalid-name,no-member
- from __future__ import absolute_import, print_function, unicode_literals
- # Salt testing libs
- try:
- from tests.support.mock import MagicMock, patch
- from salt.exceptions import CommandExecutionError
- except ImportError:
- pass
- class KernelPkgTestCase(object):
- """
- Test cases shared by all kernelpkg virtual modules
- """
- def test_active(self):
- """
- Test - Return return the active kernel version
- """
- self.assertEqual(self._kernelpkg.active(), self.KERNEL_LIST[0])
- def test_latest_available_no_results(self):
- """
- Test - Return the latest available kernel version
- """
- mock = MagicMock(return_value="")
- with patch.dict(self._kernelpkg.__salt__, {"pkg.latest_version": mock}):
- with patch.object(
- self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]
- ):
- self.assertEqual(
- self._kernelpkg.latest_available(), self.KERNEL_LIST[-1]
- )
- def test_latest_available_at_latest(self):
- """
- Test - Return the latest available kernel version
- """
- mock = MagicMock(return_value=self.LATEST)
- with patch.dict(self._kernelpkg.__salt__, {"pkg.latest_version": mock}):
- with patch.object(
- self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]
- ):
- self.assertEqual(
- self._kernelpkg.latest_available(), self.KERNEL_LIST[-1]
- )
- def test_latest_available_with_updates(self):
- """
- Test - Return the latest available kernel version
- """
- mock = MagicMock(return_value=self.LATEST)
- with patch.dict(self._kernelpkg.__salt__, {"pkg.latest_version": mock}):
- with patch.object(
- self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]
- ):
- self.assertEqual(
- self._kernelpkg.latest_available(), self.KERNEL_LIST[-1]
- )
- def test_latest_installed_with_updates(self):
- """
- Test - Return the latest installed kernel version
- """
- with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]):
- with patch.object(
- self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
- ):
- self.assertEqual(
- self._kernelpkg.latest_installed(), self.KERNEL_LIST[-1]
- )
- def test_latest_installed_at_latest(self):
- """
- Test - Return the latest installed kernel version
- """
- with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]):
- with patch.object(
- self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
- ):
- self.assertEqual(
- self._kernelpkg.latest_installed(), self.KERNEL_LIST[-1]
- )
- def test_needs_reboot_with_update(self):
- """
- Test - Return True if a new kernel is ready to be booted
- """
- with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]):
- with patch.object(
- self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[1]
- ):
- self.assertTrue(self._kernelpkg.needs_reboot())
- def test_needs_reboot_at_latest(self):
- """
- Test - Return True if a new kernel is ready to be booted
- """
- with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[1]):
- with patch.object(
- self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[1]
- ):
- self.assertFalse(self._kernelpkg.needs_reboot())
- def test_needs_reboot_order_inverted(self):
- """
- Test - Return True if a new kernel is ready to be booted
- """
- with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[1]):
- with patch.object(
- self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[0]
- ):
- self.assertFalse(self._kernelpkg.needs_reboot())
- def test_upgrade_not_needed_with_reboot(self):
- """
- Test - Upgrade function when no upgrade is available and reboot has been requested
- """
- with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]):
- with patch.object(
- self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
- ):
- result = self._kernelpkg.upgrade(reboot=True)
- self.assertIn("upgrades", result)
- self.assertEqual(result["active"], self.KERNEL_LIST[-1])
- self.assertEqual(result["latest_installed"], self.KERNEL_LIST[-1])
- self.assertEqual(result["reboot_requested"], True)
- self.assertEqual(result["reboot_required"], False)
- self._kernelpkg.__salt__["system.reboot"].assert_not_called()
- def test_upgrade_not_needed_without_reboot(self):
- """
- Test - Upgrade function when no upgrade is available and no reboot has been requested
- """
- with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]):
- with patch.object(
- self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
- ):
- result = self._kernelpkg.upgrade(reboot=False)
- self.assertIn("upgrades", result)
- self.assertEqual(result["active"], self.KERNEL_LIST[-1])
- self.assertEqual(result["latest_installed"], self.KERNEL_LIST[-1])
- self.assertEqual(result["reboot_requested"], False)
- self.assertEqual(result["reboot_required"], False)
- self._kernelpkg.__salt__["system.reboot"].assert_not_called()
- def test_upgrade_needed_with_reboot(self):
- """
- Test - Upgrade function when an upgrade is available and reboot has been requested
- """
- with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]):
- with patch.object(
- self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
- ):
- result = self._kernelpkg.upgrade(reboot=True)
- self.assertIn("upgrades", result)
- self.assertEqual(result["active"], self.KERNEL_LIST[0])
- self.assertEqual(result["latest_installed"], self.KERNEL_LIST[-1])
- self.assertEqual(result["reboot_requested"], True)
- self.assertEqual(result["reboot_required"], True)
- self.assert_called_once(self._kernelpkg.__salt__["system.reboot"])
- def test_upgrade_needed_without_reboot(self):
- """
- Test - Upgrade function when an upgrade is available and no reboot has been requested
- """
- with patch.object(self._kernelpkg, "active", return_value=self.KERNEL_LIST[0]):
- with patch.object(
- self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
- ):
- result = self._kernelpkg.upgrade(reboot=False)
- self.assertIn("upgrades", result)
- self.assertEqual(result["active"], self.KERNEL_LIST[0])
- self.assertEqual(result["latest_installed"], self.KERNEL_LIST[-1])
- self.assertEqual(result["reboot_requested"], False)
- self.assertEqual(result["reboot_required"], True)
- self._kernelpkg.__salt__["system.reboot"].assert_not_called()
- def test_upgrade_available_true(self):
- """
- Test - upgrade_available
- """
- with patch.object(
- self._kernelpkg, "latest_available", return_value=self.KERNEL_LIST[-1]
- ):
- with patch.object(
- self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[0]
- ):
- self.assertTrue(self._kernelpkg.upgrade_available())
- def test_upgrade_available_false(self):
- """
- Test - upgrade_available
- """
- with patch.object(
- self._kernelpkg, "latest_available", return_value=self.KERNEL_LIST[-1]
- ):
- with patch.object(
- self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[-1]
- ):
- self.assertFalse(self._kernelpkg.upgrade_available())
- def test_upgrade_available_inverted(self):
- """
- Test - upgrade_available
- """
- with patch.object(
- self._kernelpkg, "latest_available", return_value=self.KERNEL_LIST[0]
- ):
- with patch.object(
- self._kernelpkg, "latest_installed", return_value=self.KERNEL_LIST[-1]
- ):
- self.assertFalse(self._kernelpkg.upgrade_available())
- def test_remove_active(self):
- """
- Test - remove kernel package
- """
- mock = MagicMock(return_value={"retcode": 0, "stderr": []})
- with patch.dict(self._kernelpkg.__salt__, {"cmd.run_all": mock}):
- with patch.object(
- self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]
- ):
- with patch.object(
- self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
- ):
- self.assertRaises(
- CommandExecutionError,
- self._kernelpkg.remove,
- release=self.KERNEL_LIST[-1],
- )
- self._kernelpkg.__salt__["cmd.run_all"].assert_not_called()
- def test_remove_invalid(self):
- """
- Test - remove kernel package
- """
- mock = MagicMock(return_value={"retcode": 0, "stderr": []})
- with patch.dict(self._kernelpkg.__salt__, {"cmd.run_all": mock}):
- with patch.object(
- self._kernelpkg, "active", return_value=self.KERNEL_LIST[-1]
- ):
- with patch.object(
- self._kernelpkg, "list_installed", return_value=self.KERNEL_LIST
- ):
- self.assertRaises(
- CommandExecutionError, self._kernelpkg.remove, release="invalid"
- )
- self._kernelpkg.__salt__["cmd.run_all"].assert_not_called()
|