test_state.py 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import logging
  5. import os
  6. import shutil
  7. import sys
  8. import tempfile
  9. import textwrap
  10. import threading
  11. import time
  12. # Import Salt Testing libs
  13. from tests.support.runtests import RUNTIME_VARS
  14. from tests.support.case import ModuleCase
  15. from tests.support.helpers import with_tempdir
  16. from tests.support.unit import skipIf
  17. from tests.support.mixins import SaltReturnAssertsMixin
  18. # Import Salt libs
  19. import salt.utils.atomicfile
  20. import salt.utils.files
  21. import salt.utils.path
  22. import salt.utils.platform
  23. import salt.utils.stringutils
  24. from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
  25. # Import 3rd-party libs
  26. import pytest
  27. from salt.ext import six
  28. log = logging.getLogger(__name__)
  29. DEFAULT_ENDING = salt.utils.stringutils.to_bytes(os.linesep)
  30. def trim_line_end(line):
  31. '''
  32. Remove CRLF or LF from the end of line.
  33. '''
  34. if line[-2:] == salt.utils.stringutils.to_bytes('\r\n'):
  35. return line[:-2]
  36. elif line[-1:] == salt.utils.stringutils.to_bytes('\n'):
  37. return line[:-1]
  38. raise Exception("Invalid line ending")
  39. def reline(source, dest, force=False, ending=DEFAULT_ENDING):
  40. '''
  41. Normalize the line endings of a file.
  42. '''
  43. fp, tmp = tempfile.mkstemp()
  44. os.close(fp)
  45. with salt.utils.files.fopen(tmp, 'wb') as tmp_fd:
  46. with salt.utils.files.fopen(source, 'rb') as fd:
  47. lines = fd.readlines()
  48. for line in lines:
  49. line_noend = trim_line_end(line)
  50. tmp_fd.write(line_noend + ending)
  51. if os.path.exists(dest) and force:
  52. os.remove(dest)
  53. os.rename(tmp, dest)
  54. @pytest.mark.windows_whitelisted
  55. class StateModuleTest(ModuleCase, SaltReturnAssertsMixin):
  56. '''
  57. Validate the state module
  58. '''
  59. maxDiff = None
  60. @classmethod
  61. def setUpClass(cls):
  62. def _reline(path, ending=DEFAULT_ENDING):
  63. '''
  64. Normalize the line endings of a file.
  65. '''
  66. with salt.utils.files.fopen(path, 'rb') as fhr:
  67. lines = fhr.read().splitlines()
  68. with salt.utils.atomicfile.atomic_open(path, 'wb') as fhw:
  69. for line in lines:
  70. fhw.write(line + ending)
  71. destpath = os.path.join(RUNTIME_VARS.BASE_FILES, 'testappend', 'firstif')
  72. destpath = os.path.join(RUNTIME_VARS.BASE_FILES, 'testappend', 'secondif')
  73. _reline(destpath)
  74. cls.TIMEOUT = 600 if salt.utils.platform.is_windows() else 10
  75. def test_show_highstate(self):
  76. '''
  77. state.show_highstate
  78. '''
  79. high = self.run_function('state.show_highstate')
  80. destpath = os.path.join(RUNTIME_VARS.TMP, 'testfile')
  81. self.assertTrue(isinstance(high, dict))
  82. self.assertTrue(destpath in high)
  83. self.assertEqual(high[destpath]['__env__'], 'base')
  84. def test_show_lowstate(self):
  85. '''
  86. state.show_lowstate
  87. '''
  88. low = self.run_function('state.show_lowstate')
  89. self.assertTrue(isinstance(low, list))
  90. self.assertTrue(isinstance(low[0], dict))
  91. def test_show_states(self):
  92. '''
  93. state.show_states
  94. '''
  95. states = self.run_function('state.show_states')
  96. self.assertTrue(isinstance(states, list))
  97. self.assertTrue(isinstance(states[0], six.string_types))
  98. states = self.run_function('state.show_states', sorted=False)
  99. self.assertTrue(isinstance(states, list))
  100. self.assertTrue(isinstance(states[0], six.string_types))
  101. def test_show_states_missing_sls(self):
  102. '''
  103. Test state.show_states with a sls file
  104. defined in a top file is missing
  105. '''
  106. topfile = os.path.join(RUNTIME_VARS.TMP_STATE_TREE, 'top.sls')
  107. with salt.utils.files.fopen(topfile, 'w') as top_file:
  108. top_file.write(textwrap.dedent('''\
  109. base:
  110. '*':
  111. - doesnotexist
  112. '''))
  113. states = self.run_function('state.show_states')
  114. assert isinstance(states, list)
  115. assert states == ["No matching sls found for 'doesnotexist' in env 'base'"]
  116. def test_catch_recurse(self):
  117. '''
  118. state.show_sls used to catch a recursive ref
  119. '''
  120. err = self.run_function('state.sls', mods='recurse_fail')
  121. self.assertIn('recursive', err[0])
  122. def test_no_recurse(self):
  123. '''
  124. verify that a sls structure is NOT a recursive ref
  125. '''
  126. sls = self.run_function('state.show_sls', mods='recurse_ok')
  127. self.assertIn('snmpd', sls)
  128. def test_no_recurse_two(self):
  129. '''
  130. verify that a sls structure is NOT a recursive ref
  131. '''
  132. sls = self.run_function('state.show_sls', mods='recurse_ok_two')
  133. self.assertIn('/etc/nagios/nrpe.cfg', sls)
  134. def test_running_dictionary_consistency(self):
  135. '''
  136. Test the structure of the running dictionary so we don't change it
  137. without deprecating/documenting the change
  138. '''
  139. running_dict_fields = [
  140. '__id__',
  141. '__run_num__',
  142. '__sls__',
  143. 'changes',
  144. 'comment',
  145. 'duration',
  146. 'name',
  147. 'result',
  148. 'start_time',
  149. ]
  150. sls = self.run_function('state.single',
  151. fun='test.succeed_with_changes',
  152. name='gndn')
  153. for state, ret in sls.items():
  154. for field in running_dict_fields:
  155. self.assertIn(field, ret)
  156. def test_running_dictionary_key_sls(self):
  157. '''
  158. Ensure the __sls__ key is either null or a string
  159. '''
  160. sls1 = self.run_function('state.single',
  161. fun='test.succeed_with_changes',
  162. name='gndn')
  163. sls2 = self.run_function('state.sls', mods='gndn')
  164. for state, ret in sls1.items():
  165. self.assertTrue(isinstance(ret['__sls__'], type(None)))
  166. for state, ret in sls2.items():
  167. self.assertTrue(isinstance(ret['__sls__'], six.string_types))
  168. def _remove_request_cache_file(self):
  169. '''
  170. remove minion state request file
  171. '''
  172. cache_file = os.path.join(self.get_config('minion')['cachedir'], 'req_state.p')
  173. if os.path.exists(cache_file):
  174. os.remove(cache_file)
  175. def test_request(self):
  176. '''
  177. verify sending a state request to the minion(s)
  178. '''
  179. self._remove_request_cache_file()
  180. ret = self.run_function('state.request', mods='modules.state.requested')
  181. result = ret['cmd_|-count_root_dir_contents_|-ls -a / | wc -l_|-run']['result']
  182. self.assertEqual(result, None)
  183. def test_check_request(self):
  184. '''
  185. verify checking a state request sent to the minion(s)
  186. '''
  187. self._remove_request_cache_file()
  188. self.run_function('state.request', mods='modules.state.requested')
  189. ret = self.run_function('state.check_request')
  190. result = ret['default']['test_run']['cmd_|-count_root_dir_contents_|-ls -a / | wc -l_|-run']['result']
  191. self.assertEqual(result, None)
  192. def test_clear_request(self):
  193. '''
  194. verify clearing a state request sent to the minion(s)
  195. '''
  196. self._remove_request_cache_file()
  197. self.run_function('state.request', mods='modules.state.requested')
  198. ret = self.run_function('state.clear_request')
  199. self.assertTrue(ret)
  200. def test_run_request_succeeded(self):
  201. '''
  202. verify running a state request sent to the minion(s)
  203. '''
  204. self._remove_request_cache_file()
  205. if salt.utils.platform.is_windows():
  206. self.run_function('state.request', mods='modules.state.requested_win')
  207. else:
  208. self.run_function('state.request', mods='modules.state.requested')
  209. ret = self.run_function('state.run_request')
  210. if salt.utils.platform.is_windows():
  211. key = 'cmd_|-count_root_dir_contents_|-Get-ChildItem C:\\\\ | Measure-Object | %{$_.Count}_|-run'
  212. else:
  213. key = 'cmd_|-count_root_dir_contents_|-ls -a / | wc -l_|-run'
  214. result = ret[key]['result']
  215. self.assertTrue(result)
  216. def test_run_request_failed_no_request_staged(self):
  217. '''
  218. verify not running a state request sent to the minion(s)
  219. '''
  220. self._remove_request_cache_file()
  221. self.run_function('state.request', mods='modules.state.requested')
  222. self.run_function('state.clear_request')
  223. ret = self.run_function('state.run_request')
  224. self.assertEqual(ret, {})
  225. @with_tempdir()
  226. def test_issue_1896_file_append_source(self, base_dir):
  227. '''
  228. Verify that we can append a file's contents
  229. '''
  230. testfile = os.path.join(base_dir, 'test.append')
  231. ret = self.run_state('file.touch', name=testfile)
  232. self.assertSaltTrueReturn(ret)
  233. ret = self.run_state(
  234. 'file.append',
  235. name=testfile,
  236. source='salt://testappend/firstif')
  237. self.assertSaltTrueReturn(ret)
  238. ret = self.run_state(
  239. 'file.append',
  240. name=testfile,
  241. source='salt://testappend/secondif')
  242. self.assertSaltTrueReturn(ret)
  243. with salt.utils.files.fopen(testfile, 'r') as fp_:
  244. testfile_contents = salt.utils.stringutils.to_unicode(fp_.read())
  245. contents = textwrap.dedent('''\
  246. # set variable identifying the chroot you work in (used in the prompt below)
  247. if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
  248. debian_chroot=$(cat /etc/debian_chroot)
  249. fi
  250. # enable bash completion in interactive shells
  251. if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
  252. . /etc/bash_completion
  253. fi
  254. ''')
  255. if salt.utils.platform.is_windows():
  256. new_contents = contents.splitlines()
  257. contents = os.linesep.join(new_contents)
  258. contents += os.linesep
  259. self.assertMultiLineEqual(contents, testfile_contents)
  260. ret = self.run_state(
  261. 'file.append',
  262. name=testfile,
  263. source='salt://testappend/secondif')
  264. self.assertSaltTrueReturn(ret)
  265. ret = self.run_state(
  266. 'file.append',
  267. name=testfile,
  268. source='salt://testappend/firstif')
  269. self.assertSaltTrueReturn(ret)
  270. with salt.utils.files.fopen(testfile, 'r') as fp_:
  271. testfile_contents = salt.utils.stringutils.to_unicode(fp_.read())
  272. self.assertMultiLineEqual(contents, testfile_contents)
  273. def test_issue_1876_syntax_error(self):
  274. '''
  275. verify that we catch the following syntax error::
  276. /tmp/salttest/issue-1876:
  277. file:
  278. - managed
  279. - source: salt://testfile
  280. file.append:
  281. - text: foo
  282. '''
  283. testfile = os.path.join(RUNTIME_VARS.TMP, 'issue-1876')
  284. sls = self.run_function('state.sls', mods='issue-1876')
  285. self.assertIn(
  286. 'ID \'{0}\' in SLS \'issue-1876\' contains multiple state '
  287. 'declarations of the same type'.format(testfile),
  288. sls
  289. )
  290. def test_issue_1879_too_simple_contains_check(self):
  291. expected = textwrap.dedent('''\
  292. # set variable identifying the chroot you work in (used in the prompt below)
  293. if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
  294. debian_chroot=$(cat /etc/debian_chroot)
  295. fi
  296. # enable bash completion in interactive shells
  297. if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
  298. . /etc/bash_completion
  299. fi
  300. ''')
  301. if salt.utils.platform.is_windows():
  302. new_contents = expected.splitlines()
  303. expected = os.linesep.join(new_contents)
  304. expected += os.linesep
  305. testfile = os.path.join(RUNTIME_VARS.TMP, 'issue-1879')
  306. # Delete if exiting
  307. if os.path.isfile(testfile):
  308. os.unlink(testfile)
  309. # Create the file
  310. ret = self.run_function('state.sls', mods='issue-1879', timeout=120)
  311. self.assertSaltTrueReturn(ret)
  312. # The first append
  313. ret = self.run_function(
  314. 'state.sls', mods='issue-1879.step-1', timeout=120
  315. )
  316. self.assertSaltTrueReturn(ret)
  317. # The second append
  318. ret = self.run_function(
  319. 'state.sls', mods='issue-1879.step-2', timeout=120
  320. )
  321. self.assertSaltTrueReturn(ret)
  322. # Does it match?
  323. try:
  324. with salt.utils.files.fopen(testfile, 'r') as fp_:
  325. contents = salt.utils.stringutils.to_unicode(fp_.read())
  326. self.assertMultiLineEqual(expected, contents)
  327. # Make sure we don't re-append existing text
  328. ret = self.run_function(
  329. 'state.sls', mods='issue-1879.step-1', timeout=120
  330. )
  331. self.assertSaltTrueReturn(ret)
  332. ret = self.run_function(
  333. 'state.sls', mods='issue-1879.step-2', timeout=120
  334. )
  335. self.assertSaltTrueReturn(ret)
  336. with salt.utils.files.fopen(testfile, 'r') as fp_:
  337. contents = salt.utils.stringutils.to_unicode(fp_.read())
  338. self.assertMultiLineEqual(expected, contents)
  339. except Exception:
  340. if os.path.exists(testfile):
  341. shutil.copy(testfile, testfile + '.bak')
  342. raise
  343. finally:
  344. if os.path.exists(testfile):
  345. os.unlink(testfile)
  346. def test_include(self):
  347. tempdir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
  348. self.addCleanup(shutil.rmtree, tempdir, ignore_errors=True)
  349. pillar = {}
  350. for path in ('include-test', 'to-include-test', 'exclude-test'):
  351. pillar[path] = os.path.join(tempdir, path)
  352. ret = self.run_function('state.sls', mods='include-test', pillar=pillar)
  353. self.assertSaltTrueReturn(ret)
  354. self.assertTrue(os.path.isfile(pillar['include-test']))
  355. self.assertTrue(os.path.isfile(pillar['to-include-test']))
  356. self.assertFalse(os.path.isfile(pillar['exclude-test']))
  357. def test_exclude(self):
  358. tempdir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
  359. self.addCleanup(shutil.rmtree, tempdir, ignore_errors=True)
  360. pillar = {}
  361. for path in ('include-test', 'exclude-test', 'to-include-test'):
  362. pillar[path] = os.path.join(tempdir, path)
  363. ret = self.run_function('state.sls', mods='exclude-test', pillar=pillar)
  364. self.assertSaltTrueReturn(ret)
  365. self.assertTrue(os.path.isfile(pillar['include-test']))
  366. self.assertTrue(os.path.isfile(pillar['exclude-test']))
  367. self.assertFalse(os.path.isfile(pillar['to-include-test']))
  368. @skipIf(salt.utils.path.which_bin(KNOWN_BINARY_NAMES) is None, 'virtualenv not installed')
  369. def test_issue_2068_template_str(self):
  370. venv_dir = os.path.join(
  371. RUNTIME_VARS.TMP, 'issue-2068-template-str'
  372. )
  373. try:
  374. ret = self.run_function(
  375. 'state.sls', mods='issue-2068-template-str-no-dot',
  376. timeout=120
  377. )
  378. self.assertSaltTrueReturn(ret)
  379. finally:
  380. if os.path.isdir(venv_dir):
  381. shutil.rmtree(venv_dir)
  382. # Let's load the template from the filesystem. If running this state
  383. # with state.sls works, so should using state.template_str
  384. template_path = os.path.join(
  385. os.path.dirname(os.path.dirname(__file__)),
  386. 'files', 'file', 'base', 'issue-2068-template-str-no-dot.sls'
  387. )
  388. with salt.utils.files.fopen(template_path, 'r') as fp_:
  389. template = salt.utils.stringutils.to_unicode(fp_.read())
  390. ret = self.run_function(
  391. 'state.template_str', [template], timeout=120
  392. )
  393. self.assertSaltTrueReturn(ret)
  394. # Now using state.template
  395. ret = self.run_function(
  396. 'state.template', [template_path], timeout=120
  397. )
  398. self.assertSaltTrueReturn(ret)
  399. # Now the problematic #2068 including dot's
  400. ret = self.run_function(
  401. 'state.sls', mods='issue-2068-template-str', timeout=120
  402. )
  403. self.assertSaltTrueReturn(ret)
  404. # Let's load the template from the filesystem. If running this state
  405. # with state.sls works, so should using state.template_str
  406. template_path = os.path.join(
  407. os.path.dirname(os.path.dirname(__file__)),
  408. 'files', 'file', 'base', 'issue-2068-template-str.sls'
  409. )
  410. with salt.utils.files.fopen(template_path, 'r') as fp_:
  411. template = salt.utils.stringutils.to_unicode(fp_.read())
  412. ret = self.run_function(
  413. 'state.template_str', [template], timeout=120
  414. )
  415. self.assertSaltTrueReturn(ret)
  416. # Now using state.template
  417. ret = self.run_function(
  418. 'state.template', [template_path], timeout=120
  419. )
  420. self.assertSaltTrueReturn(ret)
  421. def test_template_invalid_items(self):
  422. TEMPLATE = textwrap.dedent('''\
  423. {0}:
  424. - issue-2068-template-str
  425. /tmp/test-template-invalid-items:
  426. file:
  427. - managed
  428. - source: salt://testfile
  429. ''')
  430. for item in ('include', 'exclude', 'extends'):
  431. ret = self.run_function(
  432. 'state.template_str', [TEMPLATE.format(item)]
  433. )
  434. self.assertTrue(isinstance(ret, list))
  435. self.assertNotEqual(ret, [])
  436. self.assertEqual(
  437. ['The \'{0}\' declaration found on \'<template-str>\' is '
  438. 'invalid when rendering single templates'.format(item)],
  439. ret
  440. )
  441. def test_pydsl(self):
  442. '''
  443. Test the basics of the pydsl
  444. '''
  445. ret = self.run_function('state.sls', mods='pydsl-1')
  446. self.assertSaltTrueReturn(ret)
  447. def test_issues_7905_and_8174_sls_syntax_error(self):
  448. '''
  449. Call sls file with yaml syntax error.
  450. Ensure theses errors are detected and presented to the user without
  451. stack traces.
  452. '''
  453. ret = self.run_function('state.sls', mods='syntax.badlist')
  454. self.assertEqual(ret, [
  455. 'State \'A\' in SLS \'syntax.badlist\' is not formed as a list'
  456. ])
  457. ret = self.run_function('state.sls', mods='syntax.badlist2')
  458. self.assertEqual(ret, [
  459. 'State \'C\' in SLS \'syntax.badlist2\' is not formed as a list'
  460. ])
  461. def test_requisites_mixed_require_prereq_use(self):
  462. '''
  463. Call sls file containing several requisites.
  464. '''
  465. expected_simple_result = {
  466. 'cmd_|-A_|-echo A_|-run': {
  467. '__run_num__': 2,
  468. 'comment': 'Command "echo A" run',
  469. 'result': True,
  470. 'changes': True},
  471. 'cmd_|-B_|-echo B_|-run': {
  472. '__run_num__': 1,
  473. 'comment': 'Command "echo B" run',
  474. 'result': True,
  475. 'changes': True},
  476. 'cmd_|-C_|-echo C_|-run': {
  477. '__run_num__': 0,
  478. 'comment': 'Command "echo C" run',
  479. 'result': True,
  480. 'changes': True}
  481. }
  482. expected_result = {
  483. 'cmd_|-A_|-echo A fifth_|-run': {
  484. '__run_num__': 4,
  485. 'comment': 'Command "echo A fifth" run',
  486. 'result': True,
  487. 'changes': True},
  488. 'cmd_|-B_|-echo B third_|-run': {
  489. '__run_num__': 2,
  490. 'comment': 'Command "echo B third" run',
  491. 'result': True,
  492. 'changes': True},
  493. 'cmd_|-C_|-echo C second_|-run': {
  494. '__run_num__': 1,
  495. 'comment': 'Command "echo C second" run',
  496. 'result': True,
  497. 'changes': True},
  498. 'cmd_|-D_|-echo D first_|-run': {
  499. '__run_num__': 0,
  500. 'comment': 'Command "echo D first" run',
  501. 'result': True,
  502. 'changes': True},
  503. 'cmd_|-E_|-echo E fourth_|-run': {
  504. '__run_num__': 3,
  505. 'comment': 'Command "echo E fourth" run',
  506. 'result': True,
  507. 'changes': True}
  508. }
  509. expected_req_use_result = {
  510. 'cmd_|-A_|-echo A_|-run': {
  511. '__run_num__': 1,
  512. 'comment': 'Command "echo A" run',
  513. 'result': True,
  514. 'changes': True},
  515. 'cmd_|-B_|-echo B_|-run': {
  516. '__run_num__': 4,
  517. 'comment': 'Command "echo B" run',
  518. 'result': True,
  519. 'changes': True},
  520. 'cmd_|-C_|-echo C_|-run': {
  521. '__run_num__': 0,
  522. 'comment': 'Command "echo C" run',
  523. 'result': True,
  524. 'changes': True},
  525. 'cmd_|-D_|-echo D_|-run': {
  526. '__run_num__': 5,
  527. 'comment': 'Command "echo D" run',
  528. 'result': True,
  529. 'changes': True},
  530. 'cmd_|-E_|-echo E_|-run': {
  531. '__run_num__': 2,
  532. 'comment': 'Command "echo E" run',
  533. 'result': True,
  534. 'changes': True},
  535. 'cmd_|-F_|-echo F_|-run': {
  536. '__run_num__': 3,
  537. 'comment': 'Command "echo F" run',
  538. 'result': True,
  539. 'changes': True}
  540. }
  541. ret = self.run_function('state.sls', mods='requisites.mixed_simple')
  542. result = self.normalize_ret(ret)
  543. self.assertReturnNonEmptySaltType(ret)
  544. self.assertEqual(expected_simple_result, result)
  545. # test Traceback recursion prereq+require #8785
  546. # TODO: this is actually failing badly
  547. #ret = self.run_function('state.sls', mods='requisites.prereq_require_recursion_error2')
  548. #self.assertEqual(
  549. # ret,
  550. # ['A recursive requisite was found, SLS "requisites.prereq_require_recursion_error2" ID "B" ID "A"']
  551. #)
  552. # test Infinite recursion prereq+require #8785 v2
  553. # TODO: this is actually failing badly
  554. #ret = self.run_function('state.sls', mods='requisites.prereq_require_recursion_error3')
  555. #self.assertEqual(
  556. # ret,
  557. # ['A recursive requisite was found, SLS "requisites.prereq_require_recursion_error2" ID "B" ID "A"']
  558. #)
  559. # test Infinite recursion prereq+require #8785 v3
  560. # TODO: this is actually failing badly, and expected result is maybe not a recursion
  561. #ret = self.run_function('state.sls', mods='requisites.prereq_require_recursion_error4')
  562. #self.assertEqual(
  563. # ret,
  564. # ['A recursive requisite was found, SLS "requisites.prereq_require_recursion_error2" ID "B" ID "A"']
  565. #)
  566. # undetected infinite loopS prevents this test from running...
  567. # TODO: this is actually failing badly
  568. #ret = self.run_function('state.sls', mods='requisites.mixed_complex1')
  569. #result = self.normalize_ret(ret)
  570. #self.assertEqual(expected_result, result)
  571. def test_watch_in(self):
  572. '''
  573. test watch_in requisite when there is a success
  574. '''
  575. ret = self.run_function('state.sls', mods='requisites.watch_in')
  576. changes = 'test_|-return_changes_|-return_changes_|-succeed_with_changes'
  577. watch = 'test_|-watch_states_|-watch_states_|-succeed_without_changes'
  578. self.assertEqual(ret[changes]['__run_num__'], 0)
  579. self.assertEqual(ret[watch]['__run_num__'], 2)
  580. self.assertEqual('Watch statement fired.', ret[watch]['comment'])
  581. self.assertEqual('Something pretended to change',
  582. ret[changes]['changes']['testing']['new'])
  583. def test_watch_in_failure(self):
  584. '''
  585. test watch_in requisite when there is a failure
  586. '''
  587. ret = self.run_function('state.sls', mods='requisites.watch_in_failure')
  588. fail = 'test_|-return_changes_|-return_changes_|-fail_with_changes'
  589. watch = 'test_|-watch_states_|-watch_states_|-succeed_without_changes'
  590. self.assertEqual(False, ret[fail]['result'])
  591. self.assertEqual('One or more requisite failed: requisites.watch_in_failure.return_changes',
  592. ret[watch]['comment'])
  593. def normalize_ret(self, ret):
  594. '''
  595. Normalize the return to the format that we'll use for result checking
  596. '''
  597. result = {}
  598. for item, descr in six.iteritems(ret):
  599. result[item] = {
  600. '__run_num__': descr['__run_num__'],
  601. 'comment': descr['comment'],
  602. 'result': descr['result'],
  603. 'changes': descr['changes'] != {} # whether there where any changes
  604. }
  605. return result
  606. def test_requisites_require_ordering_and_errors(self):
  607. '''
  608. Call sls file containing several require_in and require.
  609. Ensure that some of them are failing and that the order is right.
  610. '''
  611. expected_result = {
  612. 'cmd_|-A_|-echo A fifth_|-run': {
  613. '__run_num__': 4,
  614. 'comment': 'Command "echo A fifth" run',
  615. 'result': True,
  616. 'changes': True,
  617. },
  618. 'cmd_|-B_|-echo B second_|-run': {
  619. '__run_num__': 1,
  620. 'comment': 'Command "echo B second" run',
  621. 'result': True,
  622. 'changes': True,
  623. },
  624. 'cmd_|-C_|-echo C third_|-run': {
  625. '__run_num__': 2,
  626. 'comment': 'Command "echo C third" run',
  627. 'result': True,
  628. 'changes': True,
  629. },
  630. 'cmd_|-D_|-echo D first_|-run': {
  631. '__run_num__': 0,
  632. 'comment': 'Command "echo D first" run',
  633. 'result': True,
  634. 'changes': True,
  635. },
  636. 'cmd_|-E_|-echo E fourth_|-run': {
  637. '__run_num__': 3,
  638. 'comment': 'Command "echo E fourth" run',
  639. 'result': True,
  640. 'changes': True,
  641. },
  642. 'cmd_|-F_|-echo F_|-run': {
  643. '__run_num__': 5,
  644. 'comment': 'The following requisites were not found:\n'
  645. + ' require:\n'
  646. + ' foobar: A\n',
  647. 'result': False,
  648. 'changes': False,
  649. },
  650. 'cmd_|-G_|-echo G_|-run': {
  651. '__run_num__': 6,
  652. 'comment': 'The following requisites were not found:\n'
  653. + ' require:\n'
  654. + ' cmd: Z\n',
  655. 'result': False,
  656. 'changes': False,
  657. },
  658. 'cmd_|-H_|-echo H_|-run': {
  659. '__run_num__': 7,
  660. 'comment': 'The following requisites were not found:\n'
  661. + ' require:\n'
  662. + ' cmd: Z\n',
  663. 'result': False,
  664. 'changes': False,
  665. }
  666. }
  667. ret = self.run_function('state.sls', mods='requisites.require')
  668. result = self.normalize_ret(ret)
  669. self.assertReturnNonEmptySaltType(ret)
  670. self.assertEqual(expected_result, result)
  671. ret = self.run_function('state.sls', mods='requisites.require_error1')
  672. self.assertEqual(ret, [
  673. "Cannot extend ID 'W' in 'base:requisites.require_error1'. It is not part of the high state.\nThis is likely due to a missing include statement or an incorrectly typed ID.\nEnsure that a state with an ID of 'W' is available\nin environment 'base' and to SLS 'requisites.require_error1'"
  674. ])
  675. # issue #8235
  676. # FIXME: Why is require enforcing list syntax while require_in does not?
  677. # And why preventing it?
  678. # Currently this state fails, should return C/B/A
  679. result = {}
  680. ret = self.run_function('state.sls', mods='requisites.require_simple_nolist')
  681. self.assertEqual(ret, [
  682. 'The require statement in state \'B\' in SLS '
  683. + '\'requisites.require_simple_nolist\' needs to be formed as a list'
  684. ])
  685. # commented until a fix is made for issue #8772
  686. # TODO: this test actually fails
  687. #ret = self.run_function('state.sls', mods='requisites.require_error2')
  688. #self.assertEqual(ret, [
  689. # 'Cannot extend state foobar for ID A in "base:requisites.require_error2".'
  690. # + ' It is not part of the high state.'
  691. #])
  692. ret = self.run_function('state.sls', mods='requisites.require_recursion_error1')
  693. self.assertEqual(
  694. ret,
  695. ['A recursive requisite was found, SLS "requisites.require_recursion_error1" ID "B" ID "A"']
  696. )
  697. def test_requisites_require_any(self):
  698. '''
  699. Call sls file containing several require_in and require.
  700. Ensure that some of them are failing and that the order is right.
  701. '''
  702. expected_result = {
  703. 'cmd_|-A_|-echo A_|-run': {
  704. '__run_num__': 3,
  705. 'comment': 'Command "echo A" run',
  706. 'result': True,
  707. 'changes': True,
  708. },
  709. 'cmd_|-B_|-echo B_|-run': {
  710. '__run_num__': 0,
  711. 'comment': 'Command "echo B" run',
  712. 'result': True,
  713. 'changes': True,
  714. },
  715. 'cmd_|-C_|-$(which false)_|-run': {
  716. '__run_num__': 1,
  717. 'comment': 'Command "$(which false)" run',
  718. 'result': False,
  719. 'changes': True,
  720. },
  721. 'cmd_|-D_|-echo D_|-run': {
  722. '__run_num__': 2,
  723. 'comment': 'Command "echo D" run',
  724. 'result': True,
  725. 'changes': True,
  726. },
  727. }
  728. ret = self.run_function('state.sls', mods='requisites.require_any')
  729. result = self.normalize_ret(ret)
  730. self.assertReturnNonEmptySaltType(ret)
  731. self.assertEqual(expected_result, result)
  732. def test_requisites_require_any_fail(self):
  733. '''
  734. Call sls file containing several require_in and require.
  735. Ensure that some of them are failing and that the order is right.
  736. '''
  737. ret = self.run_function('state.sls', mods='requisites.require_any_fail')
  738. result = self.normalize_ret(ret)
  739. self.assertReturnNonEmptySaltType(ret)
  740. self.assertIn('One or more requisite failed',
  741. result['cmd_|-D_|-echo D_|-run']['comment'])
  742. def test_requisites_watch_any(self):
  743. '''
  744. Call sls file containing several require_in and require.
  745. Ensure that some of them are failing and that the order is right.
  746. '''
  747. if salt.utils.platform.is_windows():
  748. cmd_true = 'exit'
  749. cmd_false = 'exit /B 1'
  750. else:
  751. cmd_true = 'true'
  752. cmd_false = 'false'
  753. expected_result = {
  754. 'cmd_|-A_|-{0}_|-wait'.format(cmd_true): {
  755. '__run_num__': 4,
  756. 'comment': 'Command "{0}" run'.format(cmd_true),
  757. 'result': True,
  758. 'changes': True,
  759. },
  760. 'cmd_|-B_|-{0}_|-run'.format(cmd_true): {
  761. '__run_num__': 0,
  762. 'comment': 'Command "{0}" run'.format(cmd_true),
  763. 'result': True,
  764. 'changes': True,
  765. },
  766. 'cmd_|-C_|-{0}_|-run'.format(cmd_false): {
  767. '__run_num__': 1,
  768. 'comment': 'Command "{0}" run'.format(cmd_false),
  769. 'result': False,
  770. 'changes': True,
  771. },
  772. 'cmd_|-D_|-{0}_|-run'.format(cmd_true): {
  773. '__run_num__': 2,
  774. 'comment': 'Command "{0}" run'.format(cmd_true),
  775. 'result': True,
  776. 'changes': True,
  777. },
  778. 'cmd_|-E_|-{0}_|-wait'.format(cmd_true): {
  779. '__run_num__': 9,
  780. 'comment': 'Command "{0}" run'.format(cmd_true),
  781. 'result': True,
  782. 'changes': True,
  783. },
  784. 'cmd_|-F_|-{0}_|-run'.format(cmd_true): {
  785. '__run_num__': 5,
  786. 'comment': 'Command "{0}" run'.format(cmd_true),
  787. 'result': True,
  788. 'changes': True,
  789. },
  790. 'cmd_|-G_|-{0}_|-run'.format(cmd_false): {
  791. '__run_num__': 6,
  792. 'comment': 'Command "{0}" run'.format(cmd_false),
  793. 'result': False,
  794. 'changes': True,
  795. },
  796. 'cmd_|-H_|-{0}_|-run'.format(cmd_false): {
  797. '__run_num__': 7,
  798. 'comment': 'Command "{0}" run'.format(cmd_false),
  799. 'result': False,
  800. 'changes': True,
  801. },
  802. }
  803. ret = self.run_function('state.sls', mods='requisites.watch_any')
  804. result = self.normalize_ret(ret)
  805. self.assertReturnNonEmptySaltType(ret)
  806. self.assertEqual(expected_result, result)
  807. def test_requisites_watch_any_fail(self):
  808. '''
  809. Call sls file containing several require_in and require.
  810. Ensure that some of them are failing and that the order is right.
  811. '''
  812. ret = self.run_function('state.sls', mods='requisites.watch_any_fail')
  813. result = self.normalize_ret(ret)
  814. self.assertReturnNonEmptySaltType(ret)
  815. self.assertIn('One or more requisite failed',
  816. result['cmd_|-A_|-true_|-wait']['comment'])
  817. def test_requisites_onchanges_any(self):
  818. '''
  819. Call sls file containing several require_in and require.
  820. Ensure that some of them are failing and that the order is right.
  821. '''
  822. expected_result = {
  823. 'cmd_|-another_changing_state_|-echo "Changed!"_|-run': {
  824. '__run_num__': 1,
  825. 'changes': True,
  826. 'comment': 'Command "echo "Changed!"" run',
  827. 'result': True
  828. },
  829. 'cmd_|-changing_state_|-echo "Changed!"_|-run': {
  830. '__run_num__': 0,
  831. 'changes': True,
  832. 'comment': 'Command "echo "Changed!"" run',
  833. 'result': True
  834. },
  835. 'cmd_|-test_one_changing_states_|-echo "Success!"_|-run': {
  836. '__run_num__': 4,
  837. 'changes': True,
  838. 'comment': 'Command "echo "Success!"" run',
  839. 'result': True
  840. },
  841. 'cmd_|-test_two_non_changing_states_|-echo "Should not run"_|-run': {
  842. '__run_num__': 5,
  843. 'changes': False,
  844. 'comment': 'State was not run because none of the onchanges reqs changed',
  845. 'result': True
  846. },
  847. 'pip_|-another_non_changing_state_|-mock_|-installed': {
  848. '__run_num__': 3,
  849. 'changes': False,
  850. 'comment': 'Python package mock was already installed\nAll specified packages are already installed',
  851. 'result': True
  852. },
  853. 'pip_|-non_changing_state_|-mock_|-installed': {
  854. '__run_num__': 2,
  855. 'changes': False,
  856. 'comment': 'Python package mock was already installed\nAll specified packages are already installed',
  857. 'result': True
  858. }
  859. }
  860. ret = self.run_function('state.sls', mods='requisites.onchanges_any')
  861. result = self.normalize_ret(ret)
  862. self.assertReturnNonEmptySaltType(ret)
  863. self.assertEqual(expected_result, result)
  864. def test_requisites_onfail_any(self):
  865. '''
  866. Call sls file containing several require_in and require.
  867. Ensure that some of them are failing and that the order is right.
  868. '''
  869. expected_result = {
  870. 'cmd_|-a_|-exit 0_|-run': {
  871. '__run_num__': 0,
  872. 'changes': True,
  873. 'comment': 'Command "exit 0" run',
  874. 'result': True
  875. },
  876. 'cmd_|-b_|-exit 1_|-run': {
  877. '__run_num__': 1,
  878. 'changes': True,
  879. 'comment': 'Command "exit 1" run',
  880. 'result': False
  881. },
  882. 'cmd_|-c_|-exit 0_|-run': {
  883. '__run_num__': 2,
  884. 'changes': True,
  885. 'comment': 'Command "exit 0" run',
  886. 'result': True
  887. },
  888. 'cmd_|-d_|-echo itworked_|-run': {
  889. '__run_num__': 3,
  890. 'changes': True,
  891. 'comment': 'Command "echo itworked" run',
  892. 'result': True},
  893. 'cmd_|-e_|-exit 0_|-run': {
  894. '__run_num__': 4,
  895. 'changes': True,
  896. 'comment': 'Command "exit 0" run',
  897. 'result': True
  898. },
  899. 'cmd_|-f_|-exit 0_|-run': {
  900. '__run_num__': 5,
  901. 'changes': True,
  902. 'comment': 'Command "exit 0" run',
  903. 'result': True
  904. },
  905. 'cmd_|-g_|-exit 0_|-run': {
  906. '__run_num__': 6,
  907. 'changes': True,
  908. 'comment': 'Command "exit 0" run',
  909. 'result': True
  910. },
  911. 'cmd_|-h_|-echo itworked_|-run': {
  912. '__run_num__': 7,
  913. 'changes': False,
  914. 'comment': 'State was not run because onfail req did not change',
  915. 'result': True
  916. }
  917. }
  918. ret = self.run_function('state.sls', mods='requisites.onfail_any')
  919. result = self.normalize_ret(ret)
  920. self.assertReturnNonEmptySaltType(ret)
  921. self.assertEqual(expected_result, result)
  922. def test_requisites_full_sls(self):
  923. '''
  924. Teste the sls special command in requisites
  925. '''
  926. expected_result = {
  927. 'cmd_|-A_|-echo A_|-run': {
  928. '__run_num__': 2,
  929. 'comment': 'Command "echo A" run',
  930. 'result': True,
  931. 'changes': True},
  932. 'cmd_|-B_|-echo B_|-run': {
  933. '__run_num__': 0,
  934. 'comment': 'Command "echo B" run',
  935. 'result': True,
  936. 'changes': True},
  937. 'cmd_|-C_|-echo C_|-run': {
  938. '__run_num__': 1,
  939. 'comment': 'Command "echo C" run',
  940. 'result': True,
  941. 'changes': True},
  942. }
  943. ret = self.run_function('state.sls', mods='requisites.fullsls_require')
  944. self.assertReturnNonEmptySaltType(ret)
  945. result = self.normalize_ret(ret)
  946. self.assertEqual(expected_result, result)
  947. # issue #8233: traceback on prereq sls
  948. # TODO: not done
  949. #ret = self.run_function('state.sls', mods='requisites.fullsls_prereq')
  950. #self.assertEqual(['sls command can only be used with require requisite'], ret)
  951. def test_requisites_require_no_state_module(self):
  952. '''
  953. Call sls file containing several require_in and require.
  954. Ensure that some of them are failing and that the order is right.
  955. '''
  956. expected_result = {
  957. 'cmd_|-A_|-echo A fifth_|-run': {
  958. '__run_num__': 4,
  959. 'comment': 'Command "echo A fifth" run',
  960. 'result': True,
  961. 'changes': True,
  962. },
  963. 'cmd_|-B_|-echo B second_|-run': {
  964. '__run_num__': 1,
  965. 'comment': 'Command "echo B second" run',
  966. 'result': True,
  967. 'changes': True,
  968. },
  969. 'cmd_|-C_|-echo C third_|-run': {
  970. '__run_num__': 2,
  971. 'comment': 'Command "echo C third" run',
  972. 'result': True,
  973. 'changes': True,
  974. },
  975. 'cmd_|-D_|-echo D first_|-run': {
  976. '__run_num__': 0,
  977. 'comment': 'Command "echo D first" run',
  978. 'result': True,
  979. 'changes': True,
  980. },
  981. 'cmd_|-E_|-echo E fourth_|-run': {
  982. '__run_num__': 3,
  983. 'comment': 'Command "echo E fourth" run',
  984. 'result': True,
  985. 'changes': True,
  986. },
  987. 'cmd_|-G_|-echo G_|-run': {
  988. '__run_num__': 5,
  989. 'comment': 'The following requisites were not found:\n'
  990. + ' require:\n'
  991. + ' id: Z\n',
  992. 'result': False,
  993. 'changes': False,
  994. },
  995. 'cmd_|-H_|-echo H_|-run': {
  996. '__run_num__': 6,
  997. 'comment': 'The following requisites were not found:\n'
  998. + ' require:\n'
  999. + ' id: Z\n',
  1000. 'result': False,
  1001. 'changes': False,
  1002. }
  1003. }
  1004. ret = self.run_function('state.sls', mods='requisites.require_no_state_module')
  1005. result = self.normalize_ret(ret)
  1006. self.assertReturnNonEmptySaltType(ret)
  1007. self.assertEqual(expected_result, result)
  1008. def test_requisites_prereq_simple_ordering_and_errors(self):
  1009. '''
  1010. Call sls file containing several prereq_in and prereq.
  1011. Ensure that some of them are failing and that the order is right.
  1012. '''
  1013. expected_result_simple = {
  1014. 'cmd_|-A_|-echo A third_|-run': {
  1015. '__run_num__': 2,
  1016. 'comment': 'Command "echo A third" run',
  1017. 'result': True,
  1018. 'changes': True},
  1019. 'cmd_|-B_|-echo B first_|-run': {
  1020. '__run_num__': 0,
  1021. 'comment': 'Command "echo B first" run',
  1022. 'result': True,
  1023. 'changes': True},
  1024. 'cmd_|-C_|-echo C second_|-run': {
  1025. '__run_num__': 1,
  1026. 'comment': 'Command "echo C second" run',
  1027. 'result': True,
  1028. 'changes': True},
  1029. 'cmd_|-I_|-echo I_|-run': {
  1030. '__run_num__': 3,
  1031. 'comment': 'The following requisites were not found:\n'
  1032. + ' prereq:\n'
  1033. + ' cmd: Z\n',
  1034. 'result': False,
  1035. 'changes': False},
  1036. 'cmd_|-J_|-echo J_|-run': {
  1037. '__run_num__': 4,
  1038. 'comment': 'The following requisites were not found:\n'
  1039. + ' prereq:\n'
  1040. + ' foobar: A\n',
  1041. 'result': False,
  1042. 'changes': False}
  1043. }
  1044. expected_result_simple_no_state_module = {
  1045. 'cmd_|-A_|-echo A third_|-run': {
  1046. '__run_num__': 2,
  1047. 'comment': 'Command "echo A third" run',
  1048. 'result': True,
  1049. 'changes': True},
  1050. 'cmd_|-B_|-echo B first_|-run': {
  1051. '__run_num__': 0,
  1052. 'comment': 'Command "echo B first" run',
  1053. 'result': True,
  1054. 'changes': True},
  1055. 'cmd_|-C_|-echo C second_|-run': {
  1056. '__run_num__': 1,
  1057. 'comment': 'Command "echo C second" run',
  1058. 'result': True,
  1059. 'changes': True},
  1060. 'cmd_|-I_|-echo I_|-run': {
  1061. '__run_num__': 3,
  1062. 'comment': 'The following requisites were not found:\n'
  1063. + ' prereq:\n'
  1064. + ' id: Z\n',
  1065. 'result': False,
  1066. 'changes': False}
  1067. }
  1068. expected_result_simple2 = {
  1069. 'cmd_|-A_|-echo A_|-run': {
  1070. '__run_num__': 1,
  1071. 'comment': 'Command "echo A" run',
  1072. 'result': True,
  1073. 'changes': True},
  1074. 'cmd_|-B_|-echo B_|-run': {
  1075. '__run_num__': 2,
  1076. 'comment': 'Command "echo B" run',
  1077. 'result': True,
  1078. 'changes': True},
  1079. 'cmd_|-C_|-echo C_|-run': {
  1080. '__run_num__': 0,
  1081. 'comment': 'Command "echo C" run',
  1082. 'result': True,
  1083. 'changes': True},
  1084. 'cmd_|-D_|-echo D_|-run': {
  1085. '__run_num__': 3,
  1086. 'comment': 'Command "echo D" run',
  1087. 'result': True,
  1088. 'changes': True},
  1089. 'cmd_|-E_|-echo E_|-run': {
  1090. '__run_num__': 4,
  1091. 'comment': 'Command "echo E" run',
  1092. 'result': True,
  1093. 'changes': True}
  1094. }
  1095. expected_result_simple3 = {
  1096. 'cmd_|-A_|-echo A first_|-run': {
  1097. '__run_num__': 0,
  1098. 'comment': 'Command "echo A first" run',
  1099. 'result': True,
  1100. 'changes': True,
  1101. },
  1102. 'cmd_|-B_|-echo B second_|-run': {
  1103. '__run_num__': 1,
  1104. 'comment': 'Command "echo B second" run',
  1105. 'result': True,
  1106. 'changes': True,
  1107. },
  1108. 'cmd_|-C_|-echo C third_|-wait': {
  1109. '__run_num__': 2,
  1110. 'comment': '',
  1111. 'result': True,
  1112. 'changes': False,
  1113. }
  1114. }
  1115. expected_result_complex = {
  1116. 'cmd_|-A_|-echo A fourth_|-run': {
  1117. '__run_num__': 3,
  1118. 'comment': 'Command "echo A fourth" run',
  1119. 'result': True,
  1120. 'changes': True},
  1121. 'cmd_|-B_|-echo B first_|-run': {
  1122. '__run_num__': 0,
  1123. 'comment': 'Command "echo B first" run',
  1124. 'result': True,
  1125. 'changes': True},
  1126. 'cmd_|-C_|-echo C second_|-run': {
  1127. '__run_num__': 1,
  1128. 'comment': 'Command "echo C second" run',
  1129. 'result': True,
  1130. 'changes': True},
  1131. 'cmd_|-D_|-echo D third_|-run': {
  1132. '__run_num__': 2,
  1133. 'comment': 'Command "echo D third" run',
  1134. 'result': True,
  1135. 'changes': True},
  1136. }
  1137. ret = self.run_function('state.sls', mods='requisites.prereq_simple')
  1138. self.assertReturnNonEmptySaltType(ret)
  1139. result = self.normalize_ret(ret)
  1140. self.assertEqual(expected_result_simple, result)
  1141. # same test, but not using lists in yaml syntax
  1142. # TODO: issue #8235, prereq ignored when not used in list syntax
  1143. # Currently fails badly with :
  1144. # TypeError encountered executing state.sls: string indices must be integers, not str.
  1145. #expected_result_simple.pop('cmd_|-I_|-echo I_|-run')
  1146. #expected_result_simple.pop('cmd_|-J_|-echo J_|-run')
  1147. #ret = self.run_function('state.sls', mods='requisites.prereq_simple_nolist')
  1148. #result = self.normalize_ret(ret)
  1149. #self.assertEqual(expected_result_simple, result)
  1150. ret = self.run_function('state.sls', mods='requisites.prereq_simple2')
  1151. result = self.normalize_ret(ret)
  1152. self.assertReturnNonEmptySaltType(ret)
  1153. self.assertEqual(expected_result_simple2, result)
  1154. ret = self.run_function('state.sls', mods='requisites.prereq_simple3')
  1155. result = self.normalize_ret(ret)
  1156. self.assertReturnNonEmptySaltType(ret)
  1157. self.assertEqual(expected_result_simple3, result)
  1158. #ret = self.run_function('state.sls', mods='requisites.prereq_error_nolist')
  1159. #self.assertEqual(
  1160. # ret,
  1161. # ['Cannot extend ID Z in "base:requisites.prereq_error_nolist".'
  1162. # + ' It is not part of the high state.']
  1163. #)
  1164. ret = self.run_function('state.sls', mods='requisites.prereq_compile_error1')
  1165. self.assertReturnNonEmptySaltType(ret)
  1166. self.assertEqual(
  1167. ret['cmd_|-B_|-echo B_|-run']['comment'],
  1168. 'The following requisites were not found:\n'
  1169. + ' prereq:\n'
  1170. + ' foobar: A\n'
  1171. )
  1172. ret = self.run_function('state.sls', mods='requisites.prereq_compile_error2')
  1173. self.assertReturnNonEmptySaltType(ret)
  1174. self.assertEqual(
  1175. ret['cmd_|-B_|-echo B_|-run']['comment'],
  1176. 'The following requisites were not found:\n'
  1177. + ' prereq:\n'
  1178. + ' foobar: C\n'
  1179. )
  1180. ret = self.run_function('state.sls', mods='requisites.prereq_complex')
  1181. result = self.normalize_ret(ret)
  1182. self.assertEqual(expected_result_complex, result)
  1183. # issue #8210 : prereq recursion undetected
  1184. # TODO: this test fails
  1185. #ret = self.run_function('state.sls', mods='requisites.prereq_recursion_error')
  1186. #self.assertEqual(
  1187. # ret,
  1188. # ['A recursive requisite was found, SLS "requisites.prereq_recursion_error" ID "B" ID "A"']
  1189. #)
  1190. ret = self.run_function('state.sls', mods='requisites.prereq_simple_no_state_module')
  1191. result = self.normalize_ret(ret)
  1192. self.assertEqual(expected_result_simple_no_state_module, result)
  1193. def test_infinite_recursion_sls_prereq(self):
  1194. ret = self.run_function('state.sls', mods='requisites.prereq_sls_infinite_recursion')
  1195. self.assertSaltTrueReturn(ret)
  1196. def test_requisites_use(self):
  1197. '''
  1198. Call sls file containing several use_in and use.
  1199. '''
  1200. # TODO issue #8235 & #8774 some examples are still commented in the test file
  1201. ret = self.run_function('state.sls', mods='requisites.use')
  1202. self.assertReturnNonEmptySaltType(ret)
  1203. for item, descr in six.iteritems(ret):
  1204. self.assertEqual(descr['comment'], 'onlyif condition is false')
  1205. # TODO: issue #8802 : use recursions undetected
  1206. # issue is closed as use does not actually inherit requisites
  1207. # if chain-use is added after #8774 resolution theses tests would maybe become useful
  1208. #ret = self.run_function('state.sls', mods='requisites.use_recursion')
  1209. #self.assertEqual(ret, [
  1210. # 'A recursive requisite was found, SLS "requisites.use_recursion"'
  1211. # + ' ID "B" ID "A"'
  1212. #])
  1213. #ret = self.run_function('state.sls', mods='requisites.use_recursion2')
  1214. #self.assertEqual(ret, [
  1215. # 'A recursive requisite was found, SLS "requisites.use_recursion2"'
  1216. # + ' ID "C" ID "A"'
  1217. #])
  1218. #ret = self.run_function('state.sls', mods='requisites.use_auto_recursion')
  1219. #self.assertEqual(ret, [
  1220. # 'A recursive requisite was found, SLS "requisites.use_recursion"'
  1221. # + ' ID "A" ID "A"'
  1222. #])
  1223. def test_requisites_use_no_state_module(self):
  1224. '''
  1225. Call sls file containing several use_in and use.
  1226. '''
  1227. ret = self.run_function('state.sls', mods='requisites.use_no_state_module')
  1228. self.assertReturnNonEmptySaltType(ret)
  1229. for item, descr in six.iteritems(ret):
  1230. self.assertEqual(descr['comment'], 'onlyif condition is false')
  1231. def test_onlyif_req(self):
  1232. ret = self.run_function(
  1233. 'state.single',
  1234. fun='test.succeed_with_changes',
  1235. name='onlyif test',
  1236. onlyif=[
  1237. {}
  1238. ],
  1239. )['test_|-onlyif test_|-onlyif test_|-succeed_with_changes']
  1240. self.assertTrue(ret['result'])
  1241. self.assertEqual(ret['comment'], 'Success!')
  1242. ret = self.run_function(
  1243. 'state.single',
  1244. fun='test.fail_with_changes',
  1245. name='onlyif test',
  1246. onlyif=[
  1247. {'fun': 'test.false'},
  1248. ],
  1249. )['test_|-onlyif test_|-onlyif test_|-fail_with_changes']
  1250. self.assertTrue(ret['result'])
  1251. self.assertFalse(ret['changes'])
  1252. self.assertEqual(ret['comment'], 'onlyif condition is false')
  1253. ret = self.run_function(
  1254. 'state.single',
  1255. fun='test.fail_with_changes',
  1256. name='onlyif test',
  1257. onlyif=[
  1258. {'fun': 'test.true'},
  1259. ],
  1260. )['test_|-onlyif test_|-onlyif test_|-fail_with_changes']
  1261. self.assertFalse(ret['result'])
  1262. self.assertTrue(ret['changes'])
  1263. self.assertEqual(ret['comment'], 'Failure!')
  1264. ret = self.run_function(
  1265. 'state.single',
  1266. fun='test.succeed_without_changes',
  1267. name='onlyif test',
  1268. onlyif=[
  1269. {'fun': 'test.true'},
  1270. ],
  1271. )['test_|-onlyif test_|-onlyif test_|-succeed_without_changes']
  1272. self.assertTrue(ret['result'])
  1273. self.assertFalse(ret['changes'])
  1274. self.assertEqual(ret['comment'], 'Success!')
  1275. def test_onlyif_req_retcode(self):
  1276. ret = self.run_function(
  1277. 'state.single',
  1278. fun='test.succeed_with_changes',
  1279. name='onlyif test',
  1280. onlyif=[
  1281. {'fun': 'test.retcode'},
  1282. ],
  1283. )['test_|-onlyif test_|-onlyif test_|-succeed_with_changes']
  1284. self.assertTrue(ret['result'])
  1285. self.assertFalse(ret['changes'])
  1286. self.assertEqual(ret['comment'], 'onlyif condition is false')
  1287. ret = self.run_function(
  1288. 'state.single',
  1289. fun='test.succeed_with_changes',
  1290. name='onlyif test',
  1291. onlyif=[
  1292. {'fun': 'test.retcode', 'code': 0},
  1293. ],
  1294. )['test_|-onlyif test_|-onlyif test_|-succeed_with_changes']
  1295. self.assertTrue(ret['result'])
  1296. self.assertTrue(ret['changes'])
  1297. self.assertEqual(ret['comment'], 'Success!')
  1298. def test_unless_req(self):
  1299. ret = self.run_function(
  1300. 'state.single',
  1301. fun='test.succeed_with_changes',
  1302. name='unless test',
  1303. unless=[
  1304. {}
  1305. ],
  1306. )['test_|-unless test_|-unless test_|-succeed_with_changes']
  1307. self.assertTrue(ret['result'])
  1308. self.assertEqual(ret['comment'], 'Success!')
  1309. ret = self.run_function(
  1310. 'state.single',
  1311. fun='test.fail_with_changes',
  1312. name='unless test',
  1313. unless=[
  1314. {'fun': 'test.true'},
  1315. ],
  1316. )['test_|-unless test_|-unless test_|-fail_with_changes']
  1317. self.assertTrue(ret['result'])
  1318. self.assertFalse(ret['changes'])
  1319. self.assertEqual(ret['comment'], 'unless condition is true')
  1320. ret = self.run_function(
  1321. 'state.single',
  1322. fun='test.fail_with_changes',
  1323. name='unless test',
  1324. unless=[
  1325. {'fun': 'test.false'},
  1326. ],
  1327. )['test_|-unless test_|-unless test_|-fail_with_changes']
  1328. self.assertFalse(ret['result'])
  1329. self.assertTrue(ret['changes'])
  1330. self.assertEqual(ret['comment'], 'Failure!')
  1331. ret = self.run_function(
  1332. 'state.single',
  1333. fun='test.succeed_without_changes',
  1334. name='unless test',
  1335. unless=[
  1336. {'fun': 'test.false'},
  1337. ],
  1338. )['test_|-unless test_|-unless test_|-succeed_without_changes']
  1339. self.assertTrue(ret['result'])
  1340. self.assertFalse(ret['changes'])
  1341. self.assertEqual(ret['comment'], 'Success!')
  1342. def test_unless_req_retcode(self):
  1343. ret = self.run_function(
  1344. 'state.single',
  1345. fun='test.succeed_with_changes',
  1346. name='unless test',
  1347. unless=[
  1348. {'fun': 'test.retcode'},
  1349. ],
  1350. )['test_|-unless test_|-unless test_|-succeed_with_changes']
  1351. self.assertTrue(ret['result'])
  1352. self.assertTrue(ret['changes'])
  1353. self.assertEqual(ret['comment'], 'Success!')
  1354. ret = self.run_function(
  1355. 'state.single',
  1356. fun='test.succeed_with_changes',
  1357. name='unless test',
  1358. unless=[
  1359. {'fun': 'test.retcode', 'code': 0},
  1360. ],
  1361. )['test_|-unless test_|-unless test_|-succeed_with_changes']
  1362. self.assertTrue(ret['result'])
  1363. self.assertFalse(ret['changes'])
  1364. self.assertEqual(ret['comment'], 'unless condition is true')
  1365. def test_get_file_from_env_in_top_match(self):
  1366. tgt = os.path.join(RUNTIME_VARS.TMP, 'prod-cheese-file')
  1367. try:
  1368. ret = self.run_function(
  1369. 'state.highstate', minion_tgt='sub_minion'
  1370. )
  1371. self.assertSaltTrueReturn(ret)
  1372. self.assertTrue(os.path.isfile(tgt))
  1373. with salt.utils.files.fopen(tgt, 'r') as cheese:
  1374. data = salt.utils.stringutils.to_unicode(cheese.read())
  1375. self.assertIn('Gromit', data)
  1376. self.assertIn('Comte', data)
  1377. finally:
  1378. if os.path.islink(tgt):
  1379. os.unlink(tgt)
  1380. # onchanges tests
  1381. def test_onchanges_requisite(self):
  1382. '''
  1383. Tests a simple state using the onchanges requisite
  1384. '''
  1385. # Only run the state once and keep the return data
  1386. state_run = self.run_function('state.sls', mods='requisites.onchanges_simple')
  1387. # First, test the result of the state run when changes are expected to happen
  1388. test_data = state_run['cmd_|-test_changing_state_|-echo "Success!"_|-run']['comment']
  1389. expected_result = 'Command "echo "Success!"" run'
  1390. self.assertIn(expected_result, test_data)
  1391. # Then, test the result of the state run when changes are not expected to happen
  1392. test_data = state_run['cmd_|-test_non_changing_state_|-echo "Should not run"_|-run']['comment']
  1393. expected_result = 'State was not run because none of the onchanges reqs changed'
  1394. self.assertIn(expected_result, test_data)
  1395. def test_onchanges_requisite_multiple(self):
  1396. '''
  1397. Tests a simple state using the onchanges requisite
  1398. '''
  1399. # Only run the state once and keep the return data
  1400. state_run = self.run_function('state.sls',
  1401. mods='requisites.onchanges_multiple')
  1402. # First, test the result of the state run when two changes are expected to happen
  1403. test_data = state_run['cmd_|-test_two_changing_states_|-echo "Success!"_|-run']['comment']
  1404. expected_result = 'Command "echo "Success!"" run'
  1405. self.assertIn(expected_result, test_data)
  1406. # Then, test the result of the state run when two changes are not expected to happen
  1407. test_data = state_run['cmd_|-test_two_non_changing_states_|-echo "Should not run"_|-run']['comment']
  1408. expected_result = 'State was not run because none of the onchanges reqs changed'
  1409. self.assertIn(expected_result, test_data)
  1410. # Finally, test the result of the state run when only one of the onchanges requisites changes.
  1411. test_data = state_run['cmd_|-test_one_changing_state_|-echo "Success!"_|-run']['comment']
  1412. expected_result = 'Command "echo "Success!"" run'
  1413. self.assertIn(expected_result, test_data)
  1414. def test_onchanges_in_requisite(self):
  1415. '''
  1416. Tests a simple state using the onchanges_in requisite
  1417. '''
  1418. # Only run the state once and keep the return data
  1419. state_run = self.run_function('state.sls', mods='requisites.onchanges_in_simple')
  1420. # First, test the result of the state run of when changes are expected to happen
  1421. test_data = state_run['cmd_|-test_changes_expected_|-echo "Success!"_|-run']['comment']
  1422. expected_result = 'Command "echo "Success!"" run'
  1423. self.assertIn(expected_result, test_data)
  1424. # Then, test the result of the state run when changes are not expected to happen
  1425. test_data = state_run['cmd_|-test_changes_not_expected_|-echo "Should not run"_|-run']['comment']
  1426. expected_result = 'State was not run because none of the onchanges reqs changed'
  1427. self.assertIn(expected_result, test_data)
  1428. def test_onchanges_requisite_no_state_module(self):
  1429. '''
  1430. Tests a simple state using the onchanges requisite without state modules
  1431. '''
  1432. # Only run the state once and keep the return data
  1433. state_run = self.run_function('state.sls', mods='requisites.onchanges_simple_no_state_module')
  1434. test_data = state_run['cmd_|-test_changing_state_|-echo "Success!"_|-run']['comment']
  1435. expected_result = 'Command "echo "Success!"" run'
  1436. self.assertIn(expected_result, test_data)
  1437. def test_onchanges_requisite_with_duration(self):
  1438. '''
  1439. Tests a simple state using the onchanges requisite
  1440. the state will not run but results will include duration
  1441. '''
  1442. # Only run the state once and keep the return data
  1443. state_run = self.run_function('state.sls', mods='requisites.onchanges_simple')
  1444. # Then, test the result of the state run when changes are not expected to happen
  1445. # and ensure duration is included in the results
  1446. test_data = state_run['cmd_|-test_non_changing_state_|-echo "Should not run"_|-run']
  1447. self.assertIn('duration', test_data)
  1448. # onfail tests
  1449. def test_onfail_requisite(self):
  1450. '''
  1451. Tests a simple state using the onfail requisite
  1452. '''
  1453. # Only run the state once and keep the return data
  1454. state_run = self.run_function('state.sls', mods='requisites.onfail_simple')
  1455. # First, test the result of the state run when a failure is expected to happen
  1456. test_data = state_run['cmd_|-test_failing_state_|-echo "Success!"_|-run']['comment']
  1457. expected_result = 'Command "echo "Success!"" run'
  1458. self.assertIn(expected_result, test_data)
  1459. # Then, test the result of the state run when a failure is not expected to happen
  1460. test_data = state_run['cmd_|-test_non_failing_state_|-echo "Should not run"_|-run']['comment']
  1461. expected_result = 'State was not run because onfail req did not change'
  1462. self.assertIn(expected_result, test_data)
  1463. def test_multiple_onfail_requisite(self):
  1464. '''
  1465. test to ensure state is run even if only one
  1466. of the onfails fails. This is a test for the issue:
  1467. https://github.com/saltstack/salt/issues/22370
  1468. '''
  1469. state_run = self.run_function('state.sls',
  1470. mods='requisites.onfail_multiple',
  1471. timeout=self.TIMEOUT)
  1472. retcode = state_run['cmd_|-c_|-echo itworked_|-run']['changes']['retcode']
  1473. self.assertEqual(retcode, 0)
  1474. stdout = state_run['cmd_|-c_|-echo itworked_|-run']['changes']['stdout']
  1475. self.assertEqual(stdout, 'itworked')
  1476. def test_onfail_in_requisite(self):
  1477. '''
  1478. Tests a simple state using the onfail_in requisite
  1479. '''
  1480. # Only run the state once and keep the return data
  1481. state_run = self.run_function('state.sls', mods='requisites.onfail_in_simple')
  1482. # First, test the result of the state run when a failure is expected to happen
  1483. test_data = state_run['cmd_|-test_failing_state_|-echo "Success!"_|-run']['comment']
  1484. expected_result = 'Command "echo "Success!"" run'
  1485. self.assertIn(expected_result, test_data)
  1486. # Then, test the result of the state run when a failure is not expected to happen
  1487. test_data = state_run['cmd_|-test_non_failing_state_|-echo "Should not run"_|-run']['comment']
  1488. expected_result = 'State was not run because onfail req did not change'
  1489. self.assertIn(expected_result, test_data)
  1490. def test_onfail_requisite_no_state_module(self):
  1491. '''
  1492. Tests a simple state using the onfail requisite
  1493. '''
  1494. # Only run the state once and keep the return data
  1495. state_run = self.run_function('state.sls', mods='requisites.onfail_simple_no_state_module')
  1496. # First, test the result of the state run when a failure is expected to happen
  1497. test_data = state_run['cmd_|-test_failing_state_|-echo "Success!"_|-run']['comment']
  1498. expected_result = 'Command "echo "Success!"" run'
  1499. self.assertIn(expected_result, test_data)
  1500. # Then, test the result of the state run when a failure is not expected to happen
  1501. test_data = state_run['cmd_|-test_non_failing_state_|-echo "Should not run"_|-run']['comment']
  1502. expected_result = 'State was not run because onfail req did not change'
  1503. self.assertIn(expected_result, test_data)
  1504. def test_onfail_requisite_with_duration(self):
  1505. '''
  1506. Tests a simple state using the onfail requisite
  1507. '''
  1508. # Only run the state once and keep the return data
  1509. state_run = self.run_function('state.sls', mods='requisites.onfail_simple')
  1510. # Then, test the result of the state run when a failure is not expected to happen
  1511. test_data = state_run['cmd_|-test_non_failing_state_|-echo "Should not run"_|-run']
  1512. self.assertIn('duration', test_data)
  1513. def test_multiple_onfail_requisite_with_required(self):
  1514. '''
  1515. test to ensure multiple states are run
  1516. when specified as onfails for a single state.
  1517. This is a test for the issue:
  1518. https://github.com/saltstack/salt/issues/46552
  1519. '''
  1520. state_run = self.run_function('state.sls', mods='requisites.onfail_multiple_required')
  1521. retcode = state_run['cmd_|-b_|-echo b_|-run']['changes']['retcode']
  1522. self.assertEqual(retcode, 0)
  1523. retcode = state_run['cmd_|-c_|-echo c_|-run']['changes']['retcode']
  1524. self.assertEqual(retcode, 0)
  1525. retcode = state_run['cmd_|-d_|-echo d_|-run']['changes']['retcode']
  1526. self.assertEqual(retcode, 0)
  1527. stdout = state_run['cmd_|-b_|-echo b_|-run']['changes']['stdout']
  1528. self.assertEqual(stdout, 'b')
  1529. stdout = state_run['cmd_|-c_|-echo c_|-run']['changes']['stdout']
  1530. self.assertEqual(stdout, 'c')
  1531. stdout = state_run['cmd_|-d_|-echo d_|-run']['changes']['stdout']
  1532. self.assertEqual(stdout, 'd')
  1533. def test_multiple_onfail_requisite_with_required_no_run(self):
  1534. '''
  1535. test to ensure multiple states are not run
  1536. when specified as onfails for a single state
  1537. which fails.
  1538. This is a test for the issue:
  1539. https://github.com/saltstack/salt/issues/46552
  1540. '''
  1541. state_run = self.run_function('state.sls', mods='requisites.onfail_multiple_required_no_run')
  1542. expected = 'State was not run because onfail req did not change'
  1543. stdout = state_run['cmd_|-b_|-echo b_|-run']['comment']
  1544. self.assertEqual(stdout, expected)
  1545. stdout = state_run['cmd_|-c_|-echo c_|-run']['comment']
  1546. self.assertEqual(stdout, expected)
  1547. stdout = state_run['cmd_|-d_|-echo d_|-run']['comment']
  1548. self.assertEqual(stdout, expected)
  1549. # listen tests
  1550. def test_listen_requisite(self):
  1551. '''
  1552. Tests a simple state using the listen requisite
  1553. '''
  1554. # Only run the state once and keep the return data
  1555. state_run = self.run_function('state.sls', mods='requisites.listen_simple')
  1556. # First, test the result of the state run when a listener is expected to trigger
  1557. listener_state = 'cmd_|-listener_test_listening_change_state_|-echo "Listening State"_|-mod_watch'
  1558. self.assertIn(listener_state, state_run)
  1559. # Then, test the result of the state run when a listener should not trigger
  1560. absent_state = 'cmd_|-listener_test_listening_non_changing_state_|-echo "Only run once"_|-mod_watch'
  1561. self.assertNotIn(absent_state, state_run)
  1562. def test_listen_in_requisite(self):
  1563. '''
  1564. Tests a simple state using the listen_in requisite
  1565. '''
  1566. # Only run the state once and keep the return data
  1567. state_run = self.run_function('state.sls', mods='requisites.listen_in_simple')
  1568. # First, test the result of the state run when a listener is expected to trigger
  1569. listener_state = 'cmd_|-listener_test_listening_change_state_|-echo "Listening State"_|-mod_watch'
  1570. self.assertIn(listener_state, state_run)
  1571. # Then, test the result of the state run when a listener should not trigger
  1572. absent_state = 'cmd_|-listener_test_listening_non_changing_state_|-echo "Only run once"_|-mod_watch'
  1573. self.assertNotIn(absent_state, state_run)
  1574. def test_listen_in_requisite_resolution(self):
  1575. '''
  1576. Verify listen_in requisite lookups use ID declaration to check for changes
  1577. '''
  1578. # Only run the state once and keep the return data
  1579. state_run = self.run_function('state.sls', mods='requisites.listen_in_simple')
  1580. # Test the result of the state run when a listener is expected to trigger
  1581. listener_state = 'cmd_|-listener_test_listen_in_resolution_|-echo "Successful listen_in resolution"_|-mod_watch'
  1582. self.assertIn(listener_state, state_run)
  1583. def test_listen_requisite_resolution(self):
  1584. '''
  1585. Verify listen requisite lookups use ID declaration to check for changes
  1586. '''
  1587. # Only run the state once and keep the return data
  1588. state_run = self.run_function('state.sls', mods='requisites.listen_simple')
  1589. # Both listeners are expected to trigger
  1590. listener_state = 'cmd_|-listener_test_listening_resolution_one_|-echo "Successful listen resolution"_|-mod_watch'
  1591. self.assertIn(listener_state, state_run)
  1592. listener_state = 'cmd_|-listener_test_listening_resolution_two_|-echo "Successful listen resolution"_|-mod_watch'
  1593. self.assertIn(listener_state, state_run)
  1594. def test_listen_requisite_no_state_module(self):
  1595. '''
  1596. Tests a simple state using the listen requisite
  1597. '''
  1598. # Only run the state once and keep the return data
  1599. state_run = self.run_function('state.sls', mods='requisites.listen_simple_no_state_module')
  1600. # First, test the result of the state run when a listener is expected to trigger
  1601. listener_state = 'cmd_|-listener_test_listening_change_state_|-echo "Listening State"_|-mod_watch'
  1602. self.assertIn(listener_state, state_run)
  1603. # Then, test the result of the state run when a listener should not trigger
  1604. absent_state = 'cmd_|-listener_test_listening_non_changing_state_|-echo "Only run once"_|-mod_watch'
  1605. self.assertNotIn(absent_state, state_run)
  1606. def test_listen_in_requisite_resolution_names(self):
  1607. '''
  1608. Verify listen_in requisite lookups use ID declaration to check for changes
  1609. and resolves magic names state variable
  1610. '''
  1611. # Only run the state once and keep the return data
  1612. state_run = self.run_function('state.sls', mods='requisites.listen_in_names')
  1613. self.assertIn('test_|-listener_service_|-nginx_|-mod_watch', state_run)
  1614. self.assertIn('test_|-listener_service_|-crond_|-mod_watch', state_run)
  1615. def test_listen_requisite_resolution_names(self):
  1616. '''
  1617. Verify listen requisite lookups use ID declaration to check for changes
  1618. and resolves magic names state variable
  1619. '''
  1620. # Only run the state once and keep the return data
  1621. state_run = self.run_function('state.sls',
  1622. mods='requisites.listen_names',
  1623. timeout=self.TIMEOUT)
  1624. self.assertIn('test_|-listener_service_|-nginx_|-mod_watch', state_run)
  1625. self.assertIn('test_|-listener_service_|-crond_|-mod_watch', state_run)
  1626. def test_issue_30820_requisite_in_match_by_name(self):
  1627. '''
  1628. This tests the case where a requisite_in matches by name instead of ID
  1629. See https://github.com/saltstack/salt/issues/30820 for more info
  1630. '''
  1631. state_run = self.run_function(
  1632. 'state.sls',
  1633. mods='requisites.requisite_in_match_by_name'
  1634. )
  1635. bar_state = 'cmd_|-bar state_|-echo bar_|-wait'
  1636. self.assertIn(bar_state, state_run)
  1637. self.assertEqual(state_run[bar_state]['comment'],
  1638. 'Command "echo bar" run')
  1639. def test_retry_option_defaults(self):
  1640. '''
  1641. test the retry option on a simple state with defaults
  1642. ensure comment is as expected
  1643. ensure state duration is greater than default retry_interval (30 seconds)
  1644. '''
  1645. state_run = self.run_function(
  1646. 'state.sls',
  1647. mods='retry.retry_defaults'
  1648. )
  1649. retry_state = 'file_|-file_test_|-/path/to/a/non-existent/file.txt_|-exists'
  1650. expected_comment = ('Attempt 1: Returned a result of "False", with the following '
  1651. 'comment: "Specified path /path/to/a/non-existent/file.txt does not exist"\n'
  1652. 'Specified path /path/to/a/non-existent/file.txt does not exist')
  1653. self.assertEqual(state_run[retry_state]['comment'], expected_comment)
  1654. self.assertTrue(state_run[retry_state]['duration'] > 30)
  1655. self.assertEqual(state_run[retry_state]['result'], False)
  1656. def test_retry_option_custom(self):
  1657. '''
  1658. test the retry option on a simple state with custom retry values
  1659. ensure comment is as expected
  1660. ensure state duration is greater than custom defined interval * (retries - 1)
  1661. '''
  1662. state_run = self.run_function(
  1663. 'state.sls',
  1664. mods='retry.retry_custom'
  1665. )
  1666. retry_state = 'file_|-file_test_|-/path/to/a/non-existent/file.txt_|-exists'
  1667. expected_comment = ('Attempt 1: Returned a result of "False", with the following '
  1668. 'comment: "Specified path /path/to/a/non-existent/file.txt does not exist"\n'
  1669. 'Attempt 2: Returned a result of "False", with the following comment: "Specified'
  1670. ' path /path/to/a/non-existent/file.txt does not exist"\nAttempt 3: Returned'
  1671. ' a result of "False", with the following comment: "Specified path'
  1672. ' /path/to/a/non-existent/file.txt does not exist"\nAttempt 4: Returned a'
  1673. ' result of "False", with the following comment: "Specified path'
  1674. ' /path/to/a/non-existent/file.txt does not exist"\nSpecified path'
  1675. ' /path/to/a/non-existent/file.txt does not exist')
  1676. self.assertEqual(state_run[retry_state]['comment'], expected_comment)
  1677. self.assertTrue(state_run[retry_state]['duration'] > 40)
  1678. self.assertEqual(state_run[retry_state]['result'], False)
  1679. def test_retry_option_success(self):
  1680. '''
  1681. test a state with the retry option that should return True immedietly (i.e. no retries)
  1682. '''
  1683. testfile = os.path.join(RUNTIME_VARS.TMP, 'retry_file_option_success')
  1684. state_run = self.run_function(
  1685. 'state.sls',
  1686. mods='retry.retry_success'
  1687. )
  1688. os.unlink(testfile)
  1689. retry_state = 'file_|-file_test_|-{0}_|-exists'.format(testfile)
  1690. self.assertNotIn('Attempt', state_run[retry_state]['comment'])
  1691. def run_create(self, testfile):
  1692. '''
  1693. helper function to wait 30 seconds and then create the temp retry file
  1694. '''
  1695. # Wait for the requisite stae 'file_test_a' to complete before creating
  1696. # test_file
  1697. while True:
  1698. if os.path.exists(testfile + '_a'):
  1699. break
  1700. time.sleep(1)
  1701. time.sleep(30)
  1702. with salt.utils.files.fopen(testfile, 'a'):
  1703. pass
  1704. def test_retry_option_eventual_success(self):
  1705. '''
  1706. test a state with the retry option that should return True after at least 4 retry attmempt
  1707. but never run 15 attempts
  1708. '''
  1709. testfile = os.path.join(RUNTIME_VARS.TMP, 'retry_file_eventual_success')
  1710. assert not os.path.exists(testfile + '_a')
  1711. assert not os.path.exists(testfile)
  1712. create_thread = threading.Thread(target=self.run_create, args=(testfile,))
  1713. create_thread.start()
  1714. state_run = self.run_function(
  1715. 'state.sls',
  1716. mods='retry.retry_success2'
  1717. )
  1718. retry_state = 'file_|-file_test_b_|-{0}_|-exists'.format(testfile)
  1719. self.assertIn('Attempt 1:', state_run[retry_state]['comment'])
  1720. self.assertIn('Attempt 2:', state_run[retry_state]['comment'])
  1721. self.assertIn('Attempt 3:', state_run[retry_state]['comment'])
  1722. self.assertIn('Attempt 4:', state_run[retry_state]['comment'])
  1723. self.assertNotIn('Attempt 15:', state_run[retry_state]['comment'])
  1724. self.assertEqual(state_run[retry_state]['result'], True)
  1725. def test_issue_38683_require_order_failhard_combination(self):
  1726. '''
  1727. This tests the case where require, order, and failhard are all used together in a state definition.
  1728. Previously, the order option, which used in tandem with require and failhard, would cause the state
  1729. compiler to stacktrace. This exposed a logic error in the ``check_failhard`` function of the state
  1730. compiler. With the logic error resolved, this test should now pass.
  1731. See https://github.com/saltstack/salt/issues/38683 for more information.
  1732. '''
  1733. state_run = self.run_function(
  1734. 'state.sls',
  1735. mods='requisites.require_order_failhard_combo'
  1736. )
  1737. state_id = 'test_|-b_|-b_|-fail_with_changes'
  1738. self.assertIn(state_id, state_run)
  1739. self.assertEqual(state_run[state_id]['comment'], 'Failure!')
  1740. self.assertFalse(state_run[state_id]['result'])
  1741. def test_issue_46762_prereqs_on_a_state_with_unfulfilled_requirements(self):
  1742. '''
  1743. This tests the case where state C requires state A, which fails.
  1744. State C is a pre-required state for State B.
  1745. Since state A fails, state C will not run because the requisite failed,
  1746. therefore state B will not run because state C failed to run.
  1747. See https://github.com/saltstack/salt/issues/46762 for
  1748. more information.
  1749. '''
  1750. state_run = self.run_function(
  1751. 'state.sls',
  1752. mods='issue-46762'
  1753. )
  1754. state_id = 'test_|-a_|-a_|-fail_without_changes'
  1755. self.assertIn(state_id, state_run)
  1756. self.assertEqual(state_run[state_id]['comment'],
  1757. 'Failure!')
  1758. self.assertFalse(state_run[state_id]['result'])
  1759. state_id = 'test_|-b_|-b_|-nop'
  1760. self.assertIn(state_id, state_run)
  1761. self.assertEqual(state_run[state_id]['comment'],
  1762. 'One or more requisite failed: issue-46762.c')
  1763. self.assertFalse(state_run[state_id]['result'])
  1764. state_id = 'test_|-c_|-c_|-nop'
  1765. self.assertIn(state_id, state_run)
  1766. self.assertEqual(state_run[state_id]['comment'],
  1767. 'One or more requisite failed: issue-46762.a')
  1768. self.assertFalse(state_run[state_id]['result'])
  1769. def test_state_nonbase_environment(self):
  1770. '''
  1771. test state.sls with saltenv using a nonbase environment
  1772. with a salt source
  1773. '''
  1774. filename = os.path.join(RUNTIME_VARS.TMP, 'nonbase_env')
  1775. try:
  1776. ret = self.run_function(
  1777. 'state.sls',
  1778. mods='non-base-env',
  1779. saltenv='prod'
  1780. )
  1781. ret = ret[next(iter(ret))]
  1782. assert ret['result']
  1783. assert ret['comment'] == 'File {0} updated'.format(filename)
  1784. assert os.path.isfile(filename)
  1785. finally:
  1786. try:
  1787. os.remove(filename)
  1788. except OSError:
  1789. pass
  1790. @skipIf(sys.platform.startswith('win'), 'Skipped until parallel states can be fixed on Windows')
  1791. @skipIf(salt.utils.platform.is_darwin() and six.PY2, 'This test hangs on OS X on Py2')
  1792. def test_parallel_state_with_long_tag(self):
  1793. '''
  1794. This tests the case where the state being executed has a long ID dec or
  1795. name and states are being run in parallel. The filenames used for the
  1796. parallel state cache were previously based on the tag for each chunk,
  1797. and longer ID decs or name params can cause the cache file to be longer
  1798. than the operating system's max file name length. To counter this we
  1799. instead generate a SHA1 hash of the chunk's tag to use as the cache
  1800. filename. This test will ensure that long tags don't cause caching
  1801. failures.
  1802. See https://github.com/saltstack/salt/issues/49738 for more info.
  1803. '''
  1804. short_command = 'helloworld'
  1805. long_command = short_command * 25
  1806. ret = self.run_function(
  1807. 'state.sls',
  1808. mods='issue-49738',
  1809. pillar={'short_command': short_command,
  1810. 'long_command': long_command}
  1811. )
  1812. comments = sorted([x['comment'] for x in six.itervalues(ret)])
  1813. expected = sorted(['Command "{0}" run'.format(x)
  1814. for x in (short_command, long_command)])
  1815. assert comments == expected, '{0} != {1}'.format(comments, expected)
  1816. def _add_runtime_pillar(self, pillar):
  1817. '''
  1818. helper class to add pillar data at runtime
  1819. '''
  1820. import salt.utils.yaml
  1821. with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE,
  1822. 'pillar.sls'), 'w') as fp:
  1823. salt.utils.yaml.safe_dump(pillar, fp)
  1824. with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE, 'top.sls'), 'w') as fp:
  1825. fp.write(textwrap.dedent('''\
  1826. base:
  1827. '*':
  1828. - pillar
  1829. '''))
  1830. self.run_function('saltutil.refresh_pillar')
  1831. self.run_function('test.sleep', [5])
  1832. def test_state_sls_id_test(self):
  1833. '''
  1834. test state.sls_id when test is set
  1835. to true in pillar data
  1836. '''
  1837. self._add_runtime_pillar(pillar={'test': True})
  1838. testfile = os.path.join(RUNTIME_VARS.TMP, 'testfile')
  1839. comment = 'The file {0} is set to be changed\nNote: No changes made, actual changes may\nbe different due to other states.'.format(testfile)
  1840. ret = self.run_function('state.sls', ['core'])
  1841. for key, val in ret.items():
  1842. self.assertEqual(val['comment'], comment)
  1843. self.assertEqual(val['changes'], {'newfile': testfile})
  1844. def test_state_sls_id_test_state_test_post_run(self):
  1845. '''
  1846. test state.sls_id when test is set to
  1847. true post the state already being run previously
  1848. '''
  1849. file_name = os.path.join(RUNTIME_VARS.TMP, 'testfile')
  1850. ret = self.run_function('state.sls', ['core'])
  1851. for key, val in ret.items():
  1852. self.assertEqual(val['comment'],
  1853. 'File {0} updated'.format(file_name))
  1854. self.assertEqual(val['changes']['diff'], 'New file')
  1855. self._add_runtime_pillar(pillar={'test': True})
  1856. ret = self.run_function('state.sls', ['core'])
  1857. for key, val in ret.items():
  1858. self.assertEqual(
  1859. val['comment'],
  1860. 'The file {0} is in the correct state'.format(file_name))
  1861. self.assertEqual(val['changes'], {})
  1862. def test_state_sls_id_test_true(self):
  1863. '''
  1864. test state.sls_id when test=True is passed as arg
  1865. '''
  1866. file_name = os.path.join(RUNTIME_VARS.TMP, 'testfile')
  1867. ret = self.run_function('state.sls', ['core'], test=True)
  1868. for key, val in ret.items():
  1869. self.assertEqual(
  1870. val['comment'],
  1871. 'The file {0} is set to be changed\nNote: No changes made, actual changes may\nbe different due to other states.'.format(file_name))
  1872. self.assertEqual(val['changes'], {'newfile': file_name})
  1873. def test_state_sls_id_test_true_post_run(self):
  1874. '''
  1875. test state.sls_id when test is set to true as an
  1876. arg post the state already being run previously
  1877. '''
  1878. file_name = os.path.join(RUNTIME_VARS.TMP, 'testfile')
  1879. ret = self.run_function('state.sls', ['core'])
  1880. for key, val in ret.items():
  1881. self.assertEqual(val['comment'],
  1882. 'File {0} updated'.format(file_name))
  1883. self.assertEqual(val['changes']['diff'], 'New file')
  1884. ret = self.run_function('state.sls', ['core'], test=True)
  1885. for key, val in ret.items():
  1886. self.assertEqual(
  1887. val['comment'],
  1888. 'The file {0} is in the correct state'.format(file_name))
  1889. self.assertEqual(val['changes'], {})
  1890. def test_state_sls_id_test_false_pillar_true(self):
  1891. '''
  1892. test state.sls_id when test is set to false as an
  1893. arg and minion_state_test is set to True. Should
  1894. return test=False.
  1895. '''
  1896. file_name = os.path.join(RUNTIME_VARS.TMP, 'testfile')
  1897. self._add_runtime_pillar(pillar={'test': True})
  1898. ret = self.run_function('state.sls', ['core'], test=False)
  1899. for key, val in ret.items():
  1900. self.assertEqual(val['comment'],
  1901. 'File {0} updated'.format(file_name))
  1902. self.assertEqual(val['changes']['diff'], 'New file')
  1903. @skipIf(six.PY3 and salt.utils.platform.is_darwin(), 'Test is broken on macosx and PY3')
  1904. def test_issue_30161_unless_and_onlyif_together(self):
  1905. '''
  1906. test cmd.run using multiple unless options where the first cmd in the
  1907. list will pass, but the second will fail. This tests the fix for issue
  1908. #35384. (The fix is in PR #35545.)
  1909. '''
  1910. sls = self.run_function('state.sls', mods='issue-30161')
  1911. self.assertSaltTrueReturn(sls)
  1912. # We must assert against the comment here to make sure the comment reads that the
  1913. # command "echo "hello"" was run. This ensures that we made it to the last unless
  1914. # command in the state. If the comment reads "unless condition is true", or similar,
  1915. # then the unless state run bailed out after the first unless command succeeded,
  1916. # which is the bug we're regression testing for.
  1917. _expected = {'file_|-unless_false_onlyif_false_|-{0}{1}test.txt_|-managed'.format(RUNTIME_VARS.TMP, os.path.sep):
  1918. {'comment': 'onlyif condition is false\nunless condition is false',
  1919. 'name': '{0}{1}test.txt'.format(RUNTIME_VARS.TMP, os.path.sep),
  1920. 'skip_watch': True,
  1921. 'changes': {},
  1922. 'result': True},
  1923. 'file_|-unless_false_onlyif_true_|-{0}{1}test.txt_|-managed'.format(RUNTIME_VARS.TMP, os.path.sep):
  1924. {'comment': 'Empty file',
  1925. 'pchanges': {},
  1926. 'name': '{0}{1}test.txt'.format(RUNTIME_VARS.TMP, os.path.sep),
  1927. 'start_time': '18:10:20.341753',
  1928. 'result': True,
  1929. 'changes': {'new': 'file {0}{1}test.txt created'.format(RUNTIME_VARS.TMP, os.path.sep)}},
  1930. 'file_|-unless_true_onlyif_false_|-{0}{1}test.txt_|-managed'.format(RUNTIME_VARS.TMP, os.path.sep):
  1931. {'comment': 'onlyif condition is false\nunless condition is true',
  1932. 'name': '{0}{1}test.txt'.format(RUNTIME_VARS.TMP, os.path.sep),
  1933. 'start_time': '18:10:22.936446',
  1934. 'skip_watch': True,
  1935. 'changes': {},
  1936. 'result': True},
  1937. 'file_|-unless_true_onlyif_true_|-{0}{1}test.txt_|-managed'.format(RUNTIME_VARS.TMP, os.path.sep):
  1938. {'comment': 'onlyif condition is true\nunless condition is true',
  1939. 'name': '{0}{1}test.txt'.format(RUNTIME_VARS.TMP, os.path.sep),
  1940. 'skip_watch': True,
  1941. 'changes': {},
  1942. 'result': True}}
  1943. for id in _expected:
  1944. self.assertEqual(sls[id]['comment'], _expected[id]['comment'])
  1945. @skipIf(six.PY3 and salt.utils.platform.is_darwin(), 'Test is broken on macosx and PY3')
  1946. def test_state_sls_unicode_characters(self):
  1947. '''
  1948. test state.sls when state file contains non-ascii characters
  1949. '''
  1950. ret = self.run_function('state.sls', ['issue-46672'])
  1951. log.debug('== ret %s ==', type(ret))
  1952. _expected = "cmd_|-echo1_|-echo 'This is Æ test!'_|-run"
  1953. self.assertIn(_expected, ret)
  1954. @skipIf(six.PY3 and salt.utils.platform.is_darwin(), 'Test is broken on macosx and PY3')
  1955. def test_state_sls_unicode_characters_cmd_output(self):
  1956. '''
  1957. test the output from running and echo command with non-ascii
  1958. characters.
  1959. '''
  1960. ret = self.run_function('state.sls', ['issue-46672-a'], timeout=60)
  1961. key = list(ret.keys())[0]
  1962. log.debug('== ret %s ==', type(ret))
  1963. _expected = 'This is Æ test!'
  1964. if salt.utils.platform.is_windows():
  1965. # Windows cmd.exe will mangle the output using cmd's codepage.
  1966. if six.PY2:
  1967. _expected = "'This is A+ test!'"
  1968. else:
  1969. _expected = "'This is ’ test!'"
  1970. self.assertEqual(_expected, ret[key]['changes']['stdout'])
  1971. def tearDown(self):
  1972. rm_files = [os.path.join(RUNTIME_VARS.TMP, 'nonbase_env'),
  1973. os.path.join(RUNTIME_VARS.TMP, 'testfile'),
  1974. os.path.join(RUNTIME_VARS.TMP, 'test.txt'),
  1975. os.path.join(RUNTIME_VARS.TMP_STATE_TREE, 'top.sls')]
  1976. for file_ in rm_files:
  1977. if os.path.isfile(file_):
  1978. os.remove(file_)
  1979. # remove old pillar data
  1980. for filename in os.listdir(RUNTIME_VARS.TMP_PILLAR_TREE):
  1981. os.remove(os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE, filename))
  1982. self.run_function('saltutil.refresh_pillar')
  1983. self.run_function('test.sleep', [5])
  1984. def test_state_sls_integer_name(self):
  1985. '''
  1986. This tests the case where the state file is named
  1987. only with integers
  1988. '''
  1989. state_run = self.run_function(
  1990. 'state.sls',
  1991. mods='12345'
  1992. )
  1993. state_id = 'test_|-always-passes_|-always-passes_|-succeed_without_changes'
  1994. self.assertIn(state_id, state_run)
  1995. self.assertEqual(state_run[state_id]['comment'],
  1996. 'Success!')
  1997. self.assertTrue(state_run[state_id]['result'])
  1998. def test_state_sls_lazyloader_allows_recursion(self):
  1999. '''
  2000. This tests that referencing dunders like __salt__ work
  2001. context: https://github.com/saltstack/salt/pull/51499
  2002. '''
  2003. state_run = self.run_function('state.sls', mods='issue-51499')
  2004. state_id = 'test_|-always-passes_|-foo_|-succeed_without_changes'
  2005. self.assertIn(state_id, state_run)
  2006. self.assertEqual(state_run[state_id]['comment'],
  2007. 'Success!')
  2008. self.assertTrue(state_run[state_id]['result'])