-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Improved error logging for win_task.create_task_from_xml #49981
Conversation
win_task.create_task_from_xml now returns the error message when it fails to add the scheduled task to make it easier to diagnose the problem.
…_from_xml can now return a string when there was an error Fixed a bug where win_task.create_task_from_xml did not worked when the username was set to None
salt/modules/win_task.py
Outdated
failure_code = 'Unknown Failure: {0}'.format(error_code) | ||
finally: | ||
log.debug('Failed to create task: %s', failure_code) | ||
return failure_code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably raise a CommandExecutionError here instead. If the Return type is Bool, it should always be Bool. It shouldn't return 2 different types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, nice. I didn't know it existed.
salt/modules/win_task.py
Outdated
:return: True if successful, False if unsuccessful | ||
:rtype: bool | ||
:return: True if successful, False if unsuccessful, A string with the error message if there is an error | ||
:rtype: bool or str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a :raises: CommandExecutionError
for completenes
… as suggested in the core review.
salt/modules/win_task.py
Outdated
@@ -17,6 +17,7 @@ | |||
|
|||
# Import Salt libs | |||
import salt.utils.platform | |||
import salt.exceptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use this instead
from salt.exceptions import CommandExecutionError, ArgumentValueError
salt/modules/win_task.py
Outdated
@@ -647,10 +649,10 @@ def create_task_from_xml(name, | |||
try: | |||
failure_code = fc[error_code] | |||
except KeyError: | |||
failure_code = 'Unknown Failure: {0}'.format(error_code) | |||
failure_code = 'CommandExecutionErrUnknown Failure: {0}'.format(error_code) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can still be Unknown Failure
.
The stacktrace will show CommandExecutionError
''' | ||
xml_text = r"""<Malformed""" | ||
self.assertEquals( | ||
self.assertEquals( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is going to fail now. You'll probably need to do the following for this test since we're now raising an exception:
self.assertRaises(ArgumentValueError, task.create_task_from_xml, 'foo', xml_text=xml_text)
salt/modules/win_task.py
Outdated
@@ -581,7 +583,7 @@ def create_task_from_xml(name, | |||
return '{0} already exists'.format(name) | |||
|
|||
if not xml_text and not xml_path: | |||
return 'Must specify either xml_text or xml_path' | |||
raise salt.exceptions.ArgumentValueError('Must specify either xml_text or xml_path') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
raise ArgumentValueError('Must specify either xml_text or xml_path')
Test adding a task using xml | ||
''' | ||
xml_text = r"""<Malformed""" | ||
with self.assertRaises(salt.exceptions.CommandExecutionError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with self.assertRaises(CommandExecutionError):
Fixed the integration test coding style error Directly use the task module instead of using run_function. We didn't need the extra feature it provided
@Lesvek Thanks for getting all those things fixed and thanks for the contribution! Looks good now. |
Improved error logging for win_task.create_task_from_xml
What does this PR do?
Instead of only returning True or False when create_task_from_xml fail to add the scheduled task, the actual error message is returned to the user. If the error is unknown, the hex error code is returned. That way, it's easier to search for as this is the format Microsoft is using in their documentation.
Previous Behavior
Previously, when there was an error, the module only returned False, only logging the error message in the debug logs.
New Behavior
A proper exception containing the error message is now returned to the user when there is an error. New code will have to catch the exception.
Tests written?
I added 2 new tests.
Commits signed with GPG?
The commit was not signe with GPG.