1
0

test_pkg_resource.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import yaml
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch,
  14. )
  15. # Import Salt Libs
  16. import salt.utils.data
  17. import salt.utils.yaml
  18. import salt.modules.pkg_resource as pkg_resource
  19. from salt.ext import six
  20. class PkgresTestCase(TestCase, LoaderModuleMockMixin):
  21. '''
  22. Test cases for salt.modules.pkg_resource
  23. '''
  24. def setup_loader_modules(self):
  25. return {pkg_resource: {}}
  26. def test_pack_sources(self):
  27. '''
  28. Test to accepts list of dicts (or a string representing a
  29. list of dicts) and packs the key/value pairs into a single dict.
  30. '''
  31. with patch.object(salt.utils.yaml,
  32. 'safe_load',
  33. MagicMock(side_effect=yaml.parser.ParserError('f'))):
  34. with patch.dict(pkg_resource.__salt__,
  35. {'pkg.normalize_name': MagicMock()}):
  36. self.assertDictEqual(pkg_resource.pack_sources('sources'), {})
  37. self.assertDictEqual(pkg_resource.pack_sources(['A', 'a']), {})
  38. self.assertTrue(pkg_resource.pack_sources([{'A': 'a'}]))
  39. def test_parse_targets(self):
  40. '''
  41. Test to parses the input to pkg.install and
  42. returns back the package(s) to be installed. Returns a
  43. list of packages, as well as a string noting whether the
  44. packages are to come from a repository or a binary package.
  45. '''
  46. with patch.dict(pkg_resource.__grains__, {'os': 'A'}):
  47. self.assertEqual(pkg_resource.parse_targets(pkgs='a',
  48. sources='a'),
  49. (None, None))
  50. with patch.object(pkg_resource, '_repack_pkgs',
  51. return_value=False):
  52. self.assertEqual(pkg_resource.parse_targets(pkgs='a'),
  53. (None, None))
  54. with patch.object(pkg_resource, '_repack_pkgs',
  55. return_value='A'):
  56. self.assertEqual(pkg_resource.parse_targets(pkgs='a'),
  57. ('A', 'repository'))
  58. with patch.dict(pkg_resource.__grains__, {'os': 'MacOS1'}):
  59. with patch.object(pkg_resource, 'pack_sources',
  60. return_value=False):
  61. self.assertEqual(pkg_resource.parse_targets(sources='s'),
  62. (None, None))
  63. with patch.object(pkg_resource, 'pack_sources',
  64. return_value={'A': '/a'}):
  65. with patch.dict(pkg_resource.__salt__,
  66. {'config.valid_fileproto':
  67. MagicMock(return_value=False)}):
  68. self.assertEqual(pkg_resource.parse_targets(sources='s'),
  69. (['/a'], 'file'))
  70. with patch.object(pkg_resource, 'pack_sources',
  71. return_value={'A': 'a'}):
  72. with patch.dict(pkg_resource.__salt__,
  73. {'config.valid_fileproto':
  74. MagicMock(return_value=False)}):
  75. self.assertEqual(pkg_resource.parse_targets(name='n'),
  76. ({'n': None}, 'repository'))
  77. self.assertEqual(pkg_resource.parse_targets(),
  78. (None, None))
  79. def test_version(self):
  80. '''
  81. Test to Common interface for obtaining the version
  82. of installed packages.
  83. '''
  84. with patch.object(salt.utils.data, 'is_true', return_value=True):
  85. mock = MagicMock(return_value={'A': 'B'})
  86. with patch.dict(pkg_resource.__salt__,
  87. {'pkg.list_pkgs': mock}):
  88. self.assertEqual(pkg_resource.version('A'), 'B')
  89. self.assertDictEqual(pkg_resource.version(), {})
  90. mock = MagicMock(return_value={})
  91. with patch.dict(pkg_resource.__salt__, {'pkg.list_pkgs': mock}):
  92. with patch('builtins.next' if six.PY3 else '__builtin__.next') as mock_next:
  93. mock_next.side_effect = StopIteration()
  94. self.assertEqual(pkg_resource.version('A'), '')
  95. def test_add_pkg(self):
  96. '''
  97. Test to add a package to a dict of installed packages.
  98. '''
  99. self.assertIsNone(pkg_resource.add_pkg({'pkgs': []}, 'name', 'version'))
  100. def test_sort_pkglist(self):
  101. '''
  102. Test to accepts a dict obtained from pkg.list_pkgs() and sorts
  103. in place the list of versions for any packages that have multiple
  104. versions installed, so that two package lists can be compared
  105. to one another.
  106. '''
  107. self.assertIsNone(pkg_resource.sort_pkglist({}))
  108. def test_stringify(self):
  109. '''
  110. Test to takes a dict of package name/version information
  111. and joins each list of
  112. installed versions into a string.
  113. '''
  114. self.assertIsNone(pkg_resource.stringify({}))
  115. def test_version_clean(self):
  116. '''
  117. Test to clean the version string removing extra data.
  118. '''
  119. with patch.dict(pkg_resource.__salt__, {'pkg.version_clean':
  120. MagicMock(return_value='A')}):
  121. self.assertEqual(pkg_resource.version_clean('version'), 'A')
  122. self.assertEqual(pkg_resource.version_clean('v'), 'v')
  123. def test_check_extra_requirements(self):
  124. '''
  125. Test to check if the installed package already
  126. has the given requirements.
  127. '''
  128. with patch.dict(pkg_resource.__salt__, {'pkg.check_extra_requirements':
  129. MagicMock(return_value='A')}):
  130. self.assertEqual(pkg_resource.check_extra_requirements('a', 'b'),
  131. 'A')
  132. self.assertTrue(pkg_resource.check_extra_requirements('a', False))