test_kernelpkg_linux_apt.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 re
  12. try:
  13. # Import Salt Testing Libs
  14. from tests.support.mixins import LoaderModuleMockMixin
  15. from tests.support.unit import TestCase, skipIf
  16. from tests.support.mock import MagicMock, patch
  17. # Import Salt Libs
  18. from tests.support.kernelpkg import KernelPkgTestCase
  19. import salt.modules.kernelpkg_linux_apt as kernelpkg
  20. from salt.exceptions import CommandExecutionError
  21. HAS_MODULES = True
  22. except ImportError:
  23. HAS_MODULES = False
  24. @skipIf(not HAS_MODULES, 'Salt modules could not be loaded')
  25. class AptKernelPkgTestCase(KernelPkgTestCase, TestCase, LoaderModuleMockMixin):
  26. '''
  27. Test cases for salt.modules.kernelpkg_linux_apt
  28. '''
  29. _kernelpkg = kernelpkg
  30. KERNEL_LIST = ['4.4.0-70-generic', '4.4.0-71-generic', '4.5.1-14-generic']
  31. PACKAGE_DICT = {}
  32. @classmethod
  33. def setUpClass(cls):
  34. version = re.match(r'^(\d+\.\d+\.\d+)-(\d+)', cls.KERNEL_LIST[-1])
  35. cls.LATEST = '{0}.{1}'.format(version.group(1), version.group(2))
  36. for kernel in cls.KERNEL_LIST:
  37. pkg = '{0}-{1}'.format(kernelpkg._package_prefix(), kernel) # pylint: disable=protected-access
  38. cls.PACKAGE_DICT[pkg] = pkg
  39. def setup_loader_modules(self):
  40. return {
  41. kernelpkg: {
  42. '__grains__': {
  43. 'kernelrelease': self.KERNEL_LIST[0]
  44. },
  45. '__salt__': {
  46. 'pkg.install': MagicMock(return_value={}),
  47. 'pkg.latest_version': MagicMock(return_value=self.LATEST),
  48. 'pkg.list_pkgs': MagicMock(return_value=self.PACKAGE_DICT),
  49. 'pkg.purge': MagicMock(return_value=None),
  50. 'system.reboot': MagicMock(return_value=None)
  51. }
  52. }
  53. }
  54. def test_list_installed(self):
  55. '''
  56. Test - Return return the latest installed kernel version
  57. '''
  58. PACKAGE_LIST = ['{0}-{1}'.format(kernelpkg._package_prefix(), kernel) for kernel in self.KERNEL_LIST] # pylint: disable=protected-access
  59. mock = MagicMock(return_value=PACKAGE_LIST)
  60. with patch.dict(self._kernelpkg.__salt__, {'pkg.list_pkgs': mock}):
  61. self.assertListEqual(self._kernelpkg.list_installed(), self.KERNEL_LIST)
  62. def test_list_installed_none(self):
  63. '''
  64. Test - Return return the latest installed kernel version
  65. '''
  66. mock = MagicMock(return_value=None)
  67. with patch.dict(self._kernelpkg.__salt__, {'pkg.list_pkgs': mock}):
  68. self.assertListEqual(self._kernelpkg.list_installed(), [])
  69. def test_remove_success(self):
  70. '''
  71. Test - remove kernel package
  72. '''
  73. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[-1]):
  74. with patch.object(self._kernelpkg, 'list_installed', return_value=self.KERNEL_LIST):
  75. result = self._kernelpkg.remove(release=self.KERNEL_LIST[0])
  76. self.assertIn('removed', result)
  77. target = '{0}-{1}'.format(self._kernelpkg._package_prefix(), self.KERNEL_LIST[0]) # pylint: disable=protected-access
  78. self.assertListEqual(result['removed'], [target])
  79. def test_remove_error(self):
  80. '''
  81. Test - remove kernel package
  82. '''
  83. mock = MagicMock(side_effect=CommandExecutionError())
  84. with patch.dict(self._kernelpkg.__salt__, {'pkg.purge': mock}):
  85. with patch.object(self._kernelpkg, 'active', return_value=self.KERNEL_LIST[-1]):
  86. with patch.object(self._kernelpkg, 'list_installed', return_value=self.KERNEL_LIST):
  87. self.assertRaises(CommandExecutionError, self._kernelpkg.remove, release=self.KERNEL_LIST[0])