PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/checks/mock/test_windows_service.py

https://gitlab.com/meetly/dd-agent
Python | 125 lines | 91 code | 17 blank | 17 comment | 2 complexity | 011c1ac736ddf0b4744d4db92323666b MD5 | raw file
  1. # stdlib
  2. from mock import Mock
  3. # project
  4. from checks import AgentCheck
  5. from tests.checks.common import AgentCheckTest
  6. WinHttpAutoProxySvc_attr = { # Running Windows Service
  7. 'AcceptPause': False,
  8. 'AcceptStop': True,
  9. 'Caption': "WinHTTP Web Proxy Auto-Discovery Service",
  10. 'CheckPoint': 0,
  11. 'CreationClassName': "Win32_Service",
  12. 'Description': "WinHTTP implements the client HTTP stack and provides developers"
  13. " with a Win32 API and COM Automation component for sending HTTP requests"
  14. " and receiving responses. In addition, WinHTTP provides support "
  15. " for auto-discovering a proxy configuration via its implementation"
  16. " of the Web Proxy Auto-Discovery (WPAD) protocol.",
  17. 'DesktopInteract': False,
  18. 'DisplayName': "WinHTTP Web Proxy Auto-Discovery Service",
  19. 'ErrorControl': "Normal",
  20. 'ExitCode': 0,
  21. 'Name': "WinHttpAutoProxySvc",
  22. 'PathName': "C:\\Windows\\system32\\svchost.exe -k LocalService",
  23. 'ProcessId': 864,
  24. 'ServiceSpecificExitCode': 0,
  25. 'ServiceType': "Share Process",
  26. 'Started': True,
  27. 'StartMode': "Manual",
  28. 'StartName': "NT AUTHORITY\\LocalService",
  29. 'State': "Running",
  30. 'Status': "OK",
  31. 'SystemCreationClassName': "Win32_ComputerSystem",
  32. 'SystemName': "WIN-7022K3K6GF8",
  33. 'TagId': 0,
  34. 'WaitHint': 0,
  35. }
  36. WSService_attr = { # Stopped Windows Service
  37. 'AcceptPause': False,
  38. 'AcceptStop': False,
  39. 'Caption': "Windows Store Service (WSService)",
  40. 'CheckPoint': 0,
  41. 'CreationClassName': "Win32_Service",
  42. 'Description': "Provides infrastructure support for Windows Store."
  43. "This service is started on demand and if ded applications"
  44. " bought using Windows Store will not behave correctly.",
  45. 'DesktopInteract': False,
  46. 'DisplayName': "Windows Store Service (WSService)",
  47. 'ErrorControl': "Normal",
  48. 'ExitCode': 1077,
  49. 'Name': "WSService",
  50. 'PathName': "C:\\Windows\\System32\\svchost.exe -k wsappx",
  51. 'ProcessId': 0,
  52. 'ServiceSpecificExitCode': 0,
  53. 'ServiceType': "Share Process",
  54. 'Started': False,
  55. 'StartMode': "Manual",
  56. 'StartName': "LocalSystem",
  57. 'State': "Stopped",
  58. 'Status': "OK",
  59. 'SystemCreationClassName': "Win32_ComputerSystem",
  60. 'SystemName': "WIN-7022K3K6GF8",
  61. 'TagId': 0,
  62. 'WaitHint': 0,
  63. }
  64. class Mocked_Win32_Service(object):
  65. """
  66. Generate Mocked Win32 Service from given attributes
  67. """
  68. def __init__(self, **entries):
  69. self.__dict__.update(entries)
  70. class Mocked_WMI(Mock):
  71. """
  72. Mock WMI methods for test purpose
  73. """
  74. def Win32_Service(self, name):
  75. """
  76. Returns mock match Win32 Service
  77. """
  78. if name == "WinHttpAutoProxySvc":
  79. return [Mocked_Win32_Service(**WinHttpAutoProxySvc_attr)]
  80. if name == "WSService":
  81. return [Mocked_Win32_Service(**WSService_attr)]
  82. return []
  83. class WindowsServiceTestCase(AgentCheckTest):
  84. CHECK_NAME = 'windows_service'
  85. WIN_SERVICES_CONFIG = {
  86. 'host': ".",
  87. 'services': ["WinHttpAutoProxySvc", "WSService"]
  88. }
  89. def test_check(self):
  90. """
  91. Returns the right service checks
  92. """
  93. # Mocking `wmi` Python package
  94. import sys
  95. sys.modules['wmi'] = Mocked_WMI()
  96. # Run check
  97. config = {
  98. 'instances': [self.WIN_SERVICES_CONFIG]
  99. }
  100. self.run_check(config)
  101. # Test service checks
  102. self.assertServiceCheck('windows_service.state', status=AgentCheck.OK, count=1,
  103. tags=[u'service:WinHttpAutoProxySvc',
  104. u'host:' + self.check.hostname])
  105. self.assertServiceCheck('windows_service.state', status=AgentCheck.CRITICAL, count=1,
  106. tags=[u'service:WSService',
  107. u'host:' + self.check.hostname])
  108. self.coverage_report()