mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
commit
1ea2885ec2
1 changed files with 65 additions and 26 deletions
|
@ -22,6 +22,7 @@ import salt.config
|
|||
import salt.ext.six as six
|
||||
import salt.loader
|
||||
import salt.utils
|
||||
import salt.utils.files
|
||||
from salt.exceptions import SaltRenderError
|
||||
from salt.ext.six.moves import builtins
|
||||
from salt.utils import get_context
|
||||
|
@ -43,6 +44,7 @@ except ImportError:
|
|||
HAS_TIMELIB = False
|
||||
|
||||
TEMPLATES_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
BLINESEP = salt.utils.to_bytes(os.linesep)
|
||||
|
||||
|
||||
class MockFileClient(object):
|
||||
|
@ -63,18 +65,48 @@ class MockFileClient(object):
|
|||
})
|
||||
|
||||
|
||||
def _setup_test_dir(src_dir, test_dir):
|
||||
os.makedirs(test_dir)
|
||||
salt.utils.files.recursive_copy(src_dir, test_dir)
|
||||
filename = os.path.join(test_dir, 'non_ascii')
|
||||
with salt.utils.fopen(filename, 'wb') as fp:
|
||||
fp.write(b'Assun\xc3\xa7\xc3\xa3o' + BLINESEP)
|
||||
filename = os.path.join(test_dir, 'hello_simple')
|
||||
with salt.utils.fopen(filename, 'wb') as fp:
|
||||
fp.write(b'world' + BLINESEP)
|
||||
filename = os.path.join(test_dir, 'hello_import')
|
||||
lines = [
|
||||
r"{% from 'macro' import mymacro -%}",
|
||||
r"{% from 'macro' import mymacro -%}",
|
||||
r"{{ mymacro('Hey') ~ mymacro(a|default('a'), b|default('b')) }}",
|
||||
]
|
||||
with salt.utils.fopen(filename, 'wb') as fp:
|
||||
for line in lines:
|
||||
fp.write(line.encode('utf-8') + BLINESEP)
|
||||
|
||||
|
||||
class TestSaltCacheLoader(TestCase):
|
||||
def __init__(self, *args, **kws):
|
||||
super(TestSaltCacheLoader, self).__init__(*args, **kws)
|
||||
|
||||
def setUp(self):
|
||||
self.TEMPDIR = tempfile.mkdtemp()
|
||||
self.TEMPLATES_DIR = os.path.join(self.TEMPDIR, 'files', 'test')
|
||||
_setup_test_dir(
|
||||
os.path.join(TEMPLATES_DIR, 'files', 'test'),
|
||||
self.TEMPLATES_DIR
|
||||
)
|
||||
self.opts = {
|
||||
'cachedir': TEMPLATES_DIR,
|
||||
'cachedir': self.TEMPDIR,
|
||||
'file_roots': {
|
||||
'test': [os.path.join(TEMPLATES_DIR, 'files', 'test')]
|
||||
'test': [self.TEMPLATES_DIR]
|
||||
},
|
||||
'pillar_roots': {
|
||||
'test': [os.path.join(TEMPLATES_DIR, 'files', 'test')]
|
||||
'test': [self.TEMPLATES_DIR]
|
||||
}
|
||||
}
|
||||
super(TestSaltCacheLoader, self).setUp()
|
||||
|
||||
def tearDown(self):
|
||||
salt.utils.rm_rf(self.TEMPDIR)
|
||||
|
||||
def test_searchpath(self):
|
||||
'''
|
||||
|
@ -96,7 +128,7 @@ class TestSaltCacheLoader(TestCase):
|
|||
assert len(res) == 3
|
||||
# res[0] on Windows is unicode and use os.linesep so it works cross OS
|
||||
self.assertEqual(str(res[0]), 'world' + os.linesep)
|
||||
tmpl_dir = os.path.join(TEMPLATES_DIR, 'files', 'test', 'hello_simple')
|
||||
tmpl_dir = os.path.join(self.TEMPLATES_DIR, 'hello_simple')
|
||||
self.assertEqual(res[1], tmpl_dir)
|
||||
assert res[2](), 'Template up to date?'
|
||||
assert len(loader._file_client.requests)
|
||||
|
@ -155,18 +187,24 @@ class TestSaltCacheLoader(TestCase):
|
|||
|
||||
|
||||
class TestGetTemplate(TestCase):
|
||||
def __init__(self, *args, **kws):
|
||||
super(TestGetTemplate, self).__init__(*args, **kws)
|
||||
|
||||
def setUp(self):
|
||||
self.TEMPDIR = tempfile.mkdtemp()
|
||||
self.TEMPLATES_DIR = os.path.join(self.TEMPDIR, 'files', 'test')
|
||||
_setup_test_dir(
|
||||
os.path.join(TEMPLATES_DIR, 'files', 'test'),
|
||||
self.TEMPLATES_DIR
|
||||
)
|
||||
self.local_opts = {
|
||||
'cachedir': TEMPLATES_DIR,
|
||||
'cachedir': self.TEMPDIR,
|
||||
'file_client': 'local',
|
||||
'file_ignore_regex': None,
|
||||
'file_ignore_glob': None,
|
||||
'file_roots': {
|
||||
'test': [os.path.join(TEMPLATES_DIR, 'files', 'test')]
|
||||
'test': [self.TEMPLATES_DIR]
|
||||
},
|
||||
'pillar_roots': {
|
||||
'test': [os.path.join(TEMPLATES_DIR, 'files', 'test')]
|
||||
'test': [self.TEMPLATES_DIR]
|
||||
},
|
||||
'fileserver_backend': ['roots'],
|
||||
'hash_type': 'md5',
|
||||
|
@ -176,13 +214,17 @@ class TestGetTemplate(TestCase):
|
|||
}
|
||||
self.local_salt = {
|
||||
}
|
||||
super(TestGetTemplate, self).setUp()
|
||||
|
||||
def tearDown(self):
|
||||
salt.utils.rm_rf(self.TEMPDIR)
|
||||
|
||||
def test_fallback(self):
|
||||
'''
|
||||
A Template with a filesystem loader is returned as fallback
|
||||
if the file is not contained in the searchpath
|
||||
'''
|
||||
fn_ = os.path.join(TEMPLATES_DIR, 'files', 'test', 'hello_simple')
|
||||
fn_ = os.path.join(self.TEMPLATES_DIR, 'hello_simple')
|
||||
with salt.utils.fopen(fn_) as fp_:
|
||||
out = render_jinja_tmpl(
|
||||
fp_.read(),
|
||||
|
@ -194,7 +236,7 @@ class TestGetTemplate(TestCase):
|
|||
A Template with a filesystem loader is returned as fallback
|
||||
if the file is not contained in the searchpath
|
||||
'''
|
||||
filename = os.path.join(TEMPLATES_DIR, 'files', 'test', 'hello_import')
|
||||
filename = os.path.join(self.TEMPLATES_DIR, 'hello_import')
|
||||
with salt.utils.fopen(filename) as fp_:
|
||||
out = render_jinja_tmpl(
|
||||
fp_.read(),
|
||||
|
@ -210,11 +252,11 @@ class TestGetTemplate(TestCase):
|
|||
'''
|
||||
fc = MockFileClient()
|
||||
with patch.object(SaltCacheLoader, 'file_client', MagicMock(return_value=fc)):
|
||||
filename = os.path.join(TEMPLATES_DIR, 'files', 'test', 'hello_import')
|
||||
filename = os.path.join(self.TEMPLATES_DIR, 'hello_import')
|
||||
with salt.utils.fopen(filename) as fp_:
|
||||
out = render_jinja_tmpl(
|
||||
fp_.read(),
|
||||
dict(opts={'cachedir': TEMPLATES_DIR, 'file_client': 'remote',
|
||||
dict(opts={'cachedir': self.TEMPDIR, 'file_client': 'remote',
|
||||
'file_roots': self.local_opts['file_roots'],
|
||||
'pillar_roots': self.local_opts['pillar_roots']},
|
||||
a='Hi', b='Salt', saltenv='test', salt=self.local_salt))
|
||||
|
@ -233,8 +275,7 @@ class TestGetTemplate(TestCase):
|
|||
\{\{ 1/0 \}\} <======================
|
||||
\{%- endmacro %\}
|
||||
---.*'''
|
||||
filename = os.path.join(TEMPLATES_DIR,
|
||||
'files', 'test', 'hello_import_generalerror')
|
||||
filename = os.path.join(self.TEMPLATES_DIR, 'hello_import_generalerror')
|
||||
fc = MockFileClient()
|
||||
with patch.object(SaltCacheLoader, 'file_client', MagicMock(return_value=fc)):
|
||||
with salt.utils.fopen(filename) as fp_:
|
||||
|
@ -257,8 +298,7 @@ class TestGetTemplate(TestCase):
|
|||
\{\{b.greetee\}\} <-- error is here <======================
|
||||
\{%- endmacro %\}
|
||||
---'''
|
||||
filename = os.path.join(TEMPLATES_DIR,
|
||||
'files', 'test', 'hello_import_undefined')
|
||||
filename = os.path.join(self.TEMPLATES_DIR, 'hello_import_undefined')
|
||||
fc = MockFileClient()
|
||||
with patch.object(SaltCacheLoader, 'file_client', MagicMock(return_value=fc)):
|
||||
with salt.utils.fopen(filename) as fp_:
|
||||
|
@ -281,8 +321,7 @@ class TestGetTemplate(TestCase):
|
|||
\{\{ greeting ~ ' ' ~ greetee \}\} !
|
||||
\{%- endmacro %\}
|
||||
---.*'''
|
||||
filename = os.path.join(TEMPLATES_DIR,
|
||||
'files', 'test', 'hello_import_error')
|
||||
filename = os.path.join(self.TEMPLATES_DIR, 'hello_import_error')
|
||||
fc = MockFileClient()
|
||||
with patch.object(SaltCacheLoader, 'file_client', MagicMock(return_value=fc)):
|
||||
with salt.utils.fopen(filename) as fp_:
|
||||
|
@ -296,22 +335,22 @@ class TestGetTemplate(TestCase):
|
|||
def test_non_ascii_encoding(self):
|
||||
fc = MockFileClient()
|
||||
with patch.object(SaltCacheLoader, 'file_client', MagicMock(return_value=fc)):
|
||||
filename = os.path.join(TEMPLATES_DIR, 'files', 'test', 'hello_import')
|
||||
filename = os.path.join(self.TEMPLATES_DIR, 'hello_import')
|
||||
with salt.utils.fopen(filename) as fp_:
|
||||
out = render_jinja_tmpl(
|
||||
fp_.read(),
|
||||
dict(opts={'cachedir': TEMPLATES_DIR, 'file_client': 'remote',
|
||||
dict(opts={'cachedir': self.TEMPDIR, 'file_client': 'remote',
|
||||
'file_roots': self.local_opts['file_roots'],
|
||||
'pillar_roots': self.local_opts['pillar_roots']},
|
||||
a='Hi', b='Sàlt', saltenv='test', salt=self.local_salt))
|
||||
self.assertEqual(out, u'Hey world !Hi Sàlt !' + os.linesep)
|
||||
self.assertEqual(fc.requests[0]['path'], 'salt://macro')
|
||||
|
||||
filename = os.path.join(TEMPLATES_DIR, 'files', 'test', 'non_ascii')
|
||||
filename = os.path.join(self.TEMPLATES_DIR, 'non_ascii')
|
||||
with salt.utils.fopen(filename, mode='rb') as fp_:
|
||||
out = render_jinja_tmpl(
|
||||
salt.utils.to_unicode(fp_.read(), 'utf-8'),
|
||||
dict(opts={'cachedir': TEMPLATES_DIR, 'file_client': 'remote',
|
||||
dict(opts={'cachedir': self.TEMPDIR, 'file_client': 'remote',
|
||||
'file_roots': self.local_opts['file_roots'],
|
||||
'pillar_roots': self.local_opts['pillar_roots']},
|
||||
a='Hi', b='Sàlt', saltenv='test', salt=self.local_salt))
|
||||
|
@ -345,7 +384,7 @@ class TestGetTemplate(TestCase):
|
|||
self.assertEqual(response, '02')
|
||||
|
||||
def test_non_ascii(self):
|
||||
fn = os.path.join(TEMPLATES_DIR, 'files', 'test', 'non_ascii')
|
||||
fn = os.path.join(self.TEMPLATES_DIR, 'non_ascii')
|
||||
out = JINJA(fn, opts=self.local_opts, saltenv='test')
|
||||
with salt.utils.fopen(out['data'], mode='rb') as fp:
|
||||
result = salt.utils.to_unicode(fp.read(), 'utf-8')
|
||||
|
|
Loading…
Add table
Reference in a new issue