test_aix.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2009, Giampaolo Rodola'
  3. # Copyright (c) 2017, Arnon Yaari
  4. # All rights reserved.
  5. # Use of this source code is governed by a BSD-style license that can be
  6. # found in the LICENSE file.
  7. """AIX specific tests."""
  8. import re
  9. import psutil
  10. from psutil import AIX
  11. from psutil.tests import PsutilTestCase
  12. from psutil.tests import pytest
  13. from psutil.tests import sh
  14. @pytest.mark.skipif(not AIX, reason="AIX only")
  15. class AIXSpecificTestCase(PsutilTestCase):
  16. def test_virtual_memory(self):
  17. out = sh('/usr/bin/svmon -O unit=KB')
  18. re_pattern = r"memory\s*"
  19. for field in [
  20. "size",
  21. "inuse",
  22. "free",
  23. "pin",
  24. "virtual",
  25. "available",
  26. "mmode",
  27. ]:
  28. re_pattern += rf"(?P<{field}>\S+)\s+"
  29. matchobj = re.search(re_pattern, out)
  30. assert matchobj is not None
  31. KB = 1024
  32. total = int(matchobj.group("size")) * KB
  33. available = int(matchobj.group("available")) * KB
  34. used = int(matchobj.group("inuse")) * KB
  35. free = int(matchobj.group("free")) * KB
  36. psutil_result = psutil.virtual_memory()
  37. # TOLERANCE_SYS_MEM from psutil.tests is not enough. For some reason
  38. # we're seeing differences of ~1.2 MB. 2 MB is still a good tolerance
  39. # when compared to GBs.
  40. TOLERANCE_SYS_MEM = 2 * KB * KB # 2 MB
  41. assert psutil_result.total == total
  42. assert abs(psutil_result.used - used) < TOLERANCE_SYS_MEM
  43. assert abs(psutil_result.available - available) < TOLERANCE_SYS_MEM
  44. assert abs(psutil_result.free - free) < TOLERANCE_SYS_MEM
  45. def test_swap_memory(self):
  46. out = sh('/usr/sbin/lsps -a')
  47. # From the man page, "The size is given in megabytes" so we assume
  48. # we'll always have 'MB' in the result
  49. # TODO maybe try to use "swap -l" to check "used" too, but its units
  50. # are not guaranteed to be "MB" so parsing may not be consistent
  51. matchobj = re.search(
  52. r"(?P<space>\S+)\s+"
  53. r"(?P<vol>\S+)\s+"
  54. r"(?P<vg>\S+)\s+"
  55. r"(?P<size>\d+)MB",
  56. out,
  57. )
  58. assert matchobj is not None
  59. total_mb = int(matchobj.group("size"))
  60. MB = 1024**2
  61. psutil_result = psutil.swap_memory()
  62. # we divide our result by MB instead of multiplying the lsps value by
  63. # MB because lsps may round down, so we round down too
  64. assert int(psutil_result.total / MB) == total_mb
  65. def test_cpu_stats(self):
  66. out = sh('/usr/bin/mpstat -a')
  67. re_pattern = r"ALL\s*"
  68. for field in [
  69. "min",
  70. "maj",
  71. "mpcs",
  72. "mpcr",
  73. "dev",
  74. "soft",
  75. "dec",
  76. "ph",
  77. "cs",
  78. "ics",
  79. "bound",
  80. "rq",
  81. "push",
  82. "S3pull",
  83. "S3grd",
  84. "S0rd",
  85. "S1rd",
  86. "S2rd",
  87. "S3rd",
  88. "S4rd",
  89. "S5rd",
  90. "sysc",
  91. ]:
  92. re_pattern += rf"(?P<{field}>\S+)\s+"
  93. matchobj = re.search(re_pattern, out)
  94. assert matchobj is not None
  95. # numbers are usually in the millions so 1000 is ok for tolerance
  96. CPU_STATS_TOLERANCE = 1000
  97. psutil_result = psutil.cpu_stats()
  98. assert (
  99. abs(psutil_result.ctx_switches - int(matchobj.group("cs")))
  100. < CPU_STATS_TOLERANCE
  101. )
  102. assert (
  103. abs(psutil_result.syscalls - int(matchobj.group("sysc")))
  104. < CPU_STATS_TOLERANCE
  105. )
  106. assert (
  107. abs(psutil_result.interrupts - int(matchobj.group("dev")))
  108. < CPU_STATS_TOLERANCE
  109. )
  110. assert (
  111. abs(psutil_result.soft_interrupts - int(matchobj.group("soft")))
  112. < CPU_STATS_TOLERANCE
  113. )
  114. def test_cpu_count_logical(self):
  115. out = sh('/usr/bin/mpstat -a')
  116. mpstat_lcpu = int(re.search(r"lcpu=(\d+)", out).group(1))
  117. psutil_lcpu = psutil.cpu_count(logical=True)
  118. assert mpstat_lcpu == psutil_lcpu
  119. def test_net_if_addrs_names(self):
  120. out = sh('/etc/ifconfig -l')
  121. ifconfig_names = set(out.split())
  122. psutil_names = set(psutil.net_if_addrs().keys())
  123. assert ifconfig_names == psutil_names