test_win_pdh.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import pytest
  5. # Import Salt Libs
  6. import salt.utils.platform
  7. import salt.utils.win_pdh as win_pdh
  8. # Import Salt Testing Libs
  9. from tests.support.unit import TestCase, skipIf
  10. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  11. class WinPdhTestCase(TestCase):
  12. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  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. ]
  51. self.assertEqual(resulting_paths, expected_paths)
  52. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  53. def test_get_all_counters(self):
  54. results = win_pdh.get_all_counters("Processor")
  55. known_counters = [
  56. "\\Processor(*)\\% Processor Time",
  57. "\\Processor(*)\\% Idle Time",
  58. "\\Processor(*)\\DPC Rate",
  59. "\\Processor(*)\\% Privileged Time",
  60. "\\Processor(*)\\DPCs Queued/sec",
  61. "\\Processor(*)\\% Interrupt Time",
  62. "\\Processor(*)\\Interrupts/sec",
  63. ]
  64. for item in known_counters:
  65. self.assertTrue(item in results)
  66. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  67. def test_get_counters(self):
  68. counter_list = [
  69. ("Memory", None, "Available Bytes"),
  70. ("Paging File", "*", "% Usage"),
  71. ("Processor", "*", "% Processor Time"),
  72. ("Server", None, "Work Item Shortages"),
  73. ("Server Work Queues", "*", "Queue Length"),
  74. ("System", None, "Context Switches/sec"),
  75. ]
  76. results = win_pdh.get_counters(counter_list)
  77. expected_counters = [
  78. "\\Memory\\Available Bytes",
  79. "\\Paging File(*)\\% Usage",
  80. "\\Processor(*)\\% Processor Time",
  81. "\\Server\\Work Item Shortages",
  82. "\\Server Work Queues(*)\\Queue Length",
  83. "\\System\\Context Switches/sec",
  84. ]
  85. for item in expected_counters:
  86. self.assertTrue(item in results)
  87. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  88. def test_get_counter(self):
  89. results = win_pdh.get_counter("Processor", "*", "% Processor Time")
  90. self.assertTrue("\\Processor(*)\\% Processor Time" in results)