From f63f3d8c6c743914ae4a71eada6a7541f92cd78b Mon Sep 17 00:00:00 2001 From: Max Arnold Date: Tue, 23 Jul 2019 16:32:34 +0700 Subject: [PATCH 1/3] Add optional warnings to test.configurable_test_state --- salt/states/test.py | 20 ++++++++++++- tests/unit/states/test_test.py | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/salt/states/test.py b/salt/states/test.py index aea09a7a9201..36a33537da3e 100644 --- a/salt/states/test.py +++ b/salt/states/test.py @@ -36,6 +36,7 @@ - changes: True - result: False - comment: bar.baz + - warnings: A warning is-pillar-foo-present-and-bar-is-int: test.check_pillar: @@ -174,7 +175,7 @@ def fail_with_changes(name, **kwargs): # pylint: disable=unused-argument return ret -def configurable_test_state(name, changes=True, result=True, comment=''): +def configurable_test_state(name, changes=True, result=True, comment='', warnings=None): ''' A configurable test state which determines its output based on the inputs. @@ -195,6 +196,12 @@ def configurable_test_state(name, changes=True, result=True, comment=''): comment: String to fill the comment field with. Default is '' + + .. versionadded:: 2018.3.5 + + warnings: + A string (or a list of strings) to fill the warnings field with. + Default is False ''' ret = { 'name': name, @@ -240,6 +247,17 @@ def configurable_test_state(name, changes=True, result=True, comment=''): 'be either \'True\', \'False\', or ' '\'Random\'') + if warnings is None: + pass + elif isinstance(warnings, six.string_types): + ret['warnings'] = [warnings] + elif isinstance(warnings, list): + ret['warnings'] = warnings + else: + raise SaltInvocationError('You have specified the state option ' + '\'Warnings\' with invalid arguments. It must ' + 'be a string or a list of strings') + if __opts__['test']: ret['result'] = True if changes is False else None ret['comment'] = 'This is a test' if not comment else comment diff --git a/tests/unit/states/test_test.py b/tests/unit/states/test_test.py index 411b3936034d..e655379fd6e9 100644 --- a/tests/unit/states/test_test.py +++ b/tests/unit/states/test_test.py @@ -224,6 +224,57 @@ def test_configurable_test_state_result(self): mock_name, result='Cheese') + def test_configurable_test_state_warnings(self): + ''' + Test test.configurable_test_state with and without warnings + ''' + # Configure mock parameters + mock_name = 'cheese_shop' + mock_comment = "I'm afraid we're fresh out of Red Leicester sir." + mock_warning = 'Today the van broke down.' + mock_warning_list = [ + mock_warning, + "Oooooooooohhh........!" + ] + mock_changes = { + 'testing': { + 'old': 'Unchanged', + 'new': 'Something pretended to change' + } + } + + # Test default state without warnings + with patch.dict(test.__opts__, {'test': False}): + mock_ret = {'name': mock_name, + 'changes': mock_changes, + 'result': True, + 'comment': ''} + ret = test.configurable_test_state(mock_name) + self.assertDictEqual(ret, mock_ret) + + # Test default state with warnings (string) + with patch.dict(test.__opts__, {'test': False}): + mock_ret = {'name': mock_name, + 'changes': mock_changes, + 'result': True, + 'comment': '', + 'warnings': mock_warning_list} + ret = test.configurable_test_state(mock_name, + warnings=mock_warning_list) + self.assertDictEqual(ret, mock_ret) + + # Test default state with warnings (list) + with patch.dict(test.__opts__, {'test': False}): + mock_ret = {'name': mock_name, + 'changes': mock_changes, + 'result': True, + 'comment': '', + 'warnings': ['Today the van broke down.']} + ret = test.configurable_test_state(mock_name, + warnings=mock_warning) + + self.assertDictEqual(ret, mock_ret) + def test_configurable_test_state_test(self): ''' Test test.configurable_test_state with test=True with and without From d8dc82e0bb872f1b66ceed153647b31b45433be4 Mon Sep 17 00:00:00 2001 From: Max Arnold Date: Tue, 23 Jul 2019 18:17:12 +0700 Subject: [PATCH 2/3] Fix default argument docstring --- salt/states/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/states/test.py b/salt/states/test.py index 36a33537da3e..a827acc77193 100644 --- a/salt/states/test.py +++ b/salt/states/test.py @@ -201,7 +201,7 @@ def configurable_test_state(name, changes=True, result=True, comment='', warning warnings: A string (or a list of strings) to fill the warnings field with. - Default is False + Default is None ''' ret = { 'name': name, From ec4fb4f615d9ee3979747624f6e23d77aeccff64 Mon Sep 17 00:00:00 2001 From: Max Arnold Date: Thu, 19 Dec 2019 23:32:39 +0700 Subject: [PATCH 3/3] Change versionadded to Neon for test state warnings arg --- salt/states/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/states/test.py b/salt/states/test.py index a827acc77193..4354a6af817b 100644 --- a/salt/states/test.py +++ b/salt/states/test.py @@ -197,7 +197,7 @@ def configurable_test_state(name, changes=True, result=True, comment='', warning String to fill the comment field with. Default is '' - .. versionadded:: 2018.3.5 + .. versionadded:: Neon warnings: A string (or a list of strings) to fill the warnings field with.