test_win_pdh.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Testing Libs
  5. from tests.support.mock import NO_MOCK, NO_MOCK_REASON
  6. from tests.support.unit import TestCase, skipIf
  7. # Import Salt Libs
  8. import salt.utils.platform
  9. import salt.utils.win_pdh as win_pdh
  10. @skipIf(NO_MOCK, NO_MOCK_REASON)
  11. @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
  12. class WinPdhTestCase(TestCase):
  13. def test_list_objects(self):
  14. known_objects = ['Cache', 'Memory', 'Process', 'Processor', 'System']
  15. objects = win_pdh.list_objects()
  16. for item in known_objects:
  17. self.assertTrue(item in objects)
  18. def test_list_counters(self):
  19. counters = win_pdh.list_counters('Processor')
  20. known_counters = ['% Processor Time', '% User Time', '% DPC Time']
  21. for item in known_counters:
  22. self.assertTrue(item in counters)
  23. def test_list_instances(self):
  24. instances = win_pdh.list_instances('Processor')
  25. known_instances = ['0', '_Total']
  26. for item in known_instances:
  27. self.assertTrue(item in instances)
  28. def test_build_counter_list(self):
  29. counter_list = [
  30. ('Memory', None, 'Available Bytes'),
  31. ('Paging File', '*', '% Usage'),
  32. ('Processor', '*', '% Processor Time'),
  33. ('Server', None, 'Work Item Shortages'),
  34. ('Server Work Queues', '*', 'Queue Length'),
  35. ('System', None, 'Context Switches/sec'),
  36. ]
  37. resulting_list = win_pdh.build_counter_list(counter_list)
  38. for counter in resulting_list:
  39. self.assertTrue(isinstance(counter, win_pdh.Counter))
  40. resulting_paths = []
  41. for counter in resulting_list:
  42. resulting_paths.append(counter.path)
  43. expected_paths = [
  44. '\\Memory\\Available Bytes',
  45. '\\Paging File(*)\\% Usage',
  46. '\\Processor(*)\\% Processor Time',
  47. '\\Server\\Work Item Shortages',
  48. '\\Server Work Queues(*)\\Queue Length',
  49. '\\System\\Context Switches/sec']
  50. self.assertEqual(resulting_paths, expected_paths)
  51. def test_get_all_counters(self):
  52. results = win_pdh.get_all_counters('Processor')
  53. known_counters = [
  54. '\\Processor(*)\\% Processor Time',
  55. '\\Processor(*)\\% Idle Time',
  56. '\\Processor(*)\\DPC Rate',
  57. '\\Processor(*)\\% Privileged Time',
  58. '\\Processor(*)\\DPCs Queued/sec',
  59. '\\Processor(*)\\% Interrupt Time',
  60. '\\Processor(*)\\Interrupts/sec',
  61. ]
  62. for item in known_counters:
  63. self.assertTrue(item in results)
  64. def test_get_counters(self):
  65. counter_list = [
  66. ('Memory', None, 'Available Bytes'),
  67. ('Paging File', '*', '% Usage'),
  68. ('Processor', '*', '% Processor Time'),
  69. ('Server', None, 'Work Item Shortages'),
  70. ('Server Work Queues', '*', 'Queue Length'),
  71. ('System', None, 'Context Switches/sec'),
  72. ]
  73. results = win_pdh.get_counters(counter_list)
  74. expected_counters = [
  75. '\\Memory\\Available Bytes',
  76. '\\Paging File(*)\\% Usage',
  77. '\\Processor(*)\\% Processor Time',
  78. '\\Server\\Work Item Shortages',
  79. '\\Server Work Queues(*)\\Queue Length',
  80. '\\System\\Context Switches/sec'
  81. ]
  82. for item in expected_counters:
  83. self.assertTrue(item in results)
  84. def test_get_counter(self):
  85. results = win_pdh.get_counter('Processor', '*', '% Processor Time')
  86. self.assertTrue('\\Processor(*)\\% Processor Time' in results)