2013-07-02 10:16:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-11-18 18:57:10 +00:00
|
|
|
# Import python libs
|
2012-05-10 04:29:05 +00:00
|
|
|
import os
|
|
|
|
import tempfile
|
2012-11-18 18:57:10 +00:00
|
|
|
|
2013-06-27 13:10:26 +00:00
|
|
|
# Import Salt Testing libs
|
2013-07-02 10:16:47 +00:00
|
|
|
from salttesting import TestCase
|
2013-06-27 13:10:26 +00:00
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
2012-11-18 18:57:10 +00:00
|
|
|
# Import salt libs
|
|
|
|
import salt.utils
|
2012-11-12 22:08:14 +00:00
|
|
|
from salt.utils.jinja import SaltCacheLoader
|
|
|
|
from salt.utils.templates import render_jinja_tmpl
|
2013-06-24 22:53:59 +00:00
|
|
|
|
|
|
|
# Import 3rd party libs
|
|
|
|
from jinja2 import Environment
|
2012-01-29 15:01:43 +00:00
|
|
|
|
2012-05-10 04:29:05 +00:00
|
|
|
TEMPLATES_DIR = os.path.dirname(os.path.abspath(__file__))
|
2011-12-29 23:58:00 +00:00
|
|
|
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2011-12-29 23:58:00 +00:00
|
|
|
class MockFileClient(object):
|
|
|
|
'''
|
|
|
|
Does not download files but records any file request for testing
|
|
|
|
'''
|
|
|
|
def __init__(self, loader=None):
|
2012-05-29 16:40:20 +00:00
|
|
|
if loader:
|
|
|
|
loader._file_client = self
|
2011-12-29 23:58:00 +00:00
|
|
|
self.requests = []
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2011-12-29 23:58:00 +00:00
|
|
|
def get_file(self, template, dest='', makedirs=False, env='base'):
|
|
|
|
self.requests.append({
|
2012-05-10 04:29:05 +00:00
|
|
|
'path': template,
|
2011-12-29 23:58:00 +00:00
|
|
|
'dest': dest,
|
|
|
|
'makedirs': makedirs,
|
|
|
|
'env': env
|
|
|
|
})
|
|
|
|
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2012-01-29 15:01:43 +00:00
|
|
|
class TestSaltCacheLoader(TestCase):
|
2011-12-29 23:58:00 +00:00
|
|
|
def test_searchpath(self):
|
|
|
|
'''
|
|
|
|
The searchpath is based on the cachedir option and the env parameter
|
|
|
|
'''
|
2012-05-10 04:29:05 +00:00
|
|
|
tmp = tempfile.gettempdir()
|
|
|
|
loader = SaltCacheLoader({'cachedir': tmp}, env='test')
|
2013-01-14 07:58:27 +00:00
|
|
|
assert loader.searchpath == [os.path.join(tmp, 'files', 'test')]
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2011-12-29 23:58:00 +00:00
|
|
|
def test_mockclient(self):
|
|
|
|
'''
|
2012-06-30 20:10:34 +00:00
|
|
|
A MockFileClient is used that records all file requests normally sent
|
|
|
|
to the master.
|
2011-12-29 23:58:00 +00:00
|
|
|
'''
|
|
|
|
loader = SaltCacheLoader({'cachedir': TEMPLATES_DIR}, 'test')
|
|
|
|
fc = MockFileClient(loader)
|
|
|
|
res = loader.get_source(None, 'hello_simple')
|
|
|
|
assert len(res) == 3
|
2012-06-25 00:10:54 +00:00
|
|
|
# res[0] on Windows is unicode and use os.linesep so it works cross OS
|
|
|
|
self.assertEqual(str(res[0]), 'world' + os.linesep)
|
2012-05-10 04:29:05 +00:00
|
|
|
tmpl_dir = os.path.join(TEMPLATES_DIR, 'files', 'test', 'hello_simple')
|
|
|
|
self.assertEqual(res[1], tmpl_dir)
|
2013-06-24 22:53:59 +00:00
|
|
|
assert res[2](), 'Template up to date?'
|
2011-12-29 23:58:00 +00:00
|
|
|
assert len(fc.requests)
|
|
|
|
self.assertEqual(fc.requests[0]['path'], 'salt://hello_simple')
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2011-12-29 23:58:00 +00:00
|
|
|
def get_test_env(self):
|
|
|
|
'''
|
|
|
|
Setup a simple jinja test environment
|
|
|
|
'''
|
|
|
|
loader = SaltCacheLoader({'cachedir': TEMPLATES_DIR}, 'test')
|
|
|
|
fc = MockFileClient(loader)
|
|
|
|
jinja = Environment(loader=loader)
|
|
|
|
return fc, jinja
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2011-12-29 23:58:00 +00:00
|
|
|
def test_import(self):
|
|
|
|
'''
|
|
|
|
You can import and use macros from other files
|
|
|
|
'''
|
|
|
|
fc, jinja = self.get_test_env()
|
|
|
|
result = jinja.get_template('hello_import').render()
|
|
|
|
self.assertEqual(result, 'Hey world !a b !')
|
|
|
|
assert len(fc.requests) == 2
|
|
|
|
self.assertEqual(fc.requests[0]['path'], 'salt://hello_import')
|
|
|
|
self.assertEqual(fc.requests[1]['path'], 'salt://macro')
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2011-12-29 23:58:00 +00:00
|
|
|
def test_include(self):
|
|
|
|
'''
|
|
|
|
You can also include a template that imports and uses macros
|
|
|
|
'''
|
|
|
|
fc, jinja = self.get_test_env()
|
|
|
|
result = jinja.get_template('hello_include').render()
|
|
|
|
self.assertEqual(result, 'Hey world !a b !')
|
|
|
|
assert len(fc.requests) == 3
|
|
|
|
self.assertEqual(fc.requests[0]['path'], 'salt://hello_include')
|
|
|
|
self.assertEqual(fc.requests[1]['path'], 'salt://hello_import')
|
|
|
|
self.assertEqual(fc.requests[2]['path'], 'salt://macro')
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2011-12-29 23:58:00 +00:00
|
|
|
def test_include_context(self):
|
|
|
|
'''
|
|
|
|
Context variables are passes to the included template by default.
|
|
|
|
'''
|
|
|
|
_, jinja = self.get_test_env()
|
|
|
|
result = jinja.get_template('hello_include').render(a='Hi', b='Salt')
|
|
|
|
self.assertEqual(result, 'Hey world !Hi Salt !')
|
|
|
|
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2012-01-29 15:01:43 +00:00
|
|
|
class TestGetTemplate(TestCase):
|
2012-11-12 22:08:14 +00:00
|
|
|
def __init__(self, *args, **kws):
|
|
|
|
TestCase.__init__(self, *args, **kws)
|
|
|
|
self.local_opts = {
|
|
|
|
'cachedir': TEMPLATES_DIR,
|
|
|
|
'file_client': 'local',
|
|
|
|
'file_roots': {
|
2013-01-14 07:58:27 +00:00
|
|
|
'other': [os.path.join(TEMPLATES_DIR, 'files', 'test')]
|
2012-11-12 22:08:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-29 23:58:00 +00:00
|
|
|
def test_fallback(self):
|
|
|
|
'''
|
2012-09-27 21:39:49 +00:00
|
|
|
A Template with a filesystem loader is returned as fallback
|
2011-12-29 23:58:00 +00:00
|
|
|
if the file is not contained in the searchpath
|
|
|
|
'''
|
2012-11-14 03:32:55 +00:00
|
|
|
fn_ = os.path.join(TEMPLATES_DIR, 'files', 'test', 'hello_simple')
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(fn_) as fp_:
|
2012-11-14 03:32:55 +00:00
|
|
|
out = render_jinja_tmpl(
|
|
|
|
fp_.read(),
|
|
|
|
dict(opts=self.local_opts, env='other'))
|
2012-12-02 18:45:09 +00:00
|
|
|
self.assertEqual(out, 'world\n')
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2011-12-29 23:58:00 +00:00
|
|
|
def test_fallback_noloader(self):
|
|
|
|
'''
|
2012-09-27 21:39:49 +00:00
|
|
|
A Template with a filesystem loader is returned as fallback
|
|
|
|
if the file is not contained in the searchpath
|
2011-12-29 23:58:00 +00:00
|
|
|
'''
|
2012-05-10 04:29:05 +00:00
|
|
|
filename = os.path.join(TEMPLATES_DIR, 'files', 'test', 'hello_import')
|
2012-11-12 22:08:14 +00:00
|
|
|
out = render_jinja_tmpl(
|
2012-11-18 18:57:10 +00:00
|
|
|
salt.utils.fopen(filename).read(),
|
2012-11-12 22:08:14 +00:00
|
|
|
dict(opts=self.local_opts, env='other'))
|
2012-12-02 18:45:09 +00:00
|
|
|
self.assertEqual(out, 'Hey world !a b !\n')
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2011-12-29 23:58:00 +00:00
|
|
|
def test_env(self):
|
|
|
|
'''
|
|
|
|
If the template is within the searchpath it can
|
|
|
|
import, include and extend other templates.
|
|
|
|
The initial template is expected to be already cached
|
|
|
|
get_template does not request it from the master again.
|
|
|
|
'''
|
|
|
|
fc = MockFileClient()
|
|
|
|
# monkey patch file client
|
|
|
|
_fc = SaltCacheLoader.file_client
|
2012-05-10 04:29:05 +00:00
|
|
|
SaltCacheLoader.file_client = lambda loader: fc
|
|
|
|
filename = os.path.join(TEMPLATES_DIR, 'files', 'test', 'hello_import')
|
2012-11-12 22:08:14 +00:00
|
|
|
out = render_jinja_tmpl(
|
2012-11-18 18:57:10 +00:00
|
|
|
salt.utils.fopen(filename).read(),
|
2012-11-12 22:08:14 +00:00
|
|
|
dict(opts={'cachedir': TEMPLATES_DIR, 'file_client': 'remote'},
|
|
|
|
a='Hi', b='Salt', env='test'))
|
2012-12-02 18:45:09 +00:00
|
|
|
self.assertEqual(out, 'Hey world !Hi Salt !\n')
|
2011-12-29 23:58:00 +00:00
|
|
|
self.assertEqual(fc.requests[0]['path'], 'salt://macro')
|
|
|
|
SaltCacheLoader.file_client = _fc
|
2013-06-24 22:53:59 +00:00
|
|
|
|
2013-07-02 10:16:47 +00:00
|
|
|
def test_non_ascii_encoding(self):
|
|
|
|
fc = MockFileClient()
|
|
|
|
# monkey patch file client
|
|
|
|
_fc = SaltCacheLoader.file_client
|
|
|
|
SaltCacheLoader.file_client = lambda loader: fc
|
|
|
|
filename = os.path.join(TEMPLATES_DIR, 'files', 'test', 'hello_import')
|
|
|
|
out = render_jinja_tmpl(
|
|
|
|
salt.utils.fopen(filename).read(),
|
|
|
|
dict(opts={'cachedir': TEMPLATES_DIR, 'file_client': 'remote'},
|
|
|
|
a='Hi', b='Sàlt', env='test'))
|
|
|
|
self.assertEqual(out, 'Hey world !Hi Sàlt !\n')
|
|
|
|
self.assertEqual(fc.requests[0]['path'], 'salt://macro')
|
|
|
|
SaltCacheLoader.file_client = _fc
|
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests([TestSaltCacheLoader, TestGetTemplate], needs_daemon=False)
|