Merge pull request #54780 from dwoz/fix-masterless-jinja-imports

Fix masterless jinja imports
This commit is contained in:
Daniel Wozniak 2019-09-29 15:12:47 -07:00 committed by GitHub
commit b9459e6a96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 10 deletions

View file

@ -56,16 +56,18 @@ class SaltCacheLoader(BaseLoader):
and only loaded once per loader instance.
'''
_cached_pillar_client = None
_cached_client = None
@classmethod
def shutdown(cls):
if cls._cached_client is None:
return
# PillarClient and LocalClient objects do not have a destroy method
if hasattr(cls._cached_client, 'destroy'):
cls._cached_client.destroy()
cls._cached_client = None
for attr in ('_cached_client', '_cached_pillar_client'):
client = getattr(cls, attr, None)
if client is not None:
# PillarClient and LocalClient objects do not have a destroy method
if hasattr(client, 'destroy'):
client.destroy()
setattr(cls, attr, None)
def __init__(self, opts, saltenv='base', encoding='utf-8',
pillar_rend=False, _file_client=None):
@ -94,10 +96,12 @@ class SaltCacheLoader(BaseLoader):
# and use that. This avoids opening a new file_client every time this
# class is instantiated
if self._file_client is None:
if not SaltCacheLoader._cached_client:
SaltCacheLoader._cached_client = salt.fileclient.get_file_client(
self.opts, self.pillar_rend)
self._file_client = SaltCacheLoader._cached_client
attr = '_cached_pillar_client' if self.pillar_rend else '_cached_client'
cached_client = getattr(self, attr, None)
if cached_client is None:
cached_client = salt.fileclient.get_file_client(self.opts, self.pillar_rend)
setattr(SaltCacheLoader, attr, cached_client)
self._file_client = cached_client
return self._file_client
def cache_file(self, template):

View file

@ -0,0 +1 @@
{% set defaults = {'foo': 'bar'} %}

View file

@ -0,0 +1,6 @@
{% from "issue-54765-map.jinja" import defaults with context %}
issue-54765:
file.managed:
- name: {{ pillar['file_path'] }}
- contents: {{ defaults['foo'] }}

View file

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import os
import salt.utils.files
from tests.support.case import ModuleCase, ShellCase
from tests.support.helpers import with_tempdir
class JinjaRendererTest(ModuleCase):
@with_tempdir()
def test_issue_54765(self, tmpdir):
file_path = os.path.join(tmpdir, 'issue-54765')
ret = self.run_function('state.sls', mods='issue-54765', pillar={'file_path': file_path})
key = 'file_|-issue-54765_|-{}_|-managed'.format(file_path)
assert key in ret
assert ret[key]['result'] is True
with salt.utils.files.fopen(file_path, 'r') as fp:
assert fp.read().strip() == 'bar'
class JinjaRenderCallTest(ShellCase):
@with_tempdir()
def test_issue_54765(self, tmpdir):
file_path = os.path.join(tmpdir, 'issue-54765')
pillar_str = '\'{{"file_path": "{}"}}\''.format(file_path)
ret = self.run_call('state.apply issue-54765 pillar={}'.format(pillar_str), local=True)
assert ' Result: True' in ret
with salt.utils.files.fopen(file_path, 'r') as fp:
assert fp.read().strip() == 'bar'