Skip to content

Commit 4d21cb5

Browse files
authored
Merge pull request #56298 from raddessi/master.state-test
Added test function to state module
2 parents 138fc18 + a226720 commit 4d21cb5

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

doc/topics/releases/sodium.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ The old syntax for the mine_function - as a dict, or as a list with dicts that
1717
contain more than exactly one key - is still supported but discouraged in favor
1818
of the more uniform syntax of module.run.
1919

20+
State Execution Module
21+
======================
22+
23+
The :mod:`state.test <salt.modules.state.test>` function
24+
can be used to test a state on a minion. This works by executing the
25+
:mod:`state.apply <salt.modules.state.apply>` function while forcing the ``test`` kwarg
26+
to ``True`` so that the ``state.apply`` function is not required to be called by the
27+
user directly. This also allows you to add the ``state.test`` function to a minion's
28+
``minion_blackout_whitelist`` pillar if you wish to be able to test a state while a
29+
minion is in blackout.
2030

2131
New Grains
2232
==========

salt/modules/state.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"template": "highstate",
6161
"template_str": "highstate",
6262
"apply_": "highstate",
63+
"test": "highstate",
6364
"request": "highstate",
6465
"check_request": "highstate",
6566
"run_request": "highstate",
@@ -799,6 +800,21 @@ def apply_(mods=None, **kwargs):
799800
return highstate(**kwargs)
800801

801802

803+
def test(*args, **kwargs):
804+
"""
805+
.. versionadded:: Sodium
806+
807+
Alias for `state.apply` with the kwarg `test` forced to `True`.
808+
809+
This is a nicety to avoid the need to type out `test=True` and the possibility of
810+
a typo causing changes you do not intend.
811+
"""
812+
kwargs["test"] = True
813+
ret = apply_(*args, **kwargs)
814+
815+
return ret
816+
817+
802818
def request(mods=None, **kwargs):
803819
"""
804820
.. versionadded:: 2015.5.0

tests/integration/modules/test_state.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,6 +2354,37 @@ def test_state_sls_id_test_false_pillar_true(self):
23542354
self.assertEqual(val["comment"], "File {0} updated".format(file_name))
23552355
self.assertEqual(val["changes"]["diff"], "New file")
23562356

2357+
def test_state_test_pillar_false(self):
2358+
"""
2359+
test state.test forces test kwarg to True even when pillar is set to False
2360+
"""
2361+
self._add_runtime_pillar(pillar={"test": False})
2362+
testfile = os.path.join(RUNTIME_VARS.TMP, "testfile")
2363+
comment = "The file {0} is set to be changed\nNote: No changes made, actual changes may\nbe different due to other states.".format(
2364+
testfile
2365+
)
2366+
ret = self.run_function("state.test", ["core"])
2367+
2368+
for key, val in ret.items():
2369+
self.assertEqual(val["comment"], comment)
2370+
self.assertEqual(val["changes"], {"newfile": testfile})
2371+
2372+
def test_state_test_test_false_pillar_false(self):
2373+
"""
2374+
test state.test forces test kwarg to True even when pillar and kwarg are set
2375+
to False
2376+
"""
2377+
self._add_runtime_pillar(pillar={"test": False})
2378+
testfile = os.path.join(RUNTIME_VARS.TMP, "testfile")
2379+
comment = "The file {0} is set to be changed\nNote: No changes made, actual changes may\nbe different due to other states.".format(
2380+
testfile
2381+
)
2382+
ret = self.run_function("state.test", ["core"], test=False)
2383+
2384+
for key, val in ret.items():
2385+
self.assertEqual(val["comment"], comment)
2386+
self.assertEqual(val["changes"], {"newfile": testfile})
2387+
23572388
@skipIf(
23582389
six.PY3 and salt.utils.platform.is_darwin(), "Test is broken on macosx and PY3"
23592390
)

tests/unit/modules/test_state.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,23 @@ def test_apply_(self):
432432
with patch.object(state, "highstate", mock):
433433
self.assertTrue(state.apply_(None))
434434

435+
def test_test(self):
436+
"""
437+
Test to apply states in test mode
438+
"""
439+
with patch.dict(state.__opts__, {"test": False}):
440+
mock = MagicMock(return_value=True)
441+
with patch.object(state, "sls", mock):
442+
self.assertTrue(state.test(True))
443+
mock.assert_called_once_with(True, test=True)
444+
self.assertEqual(state.__opts__["test"], False)
445+
446+
mock = MagicMock(return_value=True)
447+
with patch.object(state, "highstate", mock):
448+
self.assertTrue(state.test(None))
449+
mock.assert_called_once_with(test=True)
450+
self.assertEqual(state.__opts__["test"], False)
451+
435452
def test_list_disabled(self):
436453
"""
437454
Test to list disabled states
@@ -1194,6 +1211,14 @@ def test_lock_saltenv(self):
11941211
with self.assertRaisesRegex(CommandExecutionError, lock_msg):
11951212
state.apply_(saltenv="base")
11961213

1214+
# Test "test" with SLS
1215+
with self.assertRaisesRegex(CommandExecutionError, lock_msg):
1216+
state.test("foo", saltenv="base")
1217+
1218+
# Test "test" with Highstate
1219+
with self.assertRaisesRegex(CommandExecutionError, lock_msg):
1220+
state.test(saltenv="base")
1221+
11971222
# Test highstate
11981223
with self.assertRaisesRegex(CommandExecutionError, lock_msg):
11991224
state.highstate(saltenv="base")

0 commit comments

Comments
 (0)