Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional warnings to test.configurable_test_state #53959

Merged
merged 4 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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