diff --git a/salt/loader/lazy.py b/salt/loader/lazy.py index c1bcace010f4..9a4a8f8bf381 100644 --- a/salt/loader/lazy.py +++ b/salt/loader/lazy.py @@ -145,19 +145,24 @@ def __getattr__(self, name): def __call__(self, *args, **kwargs): run_func = self.func mod = sys.modules[run_func.__module__] - if isinstance(mod.__opts__, salt.loader.context.NamedLoaderContext): - if mod.__opts__.value() is not None: - mod.__opts__.value()["test"] = self.loader.opts["test"] - else: - mod.__opts__["test"] = self.loader.opts["test"] + # All modules we've imported should have __opts__ defined. There are + # cases in the test suite where mod ends up being something other than + # a module we've loaded. + if hasattr(mod, "__opts__"): + if isinstance(mod.__opts__, salt.loader.context.NamedLoaderContext): + if mod.__opts__.value() is not None: + mod.__opts__.value()["test"] = self.loader.opts["test"] + else: + mod.__opts__["test"] = self.loader.opts["test"] if self.loader.inject_globals: run_func = global_injector_decorator(self.loader.inject_globals)(run_func) ret = self.loader.run(run_func, *args, **kwargs) - if isinstance(mod.__opts__, salt.loader.context.NamedLoaderContext): - if mod.__opts__.value() is not None: + if hasattr(mod, "__opts__"): + if isinstance(mod.__opts__, salt.loader.context.NamedLoaderContext): + if mod.__opts__.value() is not None: + self.loader.opts["test"] = mod.__opts__["test"] + else: self.loader.opts["test"] = mod.__opts__["test"] - else: - self.loader.opts["test"] = mod.__opts__["test"] return ret def __repr__(self):