77
88import pytest
99
10- import salt .loader
1110import salt .modules .cmdmod as cmd
1211import salt .modules .config as config
1312import salt .modules .grains as grains
1413import salt .modules .saltutil as saltutil
1514import salt .modules .state as state_mod
16- import salt .utils .atomicfile
17- import salt .utils .files
18- import salt .utils .path
1915import salt .utils .platform
2016import salt .utils .state as state_util
21- import salt .utils .stringutils
2217
2318log = logging .getLogger (__name__ )
2419
@@ -646,6 +641,36 @@ def test_pydsl(state, state_tree, tmp_path):
646641 assert testfile .exists ()
647642
648643
644+ def test_sls_with_state_args_dict (state , state_tree ):
645+ """
646+ Call sls file with state argument as a dict.
647+ """
648+ sls_contents = """
649+ A:
650+ test.succeed_without_changes:
651+ name: echo foo
652+ """
653+ with pytest .helpers .temp_file ("testing.sls" , sls_contents , state_tree ):
654+ ret = state .sls ("testing" )
655+ for staterun in ret :
656+ assert staterun .result is True
657+
658+
659+ def test_sls_with_state_args_list (state , state_tree ):
660+ """
661+ Call sls file with state argument as a dict instead of a list.
662+ """
663+ sls_contents = """
664+ A:
665+ test.succeed_without_changes:
666+ - name: echo foo
667+ """
668+ with pytest .helpers .temp_file ("testing.sls" , sls_contents , state_tree ):
669+ ret = state .sls ("testing" )
670+ for staterun in ret :
671+ assert staterun .result is True
672+
673+
649674def test_issues_7905_and_8174_sls_syntax_error (state , state_tree ):
650675 """
651676 Call sls file with yaml syntax error.
@@ -680,12 +705,15 @@ def test_issues_7905_and_8174_sls_syntax_error(state, state_tree):
680705 "badlist1.sls" , badlist_1_sls_contents , state_tree
681706 ), pytest .helpers .temp_file ("badlist2.sls" , badlist_2_sls_contents , state_tree ):
682707 ret = state .sls ("badlist1" )
683- assert ret .failed
684- assert ret .errors == ["State 'A' in SLS 'badlist1' is not formed as a list" ]
708+ staterun = ret ["cmd_|-A_|-A_|-run" ]
709+ assert staterun .result is False
710+ assert staterun .changes ["stderr" ] == "/bin/sh: line 1: A: command not found"
685711
686712 ret = state .sls ("badlist2" )
687713 assert ret .failed
688- assert ret .errors == ["State 'C' in SLS 'badlist2' is not formed as a list" ]
714+ assert ret .errors == [
715+ "State 'C' in SLS 'badlist2' is not formed as a list or dict"
716+ ]
689717
690718
691719def test_retry_option (state , state_tree ):
@@ -737,7 +765,7 @@ def test_retry_option_is_true(state, state_tree):
737765 for state_return in ret :
738766 assert state_return .result is False
739767 assert expected_comment in state_return .comment
740- assert state_return .full_return ["duration" ] >= 3
768+ assert state_return .full_return ["duration" ] >= 30
741769
742770
743771@pytest .mark .skip_initial_gh_actions_failure (skip = _check_skip )
@@ -769,7 +797,7 @@ def test_retry_option_success(state, state_tree, tmp_path):
769797 assert state_return .result is True
770798 assert state_return .full_return ["duration" ] < duration
771799 # It should not take 2 attempts
772- assert "Attempt 2 " not in state_return .comment
800+ assert "Attempt 1 " not in state_return .comment
773801
774802
775803@pytest .mark .skip_on_windows (
@@ -820,41 +848,44 @@ def test_retry_option_eventual_success(state, state_tree, tmp_path):
820848 testfile1 = tmp_path / "testfile-1"
821849 testfile2 = tmp_path / "testfile-2"
822850
851+ interval = 2
852+
823853 def create_testfile (testfile1 , testfile2 ):
824854 while True :
825855 if testfile1 .exists ():
826856 break
827- time .sleep (2 )
857+ time .sleep (interval )
828858 testfile2 .touch ()
829859
830860 thread = threading .Thread (target = create_testfile , args = (testfile1 , testfile2 ))
831- sls_contents = """
861+ sls_contents = f """
832862 file_test_a:
833863 file.managed:
834- - name: {}
864+ - name: { testfile1 }
835865 - content: 'a'
836866
837867 file_test:
838868 file.exists:
839- - name: {}
869+ - name: { testfile2 }
840870 - retry:
841871 until: True
842872 attempts: 5
843- interval: 2
873+ interval: { interval }
844874 splay: 0
845875 - require:
846876 - file_test_a
847- """ .format (
848- testfile1 , testfile2
849- )
877+ """
850878 with pytest .helpers .temp_file ("retry.sls" , sls_contents , state_tree ):
851879 thread .start ()
852880 ret = state .sls ("retry" )
853- for state_return in ret :
881+ for num , state_return in enumerate ( ret ) :
854882 assert state_return .result is True
855- assert state_return .full_return ["duration" ] > 4
856- # It should not take 5 attempts
857- assert "Attempt 5" not in state_return .comment
883+ assert state_return .full_return ["duration" ] > interval
884+ if num == 1 :
885+ # It should retry at least 1 time
886+ assert "Attempt 1" in state_return .comment
887+ # It should not take 5 attempts
888+ assert "Attempt 5" not in state_return .comment
858889
859890
860891@pytest .mark .skip_on_windows (
@@ -867,45 +898,48 @@ def test_retry_option_eventual_success_parallel(state, state_tree, tmp_path):
867898 testfile1 = tmp_path / "testfile-1"
868899 testfile2 = tmp_path / "testfile-2"
869900
901+ interval = 2
902+
870903 def create_testfile (testfile1 , testfile2 ):
871904 while True :
872905 if testfile1 .exists ():
873906 break
874- time .sleep (2 )
907+ time .sleep (interval )
875908 testfile2 .touch ()
876909
877910 thread = threading .Thread (target = create_testfile , args = (testfile1 , testfile2 ))
878- sls_contents = """
911+ sls_contents = f """
879912 file_test_a:
880913 file.managed:
881- - name: {}
914+ - name: { testfile1 }
882915 - content: 'a'
883916
884917 file_test:
885918 file.exists:
886- - name: {}
919+ - name: { testfile2 }
887920 - retry:
888921 until: True
889922 attempts: 5
890- interval: 2
923+ interval: { interval }
891924 splay: 0
892925 - parallel: True
893926 - require:
894927 - file_test_a
895- """ .format (
896- testfile1 , testfile2
897- )
928+ """
898929 with pytest .helpers .temp_file ("retry.sls" , sls_contents , state_tree ):
899930 thread .start ()
900931 ret = state .sls (
901932 "retry" , __pub_jid = "1"
902933 ) # Because these run in parallel we need a fake JID
903- for state_return in ret :
934+ for num , state_return in enumerate ( ret ) :
904935 log .debug ("=== state_return %s ===" , state_return )
905936 assert state_return .result is True
906- assert state_return .full_return ["duration" ] > 4
907- # It should not take 5 attempts
908- assert "Attempt 5" not in state_return .comment
937+ assert state_return .full_return ["duration" ] > interval
938+ if num == 1 :
939+ # It should retry at least 1 time
940+ assert "Attempt 1" in state_return .comment
941+ # It should not take 5 attempts
942+ assert "Attempt 5" not in state_return .comment
909943
910944
911945def test_state_non_base_environment (state , state_tree_prod , tmp_path ):
@@ -1131,8 +1165,8 @@ def test_it(name):
11311165 """
11321166 This should not fail on spawning platforms:
11331167 requires_env.test_it:
1134- - name: foo
1135- - parallel: true
1168+ name: foo
1169+ parallel: true
11361170 """
11371171 )
11381172 with pytest .helpers .temp_file (
0 commit comments