test_jinja.py 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. """
  2. Tests for salt.utils.jinja
  3. """
  4. import ast
  5. import builtins
  6. import copy
  7. import datetime
  8. import os
  9. import pprint
  10. import random
  11. import re
  12. import tempfile
  13. import salt.config
  14. import salt.loader
  15. # dateutils is needed so that the strftime jinja filter is loaded
  16. import salt.utils.dateutils # pylint: disable=unused-import
  17. import salt.utils.files
  18. import salt.utils.json
  19. import salt.utils.stringutils
  20. import salt.utils.yaml
  21. from jinja2 import DictLoader, Environment, Markup, exceptions
  22. from salt.exceptions import SaltRenderError
  23. from salt.utils.decorators.jinja import JinjaFilter
  24. from salt.utils.jinja import (
  25. SaltCacheLoader,
  26. SerializerExtension,
  27. ensure_sequence_filter,
  28. indent,
  29. tojson,
  30. )
  31. from salt.utils.odict import OrderedDict
  32. from salt.utils.templates import JINJA, render_jinja_tmpl
  33. from tests.support.case import ModuleCase
  34. from tests.support.helpers import requires_network
  35. from tests.support.mock import MagicMock, Mock, patch
  36. from tests.support.runtests import RUNTIME_VARS
  37. from tests.support.unit import TestCase, skipIf
  38. try:
  39. import timelib # pylint: disable=W0611
  40. HAS_TIMELIB = True
  41. except ImportError:
  42. HAS_TIMELIB = False
  43. BLINESEP = salt.utils.stringutils.to_bytes(os.linesep)
  44. class JinjaTestCase(TestCase):
  45. def test_tojson(self):
  46. """
  47. Test the ported tojson filter. Non-ascii unicode content should be
  48. dumped with ensure_ascii=True.
  49. """
  50. data = {"Non-ascii words": ["süß", "спам", "яйца"]}
  51. result = tojson(data)
  52. expected = (
  53. '{"Non-ascii words": ["s\\u00fc\\u00df", '
  54. '"\\u0441\\u043f\\u0430\\u043c", '
  55. '"\\u044f\\u0439\\u0446\\u0430"]}'
  56. )
  57. assert result == expected, result
  58. def test_indent(self):
  59. """
  60. Test the indent filter with Markup object as input. Double-quotes
  61. should not be URL-encoded.
  62. """
  63. data = Markup('foo:\n "bar"')
  64. result = indent(data)
  65. expected = Markup('foo:\n "bar"')
  66. assert result == expected, result
  67. class MockFileClient:
  68. """
  69. Does not download files but records any file request for testing
  70. """
  71. def __init__(self, loader=None):
  72. if loader:
  73. loader._file_client = self
  74. self.requests = []
  75. def get_file(self, template, dest="", makedirs=False, saltenv="base"):
  76. self.requests.append(
  77. {"path": template, "dest": dest, "makedirs": makedirs, "saltenv": saltenv}
  78. )
  79. def _setup_test_dir(src_dir, test_dir):
  80. os.makedirs(test_dir)
  81. salt.utils.files.recursive_copy(src_dir, test_dir)
  82. filename = os.path.join(test_dir, "non_ascii")
  83. with salt.utils.files.fopen(filename, "wb") as fp:
  84. fp.write(b"Assun\xc3\xa7\xc3\xa3o" + BLINESEP)
  85. filename = os.path.join(test_dir, "hello_simple")
  86. with salt.utils.files.fopen(filename, "wb") as fp:
  87. fp.write(b"world" + BLINESEP)
  88. filename = os.path.join(test_dir, "hello_import")
  89. lines = [
  90. r"{% from 'macro' import mymacro -%}",
  91. r"{% from 'macro' import mymacro -%}",
  92. r"{{ mymacro('Hey') ~ mymacro(a|default('a'), b|default('b')) }}",
  93. ]
  94. with salt.utils.files.fopen(filename, "wb") as fp:
  95. for line in lines:
  96. fp.write(line.encode("utf-8") + BLINESEP)
  97. class TestSaltCacheLoader(TestCase):
  98. def setUp(self):
  99. self.tempdir = tempfile.mkdtemp()
  100. self.template_dir = os.path.join(self.tempdir, "files", "test")
  101. _setup_test_dir(
  102. os.path.join(RUNTIME_VARS.BASE_FILES, "templates"), self.template_dir
  103. )
  104. self.opts = {
  105. "file_buffer_size": 1048576,
  106. "cachedir": self.tempdir,
  107. "file_roots": {"test": [self.template_dir]},
  108. "pillar_roots": {"test": [self.template_dir]},
  109. "extension_modules": os.path.join(
  110. os.path.dirname(os.path.abspath(__file__)), "extmods"
  111. ),
  112. }
  113. super().setUp()
  114. def tearDown(self):
  115. salt.utils.files.rm_rf(self.tempdir)
  116. self.tempdir = self.template_dir = self.opts
  117. def test_searchpath(self):
  118. """
  119. The searchpath is based on the cachedir option and the saltenv parameter
  120. """
  121. tmp = tempfile.gettempdir()
  122. opts = copy.deepcopy(self.opts)
  123. opts.update({"cachedir": tmp})
  124. loader = self.get_loader(opts=opts, saltenv="test")
  125. assert loader.searchpath == [os.path.join(tmp, "files", "test")]
  126. def test_mockclient(self):
  127. """
  128. A MockFileClient is used that records all file requests normally sent
  129. to the master.
  130. """
  131. loader = self.get_loader(opts=self.opts, saltenv="test")
  132. res = loader.get_source(None, "hello_simple")
  133. assert len(res) == 3
  134. # res[0] on Windows is unicode and use os.linesep so it works cross OS
  135. self.assertEqual(str(res[0]), "world" + os.linesep)
  136. tmpl_dir = os.path.join(self.template_dir, "hello_simple")
  137. self.assertEqual(res[1], tmpl_dir)
  138. assert res[2](), "Template up to date?"
  139. assert loader._file_client.requests
  140. self.assertEqual(loader._file_client.requests[0]["path"], "salt://hello_simple")
  141. def get_loader(self, opts=None, saltenv="base"):
  142. """
  143. Now that we instantiate the client in the __init__, we need to mock it
  144. """
  145. if opts is None:
  146. opts = self.opts
  147. with patch.object(SaltCacheLoader, "file_client", Mock()):
  148. loader = SaltCacheLoader(opts, saltenv)
  149. self.addCleanup(setattr, SaltCacheLoader, "_cached_client", None)
  150. # Create a mock file client and attach it to the loader
  151. MockFileClient(loader)
  152. return loader
  153. def get_test_saltenv(self):
  154. """
  155. Setup a simple jinja test environment
  156. """
  157. loader = self.get_loader(saltenv="test")
  158. jinja = Environment(loader=loader)
  159. return loader._file_client, jinja
  160. def test_import(self):
  161. """
  162. You can import and use macros from other files
  163. """
  164. fc, jinja = self.get_test_saltenv()
  165. result = jinja.get_template("hello_import").render()
  166. self.assertEqual(result, "Hey world !a b !")
  167. assert len(fc.requests) == 2
  168. self.assertEqual(fc.requests[0]["path"], "salt://hello_import")
  169. self.assertEqual(fc.requests[1]["path"], "salt://macro")
  170. def test_relative_import(self):
  171. """
  172. You can import using relative paths
  173. issue-13889
  174. """
  175. fc, jinja = self.get_test_saltenv()
  176. tmpl = jinja.get_template(os.path.join("relative", "rhello"))
  177. result = tmpl.render()
  178. self.assertEqual(result, "Hey world !a b !")
  179. assert len(fc.requests) == 3
  180. self.assertEqual(
  181. fc.requests[0]["path"], os.path.join("salt://relative", "rhello")
  182. )
  183. self.assertEqual(
  184. fc.requests[1]["path"], os.path.join("salt://relative", "rmacro")
  185. )
  186. self.assertEqual(fc.requests[2]["path"], "salt://macro")
  187. # This must fail when rendered: attempts to import from outside file root
  188. template = jinja.get_template("relative/rescape")
  189. self.assertRaises(exceptions.TemplateNotFound, template.render)
  190. def test_include(self):
  191. """
  192. You can also include a template that imports and uses macros
  193. """
  194. fc, jinja = self.get_test_saltenv()
  195. result = jinja.get_template("hello_include").render()
  196. self.assertEqual(result, "Hey world !a b !")
  197. assert len(fc.requests) == 3
  198. self.assertEqual(fc.requests[0]["path"], "salt://hello_include")
  199. self.assertEqual(fc.requests[1]["path"], "salt://hello_import")
  200. self.assertEqual(fc.requests[2]["path"], "salt://macro")
  201. def test_include_context(self):
  202. """
  203. Context variables are passes to the included template by default.
  204. """
  205. _, jinja = self.get_test_saltenv()
  206. result = jinja.get_template("hello_include").render(a="Hi", b="Salt")
  207. self.assertEqual(result, "Hey world !Hi Salt !")
  208. def test_cached_file_client(self):
  209. """
  210. Multiple instantiations of SaltCacheLoader use the cached file client
  211. """
  212. with patch("salt.transport.client.ReqChannel.factory", Mock()):
  213. loader_a = SaltCacheLoader(self.opts)
  214. loader_b = SaltCacheLoader(self.opts)
  215. assert loader_a._file_client is loader_b._file_client
  216. def test_file_client_kwarg(self):
  217. """
  218. A file client can be passed to SaltCacheLoader overriding the any
  219. cached file client
  220. """
  221. mfc = MockFileClient()
  222. loader = SaltCacheLoader(self.opts, _file_client=mfc)
  223. assert loader._file_client is mfc
  224. def test_cache_loader_shutdown(self):
  225. """
  226. The shudown method can be called without raising an exception when the
  227. file_client does not have a destroy method
  228. """
  229. mfc = MockFileClient()
  230. assert not hasattr(mfc, "destroy")
  231. loader = SaltCacheLoader(self.opts, _file_client=mfc)
  232. assert loader._file_client is mfc
  233. # Shutdown method should not raise any exceptions
  234. loader.shutdown()
  235. class TestGetTemplate(TestCase):
  236. def setUp(self):
  237. self.tempdir = tempfile.mkdtemp()
  238. self.template_dir = os.path.join(self.tempdir, "files", "test")
  239. _setup_test_dir(
  240. os.path.join(RUNTIME_VARS.BASE_FILES, "templates"), self.template_dir
  241. )
  242. self.local_opts = {
  243. "file_buffer_size": 1048576,
  244. "cachedir": self.tempdir,
  245. "file_client": "local",
  246. "file_ignore_regex": None,
  247. "file_ignore_glob": None,
  248. "file_roots": {"test": [self.template_dir]},
  249. "pillar_roots": {"test": [self.template_dir]},
  250. "fileserver_backend": ["roots"],
  251. "hash_type": "md5",
  252. "extension_modules": os.path.join(
  253. os.path.dirname(os.path.abspath(__file__)), "extmods"
  254. ),
  255. }
  256. self.local_salt = {}
  257. super().setUp()
  258. def tearDown(self):
  259. salt.utils.files.rm_rf(self.tempdir)
  260. self.tempdir = self.template_dir = self.local_opts = self.local_salt = None
  261. def test_fallback(self):
  262. """
  263. A Template with a filesystem loader is returned as fallback
  264. if the file is not contained in the searchpath
  265. """
  266. fn_ = os.path.join(self.template_dir, "hello_simple")
  267. with salt.utils.files.fopen(fn_) as fp_:
  268. out = render_jinja_tmpl(
  269. salt.utils.stringutils.to_unicode(fp_.read()),
  270. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  271. )
  272. self.assertEqual(out, "world" + os.linesep)
  273. def test_fallback_noloader(self):
  274. """
  275. A Template with a filesystem loader is returned as fallback
  276. if the file is not contained in the searchpath
  277. """
  278. filename = os.path.join(self.template_dir, "hello_import")
  279. with salt.utils.files.fopen(filename) as fp_:
  280. out = render_jinja_tmpl(
  281. salt.utils.stringutils.to_unicode(fp_.read()),
  282. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  283. )
  284. self.assertEqual(out, "Hey world !a b !" + os.linesep)
  285. def test_saltenv(self):
  286. """
  287. If the template is within the searchpath it can
  288. import, include and extend other templates.
  289. The initial template is expected to be already cached
  290. get_template does not request it from the master again.
  291. """
  292. fc = MockFileClient()
  293. with patch.object(SaltCacheLoader, "file_client", MagicMock(return_value=fc)):
  294. filename = os.path.join(self.template_dir, "hello_import")
  295. with salt.utils.files.fopen(filename) as fp_:
  296. out = render_jinja_tmpl(
  297. salt.utils.stringutils.to_unicode(fp_.read()),
  298. dict(
  299. opts={
  300. "cachedir": self.tempdir,
  301. "file_client": "remote",
  302. "file_roots": self.local_opts["file_roots"],
  303. "pillar_roots": self.local_opts["pillar_roots"],
  304. },
  305. a="Hi",
  306. b="Salt",
  307. saltenv="test",
  308. salt=self.local_salt,
  309. ),
  310. )
  311. self.assertEqual(out, "Hey world !Hi Salt !" + os.linesep)
  312. self.assertEqual(fc.requests[0]["path"], "salt://macro")
  313. def test_macro_additional_log_for_generalexc(self):
  314. """
  315. If we failed in a macro because of e.g. a TypeError, get
  316. more output from trace.
  317. """
  318. expected = r"""Jinja error:.*division.*
  319. .*macrogeneral\(2\):
  320. ---
  321. \{% macro mymacro\(\) -%\}
  322. \{\{ 1/0 \}\} <======================
  323. \{%- endmacro %\}
  324. ---.*"""
  325. filename = os.path.join(self.template_dir, "hello_import_generalerror")
  326. fc = MockFileClient()
  327. with patch.object(SaltCacheLoader, "file_client", MagicMock(return_value=fc)):
  328. with salt.utils.files.fopen(filename) as fp_:
  329. self.assertRaisesRegex(
  330. SaltRenderError,
  331. expected,
  332. render_jinja_tmpl,
  333. salt.utils.stringutils.to_unicode(fp_.read()),
  334. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  335. )
  336. def test_macro_additional_log_for_undefined(self):
  337. """
  338. If we failed in a macro because of undefined variables, get
  339. more output from trace.
  340. """
  341. expected = r"""Jinja variable 'b' is undefined
  342. .*macroundefined\(2\):
  343. ---
  344. \{% macro mymacro\(\) -%\}
  345. \{\{b.greetee\}\} <-- error is here <======================
  346. \{%- endmacro %\}
  347. ---"""
  348. filename = os.path.join(self.template_dir, "hello_import_undefined")
  349. fc = MockFileClient()
  350. with patch.object(SaltCacheLoader, "file_client", MagicMock(return_value=fc)):
  351. with salt.utils.files.fopen(filename) as fp_:
  352. self.assertRaisesRegex(
  353. SaltRenderError,
  354. expected,
  355. render_jinja_tmpl,
  356. salt.utils.stringutils.to_unicode(fp_.read()),
  357. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  358. )
  359. def test_macro_additional_log_syntaxerror(self):
  360. """
  361. If we failed in a macro, get more output from trace.
  362. """
  363. expected = r"""Jinja syntax error: expected token .*end.*got '-'.*
  364. .*macroerror\(2\):
  365. ---
  366. # macro
  367. \{% macro mymacro\(greeting, greetee='world'\) -\} <-- error is here <======================
  368. \{\{ greeting ~ ' ' ~ greetee \}\} !
  369. \{%- endmacro %\}
  370. ---.*"""
  371. filename = os.path.join(self.template_dir, "hello_import_error")
  372. fc = MockFileClient()
  373. with patch.object(SaltCacheLoader, "file_client", MagicMock(return_value=fc)):
  374. with salt.utils.files.fopen(filename) as fp_:
  375. self.assertRaisesRegex(
  376. SaltRenderError,
  377. expected,
  378. render_jinja_tmpl,
  379. salt.utils.stringutils.to_unicode(fp_.read()),
  380. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  381. )
  382. def test_non_ascii_encoding(self):
  383. fc = MockFileClient()
  384. with patch.object(SaltCacheLoader, "file_client", MagicMock(return_value=fc)):
  385. filename = os.path.join(self.template_dir, "hello_import")
  386. with salt.utils.files.fopen(filename) as fp_:
  387. out = render_jinja_tmpl(
  388. salt.utils.stringutils.to_unicode(fp_.read()),
  389. dict(
  390. opts={
  391. "cachedir": self.tempdir,
  392. "file_client": "remote",
  393. "file_roots": self.local_opts["file_roots"],
  394. "pillar_roots": self.local_opts["pillar_roots"],
  395. },
  396. a="Hi",
  397. b="Sàlt",
  398. saltenv="test",
  399. salt=self.local_salt,
  400. ),
  401. )
  402. self.assertEqual(
  403. out,
  404. salt.utils.stringutils.to_unicode("Hey world !Hi Sàlt !" + os.linesep),
  405. )
  406. self.assertEqual(fc.requests[0]["path"], "salt://macro")
  407. filename = os.path.join(self.template_dir, "non_ascii")
  408. with salt.utils.files.fopen(filename, "rb") as fp_:
  409. out = render_jinja_tmpl(
  410. salt.utils.stringutils.to_unicode(fp_.read(), "utf-8"),
  411. dict(
  412. opts={
  413. "cachedir": self.tempdir,
  414. "file_client": "remote",
  415. "file_roots": self.local_opts["file_roots"],
  416. "pillar_roots": self.local_opts["pillar_roots"],
  417. },
  418. a="Hi",
  419. b="Sàlt",
  420. saltenv="test",
  421. salt=self.local_salt,
  422. ),
  423. )
  424. self.assertEqual("Assunção" + os.linesep, out)
  425. self.assertEqual(fc.requests[0]["path"], "salt://macro")
  426. @skipIf(HAS_TIMELIB is False, "The `timelib` library is not installed.")
  427. def test_strftime(self):
  428. response = render_jinja_tmpl(
  429. '{{ "2002/12/25"|strftime }}',
  430. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  431. )
  432. self.assertEqual(response, "2002-12-25")
  433. objects = (
  434. datetime.datetime(2002, 12, 25, 12, 00, 00, 00),
  435. "2002/12/25",
  436. 1040814000,
  437. "1040814000",
  438. )
  439. for object in objects:
  440. response = render_jinja_tmpl(
  441. "{{ object|strftime }}",
  442. dict(
  443. object=object,
  444. opts=self.local_opts,
  445. saltenv="test",
  446. salt=self.local_salt,
  447. ),
  448. )
  449. self.assertEqual(response, "2002-12-25")
  450. response = render_jinja_tmpl(
  451. '{{ object|strftime("%b %d, %Y") }}',
  452. dict(
  453. object=object,
  454. opts=self.local_opts,
  455. saltenv="test",
  456. salt=self.local_salt,
  457. ),
  458. )
  459. self.assertEqual(response, "Dec 25, 2002")
  460. response = render_jinja_tmpl(
  461. '{{ object|strftime("%y") }}',
  462. dict(
  463. object=object,
  464. opts=self.local_opts,
  465. saltenv="test",
  466. salt=self.local_salt,
  467. ),
  468. )
  469. self.assertEqual(response, "02")
  470. def test_non_ascii(self):
  471. fn = os.path.join(self.template_dir, "non_ascii")
  472. out = JINJA(fn, opts=self.local_opts, saltenv="test", salt=self.local_salt)
  473. with salt.utils.files.fopen(out["data"], "rb") as fp:
  474. result = salt.utils.stringutils.to_unicode(fp.read(), "utf-8")
  475. self.assertEqual(
  476. salt.utils.stringutils.to_unicode("Assunção" + os.linesep), result
  477. )
  478. def test_get_context_has_enough_context(self):
  479. template = "1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf"
  480. context = salt.utils.stringutils.get_context(template, 8)
  481. expected = "---\n[...]\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\n[...]\n---"
  482. self.assertEqual(expected, context)
  483. def test_get_context_at_top_of_file(self):
  484. template = "1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf"
  485. context = salt.utils.stringutils.get_context(template, 1)
  486. expected = "---\n1\n2\n3\n4\n5\n6\n[...]\n---"
  487. self.assertEqual(expected, context)
  488. def test_get_context_at_bottom_of_file(self):
  489. template = "1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf"
  490. context = salt.utils.stringutils.get_context(template, 15)
  491. expected = "---\n[...]\na\nb\nc\nd\ne\nf\n---"
  492. self.assertEqual(expected, context)
  493. def test_get_context_2_context_lines(self):
  494. template = "1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf"
  495. context = salt.utils.stringutils.get_context(template, 8, num_lines=2)
  496. expected = "---\n[...]\n6\n7\n8\n9\na\n[...]\n---"
  497. self.assertEqual(expected, context)
  498. def test_get_context_with_marker(self):
  499. template = "1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf"
  500. context = salt.utils.stringutils.get_context(
  501. template, 8, num_lines=2, marker=" <---"
  502. )
  503. expected = "---\n[...]\n6\n7\n8 <---\n9\na\n[...]\n---"
  504. self.assertEqual(expected, context)
  505. def test_render_with_syntax_error(self):
  506. template = "hello\n\n{{ bad\n\nfoo"
  507. expected = r".*---\nhello\n\n{{ bad\n\nfoo <======================\n---"
  508. self.assertRaisesRegex(
  509. SaltRenderError,
  510. expected,
  511. render_jinja_tmpl,
  512. template,
  513. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  514. )
  515. def test_render_with_utf8_syntax_error(self):
  516. with patch.object(builtins, "__salt_system_encoding__", "utf-8"):
  517. template = "hello\n\n{{ bad\n\nfoo한"
  518. expected = salt.utils.stringutils.to_str(
  519. r".*---\nhello\n\n{{ bad\n\nfoo한 <======================\n---"
  520. )
  521. self.assertRaisesRegex(
  522. SaltRenderError,
  523. expected,
  524. render_jinja_tmpl,
  525. template,
  526. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  527. )
  528. def test_render_with_undefined_variable(self):
  529. template = "hello\n\n{{ foo }}\n\nfoo"
  530. expected = r"Jinja variable \'foo\' is undefined"
  531. self.assertRaisesRegex(
  532. SaltRenderError,
  533. expected,
  534. render_jinja_tmpl,
  535. template,
  536. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  537. )
  538. def test_render_with_undefined_variable_utf8(self):
  539. template = "hello\xed\x95\x9c\n\n{{ foo }}\n\nfoo"
  540. expected = r"Jinja variable \'foo\' is undefined"
  541. self.assertRaisesRegex(
  542. SaltRenderError,
  543. expected,
  544. render_jinja_tmpl,
  545. template,
  546. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  547. )
  548. def test_render_with_undefined_variable_unicode(self):
  549. template = "hello한\n\n{{ foo }}\n\nfoo"
  550. expected = r"Jinja variable \'foo\' is undefined"
  551. self.assertRaisesRegex(
  552. SaltRenderError,
  553. expected,
  554. render_jinja_tmpl,
  555. template,
  556. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  557. )
  558. class TestJinjaDefaultOptions(TestCase):
  559. @classmethod
  560. def setUpClass(cls):
  561. cls.local_opts = {
  562. "cachedir": os.path.join(RUNTIME_VARS.TMP, "jinja-template-cache"),
  563. "file_buffer_size": 1048576,
  564. "file_client": "local",
  565. "file_ignore_regex": None,
  566. "file_ignore_glob": None,
  567. "file_roots": {
  568. "test": [os.path.join(RUNTIME_VARS.BASE_FILES, "templates")]
  569. },
  570. "pillar_roots": {
  571. "test": [os.path.join(RUNTIME_VARS.BASE_FILES, "templates")]
  572. },
  573. "fileserver_backend": ["roots"],
  574. "hash_type": "md5",
  575. "extension_modules": os.path.join(
  576. os.path.dirname(os.path.abspath(__file__)), "extmods"
  577. ),
  578. "jinja_env": {"line_comment_prefix": "##", "line_statement_prefix": "%"},
  579. }
  580. cls.local_salt = {
  581. "myvar": "zero",
  582. "mylist": [0, 1, 2, 3],
  583. }
  584. @classmethod
  585. def tearDownClass(cls):
  586. cls.local_opts = cls.local_salt = None
  587. def test_comment_prefix(self):
  588. template = """
  589. %- set myvar = 'one'
  590. ## ignored comment 1
  591. {{- myvar -}}
  592. {%- set myvar = 'two' %} ## ignored comment 2
  593. {{- myvar }} ## ignored comment 3
  594. %- if myvar == 'two':
  595. %- set myvar = 'three'
  596. %- endif
  597. {{- myvar -}}
  598. """
  599. rendered = render_jinja_tmpl(
  600. template, dict(opts=self.local_opts, saltenv="test", salt=self.local_salt)
  601. )
  602. self.assertEqual(rendered, "onetwothree")
  603. def test_statement_prefix(self):
  604. template = """
  605. {%- set mylist = ['1', '2', '3'] %}
  606. %- set mylist = ['one', 'two', 'three']
  607. %- for item in mylist:
  608. {{- item }}
  609. %- endfor
  610. """
  611. rendered = render_jinja_tmpl(
  612. template, dict(opts=self.local_opts, saltenv="test", salt=self.local_salt)
  613. )
  614. self.assertEqual(rendered, "onetwothree")
  615. class TestCustomExtensions(TestCase):
  616. @classmethod
  617. def setUpClass(cls):
  618. cls.local_opts = {
  619. "cachedir": os.path.join(RUNTIME_VARS.TMP, "jinja-template-cache"),
  620. "file_buffer_size": 1048576,
  621. "file_client": "local",
  622. "file_ignore_regex": None,
  623. "file_ignore_glob": None,
  624. "file_roots": {
  625. "test": [os.path.join(RUNTIME_VARS.BASE_FILES, "templates")]
  626. },
  627. "pillar_roots": {
  628. "test": [os.path.join(RUNTIME_VARS.BASE_FILES, "templates")]
  629. },
  630. "fileserver_backend": ["roots"],
  631. "hash_type": "md5",
  632. "extension_modules": os.path.join(
  633. os.path.dirname(os.path.abspath(__file__)), "extmods"
  634. ),
  635. }
  636. cls.local_salt = {
  637. # 'dns.A': dnsutil.A,
  638. # 'dns.AAAA': dnsutil.AAAA,
  639. # 'file.exists': filemod.file_exists,
  640. # 'file.basename': filemod.basename,
  641. # 'file.dirname': filemod.dirname
  642. }
  643. @classmethod
  644. def tearDownClass(cls):
  645. cls.local_opts = cls.local_salt = None
  646. def test_regex_escape(self):
  647. dataset = "foo?:.*/\\bar"
  648. env = Environment(extensions=[SerializerExtension])
  649. env.filters.update(JinjaFilter.salt_jinja_filters)
  650. rendered = env.from_string("{{ dataset|regex_escape }}").render(dataset=dataset)
  651. self.assertEqual(rendered, re.escape(dataset))
  652. def test_unique_string(self):
  653. dataset = "foo"
  654. unique = set(dataset)
  655. env = Environment(extensions=[SerializerExtension])
  656. env.filters.update(JinjaFilter.salt_jinja_filters)
  657. rendered = (
  658. env.from_string("{{ dataset|unique }}")
  659. .render(dataset=dataset)
  660. .strip("'{}")
  661. .split("', '")
  662. )
  663. self.assertEqual(sorted(rendered), sorted(list(unique)))
  664. def test_unique_tuple(self):
  665. dataset = ("foo", "foo", "bar")
  666. unique = set(dataset)
  667. env = Environment(extensions=[SerializerExtension])
  668. env.filters.update(JinjaFilter.salt_jinja_filters)
  669. rendered = (
  670. env.from_string("{{ dataset|unique }}")
  671. .render(dataset=dataset)
  672. .strip("'{}")
  673. .split("', '")
  674. )
  675. self.assertEqual(sorted(rendered), sorted(list(unique)))
  676. def test_unique_list(self):
  677. dataset = ["foo", "foo", "bar"]
  678. unique = ["foo", "bar"]
  679. env = Environment(extensions=[SerializerExtension])
  680. env.filters.update(JinjaFilter.salt_jinja_filters)
  681. rendered = (
  682. env.from_string("{{ dataset|unique }}")
  683. .render(dataset=dataset)
  684. .strip("'[]")
  685. .split("', '")
  686. )
  687. self.assertEqual(rendered, unique)
  688. def test_serialize_json(self):
  689. dataset = {"foo": True, "bar": 42, "baz": [1, 2, 3], "qux": 2.0}
  690. env = Environment(extensions=[SerializerExtension])
  691. rendered = env.from_string("{{ dataset|json }}").render(dataset=dataset)
  692. self.assertEqual(dataset, salt.utils.json.loads(rendered))
  693. def test_serialize_yaml(self):
  694. dataset = {
  695. "foo": True,
  696. "bar": 42,
  697. "baz": [1, 2, 3],
  698. "qux": 2.0,
  699. "spam": OrderedDict([("foo", OrderedDict([("bar", "baz"), ("qux", 42)]))]),
  700. }
  701. env = Environment(extensions=[SerializerExtension])
  702. rendered = env.from_string("{{ dataset|yaml }}").render(dataset=dataset)
  703. self.assertEqual(dataset, salt.utils.yaml.safe_load(rendered))
  704. def test_serialize_yaml_str(self):
  705. dataset = "str value"
  706. env = Environment(extensions=[SerializerExtension])
  707. rendered = env.from_string("{{ dataset|yaml }}").render(dataset=dataset)
  708. self.assertEqual(dataset, rendered)
  709. def test_serialize_yaml_unicode(self):
  710. dataset = "str value"
  711. env = Environment(extensions=[SerializerExtension])
  712. rendered = env.from_string("{{ dataset|yaml }}").render(dataset=dataset)
  713. self.assertEqual("str value", rendered)
  714. def test_serialize_python(self):
  715. dataset = {"foo": True, "bar": 42, "baz": [1, 2, 3], "qux": 2.0}
  716. env = Environment(extensions=[SerializerExtension])
  717. rendered = env.from_string("{{ dataset|python }}").render(dataset=dataset)
  718. self.assertEqual(rendered, pprint.pformat(dataset))
  719. def test_load_yaml(self):
  720. env = Environment(extensions=[SerializerExtension])
  721. rendered = env.from_string(
  722. '{% set document = "{foo: it works}"|load_yaml %}{{ document.foo }}'
  723. ).render()
  724. self.assertEqual(rendered, "it works")
  725. rendered = env.from_string(
  726. "{% set document = document|load_yaml %}" "{{ document.foo }}"
  727. ).render(document="{foo: it works}")
  728. self.assertEqual(rendered, "it works")
  729. with self.assertRaises((TypeError, exceptions.TemplateRuntimeError)):
  730. env.from_string(
  731. "{% set document = document|load_yaml %}" "{{ document.foo }}"
  732. ).render(document={"foo": "it works"})
  733. def test_load_tag(self):
  734. env = Environment(extensions=[SerializerExtension])
  735. source = (
  736. "{{ bar }}, "
  737. + "{% load_yaml as docu %}{foo: it works, {{ bar }}: baz}{% endload %}"
  738. + "{{ docu.foo }}"
  739. )
  740. rendered = env.from_string(source).render(bar="barred")
  741. self.assertEqual(rendered, "barred, it works")
  742. source = (
  743. '{{ bar }}, {% load_json as docu %}{"foo": "it works", "{{ bar }}": "baz"}{% endload %}'
  744. + "{{ docu.foo }}"
  745. )
  746. rendered = env.from_string(source).render(bar="barred")
  747. self.assertEqual(rendered, "barred, it works")
  748. with self.assertRaises(exceptions.TemplateSyntaxError):
  749. env.from_string(
  750. "{% load_yamle as document %}{foo, bar: it works}{% endload %}"
  751. ).render()
  752. with self.assertRaises(exceptions.TemplateRuntimeError):
  753. env.from_string(
  754. "{% load_json as document %}{foo, bar: it works}{% endload %}"
  755. ).render()
  756. def test_load_json(self):
  757. env = Environment(extensions=[SerializerExtension])
  758. rendered = env.from_string(
  759. '{% set document = \'{"foo": "it works"}\'|load_json %}'
  760. "{{ document.foo }}"
  761. ).render()
  762. self.assertEqual(rendered, "it works")
  763. rendered = env.from_string(
  764. "{% set document = document|load_json %}" "{{ document.foo }}"
  765. ).render(document='{"foo": "it works"}')
  766. self.assertEqual(rendered, "it works")
  767. # bad quotes
  768. with self.assertRaises(exceptions.TemplateRuntimeError):
  769. env.from_string("{{ document|load_json }}").render(
  770. document="{'foo': 'it works'}"
  771. )
  772. # not a string
  773. with self.assertRaises(exceptions.TemplateRuntimeError):
  774. env.from_string("{{ document|load_json }}").render(
  775. document={"foo": "it works"}
  776. )
  777. def test_load_yaml_template(self):
  778. loader = DictLoader({"foo": '{bar: "my god is blue", foo: [1, 2, 3]}'})
  779. env = Environment(extensions=[SerializerExtension], loader=loader)
  780. rendered = env.from_string(
  781. '{% import_yaml "foo" as doc %}{{ doc.bar }}'
  782. ).render()
  783. self.assertEqual(rendered, "my god is blue")
  784. with self.assertRaises(exceptions.TemplateNotFound):
  785. env.from_string('{% import_yaml "does not exists" as doc %}').render()
  786. def test_load_json_template(self):
  787. loader = DictLoader({"foo": '{"bar": "my god is blue", "foo": [1, 2, 3]}'})
  788. env = Environment(extensions=[SerializerExtension], loader=loader)
  789. rendered = env.from_string(
  790. '{% import_json "foo" as doc %}{{ doc.bar }}'
  791. ).render()
  792. self.assertEqual(rendered, "my god is blue")
  793. with self.assertRaises(exceptions.TemplateNotFound):
  794. env.from_string('{% import_json "does not exists" as doc %}').render()
  795. def test_load_text_template(self):
  796. loader = DictLoader({"foo": "Foo!"})
  797. env = Environment(extensions=[SerializerExtension], loader=loader)
  798. rendered = env.from_string('{% import_text "foo" as doc %}{{ doc }}').render()
  799. self.assertEqual(rendered, "Foo!")
  800. with self.assertRaises(exceptions.TemplateNotFound):
  801. env.from_string('{% import_text "does not exists" as doc %}').render()
  802. def test_profile(self):
  803. env = Environment(extensions=[SerializerExtension])
  804. source = (
  805. "{%- profile as 'profile test' %}"
  806. + "{% set var = 'val' %}"
  807. + "{%- endprofile %}"
  808. + "{{ var }}"
  809. )
  810. rendered = env.from_string(source).render()
  811. self.assertEqual(rendered, "val")
  812. def test_catalog(self):
  813. loader = DictLoader(
  814. {
  815. "doc1": '{bar: "my god is blue"}',
  816. "doc2": '{% import_yaml "doc1" as local2 %} never exported',
  817. "doc3": '{% load_yaml as local3 %}{"foo": "it works"}{% endload %} me neither',
  818. "main1": '{% from "doc2" import local2 %}{{ local2.bar }}',
  819. "main2": '{% from "doc3" import local3 %}{{ local3.foo }}',
  820. "main3": """
  821. {% import "doc2" as imported2 %}
  822. {% import "doc3" as imported3 %}
  823. {{ imported2.local2.bar }}
  824. """,
  825. "main4": """
  826. {% import "doc2" as imported2 %}
  827. {% import "doc3" as imported3 %}
  828. {{ imported3.local3.foo }}
  829. """,
  830. "main5": """
  831. {% from "doc2" import local2 as imported2 %}
  832. {% from "doc3" import local3 as imported3 %}
  833. {{ imported2.bar }}
  834. """,
  835. "main6": """
  836. {% from "doc2" import local2 as imported2 %}
  837. {% from "doc3" import local3 as imported3 %}
  838. {{ imported3.foo }}
  839. """,
  840. }
  841. )
  842. env = Environment(extensions=[SerializerExtension], loader=loader)
  843. rendered = env.get_template("main1").render()
  844. self.assertEqual(rendered, "my god is blue")
  845. rendered = env.get_template("main2").render()
  846. self.assertEqual(rendered, "it works")
  847. rendered = env.get_template("main3").render().strip()
  848. self.assertEqual(rendered, "my god is blue")
  849. rendered = env.get_template("main4").render().strip()
  850. self.assertEqual(rendered, "it works")
  851. rendered = env.get_template("main5").render().strip()
  852. self.assertEqual(rendered, "my god is blue")
  853. rendered = env.get_template("main6").render().strip()
  854. self.assertEqual(rendered, "it works")
  855. def test_nested_structures(self):
  856. env = Environment(extensions=[SerializerExtension])
  857. rendered = env.from_string("{{ data }}").render(data="foo")
  858. self.assertEqual(rendered, "foo")
  859. data = OrderedDict([("foo", OrderedDict([("bar", "baz"), ("qux", 42)]))])
  860. rendered = env.from_string("{{ data }}").render(data=data)
  861. self.assertEqual(
  862. rendered, "{'foo': {'bar': 'baz', 'qux': 42}}",
  863. )
  864. rendered = env.from_string("{{ data }}").render(
  865. data=[OrderedDict(foo="bar",), OrderedDict(baz=42,)]
  866. )
  867. self.assertEqual(
  868. rendered, "[{'foo': 'bar'}, {'baz': 42}]",
  869. )
  870. def test_set_dict_key_value(self):
  871. """
  872. Test the `set_dict_key_value` Jinja filter.
  873. """
  874. rendered = render_jinja_tmpl(
  875. "{{ {} | set_dict_key_value('foo:bar:baz', 42) }}",
  876. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  877. )
  878. self.assertEqual(rendered, "{'foo': {'bar': {'baz': 42}}}")
  879. rendered = render_jinja_tmpl(
  880. "{{ {} | set_dict_key_value('foo.bar.baz', 42, delimiter='.') }}",
  881. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  882. )
  883. self.assertEqual(rendered, "{'foo': {'bar': {'baz': 42}}}")
  884. def test_update_dict_key_value(self):
  885. """
  886. Test the `update_dict_key_value` Jinja filter.
  887. """
  888. # Use OrderedDicts to avoid random key-order-switches in the rendered string.
  889. expected = OrderedDict(
  890. [("bar", OrderedDict([("baz", OrderedDict([("qux", 1), ("quux", 3)]))]))]
  891. )
  892. dataset = OrderedDict(
  893. [("bar", OrderedDict([("baz", OrderedDict([("qux", 1)]))]))]
  894. )
  895. dataset_exp = OrderedDict([("quux", 3)])
  896. rendered = render_jinja_tmpl(
  897. "{{ foo | update_dict_key_value('bar:baz', exp) }}",
  898. dict(
  899. foo=dataset,
  900. exp=dataset_exp,
  901. opts=self.local_opts,
  902. saltenv="test",
  903. salt=self.local_salt,
  904. ),
  905. )
  906. self.assertEqual(
  907. rendered, "{'bar': {'baz': {'qux': 1, 'quux': 3}}}",
  908. )
  909. # Test incorrect usage
  910. for update_with in [42, "foo", [42]]:
  911. template = "{{ {} | update_dict_key_value('bar:baz', update_with) }}"
  912. expected = r"Cannot update {} with a {}.".format(
  913. type({}), type(update_with)
  914. )
  915. self.assertRaisesRegex(
  916. SaltRenderError,
  917. expected,
  918. render_jinja_tmpl,
  919. template,
  920. dict(
  921. update_with=update_with,
  922. opts=self.local_opts,
  923. saltenv="test",
  924. salt=self.local_salt,
  925. ),
  926. )
  927. def test_append_dict_key_value(self):
  928. """
  929. Test the `append_dict_key_value` Jinja filter.
  930. """
  931. rendered = render_jinja_tmpl(
  932. "{{ {} | append_dict_key_value('foo:bar:baz', 42) }}",
  933. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  934. )
  935. self.assertEqual(rendered, "{'foo': {'bar': {'baz': [42]}}}")
  936. rendered = render_jinja_tmpl(
  937. "{{ foo | append_dict_key_value('bar:baz', 42) }}",
  938. dict(
  939. foo={"bar": {"baz": [1, 2]}},
  940. opts=self.local_opts,
  941. saltenv="test",
  942. salt=self.local_salt,
  943. ),
  944. )
  945. self.assertEqual(
  946. rendered, "{'bar': {'baz': [1, 2, 42]}}",
  947. )
  948. def test_extend_dict_key_value(self):
  949. """
  950. Test the `extend_dict_key_value` Jinja filter.
  951. """
  952. rendered = render_jinja_tmpl(
  953. "{{ {} | extend_dict_key_value('foo:bar:baz', [42]) }}",
  954. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  955. )
  956. self.assertEqual(rendered, "{'foo': {'bar': {'baz': [42]}}}")
  957. rendered = render_jinja_tmpl(
  958. "{{ foo | extend_dict_key_value('bar:baz', [42, 43]) }}",
  959. dict(
  960. foo={"bar": {"baz": [1, 2]}},
  961. opts=self.local_opts,
  962. saltenv="test",
  963. salt=self.local_salt,
  964. ),
  965. )
  966. self.assertEqual(
  967. rendered, "{'bar': {'baz': [1, 2, 42, 43]}}",
  968. )
  969. # Edge cases
  970. rendered = render_jinja_tmpl(
  971. "{{ {} | extend_dict_key_value('foo:bar:baz', 'quux') }}",
  972. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  973. )
  974. self.assertEqual(rendered, "{'foo': {'bar': {'baz': ['q', 'u', 'u', 'x']}}}")
  975. # Beware! When supplying a dict, the list gets extended with the dict coerced to a list,
  976. # which will only contain the keys of the dict.
  977. rendered = render_jinja_tmpl(
  978. "{{ {} | extend_dict_key_value('foo:bar:baz', {'foo': 'bar'}) }}",
  979. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  980. )
  981. self.assertEqual(rendered, "{'foo': {'bar': {'baz': ['foo']}}}")
  982. # Test incorrect usage
  983. template = "{{ {} | extend_dict_key_value('bar:baz', 42) }}"
  984. expected = r"Cannot extend {} with a {}.".format(type([]), type(42))
  985. self.assertRaisesRegex(
  986. SaltRenderError,
  987. expected,
  988. render_jinja_tmpl,
  989. template,
  990. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  991. )
  992. def test_sequence(self):
  993. env = Environment()
  994. env.filters["sequence"] = ensure_sequence_filter
  995. rendered = env.from_string("{{ data | sequence | length }}").render(data="foo")
  996. self.assertEqual(rendered, "1")
  997. rendered = env.from_string("{{ data | sequence | length }}").render(
  998. data=["foo", "bar"]
  999. )
  1000. self.assertEqual(rendered, "2")
  1001. rendered = env.from_string("{{ data | sequence | length }}").render(
  1002. data=("foo", "bar")
  1003. )
  1004. self.assertEqual(rendered, "2")
  1005. rendered = env.from_string("{{ data | sequence | length }}").render(
  1006. data={"foo", "bar"}
  1007. )
  1008. self.assertEqual(rendered, "2")
  1009. rendered = env.from_string("{{ data | sequence | length }}").render(
  1010. data={"foo": "bar"}
  1011. )
  1012. self.assertEqual(rendered, "1")
  1013. def test_camel_to_snake_case(self):
  1014. """
  1015. Test the `to_snake_case` Jinja filter.
  1016. """
  1017. rendered = render_jinja_tmpl(
  1018. "{{ 'abcdEfghhIjkLmnoP' | to_snake_case }}",
  1019. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1020. )
  1021. self.assertEqual(rendered, "abcd_efghh_ijk_lmno_p")
  1022. def test_snake_to_camel_case(self):
  1023. """
  1024. Test the `to_camelcase` Jinja filter.
  1025. """
  1026. rendered = render_jinja_tmpl(
  1027. "{{ 'the_fox_jumped_over_the_lazy_dog' | to_camelcase }}",
  1028. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1029. )
  1030. self.assertEqual(rendered, "theFoxJumpedOverTheLazyDog")
  1031. rendered = render_jinja_tmpl(
  1032. "{{ 'the_fox_jumped_over_the_lazy_dog' | to_camelcase(uppercamel=True) }}",
  1033. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1034. )
  1035. self.assertEqual(rendered, "TheFoxJumpedOverTheLazyDog")
  1036. def test_is_ip(self):
  1037. """
  1038. Test the `is_ip` Jinja filter.
  1039. """
  1040. rendered = render_jinja_tmpl(
  1041. "{{ '192.168.0.1' | is_ip }}",
  1042. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1043. )
  1044. self.assertEqual(rendered, "True")
  1045. rendered = render_jinja_tmpl(
  1046. "{{ 'FE80::' | is_ip }}",
  1047. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1048. )
  1049. self.assertEqual(rendered, "True")
  1050. rendered = render_jinja_tmpl(
  1051. "{{ 'random' | is_ip }}",
  1052. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1053. )
  1054. self.assertEqual(rendered, "False")
  1055. def test_is_ipv4(self):
  1056. """
  1057. Test the `is_ipv4` Jinja filter.
  1058. """
  1059. rendered = render_jinja_tmpl(
  1060. "{{ '192.168.0.1' | is_ipv4 }}",
  1061. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1062. )
  1063. self.assertEqual(rendered, "True")
  1064. rendered = render_jinja_tmpl(
  1065. "{{ 'FE80::' | is_ipv4 }}",
  1066. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1067. )
  1068. self.assertEqual(rendered, "False")
  1069. rendered = render_jinja_tmpl(
  1070. "{{ 'random' | is_ipv4 }}",
  1071. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1072. )
  1073. self.assertEqual(rendered, "False")
  1074. def test_is_ipv6(self):
  1075. """
  1076. Test the `is_ipv6` Jinja filter.
  1077. """
  1078. rendered = render_jinja_tmpl(
  1079. "{{ '192.168.0.1' | is_ipv6 }}",
  1080. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1081. )
  1082. self.assertEqual(rendered, "False")
  1083. rendered = render_jinja_tmpl(
  1084. "{{ 'fe80::20d:b9ff:fe01:ea8%eth0' | is_ipv6 }}",
  1085. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1086. )
  1087. self.assertEqual(rendered, "True")
  1088. rendered = render_jinja_tmpl(
  1089. "{{ 'FE80::' | is_ipv6 }}",
  1090. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1091. )
  1092. self.assertEqual(rendered, "True")
  1093. rendered = render_jinja_tmpl(
  1094. "{{ 'random' | is_ipv6 }}",
  1095. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1096. )
  1097. self.assertEqual(rendered, "False")
  1098. def test_ipaddr(self):
  1099. """
  1100. Test the `ipaddr` Jinja filter.
  1101. """
  1102. rendered = render_jinja_tmpl(
  1103. "{{ '::' | ipaddr }}",
  1104. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1105. )
  1106. self.assertEqual(rendered, "::")
  1107. rendered = render_jinja_tmpl(
  1108. "{{ '192.168.0.1' | ipaddr }}",
  1109. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1110. )
  1111. self.assertEqual(rendered, "192.168.0.1")
  1112. # provides a list with valid IP addresses only
  1113. rendered = render_jinja_tmpl(
  1114. "{{ ['192.168.0.1', '172.17.17.1', 'foo', 'bar', '::'] | ipaddr | join(', ') }}",
  1115. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1116. )
  1117. self.assertEqual(rendered, "192.168.0.1, 172.17.17.1, ::")
  1118. # return only multicast addresses
  1119. rendered = render_jinja_tmpl(
  1120. "{{ ['224.0.0.1', 'FF01::1', '::'] | ipaddr(options='multicast') | join(', ') }}",
  1121. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1122. )
  1123. self.assertEqual(rendered, "224.0.0.1, ff01::1")
  1124. def test_ipv4(self):
  1125. """
  1126. Test the `ipv4` Jinja filter.
  1127. """
  1128. rendered = render_jinja_tmpl(
  1129. "{{ '192.168.0.1' | ipv4 }}",
  1130. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1131. )
  1132. self.assertEqual(rendered, "192.168.0.1")
  1133. rendered = render_jinja_tmpl(
  1134. "{{ ['192.168.0.1', '172.17.17.1'] | ipv4 | join(', ')}}",
  1135. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1136. )
  1137. self.assertEqual(rendered, "192.168.0.1, 172.17.17.1")
  1138. rendered = render_jinja_tmpl(
  1139. "{{ 'fe80::' | ipv4 }}",
  1140. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1141. )
  1142. self.assertEqual(rendered, "None")
  1143. rendered = render_jinja_tmpl(
  1144. "{{ 'random' | ipv4 }}",
  1145. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1146. )
  1147. self.assertEqual(rendered, "None")
  1148. rendered = render_jinja_tmpl(
  1149. "{{ '192.168.0.1' | ipv4(options='lo') }}",
  1150. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1151. )
  1152. self.assertEqual(rendered, "None")
  1153. rendered = render_jinja_tmpl(
  1154. "{{ '127.0.0.1' | ipv4(options='lo') }}",
  1155. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1156. )
  1157. self.assertEqual(rendered, "127.0.0.1")
  1158. def test_ipv6(self):
  1159. """
  1160. Test the `ipv6` Jinja filter.
  1161. """
  1162. rendered = render_jinja_tmpl(
  1163. "{{ '192.168.0.1' | ipv6 }}",
  1164. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1165. )
  1166. self.assertEqual(rendered, "None")
  1167. rendered = render_jinja_tmpl(
  1168. "{{ 'random' | ipv6 }}",
  1169. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1170. )
  1171. self.assertEqual(rendered, "None")
  1172. # returns the standard format value
  1173. rendered = render_jinja_tmpl(
  1174. "{{ 'FE80:0:0::0' | ipv6 }}",
  1175. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1176. )
  1177. self.assertEqual(rendered, "fe80::")
  1178. # fe80:: is link local therefore will be returned
  1179. rendered = render_jinja_tmpl(
  1180. "{{ 'fe80::' | ipv6(options='ll') }}",
  1181. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1182. )
  1183. self.assertEqual(rendered, "fe80::")
  1184. # fe80:: is not loopback
  1185. rendered = render_jinja_tmpl(
  1186. "{{ 'fe80::' | ipv6(options='lo') }}",
  1187. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1188. )
  1189. self.assertEqual(rendered, "None")
  1190. # returns only IPv6 addresses in the list
  1191. rendered = render_jinja_tmpl(
  1192. "{{ ['fe80::', '192.168.0.1'] | ipv6 | join(', ') }}",
  1193. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1194. )
  1195. self.assertEqual(rendered, "fe80::")
  1196. rendered = render_jinja_tmpl(
  1197. "{{ ['fe80::', '::'] | ipv6 | join(', ') }}",
  1198. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1199. )
  1200. self.assertEqual(rendered, "fe80::, ::")
  1201. def test_network_hosts(self):
  1202. """
  1203. Test the `network_hosts` Jinja filter.
  1204. """
  1205. rendered = render_jinja_tmpl(
  1206. "{{ '192.168.0.1/30' | network_hosts | join(', ') }}",
  1207. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1208. )
  1209. self.assertEqual(rendered, "192.168.0.1, 192.168.0.2")
  1210. def test_network_size(self):
  1211. """
  1212. Test the `network_size` Jinja filter.
  1213. """
  1214. rendered = render_jinja_tmpl(
  1215. "{{ '192.168.0.1' | network_size }}",
  1216. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1217. )
  1218. self.assertEqual(rendered, "1")
  1219. rendered = render_jinja_tmpl(
  1220. "{{ '192.168.0.1/8' | network_size }}",
  1221. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1222. )
  1223. self.assertEqual(rendered, "16777216")
  1224. @requires_network()
  1225. def test_http_query(self):
  1226. """
  1227. Test the `http_query` Jinja filter.
  1228. """
  1229. urls = (
  1230. # These cannot be HTTPS urls since urllib2 chokes on those
  1231. "http://saltstack.com",
  1232. "http://community.saltstack.com",
  1233. "http://google.com",
  1234. "http://duckduckgo.com",
  1235. )
  1236. for backend in ("requests", "tornado", "urllib2"):
  1237. rendered = render_jinja_tmpl(
  1238. "{{ '"
  1239. + random.choice(urls)
  1240. + "' | http_query(backend='"
  1241. + backend
  1242. + "') }}",
  1243. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1244. )
  1245. self.assertIsInstance(
  1246. rendered, str, "Failed with rendered template: {}".format(rendered)
  1247. )
  1248. dict_reply = ast.literal_eval(rendered)
  1249. self.assertIsInstance(
  1250. dict_reply, dict, "Failed with rendered template: {}".format(rendered)
  1251. )
  1252. self.assertIn(
  1253. "body",
  1254. dict_reply,
  1255. "'body' not found in request response({}). Rendered template: {!r}".format(
  1256. dict_reply, rendered
  1257. ),
  1258. )
  1259. self.assertIsInstance(
  1260. dict_reply["body"],
  1261. str,
  1262. "Failed with rendered template: {}".format(rendered),
  1263. )
  1264. def test_to_bool(self):
  1265. """
  1266. Test the `to_bool` Jinja filter.
  1267. """
  1268. rendered = render_jinja_tmpl(
  1269. "{{ 1 | to_bool }}",
  1270. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1271. )
  1272. self.assertEqual(rendered, "True")
  1273. rendered = render_jinja_tmpl(
  1274. "{{ 'True' | to_bool }}",
  1275. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1276. )
  1277. self.assertEqual(rendered, "True")
  1278. rendered = render_jinja_tmpl(
  1279. "{{ 0 | to_bool }}",
  1280. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1281. )
  1282. self.assertEqual(rendered, "False")
  1283. rendered = render_jinja_tmpl(
  1284. "{{ 'Yes' | to_bool }}",
  1285. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1286. )
  1287. self.assertEqual(rendered, "True")
  1288. def test_quote(self):
  1289. """
  1290. Test the `quote` Jinja filter.
  1291. """
  1292. rendered = render_jinja_tmpl(
  1293. "{{ 'random' | quote }}",
  1294. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1295. )
  1296. self.assertEqual(rendered, "random")
  1297. def test_regex_search(self):
  1298. """
  1299. Test the `regex_search` Jinja filter.
  1300. """
  1301. rendered = render_jinja_tmpl(
  1302. "{{ 'abcdefabcdef' | regex_search('BC(.*)', ignorecase=True) }}",
  1303. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1304. )
  1305. self.assertEqual(
  1306. rendered, "('defabcdef',)"
  1307. ) # because search looks only at the beginning
  1308. def test_regex_match(self):
  1309. """
  1310. Test the `regex_match` Jinja filter.
  1311. """
  1312. rendered = render_jinja_tmpl(
  1313. "{{ 'abcdefabcdef' | regex_match('BC(.*)', ignorecase=True)}}",
  1314. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1315. )
  1316. self.assertEqual(rendered, "None")
  1317. def test_regex_replace(self):
  1318. """
  1319. Test the `regex_replace` Jinja filter.
  1320. """
  1321. rendered = render_jinja_tmpl(
  1322. r"{{ 'lets replace spaces' | regex_replace('\s+', '__') }}",
  1323. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1324. )
  1325. self.assertEqual(rendered, "lets__replace__spaces")
  1326. def test_uuid(self):
  1327. """
  1328. Test the `uuid` Jinja filter.
  1329. """
  1330. rendered = render_jinja_tmpl(
  1331. "{{ 'random' | uuid }}",
  1332. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1333. )
  1334. self.assertEqual(rendered, "3652b285-26ad-588e-a5dc-c2ee65edc804")
  1335. def test_min(self):
  1336. """
  1337. Test the `min` Jinja filter.
  1338. """
  1339. rendered = render_jinja_tmpl(
  1340. "{{ [1, 2, 3] | min }}",
  1341. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1342. )
  1343. self.assertEqual(rendered, "1")
  1344. def test_max(self):
  1345. """
  1346. Test the `max` Jinja filter.
  1347. """
  1348. rendered = render_jinja_tmpl(
  1349. "{{ [1, 2, 3] | max }}",
  1350. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1351. )
  1352. self.assertEqual(rendered, "3")
  1353. def test_avg(self):
  1354. """
  1355. Test the `avg` Jinja filter.
  1356. """
  1357. rendered = render_jinja_tmpl(
  1358. "{{ [1, 2, 3] | avg }}",
  1359. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1360. )
  1361. self.assertEqual(rendered, "2.0")
  1362. def test_union(self):
  1363. """
  1364. Test the `union` Jinja filter.
  1365. """
  1366. rendered = render_jinja_tmpl(
  1367. "{{ [1, 2, 3] | union([2, 3, 4]) | join(', ') }}",
  1368. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1369. )
  1370. self.assertEqual(rendered, "1, 2, 3, 4")
  1371. def test_intersect(self):
  1372. """
  1373. Test the `intersect` Jinja filter.
  1374. """
  1375. rendered = render_jinja_tmpl(
  1376. "{{ [1, 2, 3] | intersect([2, 3, 4]) | join(', ') }}",
  1377. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1378. )
  1379. self.assertEqual(rendered, "2, 3")
  1380. def test_difference(self):
  1381. """
  1382. Test the `difference` Jinja filter.
  1383. """
  1384. rendered = render_jinja_tmpl(
  1385. "{{ [1, 2, 3] | difference([2, 3, 4]) | join(', ') }}",
  1386. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1387. )
  1388. self.assertEqual(rendered, "1")
  1389. def test_symmetric_difference(self):
  1390. """
  1391. Test the `symmetric_difference` Jinja filter.
  1392. """
  1393. rendered = render_jinja_tmpl(
  1394. "{{ [1, 2, 3] | symmetric_difference([2, 3, 4]) | join(', ') }}",
  1395. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1396. )
  1397. self.assertEqual(rendered, "1, 4")
  1398. def test_method_call(self):
  1399. """
  1400. Test the `method_call` Jinja filter.
  1401. """
  1402. rendered = render_jinja_tmpl(
  1403. "{{ 6|method_call('bit_length') }}",
  1404. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1405. )
  1406. self.assertEqual(rendered, "3")
  1407. rendered = render_jinja_tmpl(
  1408. "{{ 6.7|method_call('is_integer') }}",
  1409. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1410. )
  1411. self.assertEqual(rendered, "False")
  1412. rendered = render_jinja_tmpl(
  1413. "{{ 'absaltba'|method_call('strip', 'ab') }}",
  1414. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1415. )
  1416. self.assertEqual(rendered, "salt")
  1417. rendered = render_jinja_tmpl(
  1418. "{{ [1, 2, 1, 3, 4]|method_call('index', 1, 1, 3) }}",
  1419. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1420. )
  1421. self.assertEqual(rendered, "2")
  1422. # have to use `dictsort` to keep test result deterministic
  1423. rendered = render_jinja_tmpl(
  1424. "{{ {}|method_call('fromkeys', ['a', 'b', 'c'], 0)|dictsort }}",
  1425. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1426. )
  1427. self.assertEqual(rendered, "[('a', 0), ('b', 0), ('c', 0)]")
  1428. # missing object method test
  1429. rendered = render_jinja_tmpl(
  1430. "{{ 6|method_call('bit_width') }}",
  1431. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1432. )
  1433. self.assertEqual(rendered, "None")
  1434. def test_md5(self):
  1435. """
  1436. Test the `md5` Jinja filter.
  1437. """
  1438. rendered = render_jinja_tmpl(
  1439. "{{ 'random' | md5 }}",
  1440. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1441. )
  1442. self.assertEqual(rendered, "7ddf32e17a6ac5ce04a8ecbf782ca509")
  1443. def test_sha256(self):
  1444. """
  1445. Test the `sha256` Jinja filter.
  1446. """
  1447. rendered = render_jinja_tmpl(
  1448. "{{ 'random' | sha256 }}",
  1449. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1450. )
  1451. self.assertEqual(
  1452. rendered, "a441b15fe9a3cf56661190a0b93b9dec7d04127288cc87250967cf3b52894d11"
  1453. )
  1454. def test_sha512(self):
  1455. """
  1456. Test the `sha512` Jinja filter.
  1457. """
  1458. rendered = render_jinja_tmpl(
  1459. "{{ 'random' | sha512 }}",
  1460. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1461. )
  1462. self.assertEqual(
  1463. rendered,
  1464. str(
  1465. "811a90e1c8e86c7b4c0eef5b2c0bf0ec1b19c4b1b5a242e6455be93787cb473cb7bc"
  1466. "9b0fdeb960d00d5c6881c2094dd63c5c900ce9057255e2a4e271fc25fef1"
  1467. ),
  1468. )
  1469. def test_hmac(self):
  1470. """
  1471. Test the `hmac` Jinja filter.
  1472. """
  1473. rendered = render_jinja_tmpl(
  1474. "{{ 'random' | hmac('secret', 'blah') }}",
  1475. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1476. )
  1477. self.assertEqual(rendered, "False")
  1478. rendered = render_jinja_tmpl(
  1479. (
  1480. "{{ 'get salted' | "
  1481. "hmac('shared secret', 'eBWf9bstXg+NiP5AOwppB5HMvZiYMPzEM9W5YMm/AmQ=') }}"
  1482. ),
  1483. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1484. )
  1485. self.assertEqual(rendered, "True")
  1486. def test_base64_encode(self):
  1487. """
  1488. Test the `base64_encode` Jinja filter.
  1489. """
  1490. rendered = render_jinja_tmpl(
  1491. "{{ 'random' | base64_encode }}",
  1492. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1493. )
  1494. self.assertEqual(rendered, "cmFuZG9t")
  1495. def test_base64_decode(self):
  1496. """
  1497. Test the `base64_decode` Jinja filter.
  1498. """
  1499. rendered = render_jinja_tmpl(
  1500. "{{ 'cmFuZG9t' | base64_decode }}",
  1501. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1502. )
  1503. self.assertEqual(rendered, "random")
  1504. def test_json_query(self):
  1505. """
  1506. Test the `json_query` Jinja filter.
  1507. """
  1508. rendered = render_jinja_tmpl(
  1509. "{{ [1, 2, 3] | json_query('[1]')}}",
  1510. dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
  1511. )
  1512. self.assertEqual(rendered, "2")
  1513. # def test_print(self):
  1514. # env = Environment(extensions=[SerializerExtension])
  1515. # source = '{% import_yaml "toto.foo" as docu %}'
  1516. # name, filename = None, '<filename>'
  1517. # parsed = env._parse(source, name, filename)
  1518. # print parsed
  1519. # print
  1520. # compiled = env._generate(parsed, name, filename)
  1521. # print compiled
  1522. # return
  1523. class TestDotNotationLookup(ModuleCase):
  1524. """
  1525. Tests to call Salt functions via Jinja with various lookup syntaxes
  1526. """
  1527. def setUp(self):
  1528. functions = {
  1529. "mocktest.ping": lambda: True,
  1530. "mockgrains.get": lambda x: "jerry",
  1531. }
  1532. minion_opts = salt.config.minion_config(
  1533. os.path.join(RUNTIME_VARS.TMP_CONF_DIR, "minion")
  1534. )
  1535. render = salt.loader.render(minion_opts, functions)
  1536. self.jinja = render.get("jinja")
  1537. def tearDown(self):
  1538. del self.jinja
  1539. def render(self, tmpl_str, context=None):
  1540. return self.jinja(tmpl_str, context=context or {}, argline="-s").read()
  1541. def test_normlookup(self):
  1542. """
  1543. Sanity-check the normal dictionary-lookup syntax for our stub function
  1544. """
  1545. tmpl_str = """Hello, {{ salt['mocktest.ping']() }}."""
  1546. with patch.object(SaltCacheLoader, "file_client", Mock()):
  1547. ret = self.render(tmpl_str)
  1548. self.assertEqual(ret, "Hello, True.")
  1549. def test_dotlookup(self):
  1550. """
  1551. Check calling a stub function using awesome dot-notation
  1552. """
  1553. tmpl_str = """Hello, {{ salt.mocktest.ping() }}."""
  1554. with patch.object(SaltCacheLoader, "file_client", Mock()):
  1555. ret = self.render(tmpl_str)
  1556. self.assertEqual(ret, "Hello, True.")
  1557. def test_shadowed_dict_method(self):
  1558. """
  1559. Check calling a stub function with a name that shadows a ``dict``
  1560. method name
  1561. """
  1562. tmpl_str = """Hello, {{ salt.mockgrains.get('id') }}."""
  1563. with patch.object(SaltCacheLoader, "file_client", Mock()):
  1564. ret = self.render(tmpl_str)
  1565. self.assertEqual(ret, "Hello, jerry.")