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

Improved error logging for win_task.create_task_from_xml #49981

Merged
merged 7 commits into from
Oct 12, 2018
Merged

Improved error logging for win_task.create_task_from_xml #49981

merged 7 commits into from
Oct 12, 2018

Conversation

Lesvek
Copy link

@Lesvek Lesvek commented Oct 10, 2018

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.

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
@ghost ghost self-requested a review October 10, 2018 18:44
failure_code = 'Unknown Failure: {0}'.format(error_code)
finally:
log.debug('Failed to create task: %s', failure_code)
return failure_code
Copy link
Contributor

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.

Copy link
Author

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.

: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
Copy link
Contributor

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

@Lesvek Lesvek changed the title Improved error logging for win_task.create_task_from_xml WIP Improved error logging for win_task.create_task_from_xml Oct 10, 2018
@@ -17,6 +17,7 @@

# Import Salt libs
import salt.utils.platform
import salt.exceptions
Copy link
Contributor

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

@@ -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)
Copy link
Contributor

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

salt/modules/win_task.py Show resolved Hide resolved
tests/integration/modules/test_win_task.py Show resolved Hide resolved
'''
xml_text = r"""<Malformed"""
self.assertEquals(
self.assertEquals(
Copy link
Contributor

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)

@@ -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')
Copy link
Contributor

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):
Copy link
Contributor

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 Lesvek changed the title WIP Improved error logging for win_task.create_task_from_xml Improved error logging for win_task.create_task_from_xml Oct 11, 2018
@twangboy
Copy link
Contributor

@Lesvek Thanks for getting all those things fixed and thanks for the contribution! Looks good now.

@rallytime rallytime merged commit 31f2c43 into saltstack:develop Oct 12, 2018
@twangboy twangboy mentioned this pull request Apr 20, 2020
3 tasks
twangboy pushed a commit to twangboy/salt that referenced this pull request Apr 20, 2020
Improved error logging for win_task.create_task_from_xml
@twangboy twangboy added the has master-port port to master has been created label Apr 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-jam has master-port port to master has been created
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants