@@ -18,7 +18,7 @@ def account():
1818
1919
2020@pytest .fixture
21- def echo_script ( state_tree ):
21+ def echo_script_contents ( ):
2222 if salt .utils .platform .is_windows ():
2323 file_name = "echo_script.bat"
2424 contents = dedent (
@@ -39,12 +39,25 @@ def echo_script(state_tree):
3939 echo "a: $a, b: $b"
4040 """
4141 )
42+ return file_name , contents
43+
44+
45+ @pytest .fixture
46+ def echo_script (state_tree , echo_script_contents ):
47+ file_name , contents = echo_script_contents
4248 with pytest .helpers .temp_file (file_name , contents , state_tree / "echo-script" ):
4349 yield file_name
4450
4551
4652@pytest .fixture
47- def pipe_script (state_tree ):
53+ def echo_script_with_space (state_tree , echo_script_contents ):
54+ file_name , contents = echo_script_contents
55+ with pytest .helpers .temp_file (file_name , contents , state_tree / "echo script" ):
56+ yield file_name
57+
58+
59+ @pytest .fixture
60+ def pipe_script_contents ():
4861 if salt .utils .platform .is_windows ():
4962 file_name = "pipe_script.bat"
5063 contents = dedent (
@@ -69,6 +82,12 @@ def pipe_script(state_tree):
6982 fi
7083 """
7184 )
85+ return file_name , contents
86+
87+
88+ @pytest .fixture
89+ def pipe_script (pipe_script_contents , state_tree ):
90+ file_name , contents = pipe_script_contents
7291 with pytest .helpers .temp_file (file_name , contents , state_tree / "echo-script" ) as f :
7392 if not salt .utils .platform .is_windows ():
7493 current_perms = f .stat ().st_mode
@@ -78,6 +97,22 @@ def pipe_script(state_tree):
7897 yield f
7998
8099
100+ @pytest .fixture
101+ def pipe_script_with_space (pipe_script_contents , state_tree ):
102+ file_name , contents = pipe_script_contents
103+ with pytest .helpers .temp_directory () as temp_path :
104+ temp_file_path = temp_path / "dir with spaces" / file_name
105+ temp_file_path .parent .mkdir (parents = True )
106+ assert temp_file_path .parent .exists ()
107+ temp_file_path .write_text (contents )
108+ if not salt .utils .platform .is_windows ():
109+ current_perms = temp_file_path .stat ().st_mode
110+ new_perms = current_perms | stat .S_IXUSR
111+ temp_file_path .chmod (new_perms )
112+ temp_file_path .chmod (0o755 )
113+ yield temp_file_path
114+
115+
81116@pytest .mark .parametrize (
82117 "args, expected" ,
83118 [
@@ -87,7 +122,7 @@ def pipe_script(state_tree):
87122 (["foo foo" , "bar bar" ], "a: foo foo, b: bar bar" ),
88123 ],
89124)
90- def test_echo (modules , echo_script , args , expected ):
125+ def test_script_args (modules , echo_script , args , expected ):
91126 """
92127 Test argument processing with a batch script
93128 """
@@ -105,7 +140,25 @@ def test_echo(modules, echo_script, args, expected):
105140 (["foo foo" , "bar bar" ], "a: foo foo, b: bar bar" ),
106141 ],
107142)
108- def test_echo_runas (modules , account , echo_script , args , expected ):
143+ def test_script_args_with_space (modules , echo_script_with_space , args , expected ):
144+ """
145+ Test argument processing with a batch script
146+ """
147+ script = f"salt://echo script/{ echo_script_with_space } "
148+ result = modules .cmd .script (script , args = args )
149+ assert result ["stdout" ] == expected
150+
151+
152+ @pytest .mark .parametrize (
153+ "args, expected" ,
154+ [
155+ ("foo bar" , "a: foo, b: bar" ),
156+ ('foo "bar bar"' , "a: foo, b: bar bar" ),
157+ (["foo" , "bar" ], "a: foo, b: bar" ),
158+ (["foo foo" , "bar bar" ], "a: foo foo, b: bar bar" ),
159+ ],
160+ )
161+ def test_script_args_runas (modules , account , echo_script , args , expected ):
109162 """
110163 Test argument processing with a batch/bash script and runas
111164 """
@@ -119,7 +172,30 @@ def test_echo_runas(modules, account, echo_script, args, expected):
119172 assert result ["stdout" ] == expected
120173
121174
122- def test_pipe_run_python_shell_true (modules , pipe_script ):
175+ @pytest .mark .parametrize (
176+ "args, expected" ,
177+ [
178+ ("foo bar" , "a: foo, b: bar" ),
179+ ('foo "bar bar"' , "a: foo, b: bar bar" ),
180+ (["foo" , "bar" ], "a: foo, b: bar" ),
181+ (["foo foo" , "bar bar" ], "a: foo foo, b: bar bar" ),
182+ ],
183+ )
184+ def test_script_args_runas_with_space (modules , account , echo_script_with_space , args , expected ):
185+ """
186+ Test argument processing with a batch/bash script and runas
187+ """
188+ script = f"salt://echo script/{ echo_script_with_space } "
189+ result = modules .cmd .script (
190+ script ,
191+ args = args ,
192+ runas = account .username ,
193+ password = account .password ,
194+ )
195+ assert result ["stdout" ] == expected
196+
197+
198+ def test_run_pipe_python_shell_true (modules , pipe_script ):
123199 if salt .utils .platform .is_windows ():
124200 cmd = f'{ str (pipe_script )} | find /c /v ""'
125201 else :
@@ -128,7 +204,7 @@ def test_pipe_run_python_shell_true(modules, pipe_script):
128204 assert result == "1"
129205
130206
131- def test_pipe_run_python_shell_false (modules , pipe_script ):
207+ def test_run_pipe_python_shell_false (modules , pipe_script ):
132208 if salt .utils .platform .is_windows ():
133209 cmd = f'{ str (pipe_script )} | find /c /v ""'
134210 # Behavior is different on Windows, I think it has to do with how cmd
@@ -141,7 +217,7 @@ def test_pipe_run_python_shell_false(modules, pipe_script):
141217 assert result == expected
142218
143219
144- def test_pipe_run_default (modules , pipe_script ):
220+ def test_run_pipe_default (modules , pipe_script ):
145221 if salt .utils .platform .is_windows ():
146222 cmd = f'{ str (pipe_script )} | find /c /v ""'
147223 else :
@@ -153,7 +229,7 @@ def test_pipe_run_default(modules, pipe_script):
153229 assert result == "1"
154230
155231
156- def test_pipe_run_shell (modules , pipe_script ):
232+ def test_run_pipe_shell (modules , pipe_script ):
157233 if salt .utils .platform .is_windows ():
158234 cmd = f'{ str (pipe_script )} | find /c /v ""'
159235 shell = "cmd"
@@ -165,3 +241,44 @@ def test_pipe_run_shell(modules, pipe_script):
165241 # test suite, the value is empty
166242 result = modules .cmd .run (cmd , shell = shell , __pub_jid = "test" )
167243 assert result == "1"
244+
245+
246+ def test_run_spaces (modules , pipe_script_with_space ):
247+ cmd = f"{ str (pipe_script_with_space )} "
248+ result = modules .cmd .run (cmd )
249+ assert result == "fine"
250+
251+
252+ def test_run_spaces_runas (modules , pipe_script_with_space , account ):
253+ cmd = f"{ str (pipe_script_with_space )} "
254+ result = modules .cmd .run (
255+ cmd ,
256+ runas = account .username ,
257+ password = account .password ,
258+ )
259+ assert result == "fine"
260+
261+
262+ def test_script_pipe_spaces (modules , pipe_script_with_space ):
263+ cmd = f"{ str (pipe_script_with_space )} "
264+ if salt .utils .platform .is_windows ():
265+ args = '| find /c /v ""'
266+ else :
267+ args = "| wc -l"
268+ result = modules .cmd .script (cmd , args = args )
269+ assert result ["stdout" ] == "1"
270+
271+
272+ def test_script_pipe_spaces_runas (modules , pipe_script_with_space , account ):
273+ cmd = f"{ str (pipe_script_with_space )} "
274+ if salt .utils .platform .is_windows ():
275+ args = '| find /c /v ""'
276+ else :
277+ args = "| wc -l"
278+ result = modules .cmd .script (
279+ cmd ,
280+ args = args ,
281+ runas = account .username ,
282+ password = account .password ,
283+ )
284+ assert result ["stdout" ] == "1"
0 commit comments