test_win_task.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import salt.modules.win_task as task
  4. import salt.utils.platform
  5. from salt.exceptions import CommandExecutionError
  6. from tests.support.case import ModuleCase
  7. from tests.support.helpers import destructiveTest
  8. from tests.support.unit import skipIf
  9. @skipIf(not salt.utils.platform.is_windows(), "windows test only")
  10. class WinTasksTest(ModuleCase):
  11. """
  12. Tests for salt.modules.win_task.
  13. """
  14. @destructiveTest
  15. def test_adding_task_with_xml(self):
  16. """
  17. Test adding a task using xml
  18. """
  19. xml_text = r"""
  20. <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  21. <RegistrationInfo>
  22. <Date>2015-06-12T15:59:35.691983</Date>
  23. <Author>System</Author>
  24. </RegistrationInfo>
  25. <Triggers>
  26. <LogonTrigger>
  27. <Enabled>true</Enabled>
  28. <Delay>PT30S</Delay>
  29. </LogonTrigger>
  30. </Triggers>
  31. <Principals>
  32. <Principal id="Author">
  33. <UserId>System</UserId>
  34. <LogonType>InteractiveToken</LogonType>
  35. <RunLevel>HighestAvailable</RunLevel>
  36. </Principal>
  37. </Principals>
  38. <Settings>
  39. <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
  40. <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
  41. <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
  42. <AllowHardTerminate>true</AllowHardTerminate>
  43. <StartWhenAvailable>false</StartWhenAvailable>
  44. <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
  45. <IdleSettings>
  46. <StopOnIdleEnd>true</StopOnIdleEnd>
  47. <RestartOnIdle>false</RestartOnIdle>
  48. </IdleSettings>
  49. <AllowStartOnDemand>true</AllowStartOnDemand>
  50. <Enabled>true</Enabled>
  51. <Hidden>false</Hidden>
  52. <RunOnlyIfIdle>false</RunOnlyIfIdle>
  53. <WakeToRun>false</WakeToRun>
  54. <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
  55. <Priority>4</Priority>
  56. </Settings>
  57. <Actions Context="Author">
  58. <Exec>
  59. <Command>echo</Command>
  60. <Arguments>"hello"</Arguments>
  61. </Exec>
  62. </Actions>
  63. </Task>
  64. """
  65. self.assertEquals(
  66. self.run_function("task.create_task_from_xml", "foo", xml_text=xml_text),
  67. True,
  68. )
  69. all_tasks = self.run_function("task.list_tasks")
  70. self.assertIn("foo", all_tasks)
  71. @destructiveTest
  72. def test_adding_task_with_invalid_xml(self):
  73. """
  74. Test adding a task using a malformed xml
  75. """
  76. xml_text = r"""<Malformed"""
  77. with self.assertRaises(CommandExecutionError):
  78. task.create_task_from_xml("foo", xml_text=xml_text)