Skip to content

Commit

Permalink
Merge pull request #53959 from max-arnold/configurable-test-state-war…
Browse files Browse the repository at this point in the history
…ning

Add optional warnings to test.configurable_test_state
  • Loading branch information
dwoz authored Dec 24, 2019
2 parents 5c49fc4 + 58934cd commit a58459c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
20 changes: 19 additions & 1 deletion salt/states/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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:: Neon
warnings:
A string (or a list of strings) to fill the warnings field with.
Default is None
'''
ret = {
'name': name,
Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/states/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a58459c

Please sign in to comment.