Skip to content

Commit af69104

Browse files
committed
cmd: tests
1 parent ed46e97 commit af69104

File tree

4 files changed

+147
-5
lines changed

4 files changed

+147
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
param (
2+
[string]$a,
3+
[string]$b
4+
)
5+
Write-Output "a: $a, b: $b"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env sh
2+
echo "a: $1, b: $2"

tests/integration/files/file/base/issue-35384.sls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cmd_run_unless_multiple:
22
cmd.run:
33
- name: echo "hello"
4+
- python_shell: True
45
- unless:
56
{% if grains["os"] == "Windows" %}
67
- "exit 0"

tests/integration/states/test_cmd.py

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Tests for the file state
2+
Tests for the cmd state
33
"""
44

55
import errno
@@ -16,6 +16,10 @@
1616
from tests.support.mixins import SaltReturnAssertsMixin
1717
from tests.support.runtests import RUNTIME_VARS
1818

19+
pytestmark = [
20+
pytest.mark.windows_whitelisted,
21+
]
22+
1923

2024
class CMDTest(ModuleCase, SaltReturnAssertsMixin):
2125
"""
@@ -25,12 +29,18 @@ class CMDTest(ModuleCase, SaltReturnAssertsMixin):
2529
@classmethod
2630
def setUpClass(cls):
2731
cls.__cmd = "dir" if salt.utils.platform.is_windows() else "ls"
32+
cls.__python_shell = True if salt.utils.platform.is_windows() else False
2833

2934
def test_run_simple(self):
3035
"""
3136
cmd.run
3237
"""
33-
ret = self.run_state("cmd.run", name=self.__cmd, cwd=tempfile.gettempdir())
38+
ret = self.run_state(
39+
"cmd.run",
40+
name=self.__cmd,
41+
python_shell=self.__python_shell,
42+
cwd=tempfile.gettempdir(),
43+
)
3444
self.assertSaltTrueReturn(ret)
3545

