2014-06-21 02:00:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
integration.loader.globals
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test Salt's loader regarding globals that it should pack in
|
|
|
|
'''
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import Python libs
|
2017-12-15 18:14:18 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2017-12-28 04:31:50 +00:00
|
|
|
import inspect
|
2014-11-21 19:05:13 +00:00
|
|
|
|
2014-06-21 02:00:10 +00:00
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
2014-06-21 02:00:10 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
import salt.loader
|
2017-12-28 04:31:50 +00:00
|
|
|
import salt.utils.yaml
|
2014-06-21 02:00:10 +00:00
|
|
|
|
2014-11-21 20:21:45 +00:00
|
|
|
# Import 3rd-party libs
|
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
|
2014-11-21 20:21:45 +00:00
|
|
|
|
2014-06-21 02:00:10 +00:00
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
class LoaderGlobalsTest(ModuleCase):
|
2014-06-21 02:00:10 +00:00
|
|
|
'''
|
|
|
|
Test all of the globals that the loader is responsible for adding to modules
|
|
|
|
|
|
|
|
This shouldn't be done here, but should rather be done per module type (in the cases where they are used)
|
|
|
|
so they can check ALL globals that they have (or should have) access to.
|
|
|
|
|
|
|
|
This is intended as a shorter term way of testing these so we don't break the loader
|
|
|
|
'''
|
|
|
|
def _verify_globals(self, mod_dict):
|
|
|
|
'''
|
|
|
|
Verify that the globals listed in the doc string (from the test) are in these modules
|
|
|
|
'''
|
|
|
|
# find the globals
|
2014-09-25 17:34:50 +00:00
|
|
|
global_vars = []
|
2014-11-21 20:21:45 +00:00
|
|
|
for val in six.itervalues(mod_dict):
|
2014-09-25 17:34:50 +00:00
|
|
|
# only find salty globals
|
|
|
|
if val.__module__.startswith('salt.loaded') and hasattr(val, '__globals__'):
|
|
|
|
global_vars.append(val.__globals__)
|
|
|
|
|
|
|
|
# if we couldn't find any, then we have no modules -- so something is broken
|
|
|
|
self.assertNotEqual(global_vars, [], msg='No modules were loaded.')
|
2014-06-21 02:00:10 +00:00
|
|
|
|
|
|
|
# get the names of the globals you should have
|
|
|
|
func_name = inspect.stack()[1][3]
|
2017-12-28 04:31:50 +00:00
|
|
|
names = next(six.itervalues(salt.utils.yaml.safe_load(getattr(self, func_name).__doc__)))
|
2014-06-21 02:00:10 +00:00
|
|
|
|
2014-09-25 17:34:50 +00:00
|
|
|
# Now, test each module!
|
|
|
|
for item in global_vars:
|
|
|
|
for name in names:
|
2015-02-06 14:48:20 +00:00
|
|
|
self.assertIn(name, list(item.keys()))
|
2014-06-21 02:00:10 +00:00
|
|
|
|
|
|
|
def test_auth(self):
|
|
|
|
'''
|
|
|
|
Test that auth mods have:
|
|
|
|
- __pillar__
|
|
|
|
- __grains__
|
|
|
|
- __salt__
|
2015-02-05 00:40:56 +00:00
|
|
|
- __context__
|
2014-06-21 02:00:10 +00:00
|
|
|
'''
|
|
|
|
self._verify_globals(salt.loader.auth(self.master_opts))
|
|
|
|
|
|
|
|
def test_runners(self):
|
|
|
|
'''
|
|
|
|
Test that runners have:
|
|
|
|
- __pillar__
|
|
|
|
- __salt__
|
|
|
|
- __opts__
|
|
|
|
- __grains__
|
2015-02-05 00:40:56 +00:00
|
|
|
- __context__
|
2014-06-21 02:00:10 +00:00
|
|
|
'''
|
|
|
|
self._verify_globals(salt.loader.runner(self.master_opts))
|
|
|
|
|
|
|
|
def test_returners(self):
|
|
|
|
'''
|
|
|
|
Test that returners have:
|
|
|
|
- __salt__
|
|
|
|
- __opts__
|
|
|
|
- __pillar__
|
|
|
|
- __grains__
|
2015-02-05 00:40:56 +00:00
|
|
|
- __context__
|
2014-06-21 02:00:10 +00:00
|
|
|
'''
|
|
|
|
self._verify_globals(salt.loader.returners(self.master_opts, {}))
|
|
|
|
|
|
|
|
def test_pillars(self):
|
|
|
|
'''
|
|
|
|
Test that pillars have:
|
|
|
|
- __salt__
|
|
|
|
- __opts__
|
|
|
|
- __pillar__
|
|
|
|
- __grains__
|
2015-02-05 00:40:56 +00:00
|
|
|
- __context__
|
2014-06-21 02:00:10 +00:00
|
|
|
'''
|
|
|
|
self._verify_globals(salt.loader.pillars(self.master_opts, {}))
|
|
|
|
|
|
|
|
def test_tops(self):
|
|
|
|
'''
|
|
|
|
Test that tops have: []
|
|
|
|
'''
|
|
|
|
self._verify_globals(salt.loader.tops(self.master_opts))
|
|
|
|
|
|
|
|
def test_outputters(self):
|
|
|
|
'''
|
|
|
|
Test that outputters have:
|
|
|
|
- __opts__
|
|
|
|
- __pillar__
|
|
|
|
- __grains__
|
2015-02-05 00:40:56 +00:00
|
|
|
- __context__
|
2014-06-21 02:00:10 +00:00
|
|
|
'''
|
|
|
|
self._verify_globals(salt.loader.outputters(self.master_opts))
|
|
|
|
|
2015-05-28 23:48:00 +00:00
|
|
|
def test_serializers(self):
|
|
|
|
'''
|
|
|
|
Test that serializers have: []
|
|
|
|
'''
|
|
|
|
self._verify_globals(salt.loader.serializers(self.master_opts))
|
|
|
|
|
2014-06-21 02:00:10 +00:00
|
|
|
def test_states(self):
|
|
|
|
'''
|
|
|
|
Test that states:
|
|
|
|
- __pillar__
|
|
|
|
- __salt__
|
|
|
|
- __opts__
|
|
|
|
- __grains__
|
2015-02-05 00:40:56 +00:00
|
|
|
- __context__
|
2014-06-21 02:00:10 +00:00
|
|
|
'''
|
2015-11-16 19:23:05 +00:00
|
|
|
self._verify_globals(salt.loader.states(self.master_opts, {}, {}, {}))
|
2014-06-21 02:00:10 +00:00
|
|
|
|
|
|
|
def test_renderers(self):
|
|
|
|
'''
|
|
|
|
Test that renderers have:
|
2015-02-05 00:40:56 +00:00
|
|
|
- __salt__ # Execution functions (i.e. __salt__['test.echo']('foo'))
|
|
|
|
- __grains__ # Grains (i.e. __grains__['os'])
|
|
|
|
- __pillar__ # Pillar data (i.e. __pillar__['foo'])
|
|
|
|
- __opts__ # Minion configuration options
|
|
|
|
- __context__ # Context dict shared amongst all modules of the same type
|
2014-06-21 02:00:10 +00:00
|
|
|
'''
|
|
|
|
self._verify_globals(salt.loader.render(self.master_opts, {}))
|