123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580 |
- import logging
- import os
- import shutil
- import sys
- import tempfile
- import textwrap
- import threading
- import time
- import pytest
- import salt.utils.atomicfile
- import salt.utils.files
- import salt.utils.path
- import salt.utils.platform
- import salt.utils.stringutils
- from salt.ext import six
- from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
- from tests.support.case import ModuleCase
- from tests.support.helpers import slowTest, with_tempdir
- from tests.support.mixins import SaltReturnAssertsMixin
- from tests.support.pytest.helpers import temp_state_file
- from tests.support.runtests import RUNTIME_VARS
- from tests.support.sminion import create_sminion
- from tests.support.unit import skipIf
- log = logging.getLogger(__name__)
- DEFAULT_ENDING = salt.utils.stringutils.to_bytes(os.linesep)
- @pytest.mark.windows_whitelisted
- class StateModuleTest(ModuleCase, SaltReturnAssertsMixin):
- """
- Validate the state module
- """
- maxDiff = None
- @classmethod
- def setUpClass(cls):
- def _reline(path, ending=DEFAULT_ENDING):
- """
- Normalize the line endings of a file.
- """
- with salt.utils.files.fopen(path, "rb") as fhr:
- lines = fhr.read().splitlines()
- with salt.utils.atomicfile.atomic_open(path, "wb") as fhw:
- for line in lines:
- fhw.write(line + ending)
- destpath = os.path.join(RUNTIME_VARS.BASE_FILES, "testappend", "firstif")
- _reline(destpath)
- destpath = os.path.join(RUNTIME_VARS.BASE_FILES, "testappend", "secondif")
- _reline(destpath)
- if salt.utils.platform.is_windows():
- cls.TIMEOUT = 600
- # Be sure to have everything sync'ed
- sminion = create_sminion()
- sminion.functions.saltutil.sync_all()
- else:
- cls.TIMEOUT = 10
- @slowTest
- def test_show_highstate(self):
- """
- state.show_highstate
- """
- high = self.run_function("state.show_highstate")
- destpath = os.path.join(RUNTIME_VARS.TMP, "testfile")
- self.assertTrue(isinstance(high, dict))
- self.assertTrue(destpath in high)
- self.assertEqual(high[destpath]["__env__"], "base")
- @slowTest
- def test_show_lowstate(self):
- """
- state.show_lowstate
- """
- low = self.run_function("state.show_lowstate")
- self.assertTrue(isinstance(low, list))
- self.assertTrue(isinstance(low[0], dict))
- @slowTest
- def test_show_states(self):
- """
- state.show_states
- """
- states = self.run_function("state.show_states")
- self.assertTrue(isinstance(states, list))
- self.assertTrue(isinstance(states[0], str))
- states = self.run_function("state.show_states", sorted=False)
- self.assertTrue(isinstance(states, list))
- self.assertTrue(isinstance(states[0], str))
- @slowTest
- def test_show_states_missing_sls(self):
- """
- Test state.show_states with a sls file
- defined in a top file is missing
- """
- topfile = os.path.join(RUNTIME_VARS.TMP_STATE_TREE, "top.sls")
- with salt.utils.files.fopen(topfile, "w") as top_file:
- top_file.write(
- textwrap.dedent(
- """\
- base:
- '*':
- - doesnotexist
- """
- )
- )
- states = self.run_function("state.show_states")
- assert isinstance(states, list)
- assert states == ["No matching sls found for 'doesnotexist' in env 'base'"]
- @slowTest
- def test_catch_recurse(self):
- """
- state.show_sls used to catch a recursive ref
- """
- err = self.run_function("state.sls", mods="recurse_fail")
- self.assertIn("recursive", err[0])
- @slowTest
- def test_no_recurse(self):
- """
- verify that a sls structure is NOT a recursive ref
- """
- sls = self.run_function("state.show_sls", mods="recurse_ok")
- self.assertIn("snmpd", sls)
- @slowTest
- def test_no_recurse_two(self):
- """
- verify that a sls structure is NOT a recursive ref
- """
- sls = self.run_function("state.show_sls", mods="recurse_ok_two")
- self.assertIn("/etc/nagios/nrpe.cfg", sls)
- @slowTest
- def test_running_dictionary_consistency(self):
- """
- Test the structure of the running dictionary so we don't change it
- without deprecating/documenting the change
- """
- running_dict_fields = [
- "__id__",
- "__run_num__",
- "__sls__",
- "changes",
- "comment",
- "duration",
- "name",
- "result",
- "start_time",
- ]
- sls = self.run_function(
- "state.single", fun="test.succeed_with_changes", name="gndn"
- )
- for state, ret in sls.items():
- for field in running_dict_fields:
- self.assertIn(field, ret)
- @slowTest
- def test_running_dictionary_key_sls(self):
- """
- Ensure the __sls__ key is either null or a string
- """
- sls1 = self.run_function(
- "state.single", fun="test.succeed_with_changes", name="gndn"
- )
- sls2 = self.run_function("state.sls", mods="gndn")
- for state, ret in sls1.items():
- self.assertTrue(isinstance(ret["__sls__"], type(None)))
- for state, ret in sls2.items():
- self.assertTrue(isinstance(ret["__sls__"], str))
- def _remove_request_cache_file(self):
- """
- remove minion state request file
- """
- cache_file = os.path.join(self.get_config("minion")["cachedir"], "req_state.p")
- if os.path.exists(cache_file):
- os.remove(cache_file)
- @slowTest
- def test_request(self):
- """
- verify sending a state request to the minion(s)
- """
- self._remove_request_cache_file()
- ret = self.run_function("state.request", mods="modules.state.requested")
- result = ret["cmd_|-count_root_dir_contents_|-ls -a / | wc -l_|-run"]["result"]
- self.assertEqual(result, None)
- @slowTest
- def test_check_request(self):
- """
- verify checking a state request sent to the minion(s)
- """
- self._remove_request_cache_file()
- self.run_function("state.request", mods="modules.state.requested")
- ret = self.run_function("state.check_request")
- result = ret["default"]["test_run"][
- "cmd_|-count_root_dir_contents_|-ls -a / | wc -l_|-run"
- ]["result"]
- self.assertEqual(result, None)
- @slowTest
- def test_clear_request(self):
- """
- verify clearing a state request sent to the minion(s)
- """
- self._remove_request_cache_file()
- self.run_function("state.request", mods="modules.state.requested")
- ret = self.run_function("state.clear_request")
- self.assertTrue(ret)
- @slowTest
- def test_run_request_succeeded(self):
- """
- verify running a state request sent to the minion(s)
- """
- self._remove_request_cache_file()
- if salt.utils.platform.is_windows():
- self.run_function("state.request", mods="modules.state.requested_win")
- else:
- self.run_function("state.request", mods="modules.state.requested")
- ret = self.run_function("state.run_request")
- if salt.utils.platform.is_windows():
- key = "cmd_|-count_root_dir_contents_|-Get-ChildItem C:\\\\ | Measure-Object | %{$_.Count}_|-run"
- else:
- key = "cmd_|-count_root_dir_contents_|-ls -a / | wc -l_|-run"
- result = ret[key]["result"]
- self.assertTrue(result)
- @slowTest
- def test_run_request_failed_no_request_staged(self):
- """
- verify not running a state request sent to the minion(s)
- """
- self._remove_request_cache_file()
- self.run_function("state.request", mods="modules.state.requested")
- self.run_function("state.clear_request")
- ret = self.run_function("state.run_request")
- self.assertEqual(ret, {})
- @with_tempdir()
- @slowTest
- def test_issue_1896_file_append_source(self, base_dir):
- """
- Verify that we can append a file's contents
- """
- testfile = os.path.join(base_dir, "test.append")
- ret = self.run_state("file.touch", name=testfile)
- self.assertSaltTrueReturn(ret)
- ret = self.run_state(
- "file.append", name=testfile, source="salt://testappend/firstif"
- )
- self.assertSaltTrueReturn(ret)
- ret = self.run_state(
- "file.append", name=testfile, source="salt://testappend/secondif"
- )
- self.assertSaltTrueReturn(ret)
- with salt.utils.files.fopen(testfile, "r") as fp_:
- testfile_contents = salt.utils.stringutils.to_unicode(fp_.read())
- contents = textwrap.dedent(
- """\
- # set variable identifying the chroot you work in (used in the prompt below)
- if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
- debian_chroot=$(cat /etc/debian_chroot)
- fi
- # enable bash completion in interactive shells
- if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
- . /etc/bash_completion
- fi
- """
- )
- if salt.utils.platform.is_windows():
- new_contents = contents.splitlines()
- contents = os.linesep.join(new_contents)
- contents += os.linesep
- self.assertMultiLineEqual(contents, testfile_contents)
- ret = self.run_state(
- "file.append", name=testfile, source="salt://testappend/secondif"
- )
- self.assertSaltTrueReturn(ret)
- ret = self.run_state(
- "file.append", name=testfile, source="salt://testappend/firstif"
- )
- self.assertSaltTrueReturn(ret)
- with salt.utils.files.fopen(testfile, "r") as fp_:
- testfile_contents = salt.utils.stringutils.to_unicode(fp_.read())
- self.assertMultiLineEqual(contents, testfile_contents)
- @slowTest
- def test_issue_1876_syntax_error(self):
- """
- verify that we catch the following syntax error::
- /tmp/salttest/issue-1876:
- file:
- - managed
- - source: salt://testfile
- file.append:
- - text: foo
- """
- testfile = os.path.join(RUNTIME_VARS.TMP, "issue-1876")
- sls = self.run_function("state.sls", mods="issue-1876")
- self.assertIn(
- "ID '{}' in SLS 'issue-1876' contains multiple state "
- "declarations of the same type".format(testfile),
- sls,
- )
- @slowTest
- def test_issue_1879_too_simple_contains_check(self):
- expected = textwrap.dedent(
- """\
- # set variable identifying the chroot you work in (used in the prompt below)
- if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
- debian_chroot=$(cat /etc/debian_chroot)
- fi
- # enable bash completion in interactive shells
- if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
- . /etc/bash_completion
- fi
- """
- )
- if salt.utils.platform.is_windows():
- new_contents = expected.splitlines()
- expected = os.linesep.join(new_contents)
- expected += os.linesep
- testfile = os.path.join(RUNTIME_VARS.TMP, "issue-1879")
- # Delete if exiting
- if os.path.isfile(testfile):
- os.unlink(testfile)
- # Create the file
- ret = self.run_function("state.sls", mods="issue-1879", timeout=120)
- self.assertSaltTrueReturn(ret)
- # The first append
- ret = self.run_function("state.sls", mods="issue-1879.step-1", timeout=120)
- self.assertSaltTrueReturn(ret)
- # The second append
- ret = self.run_function("state.sls", mods="issue-1879.step-2", timeout=120)
- self.assertSaltTrueReturn(ret)
- # Does it match?
- try:
- with salt.utils.files.fopen(testfile, "r") as fp_:
- contents = salt.utils.stringutils.to_unicode(fp_.read())
- self.assertMultiLineEqual(expected, contents)
- # Make sure we don't re-append existing text
- ret = self.run_function("state.sls", mods="issue-1879.step-1", timeout=120)
- self.assertSaltTrueReturn(ret)
- ret = self.run_function("state.sls", mods="issue-1879.step-2", timeout=120)
- self.assertSaltTrueReturn(ret)
- with salt.utils.files.fopen(testfile, "r") as fp_:
- contents = salt.utils.stringutils.to_unicode(fp_.read())
- self.assertMultiLineEqual(expected, contents)
- except Exception: # pylint: disable=broad-except
- if os.path.exists(testfile):
- shutil.copy(testfile, testfile + ".bak")
- raise
- finally:
- if os.path.exists(testfile):
- os.unlink(testfile)
- @slowTest
- def test_include(self):
- tempdir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
- self.addCleanup(shutil.rmtree, tempdir, ignore_errors=True)
- pillar = {}
- for path in ("include-test", "to-include-test", "exclude-test"):
- pillar[path] = os.path.join(tempdir, path)
- ret = self.run_function("state.sls", mods="include-test", pillar=pillar)
- self.assertSaltTrueReturn(ret)
- self.assertTrue(os.path.isfile(pillar["include-test"]))
- self.assertTrue(os.path.isfile(pillar["to-include-test"]))
- self.assertFalse(os.path.isfile(pillar["exclude-test"]))
- @slowTest
- def test_exclude(self):
- tempdir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
- self.addCleanup(shutil.rmtree, tempdir, ignore_errors=True)
- pillar = {}
- for path in ("include-test", "exclude-test", "to-include-test"):
- pillar[path] = os.path.join(tempdir, path)
- ret = self.run_function("state.sls", mods="exclude-test", pillar=pillar)
- self.assertSaltTrueReturn(ret)
- self.assertTrue(os.path.isfile(pillar["include-test"]))
- self.assertTrue(os.path.isfile(pillar["exclude-test"]))
- self.assertFalse(os.path.isfile(pillar["to-include-test"]))
- @skipIf(
- salt.utils.path.which_bin(KNOWN_BINARY_NAMES) is None,
- "virtualenv not installed",
- )
- @slowTest
- def test_issue_2068_template_str(self):
- venv_dir = os.path.join(RUNTIME_VARS.TMP, "issue-2068-template-str")
- try:
- ret = self.run_function(
- "state.sls", mods="issue-2068-template-str-no-dot", timeout=120
- )
- self.assertSaltTrueReturn(ret)
- finally:
- if os.path.isdir(venv_dir):
- shutil.rmtree(venv_dir)
- # Let's load the template from the filesystem. If running this state
- # with state.sls works, so should using state.template_str
- template_path = os.path.join(
- os.path.dirname(os.path.dirname(__file__)),
- "files",
- "file",
- "base",
- "issue-2068-template-str-no-dot.sls",
- )
- with salt.utils.files.fopen(template_path, "r") as fp_:
- template = salt.utils.stringutils.to_unicode(fp_.read())
- ret = self.run_function("state.template_str", [template], timeout=120)
- self.assertSaltTrueReturn(ret)
- # Now using state.template
- ret = self.run_function("state.template", [template_path], timeout=120)
- self.assertSaltTrueReturn(ret)
- # Now the problematic #2068 including dot's
- ret = self.run_function(
- "state.sls", mods="issue-2068-template-str", timeout=120
- )
- self.assertSaltTrueReturn(ret)
- # Let's load the template from the filesystem. If running this state
- # with state.sls works, so should using state.template_str
- template_path = os.path.join(
- os.path.dirname(os.path.dirname(__file__)),
- "files",
- "file",
- "base",
- "issue-2068-template-str.sls",
- )
- with salt.utils.files.fopen(template_path, "r") as fp_:
- template = salt.utils.stringutils.to_unicode(fp_.read())
- ret = self.run_function("state.template_str", [template], timeout=120)
- self.assertSaltTrueReturn(ret)
- # Now using state.template
- ret = self.run_function("state.template", [template_path], timeout=120)
- self.assertSaltTrueReturn(ret)
- @slowTest
- def test_template_invalid_items(self):
- TEMPLATE = textwrap.dedent(
- """\
- {0}:
- - issue-2068-template-str
- /tmp/test-template-invalid-items:
- file:
- - managed
- - source: salt://testfile
- """
- )
- for item in ("include", "exclude", "extends"):
- ret = self.run_function("state.template_str", [TEMPLATE.format(item)])
- self.assertTrue(isinstance(ret, list))
- self.assertNotEqual(ret, [])
- self.assertEqual(
- [
- "The '{}' declaration found on '<template-str>' is "
- "invalid when rendering single templates".format(item)
- ],
- ret,
- )
- @slowTest
- def test_pydsl(self):
- """
- Test the basics of the pydsl
- """
- ret = self.run_function("state.sls", mods="pydsl-1")
- self.assertSaltTrueReturn(ret)
- @slowTest
- def test_issues_7905_and_8174_sls_syntax_error(self):
- """
- Call sls file with yaml syntax error.
- Ensure theses errors are detected and presented to the user without
- stack traces.
- """
- ret = self.run_function("state.sls", mods="syntax.badlist")
- self.assertEqual(
- ret, ["State 'A' in SLS 'syntax.badlist' is not formed as a list"]
- )
- ret = self.run_function("state.sls", mods="syntax.badlist2")
- self.assertEqual(
- ret, ["State 'C' in SLS 'syntax.badlist2' is not formed as a list"]
- )
- @slowTest
- def test_requisites_mixed_require_prereq_use(self):
- """
- Call sls file containing several requisites.
- """
- expected_simple_result = {
- "cmd_|-A_|-echo A_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo A" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo B" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo C" run',
- "result": True,
- "changes": True,
- },
- }
- expected_result = {
- "cmd_|-A_|-echo A fifth_|-run": {
- "__run_num__": 4,
- "comment": 'Command "echo A fifth" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B third_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo B third" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C second_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo C second" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-D_|-echo D first_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo D first" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-E_|-echo E fourth_|-run": {
- "__run_num__": 3,
- "comment": 'Command "echo E fourth" run',
- "result": True,
- "changes": True,
- },
- }
- expected_req_use_result = {
- "cmd_|-A_|-echo A_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo A" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B_|-run": {
- "__run_num__": 4,
- "comment": 'Command "echo B" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo C" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-D_|-echo D_|-run": {
- "__run_num__": 5,
- "comment": 'Command "echo D" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-E_|-echo E_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo E" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-F_|-echo F_|-run": {
- "__run_num__": 3,
- "comment": 'Command "echo F" run',
- "result": True,
- "changes": True,
- },
- }
- ret = self.run_function("state.sls", mods="requisites.mixed_simple")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(expected_simple_result, result)
- # test Traceback recursion prereq+require #8785
- # TODO: this is actually failing badly
- # ret = self.run_function('state.sls', mods='requisites.prereq_require_recursion_error2')
- # self.assertEqual(
- # ret,
- # ['A recursive requisite was found, SLS "requisites.prereq_require_recursion_error2" ID "B" ID "A"']
- # )
- # test Infinite recursion prereq+require #8785 v2
- # TODO: this is actually failing badly
- # ret = self.run_function('state.sls', mods='requisites.prereq_require_recursion_error3')
- # self.assertEqual(
- # ret,
- # ['A recursive requisite was found, SLS "requisites.prereq_require_recursion_error2" ID "B" ID "A"']
- # )
- # test Infinite recursion prereq+require #8785 v3
- # TODO: this is actually failing badly, and expected result is maybe not a recursion
- # ret = self.run_function('state.sls', mods='requisites.prereq_require_recursion_error4')
- # self.assertEqual(
- # ret,
- # ['A recursive requisite was found, SLS "requisites.prereq_require_recursion_error2" ID "B" ID "A"']
- # )
- # undetected infinite loopS prevents this test from running...
- # TODO: this is actually failing badly
- # ret = self.run_function('state.sls', mods='requisites.mixed_complex1')
- # result = self.normalize_ret(ret)
- # self.assertEqual(expected_result, result)
- @slowTest
- def test_watch_in(self):
- """
- test watch_in requisite when there is a success
- """
- ret = self.run_function("state.sls", mods="requisites.watch_in")
- changes = "test_|-return_changes_|-return_changes_|-succeed_with_changes"
- watch = "test_|-watch_states_|-watch_states_|-succeed_without_changes"
- self.assertEqual(ret[changes]["__run_num__"], 0)
- self.assertEqual(ret[watch]["__run_num__"], 2)
- self.assertEqual("Watch statement fired.", ret[watch]["comment"])
- self.assertEqual(
- "Something pretended to change", ret[changes]["changes"]["testing"]["new"]
- )
- @slowTest
- def test_watch_in_failure(self):
- """
- test watch_in requisite when there is a failure
- """
- ret = self.run_function("state.sls", mods="requisites.watch_in_failure")
- fail = "test_|-return_changes_|-return_changes_|-fail_with_changes"
- watch = "test_|-watch_states_|-watch_states_|-succeed_without_changes"
- self.assertEqual(False, ret[fail]["result"])
- self.assertEqual(
- "One or more requisite failed: requisites.watch_in_failure.return_changes",
- ret[watch]["comment"],
- )
- def normalize_ret(self, ret):
- """
- Normalize the return to the format that we'll use for result checking
- """
- result = {}
- for item, descr in ret.items():
- result[item] = {
- "__run_num__": descr["__run_num__"],
- "comment": descr["comment"],
- "result": descr["result"],
- "changes": descr["changes"] != {}, # whether there where any changes
- }
- return result
- @slowTest
- def test_requisites_require_ordering_and_errors(self):
- """
- Call sls file containing several require_in and require.
- Ensure that some of them are failing and that the order is right.
- """
- expected_result = {
- "cmd_|-A_|-echo A fifth_|-run": {
- "__run_num__": 4,
- "comment": 'Command "echo A fifth" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B second_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo B second" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C third_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo C third" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-D_|-echo D first_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo D first" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-E_|-echo E fourth_|-run": {
- "__run_num__": 3,
- "comment": 'Command "echo E fourth" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-F_|-echo F_|-run": {
- "__run_num__": 5,
- "comment": "The following requisites were not found:\n"
- + " require:\n"
- + " foobar: A\n",
- "result": False,
- "changes": False,
- },
- "cmd_|-G_|-echo G_|-run": {
- "__run_num__": 6,
- "comment": "The following requisites were not found:\n"
- + " require:\n"
- + " cmd: Z\n",
- "result": False,
- "changes": False,
- },
- "cmd_|-H_|-echo H_|-run": {
- "__run_num__": 7,
- "comment": "The following requisites were not found:\n"
- + " require:\n"
- + " cmd: Z\n",
- "result": False,
- "changes": False,
- },
- }
- ret = self.run_function("state.sls", mods="requisites.require")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(expected_result, result)
- ret = self.run_function("state.sls", mods="requisites.require_error1")
- self.assertEqual(
- ret,
- [
- "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'"
- ],
- )
- # issue #8235
- # FIXME: Why is require enforcing list syntax while require_in does not?
- # And why preventing it?
- # Currently this state fails, should return C/B/A
- result = {}
- ret = self.run_function("state.sls", mods="requisites.require_simple_nolist")
- self.assertEqual(
- ret,
- [
- "The require statement in state 'B' in SLS "
- + "'requisites.require_simple_nolist' needs to be formed as a list"
- ],
- )
- # commented until a fix is made for issue #8772
- # TODO: this test actually fails
- # ret = self.run_function('state.sls', mods='requisites.require_error2')
- # self.assertEqual(ret, [
- # 'Cannot extend state foobar for ID A in "base:requisites.require_error2".'
- # + ' It is not part of the high state.'
- # ])
- ret = self.run_function("state.sls", mods="requisites.require_recursion_error1")
- self.assertEqual(
- ret,
- [
- 'A recursive requisite was found, SLS "requisites.require_recursion_error1" ID "B" ID "A"'
- ],
- )
- @slowTest
- def test_requisites_require_any(self):
- """
- Call sls file containing several require_in and require.
- Ensure that some of them are failing and that the order is right.
- """
- expected_result = {
- "cmd_|-A_|-echo A_|-run": {
- "__run_num__": 3,
- "comment": 'Command "echo A" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo B" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-$(which false)_|-run": {
- "__run_num__": 1,
- "comment": 'Command "$(which false)" run',
- "result": False,
- "changes": True,
- },
- "cmd_|-D_|-echo D_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo D" run',
- "result": True,
- "changes": True,
- },
- }
- ret = self.run_function("state.sls", mods="requisites.require_any")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(expected_result, result)
- @slowTest
- def test_requisites_require_any_fail(self):
- """
- Call sls file containing several require_in and require.
- Ensure that some of them are failing and that the order is right.
- """
- ret = self.run_function("state.sls", mods="requisites.require_any_fail")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertIn(
- "One or more requisite failed", result["cmd_|-D_|-echo D_|-run"]["comment"]
- )
- @slowTest
- def test_requisites_watch_any(self):
- """
- Call sls file containing several require_in and require.
- Ensure that some of them are failing and that the order is right.
- """
- if salt.utils.platform.is_windows():
- cmd_true = "exit"
- cmd_false = "exit /B 1"
- else:
- cmd_true = "true"
- cmd_false = "false"
- expected_result = {
- "cmd_|-A_|-{}_|-wait".format(cmd_true): {
- "__run_num__": 4,
- "comment": 'Command "{}" run'.format(cmd_true),
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-{}_|-run".format(cmd_true): {
- "__run_num__": 0,
- "comment": 'Command "{}" run'.format(cmd_true),
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-{}_|-run".format(cmd_false): {
- "__run_num__": 1,
- "comment": 'Command "{}" run'.format(cmd_false),
- "result": False,
- "changes": True,
- },
- "cmd_|-D_|-{}_|-run".format(cmd_true): {
- "__run_num__": 2,
- "comment": 'Command "{}" run'.format(cmd_true),
- "result": True,
- "changes": True,
- },
- "cmd_|-E_|-{}_|-wait".format(cmd_true): {
- "__run_num__": 9,
- "comment": 'Command "{}" run'.format(cmd_true),
- "result": True,
- "changes": True,
- },
- "cmd_|-F_|-{}_|-run".format(cmd_true): {
- "__run_num__": 5,
- "comment": 'Command "{}" run'.format(cmd_true),
- "result": True,
- "changes": True,
- },
- "cmd_|-G_|-{}_|-run".format(cmd_false): {
- "__run_num__": 6,
- "comment": 'Command "{}" run'.format(cmd_false),
- "result": False,
- "changes": True,
- },
- "cmd_|-H_|-{}_|-run".format(cmd_false): {
- "__run_num__": 7,
- "comment": 'Command "{}" run'.format(cmd_false),
- "result": False,
- "changes": True,
- },
- }
- ret = self.run_function("state.sls", mods="requisites.watch_any")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(expected_result, result)
- @slowTest
- def test_requisites_watch_any_fail(self):
- """
- Call sls file containing several require_in and require.
- Ensure that some of them are failing and that the order is right.
- """
- ret = self.run_function("state.sls", mods="requisites.watch_any_fail")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertIn(
- "One or more requisite failed", result["cmd_|-A_|-true_|-wait"]["comment"]
- )
- @slowTest
- def test_requisites_onchanges_any(self):
- """
- Call sls file containing several require_in and require.
- Ensure that some of them are failing and that the order is right.
- """
- expected_result = {
- 'cmd_|-another_changing_state_|-echo "Changed!"_|-run': {
- "__run_num__": 1,
- "changes": True,
- "comment": 'Command "echo "Changed!"" run',
- "result": True,
- },
- 'cmd_|-changing_state_|-echo "Changed!"_|-run': {
- "__run_num__": 0,
- "changes": True,
- "comment": 'Command "echo "Changed!"" run',
- "result": True,
- },
- 'cmd_|-test_one_changing_states_|-echo "Success!"_|-run': {
- "__run_num__": 4,
- "changes": True,
- "comment": 'Command "echo "Success!"" run',
- "result": True,
- },
- 'cmd_|-test_two_non_changing_states_|-echo "Should not run"_|-run': {
- "__run_num__": 5,
- "changes": False,
- "comment": "State was not run because none of the onchanges reqs changed",
- "result": True,
- },
- "pip_|-another_non_changing_state_|-mock_|-installed": {
- "__run_num__": 3,
- "changes": False,
- "comment": "Python package mock was already installed\nAll specified packages are already installed",
- "result": True,
- },
- "pip_|-non_changing_state_|-mock_|-installed": {
- "__run_num__": 2,
- "changes": False,
- "comment": "Python package mock was already installed\nAll specified packages are already installed",
- "result": True,
- },
- }
- ret = self.run_function("state.sls", mods="requisites.onchanges_any")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(expected_result, result)
- @slowTest
- def test_requisites_onfail_any(self):
- """
- Call sls file containing several require_in and require.
- Ensure that some of them are failing and that the order is right.
- """
- expected_result = {
- "cmd_|-a_|-exit 0_|-run": {
- "__run_num__": 0,
- "changes": True,
- "comment": 'Command "exit 0" run',
- "result": True,
- },
- "cmd_|-b_|-exit 1_|-run": {
- "__run_num__": 1,
- "changes": True,
- "comment": 'Command "exit 1" run',
- "result": False,
- },
- "cmd_|-c_|-exit 0_|-run": {
- "__run_num__": 2,
- "changes": True,
- "comment": 'Command "exit 0" run',
- "result": True,
- },
- "cmd_|-d_|-echo itworked_|-run": {
- "__run_num__": 3,
- "changes": True,
- "comment": 'Command "echo itworked" run',
- "result": True,
- },
- "cmd_|-e_|-exit 0_|-run": {
- "__run_num__": 4,
- "changes": True,
- "comment": 'Command "exit 0" run',
- "result": True,
- },
- "cmd_|-f_|-exit 0_|-run": {
- "__run_num__": 5,
- "changes": True,
- "comment": 'Command "exit 0" run',
- "result": True,
- },
- "cmd_|-g_|-exit 0_|-run": {
- "__run_num__": 6,
- "changes": True,
- "comment": 'Command "exit 0" run',
- "result": True,
- },
- "cmd_|-h_|-echo itworked_|-run": {
- "__run_num__": 7,
- "changes": False,
- "comment": "State was not run because onfail req did not change",
- "result": True,
- },
- }
- ret = self.run_function("state.sls", mods="requisites.onfail_any")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(expected_result, result)
- @slowTest
- def test_requisites_onfail_all(self):
- """
- Call sls file containing several onfail-all
- Ensure that some of them are failing and that the order is right.
- """
- expected_result = {
- "cmd_|-a_|-exit 0_|-run": {
- "__run_num__": 0,
- "changes": True,
- "comment": 'Command "exit 0" run',
- "result": True,
- },
- "cmd_|-b_|-exit 0_|-run": {
- "__run_num__": 1,
- "changes": True,
- "comment": 'Command "exit 0" run',
- "result": True,
- },
- "cmd_|-c_|-exit 0_|-run": {
- "__run_num__": 2,
- "changes": True,
- "comment": 'Command "exit 0" run',
- "result": True,
- },
- "cmd_|-d_|-exit 1_|-run": {
- "__run_num__": 3,
- "changes": True,
- "comment": 'Command "exit 1" run',
- "result": False,
- },
- "cmd_|-e_|-exit 1_|-run": {
- "__run_num__": 4,
- "changes": True,
- "comment": 'Command "exit 1" run',
- "result": False,
- },
- "cmd_|-f_|-exit 1_|-run": {
- "__run_num__": 5,
- "changes": True,
- "comment": 'Command "exit 1" run',
- "result": False,
- },
- "cmd_|-reqs also met_|-echo itonfailed_|-run": {
- "__run_num__": 9,
- "changes": True,
- "comment": 'Command "echo itonfailed" run',
- "result": True,
- },
- "cmd_|-reqs also not met_|-echo italsodidnonfail_|-run": {
- "__run_num__": 7,
- "changes": False,
- "comment": "State was not run because onfail req did not change",
- "result": True,
- },
- "cmd_|-reqs met_|-echo itonfailed_|-run": {
- "__run_num__": 8,
- "changes": True,
- "comment": 'Command "echo itonfailed" run',
- "result": True,
- },
- "cmd_|-reqs not met_|-echo itdidntonfail_|-run": {
- "__run_num__": 6,
- "changes": False,
- "comment": "State was not run because onfail req did not change",
- "result": True,
- },
- }
- ret = self.run_function("state.sls", mods="requisites.onfail_all")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(expected_result, result)
- @slowTest
- def test_requisites_full_sls(self):
- """
- Teste the sls special command in requisites
- """
- expected_result = {
- "cmd_|-A_|-echo A_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo A" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo B" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo C" run',
- "result": True,
- "changes": True,
- },
- }
- ret = self.run_function("state.sls", mods="requisites.fullsls_require")
- self.assertReturnNonEmptySaltType(ret)
- result = self.normalize_ret(ret)
- self.assertEqual(expected_result, result)
- # issue #8233: traceback on prereq sls
- # TODO: not done
- # ret = self.run_function('state.sls', mods='requisites.fullsls_prereq')
- # self.assertEqual(['sls command can only be used with require requisite'], ret)
- @slowTest
- def test_requisites_require_no_state_module(self):
- """
- Call sls file containing several require_in and require.
- Ensure that some of them are failing and that the order is right.
- """
- expected_result = {
- "cmd_|-A_|-echo A fifth_|-run": {
- "__run_num__": 4,
- "comment": 'Command "echo A fifth" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B second_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo B second" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C third_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo C third" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-D_|-echo D first_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo D first" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-E_|-echo E fourth_|-run": {
- "__run_num__": 3,
- "comment": 'Command "echo E fourth" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-G_|-echo G_|-run": {
- "__run_num__": 5,
- "comment": "The following requisites were not found:\n"
- + " require:\n"
- + " id: Z\n",
- "result": False,
- "changes": False,
- },
- "cmd_|-H_|-echo H_|-run": {
- "__run_num__": 6,
- "comment": "The following requisites were not found:\n"
- + " require:\n"
- + " id: Z\n",
- "result": False,
- "changes": False,
- },
- }
- ret = self.run_function("state.sls", mods="requisites.require_no_state_module")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(expected_result, result)
- @slowTest
- def test_requisites_prereq_simple_ordering_and_errors(self):
- """
- Call sls file containing several prereq_in and prereq.
- Ensure that some of them are failing and that the order is right.
- """
- expected_result_simple = {
- "cmd_|-A_|-echo A third_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo A third" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B first_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo B first" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C second_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo C second" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-I_|-echo I_|-run": {
- "__run_num__": 3,
- "comment": "The following requisites were not found:\n"
- + " prereq:\n"
- + " cmd: Z\n",
- "result": False,
- "changes": False,
- },
- "cmd_|-J_|-echo J_|-run": {
- "__run_num__": 4,
- "comment": "The following requisites were not found:\n"
- + " prereq:\n"
- + " foobar: A\n",
- "result": False,
- "changes": False,
- },
- }
- expected_result_simple_no_state_module = {
- "cmd_|-A_|-echo A third_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo A third" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B first_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo B first" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C second_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo C second" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-I_|-echo I_|-run": {
- "__run_num__": 3,
- "comment": "The following requisites were not found:\n"
- + " prereq:\n"
- + " id: Z\n",
- "result": False,
- "changes": False,
- },
- }
- expected_result_simple2 = {
- "cmd_|-A_|-echo A_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo A" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo B" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo C" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-D_|-echo D_|-run": {
- "__run_num__": 3,
- "comment": 'Command "echo D" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-E_|-echo E_|-run": {
- "__run_num__": 4,
- "comment": 'Command "echo E" run',
- "result": True,
- "changes": True,
- },
- }
- expected_result_simple3 = {
- "cmd_|-A_|-echo A first_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo A first" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B second_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo B second" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C third_|-wait": {
- "__run_num__": 2,
- "comment": "",
- "result": True,
- "changes": False,
- },
- }
- expected_result_complex = {
- "cmd_|-A_|-echo A fourth_|-run": {
- "__run_num__": 3,
- "comment": 'Command "echo A fourth" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-B_|-echo B first_|-run": {
- "__run_num__": 0,
- "comment": 'Command "echo B first" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-C_|-echo C second_|-run": {
- "__run_num__": 1,
- "comment": 'Command "echo C second" run',
- "result": True,
- "changes": True,
- },
- "cmd_|-D_|-echo D third_|-run": {
- "__run_num__": 2,
- "comment": 'Command "echo D third" run',
- "result": True,
- "changes": True,
- },
- }
- ret = self.run_function("state.sls", mods="requisites.prereq_simple")
- self.assertReturnNonEmptySaltType(ret)
- result = self.normalize_ret(ret)
- self.assertEqual(expected_result_simple, result)
- # same test, but not using lists in yaml syntax
- # TODO: issue #8235, prereq ignored when not used in list syntax
- # Currently fails badly with :
- # TypeError encountered executing state.sls: string indices must be integers, not str.
- # expected_result_simple.pop('cmd_|-I_|-echo I_|-run')
- # expected_result_simple.pop('cmd_|-J_|-echo J_|-run')
- # ret = self.run_function('state.sls', mods='requisites.prereq_simple_nolist')
- # result = self.normalize_ret(ret)
- # self.assertEqual(expected_result_simple, result)
- ret = self.run_function("state.sls", mods="requisites.prereq_simple2")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(expected_result_simple2, result)
- ret = self.run_function("state.sls", mods="requisites.prereq_simple3")
- result = self.normalize_ret(ret)
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(expected_result_simple3, result)
- # ret = self.run_function('state.sls', mods='requisites.prereq_error_nolist')
- # self.assertEqual(
- # ret,
- # ['Cannot extend ID Z in "base:requisites.prereq_error_nolist".'
- # + ' It is not part of the high state.']
- # )
- ret = self.run_function("state.sls", mods="requisites.prereq_compile_error1")
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(
- ret["cmd_|-B_|-echo B_|-run"]["comment"],
- "The following requisites were not found:\n"
- + " prereq:\n"
- + " foobar: A\n",
- )
- ret = self.run_function("state.sls", mods="requisites.prereq_compile_error2")
- self.assertReturnNonEmptySaltType(ret)
- self.assertEqual(
- ret["cmd_|-B_|-echo B_|-run"]["comment"],
- "The following requisites were not found:\n"
- + " prereq:\n"
- + " foobar: C\n",
- )
- ret = self.run_function("state.sls", mods="requisites.prereq_complex")
- result = self.normalize_ret(ret)
- self.assertEqual(expected_result_complex, result)
- # issue #8210 : prereq recursion undetected
- # TODO: this test fails
- # ret = self.run_function('state.sls', mods='requisites.prereq_recursion_error')
- # self.assertEqual(
- # ret,
- # ['A recursive requisite was found, SLS "requisites.prereq_recursion_error" ID "B" ID "A"']
- # )
- ret = self.run_function(
- "state.sls", mods="requisites.prereq_simple_no_state_module"
- )
- result = self.normalize_ret(ret)
- self.assertEqual(expected_result_simple_no_state_module, result)
- @slowTest
- def test_infinite_recursion_sls_prereq(self):
- ret = self.run_function(
- "state.sls", mods="requisites.prereq_sls_infinite_recursion"
- )
- self.assertSaltTrueReturn(ret)
- @slowTest
- def test_requisites_use(self):
- """
- Call sls file containing several use_in and use.
- """
- # TODO issue #8235 & #8774 some examples are still commented in the test file
- ret = self.run_function("state.sls", mods="requisites.use")
- self.assertReturnNonEmptySaltType(ret)
- for item, descr in ret.items():
- self.assertEqual(descr["comment"], "onlyif condition is false")
- # TODO: issue #8802 : use recursions undetected
- # issue is closed as use does not actually inherit requisites
- # if chain-use is added after #8774 resolution theses tests would maybe become useful
- # ret = self.run_function('state.sls', mods='requisites.use_recursion')
- # self.assertEqual(ret, [
- # 'A recursive requisite was found, SLS "requisites.use_recursion"'
- # + ' ID "B" ID "A"'
- # ])
- # ret = self.run_function('state.sls', mods='requisites.use_recursion2')
- # self.assertEqual(ret, [
- # 'A recursive requisite was found, SLS "requisites.use_recursion2"'
- # + ' ID "C" ID "A"'
- # ])
- # ret = self.run_function('state.sls', mods='requisites.use_auto_recursion')
- # self.assertEqual(ret, [
- # 'A recursive requisite was found, SLS "requisites.use_recursion"'
- # + ' ID "A" ID "A"'
- # ])
- @slowTest
- def test_requisites_use_no_state_module(self):
- """
- Call sls file containing several use_in and use.
- """
- ret = self.run_function("state.sls", mods="requisites.use_no_state_module")
- self.assertReturnNonEmptySaltType(ret)
- for item, descr in ret.items():
- self.assertEqual(descr["comment"], "onlyif condition is false")
- @slowTest
- def test_onlyif_req(self):
- ret = self.run_function(
- "state.single",
- fun="test.succeed_with_changes",
- name="onlyif test",
- onlyif=[{}],
- )["test_|-onlyif test_|-onlyif test_|-succeed_with_changes"]
- self.assertTrue(ret["result"])
- self.assertEqual(ret["comment"], "Success!")
- ret = self.run_function(
- "state.single",
- fun="test.fail_with_changes",
- name="onlyif test",
- onlyif=[{"fun": "test.false"}],
- )["test_|-onlyif test_|-onlyif test_|-fail_with_changes"]
- self.assertTrue(ret["result"])
- self.assertFalse(ret["changes"])
- self.assertEqual(ret["comment"], "onlyif condition is false")
- ret = self.run_function(
- "state.single",
- fun="test.fail_with_changes",
- name="onlyif test",
- onlyif=[{"fun": "test.true"}],
- )["test_|-onlyif test_|-onlyif test_|-fail_with_changes"]
- self.assertFalse(ret["result"])
- self.assertTrue(ret["changes"])
- self.assertEqual(ret["comment"], "Failure!")
- ret = self.run_function(
- "state.single",
- fun="test.succeed_without_changes",
- name="onlyif test",
- onlyif=[{"fun": "test.true"}],
- )["test_|-onlyif test_|-onlyif test_|-succeed_without_changes"]
- self.assertTrue(ret["result"])
- self.assertFalse(ret["changes"])
- self.assertEqual(ret["comment"], "Success!")
- @slowTest
- def test_onlyif_req_retcode(self):
- ret = self.run_function(
- "state.single",
- fun="test.succeed_with_changes",
- name="onlyif test",
- onlyif=[{"fun": "test.retcode"}],
- )["test_|-onlyif test_|-onlyif test_|-succeed_with_changes"]
- self.assertTrue(ret["result"])
- self.assertFalse(ret["changes"])
- self.assertEqual(ret["comment"], "onlyif condition is false")
- ret = self.run_function(
- "state.single",
- fun="test.succeed_with_changes",
- name="onlyif test",
- onlyif=[{"fun": "test.retcode", "code": 0}],
- )["test_|-onlyif test_|-onlyif test_|-succeed_with_changes"]
- self.assertTrue(ret["result"])
- self.assertTrue(ret["changes"])
- self.assertEqual(ret["comment"], "Success!")
- @slowTest
- def test_unless_req(self):
- ret = self.run_function(
- "state.single",
- fun="test.succeed_with_changes",
- name="unless test",
- unless=[{}],
- )["test_|-unless test_|-unless test_|-succeed_with_changes"]
- self.assertTrue(ret["result"])
- self.assertEqual(ret["comment"], "Success!")
- ret = self.run_function(
- "state.single",
- fun="test.fail_with_changes",
- name="unless test",
- unless=[{"fun": "test.true"}],
- )["test_|-unless test_|-unless test_|-fail_with_changes"]
- self.assertTrue(ret["result"])
- self.assertFalse(ret["changes"])
- self.assertEqual(ret["comment"], "unless condition is true")
- ret = self.run_function(
- "state.single",
- fun="test.fail_with_changes",
- name="unless test",
- unless=[{"fun": "test.false"}],
- )["test_|-unless test_|-unless test_|-fail_with_changes"]
- self.assertFalse(ret["result"])
- self.assertTrue(ret["changes"])
- self.assertEqual(ret["comment"], "Failure!")
- ret = self.run_function(
- "state.single",
- fun="test.succeed_without_changes",
- name="unless test",
- unless=[{"fun": "test.false"}],
- )["test_|-unless test_|-unless test_|-succeed_without_changes"]
- self.assertTrue(ret["result"])
- self.assertFalse(ret["changes"])
- self.assertEqual(ret["comment"], "Success!")
- @slowTest
- def test_unless_req_retcode(self):
- ret = self.run_function(
- "state.single",
- fun="test.succeed_with_changes",
- name="unless test",
- unless=[{"fun": "test.retcode"}],
- )["test_|-unless test_|-unless test_|-succeed_with_changes"]
- self.assertTrue(ret["result"])
- self.assertTrue(ret["changes"])
- self.assertEqual(ret["comment"], "Success!")
- ret = self.run_function(
- "state.single",
- fun="test.succeed_with_changes",
- name="unless test",
- unless=[{"fun": "test.retcode", "code": 0}],
- )["test_|-unless test_|-unless test_|-succeed_with_changes"]
- self.assertTrue(ret["result"])
- self.assertFalse(ret["changes"])
- self.assertEqual(ret["comment"], "unless condition is true")
- @slowTest
- @pytest.mark.usefixtures("salt_sub_minion")
- def test_get_file_from_env_in_top_match(self):
- tgt = os.path.join(RUNTIME_VARS.TMP, "prod-cheese-file")
- try:
- ret = self.run_function("state.highstate", minion_tgt="sub_minion")
- self.assertSaltTrueReturn(ret)
- self.assertTrue(os.path.isfile(tgt))
- with salt.utils.files.fopen(tgt, "r") as cheese:
- data = salt.utils.stringutils.to_unicode(cheese.read())
- self.assertIn("Gromit", data)
- self.assertIn("Comte", data)
- finally:
- if os.path.islink(tgt):
- os.unlink(tgt)
- # onchanges tests
- @slowTest
- def test_onchanges_requisite(self):
- """
- Tests a simple state using the onchanges requisite
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.onchanges_simple")
- # First, test the result of the state run when changes are expected to happen
- test_data = state_run['cmd_|-test_changing_state_|-echo "Success!"_|-run'][
- "comment"
- ]
- expected_result = 'Command "echo "Success!"" run'
- self.assertIn(expected_result, test_data)
- # Then, test the result of the state run when changes are not expected to happen
- test_data = state_run[
- 'cmd_|-test_non_changing_state_|-echo "Should not run"_|-run'
- ]["comment"]
- expected_result = "State was not run because none of the onchanges reqs changed"
- self.assertIn(expected_result, test_data)
- @slowTest
- def test_onchanges_requisite_multiple(self):
- """
- Tests a simple state using the onchanges requisite
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.onchanges_multiple")
- # First, test the result of the state run when two changes are expected to happen
- test_data = state_run['cmd_|-test_two_changing_states_|-echo "Success!"_|-run'][
- "comment"
- ]
- expected_result = 'Command "echo "Success!"" run'
- self.assertIn(expected_result, test_data)
- # Then, test the result of the state run when two changes are not expected to happen
- test_data = state_run[
- 'cmd_|-test_two_non_changing_states_|-echo "Should not run"_|-run'
- ]["comment"]
- expected_result = "State was not run because none of the onchanges reqs changed"
- self.assertIn(expected_result, test_data)
- # Finally, test the result of the state run when only one of the onchanges requisites changes.
- test_data = state_run['cmd_|-test_one_changing_state_|-echo "Success!"_|-run'][
- "comment"
- ]
- expected_result = 'Command "echo "Success!"" run'
- self.assertIn(expected_result, test_data)
- @slowTest
- def test_onchanges_in_requisite(self):
- """
- Tests a simple state using the onchanges_in requisite
- """
- # Only run the state once and keep the return data
- state_run = self.run_function(
- "state.sls", mods="requisites.onchanges_in_simple"
- )
- # First, test the result of the state run of when changes are expected to happen
- test_data = state_run['cmd_|-test_changes_expected_|-echo "Success!"_|-run'][
- "comment"
- ]
- expected_result = 'Command "echo "Success!"" run'
- self.assertIn(expected_result, test_data)
- # Then, test the result of the state run when changes are not expected to happen
- test_data = state_run[
- 'cmd_|-test_changes_not_expected_|-echo "Should not run"_|-run'
- ]["comment"]
- expected_result = "State was not run because none of the onchanges reqs changed"
- self.assertIn(expected_result, test_data)
- @slowTest
- def test_onchanges_requisite_no_state_module(self):
- """
- Tests a simple state using the onchanges requisite without state modules
- """
- # Only run the state once and keep the return data
- state_run = self.run_function(
- "state.sls", mods="requisites.onchanges_simple_no_state_module"
- )
- test_data = state_run['cmd_|-test_changing_state_|-echo "Success!"_|-run'][
- "comment"
- ]
- expected_result = 'Command "echo "Success!"" run'
- self.assertIn(expected_result, test_data)
- @slowTest
- def test_onchanges_requisite_with_duration(self):
- """
- Tests a simple state using the onchanges requisite
- the state will not run but results will include duration
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.onchanges_simple")
- # Then, test the result of the state run when changes are not expected to happen
- # and ensure duration is included in the results
- test_data = state_run[
- 'cmd_|-test_non_changing_state_|-echo "Should not run"_|-run'
- ]
- self.assertIn("duration", test_data)
- # onfail tests
- @slowTest
- def test_onfail_requisite(self):
- """
- Tests a simple state using the onfail requisite
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.onfail_simple")
- # First, test the result of the state run when a failure is expected to happen
- test_data = state_run['cmd_|-test_failing_state_|-echo "Success!"_|-run'][
- "comment"
- ]
- expected_result = 'Command "echo "Success!"" run'
- self.assertIn(expected_result, test_data)
- # Then, test the result of the state run when a failure is not expected to happen
- test_data = state_run[
- 'cmd_|-test_non_failing_state_|-echo "Should not run"_|-run'
- ]["comment"]
- expected_result = "State was not run because onfail req did not change"
- self.assertIn(expected_result, test_data)
- @slowTest
- def test_multiple_onfail_requisite(self):
- """
- test to ensure state is run even if only one
- of the onfails fails. This is a test for the issue:
- https://github.com/saltstack/salt/issues/22370
- """
- state_run = self.run_function(
- "state.sls", mods="requisites.onfail_multiple", timeout=self.TIMEOUT
- )
- retcode = state_run["cmd_|-c_|-echo itworked_|-run"]["changes"]["retcode"]
- self.assertEqual(retcode, 0)
- stdout = state_run["cmd_|-c_|-echo itworked_|-run"]["changes"]["stdout"]
- self.assertEqual(stdout, "itworked")
- @slowTest
- def test_onfail_in_requisite(self):
- """
- Tests a simple state using the onfail_in requisite
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.onfail_in_simple")
- # First, test the result of the state run when a failure is expected to happen
- test_data = state_run['cmd_|-test_failing_state_|-echo "Success!"_|-run'][
- "comment"
- ]
- expected_result = 'Command "echo "Success!"" run'
- self.assertIn(expected_result, test_data)
- # Then, test the result of the state run when a failure is not expected to happen
- test_data = state_run[
- 'cmd_|-test_non_failing_state_|-echo "Should not run"_|-run'
- ]["comment"]
- expected_result = "State was not run because onfail req did not change"
- self.assertIn(expected_result, test_data)
- @slowTest
- def test_onfail_requisite_no_state_module(self):
- """
- Tests a simple state using the onfail requisite
- """
- # Only run the state once and keep the return data
- state_run = self.run_function(
- "state.sls", mods="requisites.onfail_simple_no_state_module"
- )
- # First, test the result of the state run when a failure is expected to happen
- test_data = state_run['cmd_|-test_failing_state_|-echo "Success!"_|-run'][
- "comment"
- ]
- expected_result = 'Command "echo "Success!"" run'
- self.assertIn(expected_result, test_data)
- # Then, test the result of the state run when a failure is not expected to happen
- test_data = state_run[
- 'cmd_|-test_non_failing_state_|-echo "Should not run"_|-run'
- ]["comment"]
- expected_result = "State was not run because onfail req did not change"
- self.assertIn(expected_result, test_data)
- @slowTest
- def test_onfail_requisite_with_duration(self):
- """
- Tests a simple state using the onfail requisite
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.onfail_simple")
- # Then, test the result of the state run when a failure is not expected to happen
- test_data = state_run[
- 'cmd_|-test_non_failing_state_|-echo "Should not run"_|-run'
- ]
- self.assertIn("duration", test_data)
- @slowTest
- def test_multiple_onfail_requisite_with_required(self):
- """
- test to ensure multiple states are run
- when specified as onfails for a single state.
- This is a test for the issue:
- https://github.com/saltstack/salt/issues/46552
- """
- state_run = self.run_function(
- "state.sls", mods="requisites.onfail_multiple_required"
- )
- retcode = state_run["cmd_|-b_|-echo b_|-run"]["changes"]["retcode"]
- self.assertEqual(retcode, 0)
- retcode = state_run["cmd_|-c_|-echo c_|-run"]["changes"]["retcode"]
- self.assertEqual(retcode, 0)
- retcode = state_run["cmd_|-d_|-echo d_|-run"]["changes"]["retcode"]
- self.assertEqual(retcode, 0)
- stdout = state_run["cmd_|-b_|-echo b_|-run"]["changes"]["stdout"]
- self.assertEqual(stdout, "b")
- stdout = state_run["cmd_|-c_|-echo c_|-run"]["changes"]["stdout"]
- self.assertEqual(stdout, "c")
- stdout = state_run["cmd_|-d_|-echo d_|-run"]["changes"]["stdout"]
- self.assertEqual(stdout, "d")
- comment = state_run["cmd_|-e_|-echo e_|-run"]["comment"]
- self.assertEqual(comment, "State was not run because onfail req did not change")
- stdout = state_run["cmd_|-f_|-echo f_|-run"]["changes"]["stdout"]
- self.assertEqual(stdout, "f")
- @slowTest
- def test_multiple_onfail_requisite_with_required_no_run(self):
- """
- test to ensure multiple states are not run
- when specified as onfails for a single state
- which fails.
- This is a test for the issue:
- https://github.com/saltstack/salt/issues/46552
- """
- state_run = self.run_function(
- "state.sls", mods="requisites.onfail_multiple_required_no_run"
- )
- expected = "State was not run because onfail req did not change"
- stdout = state_run["cmd_|-b_|-echo b_|-run"]["comment"]
- self.assertEqual(stdout, expected)
- stdout = state_run["cmd_|-c_|-echo c_|-run"]["comment"]
- self.assertEqual(stdout, expected)
- stdout = state_run["cmd_|-d_|-echo d_|-run"]["comment"]
- self.assertEqual(stdout, expected)
- # listen tests
- @slowTest
- def test_listen_requisite(self):
- """
- Tests a simple state using the listen requisite
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.listen_simple")
- # First, test the result of the state run when a listener is expected to trigger
- listener_state = 'cmd_|-listener_test_listening_change_state_|-echo "Listening State"_|-mod_watch'
- self.assertIn(listener_state, state_run)
- # Then, test the result of the state run when a listener should not trigger
- absent_state = 'cmd_|-listener_test_listening_non_changing_state_|-echo "Only run once"_|-mod_watch'
- self.assertNotIn(absent_state, state_run)
- @slowTest
- def test_listen_in_requisite(self):
- """
- Tests a simple state using the listen_in requisite
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.listen_in_simple")
- # First, test the result of the state run when a listener is expected to trigger
- listener_state = 'cmd_|-listener_test_listening_change_state_|-echo "Listening State"_|-mod_watch'
- self.assertIn(listener_state, state_run)
- # Then, test the result of the state run when a listener should not trigger
- absent_state = 'cmd_|-listener_test_listening_non_changing_state_|-echo "Only run once"_|-mod_watch'
- self.assertNotIn(absent_state, state_run)
- @slowTest
- def test_listen_in_requisite_resolution(self):
- """
- Verify listen_in requisite lookups use ID declaration to check for changes
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.listen_in_simple")
- # Test the result of the state run when a listener is expected to trigger
- listener_state = 'cmd_|-listener_test_listen_in_resolution_|-echo "Successful listen_in resolution"_|-mod_watch'
- self.assertIn(listener_state, state_run)
- @slowTest
- def test_listen_requisite_resolution(self):
- """
- Verify listen requisite lookups use ID declaration to check for changes
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.listen_simple")
- # Both listeners are expected to trigger
- listener_state = 'cmd_|-listener_test_listening_resolution_one_|-echo "Successful listen resolution"_|-mod_watch'
- self.assertIn(listener_state, state_run)
- listener_state = 'cmd_|-listener_test_listening_resolution_two_|-echo "Successful listen resolution"_|-mod_watch'
- self.assertIn(listener_state, state_run)
- @slowTest
- def test_listen_requisite_no_state_module(self):
- """
- Tests a simple state using the listen requisite
- """
- # Only run the state once and keep the return data
- state_run = self.run_function(
- "state.sls", mods="requisites.listen_simple_no_state_module"
- )
- # First, test the result of the state run when a listener is expected to trigger
- listener_state = 'cmd_|-listener_test_listening_change_state_|-echo "Listening State"_|-mod_watch'
- self.assertIn(listener_state, state_run)
- # Then, test the result of the state run when a listener should not trigger
- absent_state = 'cmd_|-listener_test_listening_non_changing_state_|-echo "Only run once"_|-mod_watch'
- self.assertNotIn(absent_state, state_run)
- @slowTest
- def test_listen_in_requisite_resolution_names(self):
- """
- Verify listen_in requisite lookups use ID declaration to check for changes
- and resolves magic names state variable
- """
- # Only run the state once and keep the return data
- state_run = self.run_function("state.sls", mods="requisites.listen_in_names")
- self.assertIn("test_|-listener_service_|-nginx_|-mod_watch", state_run)
- self.assertIn("test_|-listener_service_|-crond_|-mod_watch", state_run)
- @slowTest
- def test_listen_requisite_resolution_names(self):
- """
- Verify listen requisite lookups use ID declaration to check for changes
- and resolves magic names state variable
- """
- # Only run the state once and keep the return data
- state_run = self.run_function(
- "state.sls", mods="requisites.listen_names", timeout=self.TIMEOUT
- )
- self.assertIn("test_|-listener_service_|-nginx_|-mod_watch", state_run)
- self.assertIn("test_|-listener_service_|-crond_|-mod_watch", state_run)
- @slowTest
- def test_issue_30820_requisite_in_match_by_name(self):
- """
- This tests the case where a requisite_in matches by name instead of ID
- See https://github.com/saltstack/salt/issues/30820 for more info
- """
- state_run = self.run_function(
- "state.sls", mods="requisites.requisite_in_match_by_name"
- )
- bar_state = "cmd_|-bar state_|-echo bar_|-wait"
- self.assertIn(bar_state, state_run)
- self.assertEqual(state_run[bar_state]["comment"], 'Command "echo bar" run')
- @slowTest
- def test_retry_option_defaults(self):
- """
- test the retry option on a simple state with defaults
- ensure comment is as expected
- ensure state duration is greater than default retry_interval (30 seconds)
- """
- state_run = self.run_function("state.sls", mods="retry.retry_defaults")
- retry_state = "file_|-file_test_|-/path/to/a/non-existent/file.txt_|-exists"
- expected_comment = (
- 'Attempt 1: Returned a result of "False", with the following '
- 'comment: "Specified path /path/to/a/non-existent/file.txt does not exist"\n'
- "Specified path /path/to/a/non-existent/file.txt does not exist"
- )
- self.assertEqual(state_run[retry_state]["comment"], expected_comment)
- self.assertTrue(state_run[retry_state]["duration"] > 30)
- self.assertEqual(state_run[retry_state]["result"], False)
- @slowTest
- def test_retry_option_custom(self):
- """
- test the retry option on a simple state with custom retry values
- ensure comment is as expected
- ensure state duration is greater than custom defined interval * (retries - 1)
- """
- state_run = self.run_function("state.sls", mods="retry.retry_custom")
- retry_state = "file_|-file_test_|-/path/to/a/non-existent/file.txt_|-exists"
- expected_comment = (
- 'Attempt 1: Returned a result of "False", with the following '
- 'comment: "Specified path /path/to/a/non-existent/file.txt does not exist"\n'
- 'Attempt 2: Returned a result of "False", with the following comment: "Specified'
- ' path /path/to/a/non-existent/file.txt does not exist"\nAttempt 3: Returned'
- ' a result of "False", with the following comment: "Specified path'
- ' /path/to/a/non-existent/file.txt does not exist"\nAttempt 4: Returned a'
- ' result of "False", with the following comment: "Specified path'
- ' /path/to/a/non-existent/file.txt does not exist"\nSpecified path'
- " /path/to/a/non-existent/file.txt does not exist"
- )
- self.assertEqual(state_run[retry_state]["comment"], expected_comment)
- self.assertTrue(state_run[retry_state]["duration"] > 40)
- self.assertEqual(state_run[retry_state]["result"], False)
- @slowTest
- def test_retry_option_success(self):
- """
- test a state with the retry option that should return True immedietly (i.e. no retries)
- """
- testfile = os.path.join(RUNTIME_VARS.TMP, "retry_file_option_success")
- state_run = self.run_function("state.sls", mods="retry.retry_success")
- os.unlink(testfile)
- retry_state = "file_|-file_test_|-{}_|-exists".format(testfile)
- self.assertNotIn("Attempt", state_run[retry_state]["comment"])
- def run_create(self, testfile):
- """
- helper function to wait 30 seconds and then create the temp retry file
- """
- # Wait for the requisite stae 'file_test_a' to complete before creating
- # test_file
- while True:
- if os.path.exists(testfile + "_a"):
- break
- time.sleep(1)
- time.sleep(30)
- with salt.utils.files.fopen(testfile, "a"):
- pass
- @slowTest
- def test_retry_option_eventual_success(self):
- """
- test a state with the retry option that should return True after at least 4 retry attmempt
- but never run 15 attempts
- """
- testfile = os.path.join(RUNTIME_VARS.TMP, "retry_file_eventual_success")
- assert not os.path.exists(testfile + "_a")
- assert not os.path.exists(testfile)
- create_thread = threading.Thread(target=self.run_create, args=(testfile,))
- create_thread.start()
- state_run = self.run_function("state.sls", mods="retry.retry_success2")
- retry_state = "file_|-file_test_b_|-{}_|-exists".format(testfile)
- self.assertIn("Attempt 1:", state_run[retry_state]["comment"])
- self.assertIn("Attempt 2:", state_run[retry_state]["comment"])
- self.assertIn("Attempt 3:", state_run[retry_state]["comment"])
- self.assertIn("Attempt 4:", state_run[retry_state]["comment"])
- self.assertNotIn("Attempt 15:", state_run[retry_state]["comment"])
- self.assertEqual(state_run[retry_state]["result"], True)
- @slowTest
- def test_issue_38683_require_order_failhard_combination(self):
- """
- This tests the case where require, order, and failhard are all used together in a state definition.
- Previously, the order option, which used in tandem with require and failhard, would cause the state
- compiler to stacktrace. This exposed a logic error in the ``check_failhard`` function of the state
- compiler. With the logic error resolved, this test should now pass.
- See https://github.com/saltstack/salt/issues/38683 for more information.
- """
- state_run = self.run_function(
- "state.sls", mods="requisites.require_order_failhard_combo"
- )
- state_id = "test_|-b_|-b_|-fail_with_changes"
- self.assertIn(state_id, state_run)
- self.assertEqual(state_run[state_id]["comment"], "Failure!")
- self.assertFalse(state_run[state_id]["result"])
- @slowTest
- def test_issue_46762_prereqs_on_a_state_with_unfulfilled_requirements(self):
- """
- This tests the case where state C requires state A, which fails.
- State C is a pre-required state for State B.
- Since state A fails, state C will not run because the requisite failed,
- therefore state B will not run because state C failed to run.
- See https://github.com/saltstack/salt/issues/46762 for
- more information.
- """
- state_run = self.run_function("state.sls", mods="issue-46762")
- state_id = "test_|-a_|-a_|-fail_without_changes"
- self.assertIn(state_id, state_run)
- self.assertEqual(state_run[state_id]["comment"], "Failure!")
- self.assertFalse(state_run[state_id]["result"])
- state_id = "test_|-b_|-b_|-nop"
- self.assertIn(state_id, state_run)
- self.assertEqual(
- state_run[state_id]["comment"],
- "One or more requisite failed: issue-46762.c",
- )
- self.assertFalse(state_run[state_id]["result"])
- state_id = "test_|-c_|-c_|-nop"
- self.assertIn(state_id, state_run)
- self.assertEqual(
- state_run[state_id]["comment"],
- "One or more requisite failed: issue-46762.a",
- )
- self.assertFalse(state_run[state_id]["result"])
- @slowTest
- def test_state_nonbase_environment(self):
- """
- test state.sls with saltenv using a nonbase environment
- with a salt source
- """
- filename = os.path.join(RUNTIME_VARS.TMP, "nonbase_env")
- try:
- ret = self.run_function("state.sls", mods="non-base-env", saltenv="prod")
- ret = ret[next(iter(ret))]
- assert ret["result"]
- assert ret["comment"] == "File {} updated".format(filename)
- assert os.path.isfile(filename)
- finally:
- try:
- os.remove(filename)
- except OSError:
- pass
- @skipIf(
- sys.platform.startswith("win"),
- "Skipped until parallel states can be fixed on Windows",
- )
- @skipIf(
- salt.utils.platform.is_darwin() and six.PY2, "This test hangs on OS X on Py2"
- )
- @slowTest
- def test_parallel_state_with_long_tag(self):
- """
- This tests the case where the state being executed has a long ID dec or
- name and states are being run in parallel. The filenames used for the
- parallel state cache were previously based on the tag for each chunk,
- and longer ID decs or name params can cause the cache file to be longer
- than the operating system's max file name length. To counter this we
- instead generate a SHA1 hash of the chunk's tag to use as the cache
- filename. This test will ensure that long tags don't cause caching
- failures.
- See https://github.com/saltstack/salt/issues/49738 for more info.
- """
- short_command = "helloworld"
- long_command = short_command * 25
- ret = self.run_function(
- "state.sls",
- mods="issue-49738",
- pillar={"short_command": short_command, "long_command": long_command},
- )
- comments = sorted([x["comment"] for x in ret.values()])
- expected = sorted(
- ['Command "{}" run'.format(x) for x in (short_command, long_command)]
- )
- assert comments == expected, "{} != {}".format(comments, expected)
- def _add_runtime_pillar(self, pillar):
- """
- helper class to add pillar data at runtime
- """
- import salt.utils.yaml
- with salt.utils.files.fopen(
- os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE, "pillar.sls"), "w"
- ) as fp:
- salt.utils.yaml.safe_dump(pillar, fp)
- with salt.utils.files.fopen(
- os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE, "top.sls"), "w"
- ) as fp:
- fp.write(
- textwrap.dedent(
- """\
- base:
- '*':
- - pillar
- """
- )
- )
- self.run_function("saltutil.refresh_pillar")
- self.run_function("test.sleep", [5])
- @slowTest
- def test_state_sls_id_test(self):
- """
- test state.sls_id when test is set
- to true in pillar data
- """
- self._add_runtime_pillar(pillar={"test": True})
- testfile = os.path.join(RUNTIME_VARS.TMP, "testfile")
- comment = "The file {} is set to be changed\nNote: No changes made, actual changes may\nbe different due to other states.".format(
- testfile
- )
- ret = self.run_function("state.sls", ["core"])
- for key, val in ret.items():
- self.assertEqual(val["comment"], comment)
- self.assertEqual(val["changes"], {"newfile": testfile})
- @slowTest
- def test_state_sls_id_test_state_test_post_run(self):
- """
- test state.sls_id when test is set to
- true post the state already being run previously
- """
- file_name = os.path.join(RUNTIME_VARS.TMP, "testfile")
- ret = self.run_function("state.sls", ["core"])
- for key, val in ret.items():
- self.assertEqual(val["comment"], "File {} updated".format(file_name))
- self.assertEqual(val["changes"]["diff"], "New file")
- self._add_runtime_pillar(pillar={"test": True})
- ret = self.run_function("state.sls", ["core"])
- for key, val in ret.items():
- self.assertEqual(
- val["comment"], "The file {} is in the correct state".format(file_name)
- )
- self.assertEqual(val["changes"], {})
- @slowTest
- def test_state_sls_id_test_true(self):
- """
- test state.sls_id when test=True is passed as arg
- """
- file_name = os.path.join(RUNTIME_VARS.TMP, "testfile")
- ret = self.run_function("state.sls", ["core"], test=True)
- for key, val in ret.items():
- self.assertEqual(
- val["comment"],
- "The file {} is set to be changed\nNote: No changes made, actual changes may\nbe different due to other states.".format(
- file_name
- ),
- )
- self.assertEqual(val["changes"], {"newfile": file_name})
- @slowTest
- def test_state_sls_id_test_true_post_run(self):
- """
- test state.sls_id when test is set to true as an
- arg post the state already being run previously
- """
- file_name = os.path.join(RUNTIME_VARS.TMP, "testfile")
- ret = self.run_function("state.sls", ["core"])
- for key, val in ret.items():
- self.assertEqual(val["comment"], "File {} updated".format(file_name))
- self.assertEqual(val["changes"]["diff"], "New file")
- ret = self.run_function("state.sls", ["core"], test=True)
- for key, val in ret.items():
- self.assertEqual(
- val["comment"], "The file {} is in the correct state".format(file_name)
- )
- self.assertEqual(val["changes"], {})
- @slowTest
- def test_state_sls_id_test_false_pillar_true(self):
- """
- test state.sls_id when test is set to false as an
- arg and minion_state_test is set to True. Should
- return test=False.
- """
- file_name = os.path.join(RUNTIME_VARS.TMP, "testfile")
- self._add_runtime_pillar(pillar={"test": True})
- ret = self.run_function("state.sls", ["core"], test=False)
- for key, val in ret.items():
- self.assertEqual(val["comment"], "File {} updated".format(file_name))
- self.assertEqual(val["changes"]["diff"], "New file")
- def test_state_test_pillar_false(self):
- """
- test state.test forces test kwarg to True even when pillar is set to False
- """
- self._add_runtime_pillar(pillar={"test": False})
- testfile = os.path.join(RUNTIME_VARS.TMP, "testfile")
- comment = "The file {} is set to be changed\nNote: No changes made, actual changes may\nbe different due to other states.".format(
- testfile
- )
- ret = self.run_function("state.test", ["core"])
- self.assertIsInstance(ret, dict)
- for key, val in ret.items():
- self.assertEqual(val["comment"], comment)
- self.assertEqual(val["changes"], {"newfile": testfile})
- def test_state_test_test_false_pillar_false(self):
- """
- test state.test forces test kwarg to True even when pillar and kwarg are set
- to False
- """
- self._add_runtime_pillar(pillar={"test": False})
- testfile = os.path.join(RUNTIME_VARS.TMP, "testfile")
- comment = "The file {} is set to be changed\nNote: No changes made, actual changes may\nbe different due to other states.".format(
- testfile
- )
- ret = self.run_function("state.test", ["core"], test=False)
- for key, val in ret.items():
- self.assertEqual(val["comment"], comment)
- self.assertEqual(val["changes"], {"newfile": testfile})
- @skipIf(
- six.PY3 and salt.utils.platform.is_darwin(), "Test is broken on macosx and PY3"
- )
- @slowTest
- def test_issue_30161_unless_and_onlyif_together(self):
- """
- test cmd.run using multiple unless options where the first cmd in the
- list will pass, but the second will fail. This tests the fix for issue
- #35384. (The fix is in PR #35545.)
- """
- sls = self.run_function("state.sls", mods="issue-30161")
- self.assertSaltTrueReturn(sls)
- # We must assert against the comment here to make sure the comment reads that the
- # command "echo "hello"" was run. This ensures that we made it to the last unless
- # command in the state. If the comment reads "unless condition is true", or similar,
- # then the unless state run bailed out after the first unless command succeeded,
- # which is the bug we're regression testing for.
- _expected = {
- "file_|-unless_false_onlyif_false_|-{}{}test.txt_|-managed".format(
- RUNTIME_VARS.TMP, os.path.sep
- ): {
- "comment": "onlyif condition is false\nunless condition is false",
- "name": "{}{}test.txt".format(RUNTIME_VARS.TMP, os.path.sep),
- "skip_watch": True,
- "changes": {},
- "result": True,
- },
- "file_|-unless_false_onlyif_true_|-{}{}test.txt_|-managed".format(
- RUNTIME_VARS.TMP, os.path.sep
- ): {
- "comment": "Empty file",
- "name": "{}{}test.txt".format(RUNTIME_VARS.TMP, os.path.sep),
- "start_time": "18:10:20.341753",
- "result": True,
- "changes": {
- "new": "file {}{}test.txt created".format(
- RUNTIME_VARS.TMP, os.path.sep
- )
- },
- },
- "file_|-unless_true_onlyif_false_|-{}{}test.txt_|-managed".format(
- RUNTIME_VARS.TMP, os.path.sep
- ): {
- "comment": "onlyif condition is false\nunless condition is true",
- "name": "{}{}test.txt".format(RUNTIME_VARS.TMP, os.path.sep),
- "start_time": "18:10:22.936446",
- "skip_watch": True,
- "changes": {},
- "result": True,
- },
- "file_|-unless_true_onlyif_true_|-{}{}test.txt_|-managed".format(
- RUNTIME_VARS.TMP, os.path.sep
- ): {
- "comment": "onlyif condition is true\nunless condition is true",
- "name": "{}{}test.txt".format(RUNTIME_VARS.TMP, os.path.sep),
- "skip_watch": True,
- "changes": {},
- "result": True,
- },
- }
- for id in _expected:
- self.assertEqual(sls[id]["comment"], _expected[id]["comment"])
- @skipIf(
- six.PY3 and salt.utils.platform.is_darwin(), "Test is broken on macosx and PY3"
- )
- @slowTest
- def test_state_sls_unicode_characters(self):
- """
- test state.sls when state file contains non-ascii characters
- """
- ret = self.run_function("state.sls", ["issue-46672"])
- log.debug("== ret %s ==", type(ret))
- _expected = "cmd_|-echo1_|-echo 'This is Æ test!'_|-run"
- self.assertIn(_expected, ret)
- def tearDown(self):
- rm_files = [
- os.path.join(RUNTIME_VARS.TMP, "nonbase_env"),
- os.path.join(RUNTIME_VARS.TMP, "testfile"),
- os.path.join(RUNTIME_VARS.TMP, "test.txt"),
- os.path.join(RUNTIME_VARS.TMP_STATE_TREE, "top.sls"),
- ]
- for file_ in rm_files:
- if os.path.isfile(file_):
- os.remove(file_)
- # remove old pillar data
- for filename in os.listdir(RUNTIME_VARS.TMP_PILLAR_TREE):
- os.remove(os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE, filename))
- self.run_function("saltutil.refresh_pillar")
- self.run_function("test.sleep", [5])
- @slowTest
- def test_state_sls_integer_name(self):
- """
- This tests the case where the state file is named
- only with integers
- """
- state_run = self.run_function("state.sls", mods="12345")
- state_id = "test_|-always-passes_|-always-passes_|-succeed_without_changes"
- self.assertIn(state_id, state_run)
- self.assertEqual(state_run[state_id]["comment"], "Success!")
- self.assertTrue(state_run[state_id]["result"])
- @slowTest
- def test_state_sls_lazyloader_allows_recursion(self):
- """
- This tests that referencing dunders like __salt__ work
- context: https://github.com/saltstack/salt/pull/51499
- """
- state_run = self.run_function("state.sls", mods="issue-51499")
- state_id = "test_|-always-passes_|-foo_|-succeed_without_changes"
- self.assertIn(state_id, state_run)
- self.assertEqual(state_run[state_id]["comment"], "Success!")
- self.assertTrue(state_run[state_id]["result"])
- @slowTest
- def test_issue_56131(self):
- module_path = os.path.join(RUNTIME_VARS.CODE_DIR, "pip.py")
- modulec_path = os.path.join(RUNTIME_VARS.CODE_DIR, "__pycache__", "pip.pyc")
- unzip_path = os.path.join(RUNTIME_VARS.TMP, "issue-56131.txt")
- def clean_paths(paths):
- for path in paths:
- try:
- os.remove(path)
- except OSError:
- log.warn("Path not found: %s", path)
- with salt.utils.files.fopen(module_path, "w") as fp:
- fp.write('raise ImportError("No module named pip")')
- self.addCleanup(clean_paths, [unzip_path, module_path, modulec_path])
- assert not os.path.exists(unzip_path)
- state_run = self.run_function(
- "state.sls",
- mods="issue-56131",
- pillar={"unzip_to": RUNTIME_VARS.TMP},
- timeout=30,
- )
- assert state_run is not False
- assert os.path.exists(unzip_path)
- @slowTest
- def test_jinja_renderer_argline(self):
- """
- This is a test case for https://github.com/saltstack/salt/issues/55124
- Renderer for this is in tests/integration/files/file/base/_renderers/issue55124.py
- """
- result = self.run_function("state.sls", mods="issue-55124")
- assert isinstance(result, dict), result
- result = result[next(iter(result))]
- assert result["result"], result
- @slowTest
- def test_parallel_state_with_requires(self):
- """
- This is a test case for https://github.com/saltstack/salt/issues/49273
- Parallel state object has any requisites
- """
- state_file = """
- barrier:
- cmd.run:
- - name: sleep 1
-
- {%- for x in range(1, 10) %}
- blah-{{x}}:
- cmd.run:
- - name: sleep 2
- - require:
- - barrier
- - barrier2
- - parallel: true
- {% endfor %}
- barrier2:
- test.nop
- """
- with temp_state_file("parallel-with-requisites.sls", state_file):
- start_time = time.time()
- result = self.run_function("state.sls", mods="parallel-with-requisites")
- end_time = time.time()
- # We're running 3 states that sleep for 10 seconds each
- # they'll run in parallel so we should be below 30 seconds
- # confirm that the total runtime is below 30s
- self.assertTrue((time.time() - start_time) < 30)
|