test_bsd.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. # TODO: (FreeBSD) add test for comparing connections with 'sockstat' cmd.
  6. """Tests specific to all BSD platforms."""
  7. import datetime
  8. import os
  9. import re
  10. import shutil
  11. import time
  12. import psutil
  13. from psutil import BSD
  14. from psutil import FREEBSD
  15. from psutil import NETBSD
  16. from psutil import OPENBSD
  17. from psutil.tests import HAS_BATTERY
  18. from psutil.tests import TOLERANCE_SYS_MEM
  19. from psutil.tests import PsutilTestCase
  20. from psutil.tests import pytest
  21. from psutil.tests import retry_on_failure
  22. from psutil.tests import sh
  23. from psutil.tests import spawn_subproc
  24. from psutil.tests import terminate
  25. if BSD:
  26. PAGESIZE = psutil._psplatform.cext.getpagesize()
  27. # muse requires root privileges
  28. MUSE_AVAILABLE = os.getuid() == 0 and shutil.which("muse")
  29. else:
  30. PAGESIZE = None
  31. MUSE_AVAILABLE = False
  32. def sysctl(cmdline):
  33. """Expects a sysctl command with an argument and parse the result
  34. returning only the value of interest.
  35. """
  36. result = sh("sysctl " + cmdline)
  37. if FREEBSD:
  38. result = result[result.find(": ") + 2 :]
  39. elif OPENBSD or NETBSD:
  40. result = result[result.find("=") + 1 :]
  41. try:
  42. return int(result)
  43. except ValueError:
  44. return result
  45. def muse(field):
  46. """Thin wrapper around 'muse' cmdline utility."""
  47. out = sh('muse')
  48. for line in out.split('\n'):
  49. if line.startswith(field):
  50. break
  51. else:
  52. raise ValueError("line not found")
  53. return int(line.split()[1])
  54. # =====================================================================
  55. # --- All BSD*
  56. # =====================================================================
  57. @pytest.mark.skipif(not BSD, reason="BSD only")
  58. class BSDTestCase(PsutilTestCase):
  59. """Generic tests common to all BSD variants."""
  60. @classmethod
  61. def setUpClass(cls):
  62. cls.pid = spawn_subproc().pid
  63. @classmethod
  64. def tearDownClass(cls):
  65. terminate(cls.pid)
  66. @pytest.mark.skipif(NETBSD, reason="-o lstart doesn't work on NETBSD")
  67. def test_process_create_time(self):
  68. output = sh(f"ps -o lstart -p {self.pid}")
  69. start_ps = output.replace('STARTED', '').strip()
  70. start_psutil = psutil.Process(self.pid).create_time()
  71. start_psutil = time.strftime(
  72. "%a %b %e %H:%M:%S %Y", time.localtime(start_psutil)
  73. )
  74. assert start_ps == start_psutil
  75. def test_disks(self):
  76. # test psutil.disk_usage() and psutil.disk_partitions()
  77. # against "df -a"
  78. def df(path):
  79. out = sh(f'df -k "{path}"').strip()
  80. lines = out.split('\n')
  81. lines.pop(0)
  82. line = lines.pop(0)
  83. dev, total, used, free = line.split()[:4]
  84. if dev == 'none':
  85. dev = ''
  86. total = int(total) * 1024
  87. used = int(used) * 1024
  88. free = int(free) * 1024
  89. return dev, total, used, free
  90. for part in psutil.disk_partitions(all=False):
  91. usage = psutil.disk_usage(part.mountpoint)
  92. dev, total, used, free = df(part.mountpoint)
  93. assert part.device == dev
  94. assert usage.total == total
  95. # 10 MB tolerance
  96. if abs(usage.free - free) > 10 * 1024 * 1024:
  97. return pytest.fail(f"psutil={usage.free}, df={free}")
  98. if abs(usage.used - used) > 10 * 1024 * 1024:
  99. return pytest.fail(f"psutil={usage.used}, df={used}")
  100. @pytest.mark.skipif(
  101. not shutil.which("sysctl"), reason="sysctl cmd not available"
  102. )
  103. def test_cpu_count_logical(self):
  104. syst = sysctl("hw.ncpu")
  105. assert psutil.cpu_count(logical=True) == syst
  106. @pytest.mark.skipif(
  107. not shutil.which("sysctl"), reason="sysctl cmd not available"
  108. )
  109. @pytest.mark.skipif(
  110. NETBSD, reason="skipped on NETBSD" # we check /proc/meminfo
  111. )
  112. def test_virtual_memory_total(self):
  113. num = sysctl('hw.physmem')
  114. assert num == psutil.virtual_memory().total
  115. @pytest.mark.skipif(
  116. not shutil.which("ifconfig"), reason="ifconfig cmd not available"
  117. )
  118. def test_net_if_stats(self):
  119. for name, stats in psutil.net_if_stats().items():
  120. try:
  121. out = sh(f"ifconfig {name}")
  122. except RuntimeError:
  123. pass
  124. else:
  125. assert stats.isup == ('RUNNING' in out)
  126. if "mtu" in out:
  127. assert stats.mtu == int(re.findall(r'mtu (\d+)', out)[0])
  128. # =====================================================================
  129. # --- FreeBSD
  130. # =====================================================================
  131. @pytest.mark.skipif(not FREEBSD, reason="FREEBSD only")
  132. class FreeBSDPsutilTestCase(PsutilTestCase):
  133. @classmethod
  134. def setUpClass(cls):
  135. cls.pid = spawn_subproc().pid
  136. @classmethod
  137. def tearDownClass(cls):
  138. terminate(cls.pid)
  139. @retry_on_failure()
  140. def test_memory_maps(self):
  141. out = sh(f"procstat -v {self.pid}")
  142. maps = psutil.Process(self.pid).memory_maps(grouped=False)
  143. lines = out.split('\n')[1:]
  144. while lines:
  145. line = lines.pop()
  146. fields = line.split()
  147. _, start, stop, _perms, res = fields[:5]
  148. map = maps.pop()
  149. assert f"{start}-{stop}" == map.addr
  150. assert int(res) == map.rss
  151. if not map.path.startswith('['):
  152. assert fields[10] == map.path
  153. def test_exe(self):
  154. out = sh(f"procstat -b {self.pid}")
  155. assert psutil.Process(self.pid).exe() == out.split('\n')[1].split()[-1]
  156. def test_cmdline(self):
  157. out = sh(f"procstat -c {self.pid}")
  158. assert ' '.join(psutil.Process(self.pid).cmdline()) == ' '.join(
  159. out.split('\n')[1].split()[2:]
  160. )
  161. def test_uids_gids(self):
  162. out = sh(f"procstat -s {self.pid}")
  163. euid, ruid, suid, egid, rgid, sgid = out.split('\n')[1].split()[2:8]
  164. p = psutil.Process(self.pid)
  165. uids = p.uids()
  166. gids = p.gids()
  167. assert uids.real == int(ruid)
  168. assert uids.effective == int(euid)
  169. assert uids.saved == int(suid)
  170. assert gids.real == int(rgid)
  171. assert gids.effective == int(egid)
  172. assert gids.saved == int(sgid)
  173. @retry_on_failure()
  174. def test_ctx_switches(self):
  175. tested = []
  176. out = sh(f"procstat -r {self.pid}")
  177. p = psutil.Process(self.pid)
  178. for line in out.split('\n'):
  179. line = line.lower().strip()
  180. if ' voluntary context' in line:
  181. pstat_value = int(line.split()[-1])
  182. psutil_value = p.num_ctx_switches().voluntary
  183. assert pstat_value == psutil_value
  184. tested.append(None)
  185. elif ' involuntary context' in line:
  186. pstat_value = int(line.split()[-1])
  187. psutil_value = p.num_ctx_switches().involuntary
  188. assert pstat_value == psutil_value
  189. tested.append(None)
  190. if len(tested) != 2:
  191. raise RuntimeError("couldn't find lines match in procstat out")
  192. @retry_on_failure()
  193. def test_cpu_times(self):
  194. tested = []
  195. out = sh(f"procstat -r {self.pid}")
  196. p = psutil.Process(self.pid)
  197. for line in out.split('\n'):
  198. line = line.lower().strip()
  199. if 'user time' in line:
  200. pstat_value = float('0.' + line.split()[-1].split('.')[-1])
  201. psutil_value = p.cpu_times().user
  202. assert pstat_value == psutil_value
  203. tested.append(None)
  204. elif 'system time' in line:
  205. pstat_value = float('0.' + line.split()[-1].split('.')[-1])
  206. psutil_value = p.cpu_times().system
  207. assert pstat_value == psutil_value
  208. tested.append(None)
  209. if len(tested) != 2:
  210. raise RuntimeError("couldn't find lines match in procstat out")
  211. @pytest.mark.skipif(not FREEBSD, reason="FREEBSD only")
  212. class FreeBSDSystemTestCase(PsutilTestCase):
  213. @staticmethod
  214. def parse_swapinfo():
  215. # the last line is always the total
  216. output = sh("swapinfo -k").splitlines()[-1]
  217. parts = re.split(r'\s+', output)
  218. if not parts:
  219. raise ValueError(f"Can't parse swapinfo: {output}")
  220. # the size is in 1k units, so multiply by 1024
  221. total, used, free = (int(p) * 1024 for p in parts[1:4])
  222. return total, used, free
  223. def test_cpu_frequency_against_sysctl(self):
  224. # Currently only cpu 0 is frequency is supported in FreeBSD
  225. # All other cores use the same frequency.
  226. sensor = "dev.cpu.0.freq"
  227. try:
  228. sysctl_result = int(sysctl(sensor))
  229. except RuntimeError:
  230. return pytest.skip("frequencies not supported by kernel")
  231. assert psutil.cpu_freq().current == sysctl_result
  232. sensor = "dev.cpu.0.freq_levels"
  233. sysctl_result = sysctl(sensor)
  234. # sysctl returns a string of the format:
  235. # <freq_level_1>/<voltage_level_1> <freq_level_2>/<voltage_level_2>...
  236. # Ordered highest available to lowest available.
  237. max_freq = int(sysctl_result.split()[0].split("/")[0])
  238. min_freq = int(sysctl_result.split()[-1].split("/")[0])
  239. assert psutil.cpu_freq().max == max_freq
  240. assert psutil.cpu_freq().min == min_freq
  241. # --- virtual_memory(); tests against sysctl
  242. @retry_on_failure()
  243. def test_vmem_active(self):
  244. syst = sysctl("vm.stats.vm.v_active_count") * PAGESIZE
  245. assert abs(psutil.virtual_memory().active - syst) < TOLERANCE_SYS_MEM
  246. @retry_on_failure()
  247. def test_vmem_inactive(self):
  248. syst = sysctl("vm.stats.vm.v_inactive_count") * PAGESIZE
  249. assert abs(psutil.virtual_memory().inactive - syst) < TOLERANCE_SYS_MEM
  250. @retry_on_failure()
  251. def test_vmem_wired(self):
  252. syst = sysctl("vm.stats.vm.v_wire_count") * PAGESIZE
  253. assert abs(psutil.virtual_memory().wired - syst) < TOLERANCE_SYS_MEM
  254. @retry_on_failure()
  255. def test_vmem_cached(self):
  256. syst = sysctl("vm.stats.vm.v_cache_count") * PAGESIZE
  257. assert abs(psutil.virtual_memory().cached - syst) < TOLERANCE_SYS_MEM
  258. @retry_on_failure()
  259. def test_vmem_free(self):
  260. syst = sysctl("vm.stats.vm.v_free_count") * PAGESIZE
  261. assert abs(psutil.virtual_memory().free - syst) < TOLERANCE_SYS_MEM
  262. @retry_on_failure()
  263. def test_vmem_buffers(self):
  264. syst = sysctl("vfs.bufspace")
  265. assert abs(psutil.virtual_memory().buffers - syst) < TOLERANCE_SYS_MEM
  266. # --- virtual_memory(); tests against muse
  267. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  268. def test_muse_vmem_total(self):
  269. num = muse('Total')
  270. assert psutil.virtual_memory().total == num
  271. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  272. @retry_on_failure()
  273. def test_muse_vmem_active(self):
  274. num = muse('Active')
  275. assert abs(psutil.virtual_memory().active - num) < TOLERANCE_SYS_MEM
  276. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  277. @retry_on_failure()
  278. def test_muse_vmem_inactive(self):
  279. num = muse('Inactive')
  280. assert abs(psutil.virtual_memory().inactive - num) < TOLERANCE_SYS_MEM
  281. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  282. @retry_on_failure()
  283. def test_muse_vmem_wired(self):
  284. num = muse('Wired')
  285. assert abs(psutil.virtual_memory().wired - num) < TOLERANCE_SYS_MEM
  286. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  287. @retry_on_failure()
  288. def test_muse_vmem_cached(self):
  289. num = muse('Cache')
  290. assert abs(psutil.virtual_memory().cached - num) < TOLERANCE_SYS_MEM
  291. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  292. @retry_on_failure()
  293. def test_muse_vmem_free(self):
  294. num = muse('Free')
  295. assert abs(psutil.virtual_memory().free - num) < TOLERANCE_SYS_MEM
  296. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  297. @retry_on_failure()
  298. def test_muse_vmem_buffers(self):
  299. num = muse('Buffer')
  300. assert abs(psutil.virtual_memory().buffers - num) < TOLERANCE_SYS_MEM
  301. def test_cpu_stats_ctx_switches(self):
  302. assert (
  303. abs(
  304. psutil.cpu_stats().ctx_switches
  305. - sysctl('vm.stats.sys.v_swtch')
  306. )
  307. < 1000
  308. )
  309. def test_cpu_stats_interrupts(self):
  310. assert (
  311. abs(psutil.cpu_stats().interrupts - sysctl('vm.stats.sys.v_intr'))
  312. < 1000
  313. )
  314. def test_cpu_stats_soft_interrupts(self):
  315. assert (
  316. abs(
  317. psutil.cpu_stats().soft_interrupts
  318. - sysctl('vm.stats.sys.v_soft')
  319. )
  320. < 1000
  321. )
  322. @retry_on_failure()
  323. def test_cpu_stats_syscalls(self):
  324. # pretty high tolerance but it looks like it's OK.
  325. assert (
  326. abs(psutil.cpu_stats().syscalls - sysctl('vm.stats.sys.v_syscall'))
  327. < 200000
  328. )
  329. # --- swap memory
  330. def test_swapmem_free(self):
  331. _total, _used, free = self.parse_swapinfo()
  332. assert abs(psutil.swap_memory().free - free) < TOLERANCE_SYS_MEM
  333. def test_swapmem_used(self):
  334. _total, used, _free = self.parse_swapinfo()
  335. assert abs(psutil.swap_memory().used - used) < TOLERANCE_SYS_MEM
  336. def test_swapmem_total(self):
  337. total, _used, _free = self.parse_swapinfo()
  338. assert abs(psutil.swap_memory().total - total) < TOLERANCE_SYS_MEM
  339. # --- others
  340. def test_boot_time(self):
  341. s = sysctl('sysctl kern.boottime')
  342. s = s[s.find(" sec = ") + 7 :]
  343. s = s[: s.find(',')]
  344. btime = int(s)
  345. assert btime == psutil.boot_time()
  346. # --- sensors_battery
  347. @pytest.mark.skipif(not HAS_BATTERY, reason="no battery")
  348. def test_sensors_battery(self):
  349. def secs2hours(secs):
  350. m, _s = divmod(secs, 60)
  351. h, m = divmod(m, 60)
  352. return f"{int(h)}:{int(m):02}"
  353. out = sh("acpiconf -i 0")
  354. fields = {x.split('\t')[0]: x.split('\t')[-1] for x in out.split("\n")}
  355. metrics = psutil.sensors_battery()
  356. percent = int(fields['Remaining capacity:'].replace('%', ''))
  357. remaining_time = fields['Remaining time:']
  358. assert metrics.percent == percent
  359. if remaining_time == 'unknown':
  360. assert metrics.secsleft == psutil.POWER_TIME_UNLIMITED
  361. else:
  362. assert secs2hours(metrics.secsleft) == remaining_time
  363. @pytest.mark.skipif(not HAS_BATTERY, reason="no battery")
  364. def test_sensors_battery_against_sysctl(self):
  365. assert psutil.sensors_battery().percent == sysctl(
  366. "hw.acpi.battery.life"
  367. )
  368. assert psutil.sensors_battery().power_plugged == (
  369. sysctl("hw.acpi.acline") == 1
  370. )
  371. secsleft = psutil.sensors_battery().secsleft
  372. if secsleft < 0:
  373. assert sysctl("hw.acpi.battery.time") == -1
  374. else:
  375. assert secsleft == sysctl("hw.acpi.battery.time") * 60
  376. @pytest.mark.skipif(HAS_BATTERY, reason="has battery")
  377. def test_sensors_battery_no_battery(self):
  378. # If no battery is present one of these calls is supposed
  379. # to fail, see:
  380. # https://github.com/giampaolo/psutil/issues/1074
  381. with pytest.raises(RuntimeError):
  382. sysctl("hw.acpi.battery.life")
  383. sysctl("hw.acpi.battery.time")
  384. sysctl("hw.acpi.acline")
  385. assert psutil.sensors_battery() is None
  386. # --- sensors_temperatures
  387. def test_sensors_temperatures_against_sysctl(self):
  388. num_cpus = psutil.cpu_count(True)
  389. for cpu in range(num_cpus):
  390. sensor = f"dev.cpu.{cpu}.temperature"
  391. # sysctl returns a string in the format 46.0C
  392. try:
  393. sysctl_result = int(float(sysctl(sensor)[:-1]))
  394. except RuntimeError:
  395. return pytest.skip("temperatures not supported by kernel")
  396. assert (
  397. abs(
  398. psutil.sensors_temperatures()["coretemp"][cpu].current
  399. - sysctl_result
  400. )
  401. < 10
  402. )
  403. sensor = f"dev.cpu.{cpu}.coretemp.tjmax"
  404. sysctl_result = int(float(sysctl(sensor)[:-1]))
  405. assert (
  406. psutil.sensors_temperatures()["coretemp"][cpu].high
  407. == sysctl_result
  408. )
  409. # =====================================================================
  410. # --- OpenBSD
  411. # =====================================================================
  412. @pytest.mark.skipif(not OPENBSD, reason="OPENBSD only")
  413. class OpenBSDTestCase(PsutilTestCase):
  414. def test_boot_time(self):
  415. s = sysctl('kern.boottime')
  416. sys_bt = datetime.datetime.strptime(s, "%a %b %d %H:%M:%S %Y")
  417. psutil_bt = datetime.datetime.fromtimestamp(psutil.boot_time())
  418. assert sys_bt == psutil_bt
  419. # =====================================================================
  420. # --- NetBSD
  421. # =====================================================================
  422. @pytest.mark.skipif(not NETBSD, reason="NETBSD only")
  423. class NetBSDTestCase(PsutilTestCase):
  424. @staticmethod
  425. def parse_meminfo(look_for):
  426. with open('/proc/meminfo') as f:
  427. for line in f:
  428. if line.startswith(look_for):
  429. return int(line.split()[1]) * 1024
  430. raise ValueError(f"can't find {look_for}")
  431. # --- virtual mem
  432. def test_vmem_total(self):
  433. assert psutil.virtual_memory().total == self.parse_meminfo("MemTotal:")
  434. def test_vmem_free(self):
  435. assert (
  436. abs(psutil.virtual_memory().free - self.parse_meminfo("MemFree:"))
  437. < TOLERANCE_SYS_MEM
  438. )
  439. def test_vmem_buffers(self):
  440. assert (
  441. abs(
  442. psutil.virtual_memory().buffers
  443. - self.parse_meminfo("Buffers:")
  444. )
  445. < TOLERANCE_SYS_MEM
  446. )
  447. def test_vmem_shared(self):
  448. assert (
  449. abs(
  450. psutil.virtual_memory().shared
  451. - self.parse_meminfo("MemShared:")
  452. )
  453. < TOLERANCE_SYS_MEM
  454. )
  455. def test_vmem_cached(self):
  456. assert (
  457. abs(psutil.virtual_memory().cached - self.parse_meminfo("Cached:"))
  458. < TOLERANCE_SYS_MEM
  459. )
  460. # --- swap mem
  461. def test_swapmem_total(self):
  462. assert (
  463. abs(psutil.swap_memory().total - self.parse_meminfo("SwapTotal:"))
  464. < TOLERANCE_SYS_MEM
  465. )
  466. def test_swapmem_free(self):
  467. assert (
  468. abs(psutil.swap_memory().free - self.parse_meminfo("SwapFree:"))
  469. < TOLERANCE_SYS_MEM
  470. )
  471. def test_swapmem_used(self):
  472. smem = psutil.swap_memory()
  473. assert smem.used == smem.total - smem.free
  474. # --- others
  475. def test_cpu_stats_interrupts(self):
  476. with open('/proc/stat', 'rb') as f:
  477. for line in f:
  478. if line.startswith(b'intr'):
  479. interrupts = int(line.split()[1])
  480. break
  481. else:
  482. raise ValueError("couldn't find line")
  483. assert abs(psutil.cpu_stats().interrupts - interrupts) < 1000
  484. def test_cpu_stats_ctx_switches(self):
  485. with open('/proc/stat', 'rb') as f:
  486. for line in f:
  487. if line.startswith(b'ctxt'):
  488. ctx_switches = int(line.split()[1])
  489. break
  490. else:
  491. raise ValueError("couldn't find line")
  492. assert abs(psutil.cpu_stats().ctx_switches - ctx_switches) < 1000