salt/tests/unit/modules/test_pkg_resource.py

161 lines
6.3 KiB
Python
Raw Normal View History

2015-04-17 12:22:13 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: Rahul Handay <rahulha@saltstack.com>
2015-04-17 12:22:13 +00:00
'''
2015-04-17 19:48:55 +00:00
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
2015-04-17 19:48:55 +00:00
import yaml
2015-04-17 12:22:13 +00:00
# Import Salt Testing Libs
2017-03-22 16:42:17 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
2015-04-17 12:22:13 +00:00
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
# Import Salt Libs
import salt.utils.data
import salt.utils.yaml
import salt.modules.pkg_resource as pkg_resource
Use explicit unicode strings + break up salt.utils This PR is part of what will be an ongoing effort to use explicit unicode strings in Salt. Because Python 3 does not suport Python 2's raw unicode string syntax (i.e. `ur'\d+'`), we must use `salt.utils.locales.sdecode()` to ensure that the raw string is unicode. However, because of how `salt/utils/__init__.py` has evolved into the hulking monstrosity it is today, this means importing a large module in places where it is not needed, which could negatively impact performance. For this reason, this PR also breaks out some of the functions from `salt/utils/__init__.py` into new/existing modules under `salt/utils/`. The long term goal will be that the modules within this directory do not depend on importing `salt.utils`. A summary of the changes in this PR is as follows: * Moves the following functions from `salt.utils` to new locations (including a deprecation warning if invoked from `salt.utils`): `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`, `dequote`, `is_hex`, `is_bin_str`, `rand_string`, `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`, `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`, `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`, `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`, `is_openbsd`, `is_aix` * Moves the functions already deprecated by @rallytime to the bottom of `salt/utils/__init__.py` for better organization, so we can keep the deprecated ones separate from the ones yet to be deprecated as we continue to break up `salt.utils` * Updates `salt/*.py` and all files under `salt/client/` to use explicit unicode string literals. * Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils import foo` becomes `import salt.utils.foo as foo`). * Renames the `test.rand_str` function to `test.random_hash` to more accurately reflect what it does * Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`) such that it returns a string matching the passed size. Previously this function would get `size` bytes from `os.urandom()`, base64-encode it, and return the result, which would in most cases not be equal to the passed size.
2017-07-25 01:47:15 +00:00
from salt.ext import six
2015-04-17 12:22:13 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-03-22 16:42:17 +00:00
class PkgresTestCase(TestCase, LoaderModuleMockMixin):
2015-04-17 12:22:13 +00:00
'''
Test cases for salt.modules.pkg_resource
'''
2017-03-22 16:42:17 +00:00
def setup_loader_modules(self):
return {pkg_resource: {}}
2015-04-17 12:22:13 +00:00
def test_pack_sources(self):
'''
Test to accepts list of dicts (or a string representing a
list of dicts) and packs the key/value pairs into a single dict.
'''
with patch.object(salt.utils.yaml,
2015-04-17 12:22:13 +00:00
'safe_load',
MagicMock(side_effect=yaml.parser.ParserError('f'))):
with patch.dict(pkg_resource.__salt__,
{'pkg.normalize_name': MagicMock()}):
self.assertDictEqual(pkg_resource.pack_sources('sources'), {})
self.assertDictEqual(pkg_resource.pack_sources(['A', 'a']), {})
self.assertTrue(pkg_resource.pack_sources([{'A': 'a'}]))
def test_parse_targets(self):
'''
Test to parses the input to pkg.install and
returns back the package(s) to be installed. Returns a
list of packages, as well as a string noting whether the
packages are to come from a repository or a binary package.
'''
with patch.dict(pkg_resource.__grains__, {'os': 'A'}):
self.assertEqual(pkg_resource.parse_targets(pkgs='a',
sources='a'),
(None, None))
with patch.object(pkg_resource, '_repack_pkgs',
return_value=False):
self.assertEqual(pkg_resource.parse_targets(pkgs='a'),
(None, None))
with patch.object(pkg_resource, '_repack_pkgs',
return_value='A'):
self.assertEqual(pkg_resource.parse_targets(pkgs='a'),
('A', 'repository'))
with patch.dict(pkg_resource.__grains__, {'os': 'MacOS1'}):
with patch.object(pkg_resource, 'pack_sources',
return_value=False):
self.assertEqual(pkg_resource.parse_targets(sources='s'),
(None, None))
with patch.object(pkg_resource, 'pack_sources',
return_value={'A': '/a'}):
2015-04-17 12:22:13 +00:00
with patch.dict(pkg_resource.__salt__,
{'config.valid_fileproto':
MagicMock(return_value=False)}):
self.assertEqual(pkg_resource.parse_targets(sources='s'),
(['/a'], 'file'))
2015-04-17 12:22:13 +00:00
with patch.object(pkg_resource, 'pack_sources',
return_value={'A': 'a'}):
with patch.dict(pkg_resource.__salt__,
{'config.valid_fileproto':
MagicMock(return_value=False)}):
self.assertEqual(pkg_resource.parse_targets(name='n'),
({'n': None}, 'repository'))
self.assertEqual(pkg_resource.parse_targets(),
(None, None))
def test_version(self):
'''
Test to Common interface for obtaining the version
of installed packages.
'''
with patch.object(salt.utils.data, 'is_true', return_value=True):
2015-04-17 12:22:13 +00:00
mock = MagicMock(return_value={'A': 'B'})
with patch.dict(pkg_resource.__salt__,
{'pkg.list_pkgs': mock}):
self.assertEqual(pkg_resource.version('A'), 'B')
self.assertDictEqual(pkg_resource.version(), {})
mock = MagicMock(return_value={})
with patch.dict(pkg_resource.__salt__, {'pkg.list_pkgs': mock}):
with patch('builtins.next' if six.PY3 else '__builtin__.next') as mock_next:
2015-04-17 12:22:13 +00:00
mock_next.side_effect = StopIteration()
self.assertEqual(pkg_resource.version('A'), '')
def test_add_pkg(self):
'''
Test to add a package to a dict of installed packages.
'''
self.assertIsNone(pkg_resource.add_pkg({'pkgs': []}, 'name', 'version'))
2015-04-17 12:22:13 +00:00
def test_sort_pkglist(self):
'''
Test to accepts a dict obtained from pkg.list_pkgs() and sorts
in place the list of versions for any packages that have multiple
versions installed, so that two package lists can be compared
to one another.
'''
self.assertIsNone(pkg_resource.sort_pkglist({}))
def test_stringify(self):
'''
Test to takes a dict of package name/version information
and joins each list of
installed versions into a string.
'''
self.assertIsNone(pkg_resource.stringify({}))
def test_version_clean(self):
'''
Test to clean the version string removing extra data.
'''
with patch.dict(pkg_resource.__salt__, {'pkg.version_clean':
MagicMock(return_value='A')}):
self.assertEqual(pkg_resource.version_clean('version'), 'A')
self.assertEqual(pkg_resource.version_clean('v'), 'v')
def test_check_extra_requirements(self):
'''
Test to check if the installed package already
has the given requirements.
'''
with patch.dict(pkg_resource.__salt__, {'pkg.check_extra_requirements':
MagicMock(return_value='A')}):
self.assertEqual(pkg_resource.check_extra_requirements('a', 'b'),
'A')
self.assertTrue(pkg_resource.check_extra_requirements('a', False))