From 283ea26b9c3fa3f5d7044018126dcd2258301fa4 Mon Sep 17 00:00:00 2001 From: Nicole Thomas Date: Fri, 12 Oct 2018 14:43:03 -0400 Subject: [PATCH 1/6] Merge pull request #49981 from Lesvek/develop Improved error logging for win_task.create_task_from_xml --- salt/modules/win_task.py | 52 +++++++++++--- tests/integration/modules/test_win_task.py | 80 ++++++++++++++++++++++ 2 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 tests/integration/modules/test_win_task.py diff --git a/salt/modules/win_task.py b/salt/modules/win_task.py index 8507c7af0aa9..bdf0978e3448 100644 --- a/salt/modules/win_task.py +++ b/salt/modules/win_task.py @@ -20,6 +20,7 @@ import salt.utils.platform import salt.utils.winapi from salt.ext.six.moves import range +from salt.exceptions import ArgumentValueError, CommandExecutionError # Import 3rd Party Libraries try: @@ -624,6 +625,11 @@ def create_task_from_xml( Returns: bool: ``True`` if successful, otherwise ``False`` + str: A string with the error message if there is an error + + Raises: + ArgumentValueError: If arguments are invalid + CommandExecutionError CLI Example: @@ -637,7 +643,7 @@ def create_task_from_xml( return "{0} already exists".format(name) if not xml_text and not xml_path: - return "Must specify either xml_text or xml_path" + raise ArgumentValueError("Must specify either xml_text or xml_path") # Create the task service object with salt.utils.winapi.Com(): @@ -665,6 +671,7 @@ def create_task_from_xml( logon_type = TASK_LOGON_INTERACTIVE_TOKEN else: password = None + logon_type = TASK_LOGON_NONE # Save the task try: @@ -674,17 +681,46 @@ def create_task_from_xml( except pythoncom.com_error as error: hr, msg, exc, arg = error.args # pylint: disable=W0633 + error_code = hex(exc[5] + 2**32) fc = { - -2147216615: "Required element or attribute missing", - -2147216616: "Value incorrectly formatted or out of range", - -2147352571: "Access denied", + 0x80041319: "Required element or attribute missing", + 0x80041318: "Value incorrectly formatted or out of range", + 0x80020005: "Access denied", + 0x80041309: "A task's trigger is not found", + 0x8004130a: "One or more of the properties required to run this " + "task have not been set", + 0x8004130c: "The Task Scheduler service is not installed on this " + "computer", + 0x8004130d: "The task object could not be opened", + 0x8004130e: "The object is either an invalid task object or is not " + "a task object", + 0x8004130f: "No account information could be found in the Task " + "Scheduler security database for the task indicated", + 0x80041310: "Unable to establish existence of the account " + "specified", + 0x80041311: "Corruption was detected in the Task Scheduler " + "security database; the database has been reset", + 0x80041313: "The task object version is either unsupported or " + "invalid", + 0x80041314: "The task has been configured with an unsupported " + "combination of account settings and run time options", + 0x80041315: "The Task Scheduler Service is not running", + 0x80041316: "The task XML contains an unexpected node", + 0x80041317: "The task XML contains an element or attribute from an " + "unexpected namespace", + 0x8004131a: "The task XML is malformed", + 0x0004131c: "The task is registered, but may fail to start. Batch " + "logon privilege needs to be enabled for the task " + "principal", + 0x8004131d: "The task XML contains too many nodes of the same type", } try: - failure_code = fc[exc[5]] + failure_code = fc[error_code] except KeyError: - failure_code = "Unknown Failure: {0}".format(error) - - log.debug("Failed to create task: %s", failure_code) + failure_code = "Unknown Failure: {0}".format(error_code) + finally: + log.debug("Failed to create task: %s", failure_code) + raise CommandExecutionError(failure_code) # Verify creation return name in list_tasks(location) diff --git a/tests/integration/modules/test_win_task.py b/tests/integration/modules/test_win_task.py new file mode 100644 index 000000000000..f9612796f7c2 --- /dev/null +++ b/tests/integration/modules/test_win_task.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, unicode_literals + +from tests.support.case import ModuleCase +from tests.support.unit import skipIf +from tests.support.helpers import destructiveTest + +import salt.modules.win_task as task +from salt.exceptions import CommandExecutionError +import salt.utils.platform + + +@skipIf(not salt.utils.platform.is_windows(), 'windows test only') +class WinTasksTest(ModuleCase): + """ + Tests for salt.modules.win_task. + """ + @destructiveTest + def test_adding_task_with_xml(self): + ''' + Test adding a task using xml + ''' + xml_text = r""" + + + 2015-06-12T15:59:35.691983 + System + + + + true + PT30S + + + + + System + InteractiveToken + HighestAvailable + + + + IgnoreNew + true + true + true + false + false + + true + false + + true + true + false + false + false + P3D + 4 + + + + echo + "hello" + + + + """ + self.assertEquals(self.run_function('task.create_task_from_xml', 'foo', xml_text=xml_text), True) + all_tasks = self.run_function('task.list_tasks') + self.assertIn('foo', all_tasks) + + @destructiveTest + def test_adding_task_with_invalid_xml(self): + ''' + Test adding a task using a malformed xml + ''' + xml_text = r""" Date: Mon, 20 Apr 2020 15:20:07 -0600 Subject: [PATCH 2/6] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0ad10ee89f8..56919a9a1cac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Versions are `MAJOR.PATCH`. ### Deprecated ### Changed +- [#56751](https://github.com/saltstack/salt/pull/56751) - Backport 49981 ### Fixed - [#56237](https://github.com/saltstack/salt/pull/56237) - Fix alphabetical ordering and remove duplicates across all documentation indexes - [@myii](https://github.com/myii) From 7b2ae025c24e7acb9a5f8d17430f79c9a7c58393 Mon Sep 17 00:00:00 2001 From: twangboy Date: Mon, 20 Apr 2020 15:34:39 -0600 Subject: [PATCH 3/6] Fix black --- salt/modules/win_task.py | 26 ++++++++++---------- tests/integration/modules/test_win_task.py | 28 ++++++++++++---------- tests/unit/modules/test_win_task.py | 2 +- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/salt/modules/win_task.py b/salt/modules/win_task.py index bdf0978e3448..72f7183fde0e 100644 --- a/salt/modules/win_task.py +++ b/salt/modules/win_task.py @@ -19,8 +19,8 @@ # Import Salt libs import salt.utils.platform import salt.utils.winapi -from salt.ext.six.moves import range from salt.exceptions import ArgumentValueError, CommandExecutionError +from salt.ext.six.moves import range # Import 3rd Party Libraries try: @@ -681,37 +681,37 @@ def create_task_from_xml( except pythoncom.com_error as error: hr, msg, exc, arg = error.args # pylint: disable=W0633 - error_code = hex(exc[5] + 2**32) + error_code = hex(exc[5] + 2 ** 32) fc = { 0x80041319: "Required element or attribute missing", 0x80041318: "Value incorrectly formatted or out of range", 0x80020005: "Access denied", 0x80041309: "A task's trigger is not found", 0x8004130a: "One or more of the properties required to run this " - "task have not been set", + "task have not been set", 0x8004130c: "The Task Scheduler service is not installed on this " - "computer", + "computer", 0x8004130d: "The task object could not be opened", 0x8004130e: "The object is either an invalid task object or is not " - "a task object", + "a task object", 0x8004130f: "No account information could be found in the Task " - "Scheduler security database for the task indicated", + "Scheduler security database for the task indicated", 0x80041310: "Unable to establish existence of the account " - "specified", + "specified", 0x80041311: "Corruption was detected in the Task Scheduler " - "security database; the database has been reset", + "security database; the database has been reset", 0x80041313: "The task object version is either unsupported or " - "invalid", + "invalid", 0x80041314: "The task has been configured with an unsupported " - "combination of account settings and run time options", + "combination of account settings and run time options", 0x80041315: "The Task Scheduler Service is not running", 0x80041316: "The task XML contains an unexpected node", 0x80041317: "The task XML contains an element or attribute from an " - "unexpected namespace", + "unexpected namespace", 0x8004131a: "The task XML is malformed", 0x0004131c: "The task is registered, but may fail to start. Batch " - "logon privilege needs to be enabled for the task " - "principal", + "logon privilege needs to be enabled for the task " + "principal", 0x8004131d: "The task XML contains too many nodes of the same type", } try: diff --git a/tests/integration/modules/test_win_task.py b/tests/integration/modules/test_win_task.py index f9612796f7c2..fca8518792b1 100644 --- a/tests/integration/modules/test_win_task.py +++ b/tests/integration/modules/test_win_task.py @@ -1,25 +1,24 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, unicode_literals -from tests.support.case import ModuleCase -from tests.support.unit import skipIf -from tests.support.helpers import destructiveTest - import salt.modules.win_task as task -from salt.exceptions import CommandExecutionError import salt.utils.platform +from salt.exceptions import CommandExecutionError +from tests.support.case import ModuleCase +from tests.support.helpers import destructiveTest +from tests.support.unit import skipIf -@skipIf(not salt.utils.platform.is_windows(), 'windows test only') +@skipIf(not salt.utils.platform.is_windows(), "windows test only") class WinTasksTest(ModuleCase): """ Tests for salt.modules.win_task. """ @destructiveTest def test_adding_task_with_xml(self): - ''' + """ Test adding a task using xml - ''' + """ xml_text = r""" @@ -66,15 +65,18 @@ def test_adding_task_with_xml(self): """ - self.assertEquals(self.run_function('task.create_task_from_xml', 'foo', xml_text=xml_text), True) - all_tasks = self.run_function('task.list_tasks') - self.assertIn('foo', all_tasks) + self.assertEquals( + self.run_function('task.create_task_from_xml', 'foo', xml_text=xml_text), + True, + ) + all_tasks = self.run_function("task.list_tasks") + self.assertIn("foo", all_tasks) @destructiveTest def test_adding_task_with_invalid_xml(self): - ''' + """ Test adding a task using a malformed xml - ''' + """ xml_text = r""" Date: Mon, 20 Apr 2020 15:39:13 -0600 Subject: [PATCH 4/6] Fix more black --- tests/integration/modules/test_win_task.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/modules/test_win_task.py b/tests/integration/modules/test_win_task.py index fca8518792b1..d48b6a2af3d6 100644 --- a/tests/integration/modules/test_win_task.py +++ b/tests/integration/modules/test_win_task.py @@ -66,7 +66,7 @@ def test_adding_task_with_xml(self): """ self.assertEquals( - self.run_function('task.create_task_from_xml', 'foo', xml_text=xml_text), + self.run_function("task.create_task_from_xml", "foo", xml_text=xml_text), True, ) all_tasks = self.run_function("task.list_tasks") @@ -79,4 +79,4 @@ def test_adding_task_with_invalid_xml(self): """ xml_text = r""" Date: Mon, 20 Apr 2020 15:45:02 -0600 Subject: [PATCH 5/6] Final black --- salt/modules/win_task.py | 16 ++++++++-------- tests/integration/modules/test_win_task.py | 1 + 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/salt/modules/win_task.py b/salt/modules/win_task.py index 72f7183fde0e..13b7557ab24e 100644 --- a/salt/modules/win_task.py +++ b/salt/modules/win_task.py @@ -687,14 +687,14 @@ def create_task_from_xml( 0x80041318: "Value incorrectly formatted or out of range", 0x80020005: "Access denied", 0x80041309: "A task's trigger is not found", - 0x8004130a: "One or more of the properties required to run this " + 0x8004130A: "One or more of the properties required to run this " "task have not been set", - 0x8004130c: "The Task Scheduler service is not installed on this " + 0x8004130C: "The Task Scheduler service is not installed on this " "computer", - 0x8004130d: "The task object could not be opened", - 0x8004130e: "The object is either an invalid task object or is not " + 0x8004130D: "The task object could not be opened", + 0x8004130E: "The object is either an invalid task object or is not " "a task object", - 0x8004130f: "No account information could be found in the Task " + 0x8004130F: "No account information could be found in the Task " "Scheduler security database for the task indicated", 0x80041310: "Unable to establish existence of the account " "specified", @@ -708,11 +708,11 @@ def create_task_from_xml( 0x80041316: "The task XML contains an unexpected node", 0x80041317: "The task XML contains an element or attribute from an " "unexpected namespace", - 0x8004131a: "The task XML is malformed", - 0x0004131c: "The task is registered, but may fail to start. Batch " + 0x8004131A: "The task XML is malformed", + 0x0004131C: "The task is registered, but may fail to start. Batch " "logon privilege needs to be enabled for the task " "principal", - 0x8004131d: "The task XML contains too many nodes of the same type", + 0x8004131D: "The task XML contains too many nodes of the same type", } try: failure_code = fc[error_code] diff --git a/tests/integration/modules/test_win_task.py b/tests/integration/modules/test_win_task.py index d48b6a2af3d6..9245ce9a7952 100644 --- a/tests/integration/modules/test_win_task.py +++ b/tests/integration/modules/test_win_task.py @@ -14,6 +14,7 @@ class WinTasksTest(ModuleCase): """ Tests for salt.modules.win_task. """ + @destructiveTest def test_adding_task_with_xml(self): """ From c4bcc9d353be43309afdcdf397a9b80afc7362a5 Mon Sep 17 00:00:00 2001 From: twangboy Date: Mon, 20 Apr 2020 15:49:32 -0600 Subject: [PATCH 6/6] Very final black --- salt/modules/win_task.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/salt/modules/win_task.py b/salt/modules/win_task.py index 13b7557ab24e..db95837a0696 100644 --- a/salt/modules/win_task.py +++ b/salt/modules/win_task.py @@ -696,12 +696,10 @@ def create_task_from_xml( "a task object", 0x8004130F: "No account information could be found in the Task " "Scheduler security database for the task indicated", - 0x80041310: "Unable to establish existence of the account " - "specified", + 0x80041310: "Unable to establish existence of the account specified", 0x80041311: "Corruption was detected in the Task Scheduler " "security database; the database has been reset", - 0x80041313: "The task object version is either unsupported or " - "invalid", + 0x80041313: "The task object version is either unsupported or invalid", 0x80041314: "The task has been configured with an unsupported " "combination of account settings and run time options", 0x80041315: "The Task Scheduler Service is not running", @@ -710,8 +708,7 @@ def create_task_from_xml( "unexpected namespace", 0x8004131A: "The task XML is malformed", 0x0004131C: "The task is registered, but may fail to start. Batch " - "logon privilege needs to be enabled for the task " - "principal", + "logon privilege needs to be enabled for the task principal", 0x8004131D: "The task XML contains too many nodes of the same type", } try: