test_scripts.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Test various scripts."""
  6. import ast
  7. import os
  8. import shutil
  9. import stat
  10. import subprocess
  11. import pytest
  12. from psutil import POSIX
  13. from psutil import WINDOWS
  14. from psutil.tests import CI_TESTING
  15. from psutil.tests import HAS_BATTERY
  16. from psutil.tests import HAS_MEMORY_MAPS
  17. from psutil.tests import HAS_SENSORS_BATTERY
  18. from psutil.tests import HAS_SENSORS_FANS
  19. from psutil.tests import HAS_SENSORS_TEMPERATURES
  20. from psutil.tests import PYTHON_EXE
  21. from psutil.tests import PYTHON_EXE_ENV
  22. from psutil.tests import ROOT_DIR
  23. from psutil.tests import SCRIPTS_DIR
  24. from psutil.tests import PsutilTestCase
  25. from psutil.tests import import_module_by_path
  26. from psutil.tests import psutil
  27. from psutil.tests import sh
  28. INTERNAL_SCRIPTS_DIR = os.path.join(SCRIPTS_DIR, "internal")
  29. SETUP_PY = os.path.join(ROOT_DIR, 'setup.py')
  30. # ===================================================================
  31. # --- Tests scripts in scripts/ directory
  32. # ===================================================================
  33. @pytest.mark.skipif(
  34. CI_TESTING and not os.path.exists(SCRIPTS_DIR),
  35. reason="can't find scripts/ directory",
  36. )
  37. class TestExampleScripts(PsutilTestCase):
  38. @staticmethod
  39. def assert_stdout(exe, *args, **kwargs):
  40. kwargs.setdefault("env", PYTHON_EXE_ENV)
  41. exe = os.path.join(SCRIPTS_DIR, exe)
  42. cmd = [PYTHON_EXE, exe]
  43. for arg in args:
  44. cmd.append(arg)
  45. try:
  46. out = sh(cmd, **kwargs).strip()
  47. except RuntimeError as err:
  48. if 'AccessDenied' in str(err):
  49. return str(err)
  50. else:
  51. raise
  52. assert out, out
  53. return out
  54. @staticmethod
  55. def assert_syntax(exe):
  56. exe = os.path.join(SCRIPTS_DIR, exe)
  57. with open(exe, encoding="utf8") as f:
  58. src = f.read()
  59. ast.parse(src)
  60. def test_coverage(self):
  61. # make sure all example scripts have a test method defined
  62. meths = dir(self)
  63. for name in os.listdir(SCRIPTS_DIR):
  64. if name.endswith('.py'):
  65. if 'test_' + os.path.splitext(name)[0] not in meths:
  66. # self.assert_stdout(name)
  67. raise self.fail(
  68. "no test defined for"
  69. f" {os.path.join(SCRIPTS_DIR, name)!r} script"
  70. )
  71. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  72. def test_executable(self):
  73. for root, dirs, files in os.walk(SCRIPTS_DIR):
  74. for file in files:
  75. if file.endswith('.py'):
  76. path = os.path.join(root, file)
  77. if not stat.S_IXUSR & os.stat(path)[stat.ST_MODE]:
  78. raise self.fail(f"{path!r} is not executable")
  79. def test_disk_usage(self):
  80. self.assert_stdout('disk_usage.py')
  81. def test_free(self):
  82. self.assert_stdout('free.py')
  83. def test_meminfo(self):
  84. self.assert_stdout('meminfo.py')
  85. def test_procinfo(self):
  86. self.assert_stdout('procinfo.py', str(os.getpid()))
  87. @pytest.mark.skipif(CI_TESTING and not psutil.users(), reason="no users")
  88. def test_who(self):
  89. self.assert_stdout('who.py')
  90. def test_ps(self):
  91. self.assert_stdout('ps.py')
  92. def test_pstree(self):
  93. self.assert_stdout('pstree.py')
  94. def test_netstat(self):
  95. self.assert_stdout('netstat.py')
  96. def test_ifconfig(self):
  97. self.assert_stdout('ifconfig.py')
  98. @pytest.mark.skipif(not HAS_MEMORY_MAPS, reason="not supported")
  99. def test_pmap(self):
  100. self.assert_stdout('pmap.py', str(os.getpid()))
  101. def test_procsmem(self):
  102. if 'uss' not in psutil.Process().memory_full_info()._fields:
  103. raise pytest.skip("not supported")
  104. self.assert_stdout('procsmem.py')
  105. def test_killall(self):
  106. self.assert_syntax('killall.py')
  107. def test_nettop(self):
  108. self.assert_syntax('nettop.py')
  109. def test_top(self):
  110. self.assert_syntax('top.py')
  111. def test_iotop(self):
  112. self.assert_syntax('iotop.py')
  113. def test_pidof(self):
  114. output = self.assert_stdout('pidof.py', psutil.Process().name())
  115. assert str(os.getpid()) in output
  116. @pytest.mark.skipif(not WINDOWS, reason="WINDOWS only")
  117. def test_winservices(self):
  118. self.assert_stdout('winservices.py')
  119. def test_cpu_distribution(self):
  120. self.assert_syntax('cpu_distribution.py')
  121. @pytest.mark.skipif(not HAS_SENSORS_TEMPERATURES, reason="not supported")
  122. def test_temperatures(self):
  123. if not psutil.sensors_temperatures():
  124. raise pytest.skip("no temperatures")
  125. self.assert_stdout('temperatures.py')
  126. @pytest.mark.skipif(not HAS_SENSORS_FANS, reason="not supported")
  127. def test_fans(self):
  128. if not psutil.sensors_fans():
  129. raise pytest.skip("no fans")
  130. self.assert_stdout('fans.py')
  131. @pytest.mark.skipif(not HAS_SENSORS_BATTERY, reason="not supported")
  132. @pytest.mark.skipif(not HAS_BATTERY, reason="no battery")
  133. def test_battery(self):
  134. self.assert_stdout('battery.py')
  135. @pytest.mark.skipif(not HAS_SENSORS_BATTERY, reason="not supported")
  136. @pytest.mark.skipif(not HAS_BATTERY, reason="no battery")
  137. def test_sensors(self):
  138. self.assert_stdout('sensors.py')
  139. # ===================================================================
  140. # --- Tests scripts in scripts/internal/ directory
  141. # ===================================================================
  142. @pytest.mark.skipif(
  143. CI_TESTING and not os.path.exists(INTERNAL_SCRIPTS_DIR),
  144. reason="can't find scripts/internal/ directory",
  145. )
  146. class TestInternalScripts(PsutilTestCase):
  147. @staticmethod
  148. def ls():
  149. for name in os.listdir(INTERNAL_SCRIPTS_DIR):
  150. if name.endswith(".py"):
  151. yield os.path.join(INTERNAL_SCRIPTS_DIR, name)
  152. def test_syntax_all(self):
  153. for path in self.ls():
  154. with open(path, encoding="utf8") as f:
  155. data = f.read()
  156. ast.parse(data)
  157. @pytest.mark.skipif(CI_TESTING, reason="not on CI")
  158. def test_import_all(self):
  159. for path in self.ls():
  160. try:
  161. import_module_by_path(path)
  162. except SystemExit:
  163. pass
  164. # ===================================================================
  165. # --- Tests for setup.py script
  166. # ===================================================================
  167. @pytest.mark.skipif(
  168. CI_TESTING and not os.path.exists(SETUP_PY), reason="can't find setup.py"
  169. )
  170. class TestSetupScript(PsutilTestCase):
  171. def test_invocation(self):
  172. module = import_module_by_path(SETUP_PY)
  173. with pytest.raises(SystemExit):
  174. module.setup()
  175. assert module.get_version() == psutil.__version__
  176. @pytest.mark.skipif(
  177. not shutil.which("python2.7"), reason="python2.7 not installed"
  178. )
  179. def test_python2(self):
  180. # There's a duplicate of this test in scripts/internal
  181. # directory, which is only executed by CI. We replicate it here
  182. # to run it when developing locally.
  183. p = subprocess.Popen(
  184. [shutil.which("python2.7"), SETUP_PY],
  185. stdout=subprocess.PIPE,
  186. stderr=subprocess.PIPE,
  187. universal_newlines=True,
  188. )
  189. stdout, stderr = p.communicate()
  190. assert p.wait() == 1
  191. assert not stdout
  192. assert "psutil no longer supports Python 2.7" in stderr
  193. assert "Latest version supporting Python 2.7 is" in stderr