3646
def test_run_output_loglevel(self):
@@ -40,6 +50,7 @@ def test_run_output_loglevel(self):
4050
ret = self.run_state(
4151
"cmd.run",
4252
name=self.__cmd,
53+
python_shell=self.__python_shell,
4354
cwd=tempfile.gettempdir(),
4455
output_loglevel="quiet",
4556
)
@@ -50,15 +61,24 @@ def test_run_simple_test_true(self):
5061
cmd.run test interface
5162
"""
5263
ret = self.run_state(
53-
"cmd.run", name=self.__cmd, cwd=tempfile.gettempdir(), test=True
64+
"cmd.run",
65+
name=self.__cmd,
66+
python_shell=self.__python_shell,
67+
cwd=tempfile.gettempdir(),
68+
test=True,
5469
)
5570
self.assertSaltNoneReturn(ret)
5671

5772
def test_run_hide_output(self):
5873
"""
5974
cmd.run with output hidden
6075
"""
61-
ret = self.run_state("cmd.run", name=self.__cmd, hide_output=True)
76+
ret = self.run_state(
77+
"cmd.run",
78+
name=self.__cmd,
79+
python_shell=self.__python_shell,
80+
hide_output=True,
81+
)
6282
self.assertSaltTrueReturn(ret)
6383
ret = ret[next(iter(ret))]
6484
self.assertEqual(ret["changes"]["stdout"], "")
@@ -163,6 +183,7 @@ def test_run_creates_exists(self):
163183
echo >> {0}:
164184
cmd.run:
165185
- creates: {0}
186+
- python_shell: True
166187
""".format(
167188
self.test_file
168189
)
@@ -189,6 +210,7 @@ def test_run_creates_new(self):
189210
echo >> {0}:
190211
cmd.run:
191212
- creates: {0}
213+
- python_shell: True
192214
""".format(
193215
self.test_file
194216
)
@@ -214,7 +236,8 @@ def test_run_redirect(self):
214236
textwrap.dedent(
215237
"""
216238
echo test > {}:
217-
cmd.run
239+
cmd.run:
240+
- python_shell: True
218241
""".format(
219242
self.test_file
220243
)
@@ -256,12 +279,14 @@ def test_run_watch(self):
256279
saltines:
257280
cmd.run:
258281
- name: echo changed=true
282+
- python_shell: True
259283
- cwd: /
260284
- stateful: True
261285
262286
biscuits:
263287
cmd.wait:
264288
- name: echo biscuits
289+
- python_shell: True
265290
- cwd: /
266291
- watch:
267292
- cmd: saltines
@@ -273,3 +298,112 @@ def test_run_watch(self):
273298
ret = self.run_function("state.sls", [self.state_name])
274299
self.assertTrue(ret[saltines_key]["result"])
275300
self.assertTrue(ret[biscuits_key]["result"])
301+
302+
303+
@pytest.mark.slow_test
304+
class CMDRun(ModuleCase, SaltReturnAssertsMixin):
305+
"""
306+
Validate the run function of the cmd state
307+
"""
308+
309+
def test_run_shell(self):
310+
expected = "foo bar"
311+
ret = self.run_state(
312+
"cmd.run",
313+
name="echo foo bar",
314+
python_shell=True,
315+
)
316+
self.assertSaltTrueReturn(ret)
317+
self.assertSaltStateChangesEqual(ret, expected, "stdout")
318+
319+
def test_run_no_shell(self):
320+
ret = self.run_state(
321+
"cmd.run",
322+
name="whoami",
323+
python_shell=False,
324+
)
325+
self.assertSaltTrueReturn(ret)
326+
327+
def test_run_no_shell_fail(self):
328+
expected = "Unable to run command"
329+
ret = self.run_state(
330+
"cmd.run",
331+
name="echo foo bar",
332+
)
333+
self.assertSaltFalseReturn(ret)
334+
self.assertInSaltComment(expected, ret)
335+
336+
@pytest.mark.skip_on_windows
337+
def test_run_args_string(self):
338+
expected = "a: foo bar, b: baz qux"
339+
ret = self.run_state(
340+
"cmd.run",
341+
name="printf",
342+
args='"a: %s, b: %s\n" "foo bar" "baz qux"',
343+
python_shell=False,
344+
)
345+
self.assertSaltTrueReturn(ret)
346+
self.assertSaltStateChangesEqual(ret, expected, "stdout")
347+
348+
@pytest.mark.skip_on_windows
349+
def test_run_args_list(self):
350+
expected = "a: foo bar, b: baz qux"
351+
ret = self.run_state(
352+
"cmd.run",
353+
name="printf",
354+
args=["a: %s, b: %s\n", "foo bar", "baz qux"],
355+
python_shell=False,
356+
)
357+
self.assertSaltTrueReturn(ret)
358+
self.assertSaltStateChangesEqual(ret, expected, "stdout")
359+
360+
361+
@pytest.mark.slow_test
362+
class CMDScript(ModuleCase, SaltReturnAssertsMixin):
363+
"""
364+
Validate the script function of the cmd state
365+
"""
366+
367+
@pytest.mark.skip_on_windows
368+
def test_script_args_string(self):
369+
expected = "a: foo bar, b: baz qux"
370+
ret = self.run_state(
371+
"cmd.script",
372+
name="salt://echo.sh",
373+
args='"foo bar" "baz qux"',
374+
)
375+
self.assertSaltTrueReturn(ret)
376+
self.assertSaltStateChangesEqual(ret, expected, "stdout")
377+
378+
@pytest.mark.skip_unless_on_windows
379+
def test_script_args_string_win(self):
380+
expected = "a: foo bar, b: baz qux"
381+
ret = self.run_state(
382+
"cmd.script",
383+
name="salt://echo.ps1",
384+
args='"foo bar" "baz qux"',
385+
)
386+
self.assertSaltTrueReturn(ret)
387+
self.assertSaltStateChangesEqual(ret, expected, "stdout")
388+
389+
@pytest.mark.skip_on_windows
390+
def test_script_args_list(self):
391+
expected = "a: foo bar, b: baz qux"
392+
ret = self.run_state(
393+
"cmd.script",
394+
name="salt://echo.sh",
395+
args=["foo bar", "baz qux"],
396+
)
397+
self.assertSaltTrueReturn(ret)
398+
self.assertSaltStateChangesEqual(ret, expected, "stdout")
399+
400+
@pytest.mark.skip_unless_on_windows
401+
def test_script_args_list_win(self):
402+
expected = "a: foo bar, b: baz qux"
403+
ret = self.run_state(
404+
"cmd.script",
405+
name="salt://echo.ps1",
406+
args=["foo bar", "baz qux"],
407+
)
408+
self.assertSaltTrueReturn(ret)
409+
self.assertSaltStateChangesEqual(ret, expected, "stdout")

0 commit comments

Comments
 (0)