test_aix.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 unittest
  10. import psutil
  11. from psutil import AIX
  12. from psutil.tests import PsutilTestCase
  13. from psutil.tests import sh
  14. @unittest.skipIf(not AIX, "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 ("size inuse free pin virtual available mmode").split():
  20. re_pattern += r"(?P<%s>\S+)\s+" % (field,)
  21. matchobj = re.search(re_pattern, out)
  22. self.assertIsNotNone(
  23. matchobj, "svmon command returned unexpected output")
  24. KB = 1024
  25. total = int(matchobj.group("size")) * KB
  26. available = int(matchobj.group("available")) * KB
  27. used = int(matchobj.group("inuse")) * KB
  28. free = int(matchobj.group("free")) * KB
  29. psutil_result = psutil.virtual_memory()
  30. # TOLERANCE_SYS_MEM from psutil.tests is not enough. For some reason
  31. # we're seeing differences of ~1.2 MB. 2 MB is still a good tolerance
  32. # when compared to GBs.
  33. TOLERANCE_SYS_MEM = 2 * KB * KB # 2 MB
  34. self.assertEqual(psutil_result.total, total)
  35. self.assertAlmostEqual(
  36. psutil_result.used, used, delta=TOLERANCE_SYS_MEM)
  37. self.assertAlmostEqual(
  38. psutil_result.available, available, delta=TOLERANCE_SYS_MEM)
  39. self.assertAlmostEqual(
  40. psutil_result.free, free, delta=TOLERANCE_SYS_MEM)
  41. def test_swap_memory(self):
  42. out = sh('/usr/sbin/lsps -a')
  43. # From the man page, "The size is given in megabytes" so we assume
  44. # we'll always have 'MB' in the result
  45. # TODO maybe try to use "swap -l" to check "used" too, but its units
  46. # are not guaranteed to be "MB" so parsing may not be consistent
  47. matchobj = re.search(r"(?P<space>\S+)\s+"
  48. r"(?P<vol>\S+)\s+"
  49. r"(?P<vg>\S+)\s+"
  50. r"(?P<size>\d+)MB", out)
  51. self.assertIsNotNone(
  52. matchobj, "lsps command returned unexpected output")
  53. total_mb = int(matchobj.group("size"))
  54. MB = 1024 ** 2
  55. psutil_result = psutil.swap_memory()
  56. # we divide our result by MB instead of multiplying the lsps value by
  57. # MB because lsps may round down, so we round down too
  58. self.assertEqual(int(psutil_result.total / MB), total_mb)
  59. def test_cpu_stats(self):
  60. out = sh('/usr/bin/mpstat -a')
  61. re_pattern = r"ALL\s*"
  62. for field in ("min maj mpcs mpcr dev soft dec ph cs ics bound rq "
  63. "push S3pull S3grd S0rd S1rd S2rd S3rd S4rd S5rd "
  64. "sysc").split():
  65. re_pattern += r"(?P<%s>\S+)\s+" % (field,)
  66. matchobj = re.search(re_pattern, out)
  67. self.assertIsNotNone(
  68. matchobj, "mpstat command returned unexpected output")
  69. # numbers are usually in the millions so 1000 is ok for tolerance
  70. CPU_STATS_TOLERANCE = 1000
  71. psutil_result = psutil.cpu_stats()
  72. self.assertAlmostEqual(
  73. psutil_result.ctx_switches,
  74. int(matchobj.group("cs")),
  75. delta=CPU_STATS_TOLERANCE)
  76. self.assertAlmostEqual(
  77. psutil_result.syscalls,
  78. int(matchobj.group("sysc")),
  79. delta=CPU_STATS_TOLERANCE)
  80. self.assertAlmostEqual(
  81. psutil_result.interrupts,
  82. int(matchobj.group("dev")),
  83. delta=CPU_STATS_TOLERANCE)
  84. self.assertAlmostEqual(
  85. psutil_result.soft_interrupts,
  86. int(matchobj.group("soft")),
  87. delta=CPU_STATS_TOLERANCE)
  88. def test_cpu_count_logical(self):
  89. out = sh('/usr/bin/mpstat -a')
  90. mpstat_lcpu = int(re.search(r"lcpu=(\d+)", out).group(1))
  91. psutil_lcpu = psutil.cpu_count(logical=True)
  92. self.assertEqual(mpstat_lcpu, psutil_lcpu)
  93. def test_net_if_addrs_names(self):
  94. out = sh('/etc/ifconfig -l')
  95. ifconfig_names = set(out.split())
  96. psutil_names = set(psutil.net_if_addrs().keys())
  97. self.assertSetEqual(ifconfig_names, psutil_names)
  98. if __name__ == '__main__':
  99. from psutil.tests.runner import run_from_name
  100. run_from_name(__file__)