diff --git a/salt/client/ssh/__init__.py b/salt/client/ssh/__init__.py index 278fd5887ab9..1f5ec1c9e73e 100644 --- a/salt/client/ssh/__init__.py +++ b/salt/client/ssh/__init__.py @@ -1,7 +1,6 @@ """ Create ssh executor system """ -# Import python libs import base64 import binascii @@ -28,8 +27,6 @@ import salt.loader import salt.log import salt.minion - -# Import salt libs import salt.output import salt.roster import salt.serializers.yaml @@ -46,8 +43,6 @@ import salt.utils.thin import salt.utils.url import salt.utils.verify - -# Import 3rd-party libs from salt.ext import six from salt.ext.six.moves import input # pylint: disable=import-error,redefined-builtin from salt.template import compile_template @@ -1128,6 +1123,7 @@ def run_wfunc(self): minion_opts=self.minion_opts, **self.target ) + opts_pkg = pre_wrapper["test.opts_pkg"]() # pylint: disable=E1102 if "_error" in opts_pkg: # Refresh failed @@ -1141,6 +1137,8 @@ def run_wfunc(self): opts_pkg["extension_modules"] = self.opts["extension_modules"] opts_pkg["module_dirs"] = self.opts["module_dirs"] opts_pkg["_ssh_version"] = self.opts["_ssh_version"] + opts_pkg["thin_dir"] = self.opts["thin_dir"] + opts_pkg["master_tops"] = self.opts["master_tops"] opts_pkg["__master_opts__"] = self.context["master_opts"] if "known_hosts_file" in self.opts: opts_pkg["known_hosts_file"] = self.opts["known_hosts_file"] @@ -1203,6 +1201,7 @@ def run_wfunc(self): minion_opts=self.minion_opts, **self.target ) + wrapper.fsclient.opts["cachedir"] = opts["cachedir"] self.wfuncs = salt.loader.ssh_wrapper(opts, wrapper, self.context) wrapper.wfuncs = self.wfuncs diff --git a/salt/client/ssh/wrapper/state.py b/salt/client/ssh/wrapper/state.py index a5eee0e43de5..bbe8385ae08f 100644 --- a/salt/client/ssh/wrapper/state.py +++ b/salt/client/ssh/wrapper/state.py @@ -1,10 +1,7 @@ -# -*- coding: utf-8 -*- """ Create ssh executor system """ -from __future__ import absolute_import, print_function -# Import python libs import logging import os import time @@ -25,13 +22,8 @@ import salt.utils.platform import salt.utils.state import salt.utils.thin - -# Import salt libs from salt.exceptions import SaltInvocationError -# Import 3rd-party libs -from salt.ext import six - __func_alias__ = {"apply_": "apply"} log = logging.getLogger(__name__) @@ -51,7 +43,7 @@ def _ssh_state(chunks, st_kwargs, kwargs, test=False): __context__["fileclient"], chunks, file_refs, __pillar__, st_kwargs["id_"] ) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, __opts__["hash_type"]) - cmd = "state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}".format( + cmd = "state.pkg {}/salt_state.tgz test={} pkg_sum={} hash_type={}".format( __opts__["thin_dir"], test, trans_tar_sum, __opts__["hash_type"] ) single = salt.client.ssh.Single( @@ -61,13 +53,13 @@ def _ssh_state(chunks, st_kwargs, kwargs, test=False): minion_opts=__salt__.minion_opts, **st_kwargs ) - single.shell.send(trans_tar, "{0}/salt_state.tgz".format(__opts__["thin_dir"])) + single.shell.send(trans_tar, "{}/salt_state.tgz".format(__opts__["thin_dir"])) stdout, stderr, _ = single.cmd_block() # Clean up our tar try: os.remove(trans_tar) - except (OSError, IOError): + except OSError: pass # Read in the JSON data and return the data structure @@ -130,7 +122,7 @@ def _merge_extra_filerefs(*args): """ ret = [] for arg in args: - if isinstance(arg, six.string_types): + if isinstance(arg, str): if arg: ret.extend(arg.split(",")) elif isinstance(arg, list): @@ -154,7 +146,7 @@ def _cleanup_slsmod_high_data(high_data): Set "slsmod" keys to None to make high_data JSON serializable """ - for i in six.itervalues(high_data): + for i in high_data.values(): if "stateconf" in i: stateconf_data = i["stateconf"][1] if "slsmod" in stateconf_data: @@ -165,7 +157,7 @@ def _parse_mods(mods): """ Parse modules. """ - if isinstance(mods, six.string_types): + if isinstance(mods, str): mods = [item.strip() for item in mods.split(",") if item.strip()] return mods @@ -184,9 +176,9 @@ def sls(mods, saltenv="base", test=None, exclude=None, **kwargs): ) st_.push_active() mods = _parse_mods(mods) - high_data, errors = st_.render_highstate({saltenv: mods}) + high_data, errors = st_.render_highstate({saltenv: mods}, context=__context__) if exclude: - if isinstance(exclude, six.string_types): + if isinstance(exclude, str): exclude = exclude.split(",") if "__exclude__" in high_data: high_data["__exclude__"].extend(exclude) @@ -226,7 +218,7 @@ def sls(mods, saltenv="base", test=None, exclude=None, **kwargs): roster_grains, ) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts["hash_type"]) - cmd = "state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}".format( + cmd = "state.pkg {}/salt_state.tgz test={} pkg_sum={} hash_type={}".format( opts["thin_dir"], test, trans_tar_sum, opts["hash_type"] ) single = salt.client.ssh.Single( @@ -236,13 +228,13 @@ def sls(mods, saltenv="base", test=None, exclude=None, **kwargs): minion_opts=__salt__.minion_opts, **st_kwargs ) - single.shell.send(trans_tar, "{0}/salt_state.tgz".format(opts["thin_dir"])) + single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"])) stdout, stderr, _ = single.cmd_block() # Clean up our tar try: os.remove(trans_tar) - except (OSError, IOError): + except OSError: pass # Read in the JSON data and return the data structure @@ -250,7 +242,7 @@ def sls(mods, saltenv="base", test=None, exclude=None, **kwargs): return salt.utils.json.loads(stdout) except Exception as e: # pylint: disable=broad-except log.error("JSON Render failed for: %s\n%s", stdout, stderr) - log.error(six.text_type(e)) + log.error(str(e)) # If for some reason the json load fails, return the stdout return stdout @@ -274,8 +266,8 @@ def running(concurrent=False): active = __salt__["saltutil.is_running"]("state.*") for data in active: err = ( - 'The function "{0}" is running as PID {1} and was started at ' - "{2} with jid {3}" + 'The function "{}" is running as PID {} and was started at ' + "{} with jid {}" ).format( data["fun"], data["pid"], @@ -368,7 +360,7 @@ def low(data, **kwargs): roster_grains, ) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, __opts__["hash_type"]) - cmd = "state.pkg {0}/salt_state.tgz pkg_sum={1} hash_type={2}".format( + cmd = "state.pkg {}/salt_state.tgz pkg_sum={} hash_type={}".format( __opts__["thin_dir"], trans_tar_sum, __opts__["hash_type"] ) single = salt.client.ssh.Single( @@ -378,13 +370,13 @@ def low(data, **kwargs): minion_opts=__salt__.minion_opts, **st_kwargs ) - single.shell.send(trans_tar, "{0}/salt_state.tgz".format(__opts__["thin_dir"])) + single.shell.send(trans_tar, "{}/salt_state.tgz".format(__opts__["thin_dir"])) stdout, stderr, _ = single.cmd_block() # Clean up our tar try: os.remove(trans_tar) - except (OSError, IOError): + except OSError: pass # Read in the JSON data and return the data structure @@ -392,7 +384,7 @@ def low(data, **kwargs): return salt.utils.json.loads(stdout) except Exception as e: # pylint: disable=broad-except log.error("JSON Render failed for: %s\n%s", stdout, stderr) - log.error(six.text_type(e)) + log.error(str(e)) # If for some reason the json load fails, return the stdout return stdout @@ -454,7 +446,7 @@ def high(data, **kwargs): roster_grains, ) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts["hash_type"]) - cmd = "state.pkg {0}/salt_state.tgz pkg_sum={1} hash_type={2}".format( + cmd = "state.pkg {}/salt_state.tgz pkg_sum={} hash_type={}".format( opts["thin_dir"], trans_tar_sum, opts["hash_type"] ) single = salt.client.ssh.Single( @@ -464,13 +456,13 @@ def high(data, **kwargs): minion_opts=__salt__.minion_opts, **st_kwargs ) - single.shell.send(trans_tar, "{0}/salt_state.tgz".format(opts["thin_dir"])) + single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"])) stdout, stderr, _ = single.cmd_block() # Clean up our tar try: os.remove(trans_tar) - except (OSError, IOError): + except OSError: pass # Read in the JSON data and return the data structure @@ -478,7 +470,7 @@ def high(data, **kwargs): return salt.utils.json.loads(stdout) except Exception as e: # pylint: disable=broad-except log.error("JSON Render failed for: %s\n%s", stdout, stderr) - log.error(six.text_type(e)) + log.error(str(e)) # If for some reason the json load fails, return the stdout return stdout @@ -539,10 +531,10 @@ def request(mods=None, **kwargs): try: if salt.utils.platform.is_windows(): # Make sure cache file isn't read-only - __salt__["cmd.run"]('attrib -R "{0}"'.format(notify_path)) + __salt__["cmd.run"]('attrib -R "{}"'.format(notify_path)) with salt.utils.files.fopen(notify_path, "w+b") as fp_: serial.dump(req, fp_) - except (IOError, OSError): + except OSError: log.error( "Unable to write state request file %s. Check permission.", notify_path ) @@ -593,7 +585,7 @@ def clear_request(name=None): if not name: try: os.remove(notify_path) - except (IOError, OSError): + except OSError: pass else: req = check_request() @@ -605,10 +597,10 @@ def clear_request(name=None): try: if salt.utils.platform.is_windows(): # Make sure cache file isn't read-only - __salt__["cmd.run"]('attrib -R "{0}"'.format(notify_path)) + __salt__["cmd.run"]('attrib -R "{}"'.format(notify_path)) with salt.utils.files.fopen(notify_path, "w+b") as fp_: serial.dump(req, fp_) - except (IOError, OSError): + except OSError: log.error( "Unable to write state request file %s. Check permission.", notify_path, @@ -641,7 +633,7 @@ def run_request(name="default", **kwargs): ret = apply_(n_req["mods"], **n_req["kwargs"]) try: os.remove(os.path.join(__opts__["cachedir"], "req_state.p")) - except (IOError, OSError): + except OSError: pass return ret return {} @@ -695,7 +687,7 @@ def highstate(test=None, **kwargs): roster_grains, ) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts["hash_type"]) - cmd = "state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}".format( + cmd = "state.pkg {}/salt_state.tgz test={} pkg_sum={} hash_type={}".format( opts["thin_dir"], test, trans_tar_sum, opts["hash_type"] ) single = salt.client.ssh.Single( @@ -705,13 +697,13 @@ def highstate(test=None, **kwargs): minion_opts=__salt__.minion_opts, **st_kwargs ) - single.shell.send(trans_tar, "{0}/salt_state.tgz".format(opts["thin_dir"])) + single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"])) stdout, stderr, _ = single.cmd_block() # Clean up our tar try: os.remove(trans_tar) - except (OSError, IOError): + except OSError: pass # Read in the JSON data and return the data structure @@ -719,7 +711,7 @@ def highstate(test=None, **kwargs): return salt.utils.json.loads(stdout) except Exception as e: # pylint: disable=broad-except log.error("JSON Render failed for: %s\n%s", stdout, stderr) - log.error(six.text_type(e)) + log.error(str(e)) # If for some reason the json load fails, return the stdout return stdout @@ -772,7 +764,7 @@ def top(topfn, test=None, **kwargs): roster_grains, ) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts["hash_type"]) - cmd = "state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}".format( + cmd = "state.pkg {}/salt_state.tgz test={} pkg_sum={} hash_type={}".format( opts["thin_dir"], test, trans_tar_sum, opts["hash_type"] ) single = salt.client.ssh.Single( @@ -782,13 +774,13 @@ def top(topfn, test=None, **kwargs): minion_opts=__salt__.minion_opts, **st_kwargs ) - single.shell.send(trans_tar, "{0}/salt_state.tgz".format(opts["thin_dir"])) + single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"])) stdout, stderr, _ = single.cmd_block() # Clean up our tar try: os.remove(trans_tar) - except (OSError, IOError): + except OSError: pass # Read in the JSON data and return the data structure @@ -796,7 +788,7 @@ def top(topfn, test=None, **kwargs): return salt.utils.json.loads(stdout) except Exception as e: # pylint: disable=broad-except log.error("JSON Render failed for: %s\n%s", stdout, stderr) - log.error(six.text_type(e)) + log.error(str(e)) # If for some reason the json load fails, return the stdout return stdout @@ -918,8 +910,8 @@ def sls_id(id_, mods, test=None, queue=False, **kwargs): if not chunk: raise SaltInvocationError( - "No matches for ID '{0}' found in SLS '{1}' within saltenv " - "'{2}'".format(id_, mods, opts["saltenv"]) + "No matches for ID '{}' found in SLS '{}' within saltenv " + "'{}'".format(id_, mods, opts["saltenv"]) ) ret = _ssh_state(chunk, st_kwargs, kwargs, test=test) @@ -1116,7 +1108,7 @@ def single(fun, name, test=None, **kwargs): trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts["hash_type"]) # We use state.pkg to execute the "state package" - cmd = "state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}".format( + cmd = "state.pkg {}/salt_state.tgz test={} pkg_sum={} hash_type={}".format( opts["thin_dir"], test, trans_tar_sum, opts["hash_type"] ) @@ -1130,7 +1122,7 @@ def single(fun, name, test=None, **kwargs): ) # Copy the tar down - single.shell.send(trans_tar, "{0}/salt_state.tgz".format(opts["thin_dir"])) + single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"])) # Run the state.pkg command on the target stdout, stderr, _ = single.cmd_block() @@ -1138,7 +1130,7 @@ def single(fun, name, test=None, **kwargs): # Clean up our tar try: os.remove(trans_tar) - except (OSError, IOError): + except OSError: pass # Read in the JSON data and return the data structure @@ -1146,7 +1138,7 @@ def single(fun, name, test=None, **kwargs): return salt.utils.json.loads(stdout) except Exception as e: # pylint: disable=broad-except log.error("JSON Render failed for: %s\n%s", stdout, stderr) - log.error(six.text_type(e)) + log.error(str(e)) # If for some reason the json load fails, return the stdout return stdout diff --git a/salt/loader.py b/salt/loader.py index 22c5027813e3..c3f1307f230f 100644 --- a/salt/loader.py +++ b/salt/loader.py @@ -4,7 +4,6 @@ plugin interfaces used by Salt. """ -# Import python libs import functools import inspect @@ -20,7 +19,6 @@ from collections.abc import MutableMapping from zipimport import zipimporter -# Import salt libs import salt.config import salt.defaults.events import salt.defaults.exitcodes @@ -37,8 +35,6 @@ import salt.utils.stringutils import salt.utils.versions from salt.exceptions import LoaderError - -# Import 3rd-party libs from salt.ext import six from salt.ext.six.moves import reload_module from salt.template import check_render_pipe_str @@ -1209,7 +1205,10 @@ def __init__( self.module_dirs = module_dirs self.tag = tag - self.loaded_base_name = loaded_base_name or LOADED_BASE_NAME + if loaded_base_name: + self.loaded_base_name = loaded_base_name + else: + self.loaded_base_name = "{}_{}".format(LOADED_BASE_NAME, id(self)) self.mod_type_check = mod_type_check or _mod_type if "__context__" not in self.pack: @@ -1265,6 +1264,12 @@ def __init__( _generate_module("{}.ext".format(self.loaded_base_name)) _generate_module("{}.ext.{}".format(self.loaded_base_name, tag)) + def clean_modules(self): + for name in list(sys.modules): + if name.startswith(self.loaded_base_name): + mod = sys.modules.pop(name) + del mod + def __getitem__(self, item): """ Override the __getitem__ in order to decorate the returned function if we need diff --git a/salt/state.py b/salt/state.py index 99cca7b4fb47..2337e1b300ad 100644 --- a/salt/state.py +++ b/salt/state.py @@ -11,7 +11,6 @@ } """ -# Import python libs import copy import datetime @@ -26,8 +25,6 @@ import traceback import salt.fileclient - -# Import salt libs import salt.loader import salt.minion import salt.pillar @@ -51,7 +48,6 @@ import salt.utils.yamlloader as yamlloader from salt.exceptions import CommandExecutionError, SaltRenderError, SaltReqTimeoutError -# Import third party libs # pylint: disable=import-error,no-name-in-module,redefined-builtin from salt.ext.six.moves import map, range, reload_module from salt.serializers.msgpack import deserialize as msgpack_deserialize @@ -3920,7 +3916,7 @@ def load_dynamic(self, matches): self.state.opts["pillar"] = self.state._gather_pillar() self.state.module_refresh() - def render_state(self, sls, saltenv, mods, matches, local=False): + def render_state(self, sls, saltenv, mods, matches, local=False, context=None): """ Render a state file and retrieve all of the include states """ @@ -3953,6 +3949,7 @@ def render_state(self, sls, saltenv, mods, matches, local=False): saltenv, sls, rendered_sls=mods, + context=context, ) except SaltRenderError as exc: msg = "Rendering SLS '{}:{}' failed: {}".format(saltenv, sls, exc) @@ -4252,7 +4249,7 @@ def _handle_exclude(self, state, sls, saltenv, errors): errors.append(err) state.setdefault("__exclude__", []).extend(exc) - def render_highstate(self, matches): + def render_highstate(self, matches, context=None): """ Gather the state files and render them into a single unified salt high data structure. @@ -4283,7 +4280,9 @@ def render_highstate(self, matches): r_env = "{}:{}".format(saltenv, sls) if r_env in mods: continue - state, errors = self.render_state(sls, saltenv, mods, matches) + state, errors = self.render_state( + sls, saltenv, mods, matches, context=context + ) if state: self.merge_included_states(highstate, state, errors) for i, error in enumerate(errors[:]): diff --git a/salt/template.py b/salt/template.py index 1c95b6908c22..fa3d8e7884fc 100644 --- a/salt/template.py +++ b/salt/template.py @@ -1,26 +1,18 @@ -# -*- coding: utf-8 -*- """ Manage basic template commands """ -from __future__ import absolute_import, print_function, unicode_literals import codecs import logging import os - -# Import Python libs import time -# Import Salt libs import salt.utils.data import salt.utils.files import salt.utils.sanitizers import salt.utils.stringio import salt.utils.versions - -# Import 3rd-party libs -from salt.ext import six from salt.ext.six.moves import StringIO log = logging.getLogger(__name__) @@ -42,6 +34,7 @@ def compile_template( saltenv="base", sls="", input_data="", + context=None, **kwargs ): """ @@ -67,7 +60,7 @@ def compile_template( if template != ":string:": # Template was specified incorrectly - if not isinstance(template, six.string_types): + if not isinstance(template, str): log.error("Template was specified incorrectly: %s", template) return ret # Template does not exist @@ -99,6 +92,8 @@ def compile_template( if salt.utils.stringio.is_readable(input_data): input_data.seek(0) # pylint: disable=no-member render_kwargs = dict(renderers=renderers, tmplpath=template) + if context: + render_kwargs["context"] = context render_kwargs.update(kwargs) if argline: render_kwargs["argline"] = argline @@ -138,7 +133,7 @@ def compile_template( is_stringio = False contents = ret - if isinstance(contents, six.string_types): + if isinstance(contents, str): if "\r\n" not in contents: contents = contents.replace("\n", "\r\n") ret = StringIO(contents) if is_stringio else contents @@ -213,7 +208,7 @@ def template_shebang(template, renderers, default, blacklist, whitelist, input_d ): fmt, tmpl = comb.split("_") - OLD_STYLE_RENDERERS[comb] = "{0}|{1}".format(tmpl, fmt) + OLD_STYLE_RENDERERS[comb] = "{}|{}".format(tmpl, fmt) def check_render_pipe_str(pipestr, renderers, blacklist, whitelist): diff --git a/salt/utils/templates.py b/salt/utils/templates.py index 165baac7e3ba..3ced286a59e9 100644 --- a/salt/utils/templates.py +++ b/salt/utils/templates.py @@ -1,11 +1,8 @@ -# -*- coding: utf-8 -*- """ Template render systems """ -from __future__ import absolute_import, print_function, unicode_literals -# Import Python libs import codecs import logging import os @@ -13,11 +10,8 @@ import tempfile import traceback -# Import 3rd-party libs import jinja2 import jinja2.ext - -# Import Salt libs import salt.utils.data import salt.utils.dateutils import salt.utils.files @@ -56,7 +50,7 @@ SLS_ENCODER = codecs.getencoder(SLS_ENCODING) -class AliasedLoader(object): +class AliasedLoader: """ Light wrapper around the LazyLoader to redirect 'cmd.run' calls to 'cmd.shell', for easy use of shellisms during templating calls @@ -81,7 +75,7 @@ def __contains__(self, name): return name in self.wrapped -class AliasedModule(object): +class AliasedModule: """ Light wrapper around module objects returned by the LazyLoader's getattr for the purposes of `salt.cmd.run()` syntax in templates @@ -139,7 +133,7 @@ def render_tmpl( context["sls_path"] = slspath.replace("/", "_") context["slspath"] = slspath - if isinstance(tmplsrc, six.string_types): + if isinstance(tmplsrc, str): if from_str: tmplstr = tmplsrc else: @@ -148,7 +142,7 @@ def render_tmpl( tmplsrc = os.path.join(tmplpath, tmplsrc) with codecs.open(tmplsrc, "r", SLS_ENCODING) as _tmplsrc: tmplstr = _tmplsrc.read() - except (UnicodeDecodeError, ValueError, OSError, IOError) as exc: + except (UnicodeDecodeError, ValueError, OSError) as exc: if salt.utils.files.is_binary(tmplsrc): # Template is a bin file, return the raw file return dict(result=True, data=tmplsrc) @@ -158,7 +152,7 @@ def render_tmpl( exc, exc_info_on_loglevel=logging.DEBUG, ) - six.reraise(*sys.exc_info()) + raise else: # assume tmplsrc is file-like. tmplstr = tmplsrc.read() tmplsrc.close() @@ -270,14 +264,14 @@ def _get_jinja_error(trace, context=None): # error log place at the beginning if add_log: if template_path: - out = "\n{0}\n".format(msg.splitlines()[0]) + out = "\n{}\n".format(msg.splitlines()[0]) with salt.utils.files.fopen(template_path) as fp_: template_contents = salt.utils.stringutils.to_unicode(fp_.read()) out += salt.utils.stringutils.get_context( template_contents, line, marker=" <======================" ) else: - out = "\n{0}\n".format(msg) + out = "\n{}\n".format(msg) line = 0 return line, out @@ -287,8 +281,9 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None): saltenv = context["saltenv"] loader = None newline = False + file_client = context.get("fileclient", None) - if tmplstr and not isinstance(tmplstr, six.text_type): + if tmplstr and not isinstance(tmplstr, str): # https://jinja.palletsprojects.com/en/2.11.x/api/#unicode tmplstr = tmplstr.decode(SLS_ENCODING) @@ -302,7 +297,10 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None): loader = jinja2.FileSystemLoader(os.path.dirname(tmplpath)) else: loader = salt.utils.jinja.SaltCacheLoader( - opts, saltenv, pillar_rend=context.get("_pillar_rend", False) + opts, + saltenv, + pillar_rend=context.get("_pillar_rend", False), + _file_client=file_client, ) env_args = {"extensions": [], "loader": loader} @@ -341,7 +339,7 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None): opt_jinja_sls_env["lstrip_blocks"] = True def opt_jinja_env_helper(opts, optname): - for k, v in six.iteritems(opts): + for k, v in opts.items(): k = k.lower() if hasattr(jinja2.defaults, k.upper()): log.debug("Jinja2 environment %s was set to %s by %s", k, v, optname) @@ -378,8 +376,8 @@ def opt_jinja_env_helper(opts, optname): jinja_env.tests["list"] = salt.utils.data.is_list decoded_context = {} - for key, value in six.iteritems(context): - if not isinstance(value, six.string_types): + for key, value in context.items(): + if not isinstance(value, str): decoded_context[key] = value continue @@ -404,7 +402,7 @@ def opt_jinja_env_helper(opts, optname): tmplstr = "" # Don't include the line number, since it is misreported # https://github.com/mitsuhiko/jinja2/issues/276 - raise SaltRenderError("Jinja variable {0}{1}".format(exc, out), buf=tmplstr) + raise SaltRenderError("Jinja variable {}{}".format(exc, out), buf=tmplstr) except ( jinja2.exceptions.TemplateRuntimeError, jinja2.exceptions.TemplateSyntaxError, @@ -414,7 +412,7 @@ def opt_jinja_env_helper(opts, optname): if not line: tmplstr = "" raise SaltRenderError( - "Jinja syntax error: {0}{1}".format(exc, out), line, tmplstr + "Jinja syntax error: {}{}".format(exc, out), line, tmplstr ) except (SaltInvocationError, CommandExecutionError) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) @@ -422,7 +420,7 @@ def opt_jinja_env_helper(opts, optname): if not line: tmplstr = "" raise SaltRenderError( - "Problem running salt function in Jinja template: {0}{1}".format(exc, out), + "Problem running salt function in Jinja template: {}{}".format(exc, out), line, tmplstr, ) @@ -433,7 +431,7 @@ def opt_jinja_env_helper(opts, optname): if not line: tmplstr = "" else: - tmplstr += "\n{0}".format(tracestr) + tmplstr += "\n{}".format(tracestr) log.debug("Jinja Error") log.debug("Exception:", exc_info=True) log.debug("Out: %s", out) @@ -442,7 +440,7 @@ def opt_jinja_env_helper(opts, optname): log.debug("TraceStr: %s", tracestr) raise SaltRenderError( - "Jinja error: {0}{1}".format(exc, out), line, tmplstr, trace=tracestr + "Jinja error: {}{}".format(exc, out), line, tmplstr, trace=tracestr ) # Workaround a bug in Jinja that removes the final newline @@ -533,13 +531,13 @@ def render_cheetah_tmpl(tmplstr, context, tmplpath=None): data = tclass(namespaces=[context]) # Figure out which method to call based on the type of tmplstr - if six.PY3 and isinstance(tmplstr, six.string_types): + if six.PY3 and isinstance(tmplstr, str): # This should call .__unicode__() res = str(data) - elif six.PY2 and isinstance(tmplstr, six.text_type): + elif six.PY2 and isinstance(tmplstr, str): # Expicitly call .__unicode__() res = data.__unicode__() - elif isinstance(tmplstr, six.binary_type): + elif isinstance(tmplstr, bytes): # This should call .__str() res = str(data) else: @@ -589,7 +587,7 @@ def py(sfn, string=False, **kwargs): # pylint: disable=C0103 setattr(mod, "__env__", kwargs["saltenv"]) builtins = ["salt", "grains", "pillar", "opts"] for builtin in builtins: - arg = "__{0}__".format(builtin) + arg = "__{}__".format(builtin) setattr(mod, arg, kwargs[builtin]) for kwarg in kwargs: diff --git a/tests/support/events.py b/tests/support/events.py index 5d4bed19a41b..9e99de66b673 100644 --- a/tests/support/events.py +++ b/tests/support/events.py @@ -1,18 +1,14 @@ -# -*- coding: utf-8 -*- """ tests.support.events ~~~~~~~~~~~~~~~~~~~~ """ -# Import Python libs -from __future__ import absolute_import, unicode_literals import multiprocessing import os import time from contextlib import contextmanager -# Import Salt libs import salt.utils.event from salt.utils.process import clean_proc @@ -26,7 +22,7 @@ def eventpublisher_process(sock_dir): # Travis is slow time.sleep(10) else: - time.sleep(2) + time.sleep(8) yield finally: clean_proc(proc) @@ -34,7 +30,7 @@ def eventpublisher_process(sock_dir): class EventSender(multiprocessing.Process): def __init__(self, data, tag, wait, sock_dir): - super(EventSender, self).__init__() + super().__init__() self.data = data self.tag = tag self.wait = wait diff --git a/tests/unit/test_loader.py b/tests/unit/test_loader.py index d2d046a0b3a2..3a37145d58ed 100644 --- a/tests/unit/test_loader.py +++ b/tests/unit/test_loader.py @@ -26,8 +26,6 @@ from salt.ext import six from salt.ext.six.moves import range from tests.support.case import ModuleCase - -# Import Salt Testing libs from tests.support.helpers import slowTest from tests.support.mock import MagicMock, patch from tests.support.runtests import RUNTIME_VARS @@ -1240,6 +1238,45 @@ def test_reload(self): self._verify_libs() +class LoaderMultipleGlobalTest(ModuleCase): + """ + Tests when using multiple lazyloaders + """ + + def setUp(self): + opts = salt.config.minion_config(None) + self.loader1 = salt.loader.LazyLoader( + salt.loader._module_dirs(copy.deepcopy(opts), "modules", "module"), + copy.deepcopy(opts), + pack={}, + tag="module", + ) + self.loader2 = salt.loader.LazyLoader( + salt.loader._module_dirs(copy.deepcopy(opts), "modules", "module"), + copy.deepcopy(opts), + pack={}, + tag="module", + ) + + def tearDown(self): + del self.loader1 + del self.loader2 + + def test_loader_globals(self): + """ + Test to ensure loaders do not edit + each others loader's namespace + """ + self.loader1.pack["__foo__"] = "bar1" + func1 = self.loader1["test.ping"] + + self.loader2.pack["__foo__"] = "bar2" + func2 = self.loader2["test.ping"] + + assert func1.__globals__["__foo__"] == "bar1" + assert func2.__globals__["__foo__"] == "bar2" + + class LoaderGlobalsTest(ModuleCase): """ Test all of the globals that the loader is responsible for adding to modules