test_scripts.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 LINUX
  13. from psutil import POSIX
  14. from psutil import WINDOWS
  15. from psutil.tests import CI_TESTING
  16. from psutil.tests import HAS_BATTERY
  17. from psutil.tests import HAS_MEMORY_MAPS
  18. from psutil.tests import HAS_SENSORS_BATTERY
  19. from psutil.tests import HAS_SENSORS_FANS
  20. from psutil.tests import HAS_SENSORS_TEMPERATURES
  21. from psutil.tests import PYTHON_EXE
  22. from psutil.tests import PYTHON_EXE_ENV
  23. from psutil.tests import ROOT_DIR
  24. from psutil.tests import SCRIPTS_DIR
  25. from psutil.tests import PsutilTestCase
  26. from psutil.tests import import_module_by_path
  27. from psutil.tests import psutil
  28. from psutil.tests import sh
  29. INTERNAL_SCRIPTS_DIR = os.path.join(SCRIPTS_DIR, "internal")
  30. SETUP_PY = os.path.join(ROOT_DIR, 'setup.py')
  31. # ===================================================================
  32. # --- Tests scripts in scripts/ directory
  33. # ===================================================================
  34. @pytest.mark.skipif(
  35. CI_TESTING and not os.path.exists(SCRIPTS_DIR),
  36. reason="can't find scripts/ directory",
  37. )
  38. class TestExampleScripts(PsutilTestCase):
  39. @staticmethod
  40. def assert_stdout(exe, *args):
  41. env = PYTHON_EXE_ENV.copy()
  42. env.pop("PSUTIL_DEBUG") # avoid spamming to stderr
  43. exe = os.path.join(SCRIPTS_DIR, exe)
  44. cmd = [PYTHON_EXE, exe, *args]
  45. try:
  46. out = sh(cmd, env=env).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. return pytest.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. return pytest.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. return 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. return 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. return 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. # don't care about other platforms, this is really just for myself
  158. @pytest.mark.skipif(not LINUX, reason="not on LINUX")
  159. @pytest.mark.skipif(CI_TESTING, reason="not on CI")
  160. def test_import_all(self):
  161. for path in self.ls():
  162. try:
  163. import_module_by_path(path)
  164. except SystemExit:
  165. pass
  166. # ===================================================================
  167. # --- Tests for setup.py script
  168. # ===================================================================
  169. @pytest.mark.skipif(
  170. CI_TESTING and not os.path.exists(SETUP_PY), reason="can't find setup.py"
  171. )
  172. class TestSetupScript(PsutilTestCase):
  173. def test_invocation(self):
  174. module = import_module_by_path(SETUP_PY)
  175. with pytest.raises(SystemExit):
  176. module.setup()
  177. assert module.get_version() == psutil.__version__
  178. @pytest.mark.skipif(
  179. not shutil.which("python2.7"), reason="python2.7 not installed"
  180. )
  181. def test_python2(self):
  182. # There's a duplicate of this test in scripts/internal
  183. # directory, which is only executed by CI. We replicate it here
  184. # to run it when developing locally.
  185. p = subprocess.Popen(
  186. [shutil.which("python2.7"), SETUP_PY],
  187. stdout=subprocess.PIPE,
  188. stderr=subprocess.PIPE,
  189. universal_newlines=True,
  190. )
  191. stdout, stderr = p.communicate()
  192. assert p.wait() == 1
  193. assert not stdout
  194. assert "psutil no longer supports Python 2.7" in stderr
  195. assert "Latest version supporting Python 2.7 is" in stderr