test_misc.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. """Miscellaneous tests."""
  6. import collections
  7. import contextlib
  8. import io
  9. import json
  10. import os
  11. import pickle
  12. import socket
  13. import sys
  14. from unittest import mock
  15. import psutil
  16. import psutil.tests
  17. from psutil import WINDOWS
  18. from psutil._common import bcat
  19. from psutil._common import cat
  20. from psutil._common import debug
  21. from psutil._common import isfile_strict
  22. from psutil._common import memoize
  23. from psutil._common import memoize_when_activated
  24. from psutil._common import parse_environ_block
  25. from psutil._common import supports_ipv6
  26. from psutil._common import wrap_numbers
  27. from psutil.tests import HAS_NET_IO_COUNTERS
  28. from psutil.tests import PsutilTestCase
  29. from psutil.tests import process_namespace
  30. from psutil.tests import pytest
  31. from psutil.tests import reload_module
  32. from psutil.tests import system_namespace
  33. # ===================================================================
  34. # --- Test classes' repr(), str(), ...
  35. # ===================================================================
  36. class TestSpecialMethods(PsutilTestCase):
  37. def test_check_pid_range(self):
  38. with pytest.raises(OverflowError):
  39. psutil._psplatform.cext.check_pid_range(2**128)
  40. with pytest.raises(psutil.NoSuchProcess):
  41. psutil.Process(2**128)
  42. def test_process__repr__(self, func=repr):
  43. p = psutil.Process(self.spawn_testproc().pid)
  44. r = func(p)
  45. assert "psutil.Process" in r
  46. assert f"pid={p.pid}" in r
  47. assert f"name='{p.name()}'" in r.replace("name=u'", "name='")
  48. assert "status=" in r
  49. assert "exitcode=" not in r
  50. p.terminate()
  51. p.wait()
  52. r = func(p)
  53. assert "status='terminated'" in r
  54. assert "exitcode=" in r
  55. with mock.patch.object(
  56. psutil.Process,
  57. "name",
  58. side_effect=psutil.ZombieProcess(os.getpid()),
  59. ):
  60. p = psutil.Process()
  61. r = func(p)
  62. assert f"pid={p.pid}" in r
  63. assert "status='zombie'" in r
  64. assert "name=" not in r
  65. with mock.patch.object(
  66. psutil.Process,
  67. "name",
  68. side_effect=psutil.NoSuchProcess(os.getpid()),
  69. ):
  70. p = psutil.Process()
  71. r = func(p)
  72. assert f"pid={p.pid}" in r
  73. assert "terminated" in r
  74. assert "name=" not in r
  75. with mock.patch.object(
  76. psutil.Process,
  77. "name",
  78. side_effect=psutil.AccessDenied(os.getpid()),
  79. ):
  80. p = psutil.Process()
  81. r = func(p)
  82. assert f"pid={p.pid}" in r
  83. assert "name=" not in r
  84. def test_process__str__(self):
  85. self.test_process__repr__(func=str)
  86. def test_error__repr__(self):
  87. assert repr(psutil.Error()) == "psutil.Error()"
  88. def test_error__str__(self):
  89. assert str(psutil.Error()) == ""
  90. def test_no_such_process__repr__(self):
  91. assert (
  92. repr(psutil.NoSuchProcess(321))
  93. == "psutil.NoSuchProcess(pid=321, msg='process no longer exists')"
  94. )
  95. assert (
  96. repr(psutil.NoSuchProcess(321, name="name", msg="msg"))
  97. == "psutil.NoSuchProcess(pid=321, name='name', msg='msg')"
  98. )
  99. def test_no_such_process__str__(self):
  100. assert (
  101. str(psutil.NoSuchProcess(321))
  102. == "process no longer exists (pid=321)"
  103. )
  104. assert (
  105. str(psutil.NoSuchProcess(321, name="name", msg="msg"))
  106. == "msg (pid=321, name='name')"
  107. )
  108. def test_zombie_process__repr__(self):
  109. assert (
  110. repr(psutil.ZombieProcess(321))
  111. == 'psutil.ZombieProcess(pid=321, msg="PID still '
  112. 'exists but it\'s a zombie")'
  113. )
  114. assert (
  115. repr(psutil.ZombieProcess(321, name="name", ppid=320, msg="foo"))
  116. == "psutil.ZombieProcess(pid=321, ppid=320, name='name',"
  117. " msg='foo')"
  118. )
  119. def test_zombie_process__str__(self):
  120. assert (
  121. str(psutil.ZombieProcess(321))
  122. == "PID still exists but it's a zombie (pid=321)"
  123. )
  124. assert (
  125. str(psutil.ZombieProcess(321, name="name", ppid=320, msg="foo"))
  126. == "foo (pid=321, ppid=320, name='name')"
  127. )
  128. def test_access_denied__repr__(self):
  129. assert repr(psutil.AccessDenied(321)) == "psutil.AccessDenied(pid=321)"
  130. assert (
  131. repr(psutil.AccessDenied(321, name="name", msg="msg"))
  132. == "psutil.AccessDenied(pid=321, name='name', msg='msg')"
  133. )
  134. def test_access_denied__str__(self):
  135. assert str(psutil.AccessDenied(321)) == "(pid=321)"
  136. assert (
  137. str(psutil.AccessDenied(321, name="name", msg="msg"))
  138. == "msg (pid=321, name='name')"
  139. )
  140. def test_timeout_expired__repr__(self):
  141. assert (
  142. repr(psutil.TimeoutExpired(5))
  143. == "psutil.TimeoutExpired(seconds=5, msg='timeout after 5"
  144. " seconds')"
  145. )
  146. assert (
  147. repr(psutil.TimeoutExpired(5, pid=321, name="name"))
  148. == "psutil.TimeoutExpired(pid=321, name='name', seconds=5, "
  149. "msg='timeout after 5 seconds')"
  150. )
  151. def test_timeout_expired__str__(self):
  152. assert str(psutil.TimeoutExpired(5)) == "timeout after 5 seconds"
  153. assert (
  154. str(psutil.TimeoutExpired(5, pid=321, name="name"))
  155. == "timeout after 5 seconds (pid=321, name='name')"
  156. )
  157. def test_process__eq__(self):
  158. p1 = psutil.Process()
  159. p2 = psutil.Process()
  160. assert p1 == p2
  161. p2._ident = (0, 0)
  162. assert p1 != p2
  163. assert p1 != 'foo'
  164. def test_process__hash__(self):
  165. s = {psutil.Process(), psutil.Process()}
  166. assert len(s) == 1
  167. # ===================================================================
  168. # --- Misc, generic, corner cases
  169. # ===================================================================
  170. class TestMisc(PsutilTestCase):
  171. def test__all__(self):
  172. dir_psutil = dir(psutil)
  173. for name in dir_psutil:
  174. if name in {
  175. 'debug',
  176. 'tests',
  177. 'test',
  178. 'PermissionError',
  179. 'ProcessLookupError',
  180. }:
  181. continue
  182. if not name.startswith('_'):
  183. try:
  184. __import__(name)
  185. except ImportError:
  186. if name not in psutil.__all__:
  187. fun = getattr(psutil, name)
  188. if fun is None:
  189. continue
  190. if (
  191. fun.__doc__ is not None
  192. and 'deprecated' not in fun.__doc__.lower()
  193. ):
  194. raise self.fail(f"{name!r} not in psutil.__all__")
  195. # Import 'star' will break if __all__ is inconsistent, see:
  196. # https://github.com/giampaolo/psutil/issues/656
  197. # Can't do `from psutil import *` as it won't work
  198. # so we simply iterate over __all__.
  199. for name in psutil.__all__:
  200. assert name in dir_psutil
  201. def test_version(self):
  202. assert (
  203. '.'.join([str(x) for x in psutil.version_info])
  204. == psutil.__version__
  205. )
  206. def test_process_as_dict_no_new_names(self):
  207. # See https://github.com/giampaolo/psutil/issues/813
  208. p = psutil.Process()
  209. p.foo = '1'
  210. assert 'foo' not in p.as_dict()
  211. def test_serialization(self):
  212. def check(ret):
  213. json.loads(json.dumps(ret))
  214. a = pickle.dumps(ret)
  215. b = pickle.loads(a)
  216. assert ret == b
  217. # --- process APIs
  218. proc = psutil.Process()
  219. check(psutil.Process().as_dict())
  220. ns = process_namespace(proc)
  221. for fun, name in ns.iter(ns.getters, clear_cache=True):
  222. with self.subTest(proc=proc, name=name):
  223. try:
  224. ret = fun()
  225. except psutil.Error:
  226. pass
  227. else:
  228. check(ret)
  229. # --- system APIs
  230. ns = system_namespace()
  231. for fun, name in ns.iter(ns.getters):
  232. if name in {"win_service_iter", "win_service_get"}:
  233. continue
  234. with self.subTest(name=name):
  235. try:
  236. ret = fun()
  237. except psutil.AccessDenied:
  238. pass
  239. else:
  240. check(ret)
  241. # --- exception classes
  242. b = pickle.loads(
  243. pickle.dumps(
  244. psutil.NoSuchProcess(pid=4567, name='name', msg='msg')
  245. )
  246. )
  247. assert isinstance(b, psutil.NoSuchProcess)
  248. assert b.pid == 4567
  249. assert b.name == 'name'
  250. assert b.msg == 'msg'
  251. b = pickle.loads(
  252. pickle.dumps(
  253. psutil.ZombieProcess(pid=4567, name='name', ppid=42, msg='msg')
  254. )
  255. )
  256. assert isinstance(b, psutil.ZombieProcess)
  257. assert b.pid == 4567
  258. assert b.ppid == 42
  259. assert b.name == 'name'
  260. assert b.msg == 'msg'
  261. b = pickle.loads(
  262. pickle.dumps(psutil.AccessDenied(pid=123, name='name', msg='msg'))
  263. )
  264. assert isinstance(b, psutil.AccessDenied)
  265. assert b.pid == 123
  266. assert b.name == 'name'
  267. assert b.msg == 'msg'
  268. b = pickle.loads(
  269. pickle.dumps(
  270. psutil.TimeoutExpired(seconds=33, pid=4567, name='name')
  271. )
  272. )
  273. assert isinstance(b, psutil.TimeoutExpired)
  274. assert b.seconds == 33
  275. assert b.pid == 4567
  276. assert b.name == 'name'
  277. def test_ad_on_process_creation(self):
  278. # We are supposed to be able to instantiate Process also in case
  279. # of zombie processes or access denied.
  280. with mock.patch.object(
  281. psutil.Process, '_get_ident', side_effect=psutil.AccessDenied
  282. ) as meth:
  283. psutil.Process()
  284. assert meth.called
  285. with mock.patch.object(
  286. psutil.Process, '_get_ident', side_effect=psutil.ZombieProcess(1)
  287. ) as meth:
  288. psutil.Process()
  289. assert meth.called
  290. with mock.patch.object(
  291. psutil.Process, '_get_ident', side_effect=ValueError
  292. ) as meth:
  293. with pytest.raises(ValueError):
  294. psutil.Process()
  295. assert meth.called
  296. with mock.patch.object(
  297. psutil.Process, '_get_ident', side_effect=psutil.NoSuchProcess(1)
  298. ) as meth:
  299. with self.assertRaises(psutil.NoSuchProcess):
  300. psutil.Process()
  301. assert meth.called
  302. def test_sanity_version_check(self):
  303. # see: https://github.com/giampaolo/psutil/issues/564
  304. with mock.patch(
  305. "psutil._psplatform.cext.version", return_value="0.0.0"
  306. ):
  307. with pytest.raises(ImportError) as cm:
  308. reload_module(psutil)
  309. assert "version conflict" in str(cm.value).lower()
  310. # ===================================================================
  311. # --- psutil/_common.py utils
  312. # ===================================================================
  313. class TestMemoizeDecorator(PsutilTestCase):
  314. def setUp(self):
  315. self.calls = []
  316. tearDown = setUp
  317. def run_against(self, obj, expected_retval=None):
  318. # no args
  319. for _ in range(2):
  320. ret = obj()
  321. assert self.calls == [((), {})]
  322. if expected_retval is not None:
  323. assert ret == expected_retval
  324. # with args
  325. for _ in range(2):
  326. ret = obj(1)
  327. assert self.calls == [((), {}), ((1,), {})]
  328. if expected_retval is not None:
  329. assert ret == expected_retval
  330. # with args + kwargs
  331. for _ in range(2):
  332. ret = obj(1, bar=2)
  333. assert self.calls == [((), {}), ((1,), {}), ((1,), {'bar': 2})]
  334. if expected_retval is not None:
  335. assert ret == expected_retval
  336. # clear cache
  337. assert len(self.calls) == 3
  338. obj.cache_clear()
  339. ret = obj()
  340. if expected_retval is not None:
  341. assert ret == expected_retval
  342. assert len(self.calls) == 4
  343. # docstring
  344. assert obj.__doc__ == "My docstring."
  345. def test_function(self):
  346. @memoize
  347. def foo(*args, **kwargs):
  348. """My docstring."""
  349. baseclass.calls.append((args, kwargs))
  350. return 22
  351. baseclass = self
  352. self.run_against(foo, expected_retval=22)
  353. def test_class(self):
  354. @memoize
  355. class Foo:
  356. """My docstring."""
  357. def __init__(self, *args, **kwargs):
  358. baseclass.calls.append((args, kwargs))
  359. def bar(self):
  360. return 22
  361. baseclass = self
  362. self.run_against(Foo, expected_retval=None)
  363. assert Foo().bar() == 22
  364. def test_class_singleton(self):
  365. # @memoize can be used against classes to create singletons
  366. @memoize
  367. class Bar:
  368. def __init__(self, *args, **kwargs):
  369. pass
  370. assert Bar() is Bar()
  371. assert id(Bar()) == id(Bar())
  372. assert id(Bar(1)) == id(Bar(1))
  373. assert id(Bar(1, foo=3)) == id(Bar(1, foo=3))
  374. assert id(Bar(1)) != id(Bar(2))
  375. def test_staticmethod(self):
  376. class Foo:
  377. @staticmethod
  378. @memoize
  379. def bar(*args, **kwargs):
  380. """My docstring."""
  381. baseclass.calls.append((args, kwargs))
  382. return 22
  383. baseclass = self
  384. self.run_against(Foo().bar, expected_retval=22)
  385. def test_classmethod(self):
  386. class Foo:
  387. @classmethod
  388. @memoize
  389. def bar(cls, *args, **kwargs):
  390. """My docstring."""
  391. baseclass.calls.append((args, kwargs))
  392. return 22
  393. baseclass = self
  394. self.run_against(Foo().bar, expected_retval=22)
  395. def test_original(self):
  396. # This was the original test before I made it dynamic to test it
  397. # against different types. Keeping it anyway.
  398. @memoize
  399. def foo(*args, **kwargs):
  400. """Foo docstring."""
  401. calls.append(None)
  402. return (args, kwargs)
  403. calls = []
  404. # no args
  405. for _ in range(2):
  406. ret = foo()
  407. expected = ((), {})
  408. assert ret == expected
  409. assert len(calls) == 1
  410. # with args
  411. for _ in range(2):
  412. ret = foo(1)
  413. expected = ((1,), {})
  414. assert ret == expected
  415. assert len(calls) == 2
  416. # with args + kwargs
  417. for _ in range(2):
  418. ret = foo(1, bar=2)
  419. expected = ((1,), {'bar': 2})
  420. assert ret == expected
  421. assert len(calls) == 3
  422. # clear cache
  423. foo.cache_clear()
  424. ret = foo()
  425. expected = ((), {})
  426. assert ret == expected
  427. assert len(calls) == 4
  428. # docstring
  429. assert foo.__doc__ == "Foo docstring."
  430. class TestCommonModule(PsutilTestCase):
  431. def test_memoize_when_activated(self):
  432. class Foo:
  433. @memoize_when_activated
  434. def foo(self):
  435. calls.append(None)
  436. f = Foo()
  437. calls = []
  438. f.foo()
  439. f.foo()
  440. assert len(calls) == 2
  441. # activate
  442. calls = []
  443. f.foo.cache_activate(f)
  444. f.foo()
  445. f.foo()
  446. assert len(calls) == 1
  447. # deactivate
  448. calls = []
  449. f.foo.cache_deactivate(f)
  450. f.foo()
  451. f.foo()
  452. assert len(calls) == 2
  453. def test_parse_environ_block(self):
  454. def k(s):
  455. return s.upper() if WINDOWS else s
  456. assert parse_environ_block("a=1\0") == {k("a"): "1"}
  457. assert parse_environ_block("a=1\0b=2\0\0") == {
  458. k("a"): "1",
  459. k("b"): "2",
  460. }
  461. assert parse_environ_block("a=1\0b=\0\0") == {k("a"): "1", k("b"): ""}
  462. # ignore everything after \0\0
  463. assert parse_environ_block("a=1\0b=2\0\0c=3\0") == {
  464. k("a"): "1",
  465. k("b"): "2",
  466. }
  467. # ignore everything that is not an assignment
  468. assert parse_environ_block("xxx\0a=1\0") == {k("a"): "1"}
  469. assert parse_environ_block("a=1\0=b=2\0") == {k("a"): "1"}
  470. # do not fail if the block is incomplete
  471. assert parse_environ_block("a=1\0b=2") == {k("a"): "1"}
  472. def test_supports_ipv6(self):
  473. self.addCleanup(supports_ipv6.cache_clear)
  474. if supports_ipv6():
  475. with mock.patch('psutil._common.socket') as s:
  476. s.has_ipv6 = False
  477. supports_ipv6.cache_clear()
  478. assert not supports_ipv6()
  479. supports_ipv6.cache_clear()
  480. with mock.patch(
  481. 'psutil._common.socket.socket', side_effect=OSError
  482. ) as s:
  483. assert not supports_ipv6()
  484. assert s.called
  485. supports_ipv6.cache_clear()
  486. with mock.patch(
  487. 'psutil._common.socket.socket', side_effect=socket.gaierror
  488. ) as s:
  489. assert not supports_ipv6()
  490. supports_ipv6.cache_clear()
  491. assert s.called
  492. supports_ipv6.cache_clear()
  493. with mock.patch(
  494. 'psutil._common.socket.socket.bind',
  495. side_effect=socket.gaierror,
  496. ) as s:
  497. assert not supports_ipv6()
  498. supports_ipv6.cache_clear()
  499. assert s.called
  500. else:
  501. with pytest.raises(OSError):
  502. sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
  503. try:
  504. sock.bind(("::1", 0))
  505. finally:
  506. sock.close()
  507. def test_isfile_strict(self):
  508. this_file = os.path.abspath(__file__)
  509. assert isfile_strict(this_file)
  510. assert not isfile_strict(os.path.dirname(this_file))
  511. with mock.patch('psutil._common.os.stat', side_effect=PermissionError):
  512. with pytest.raises(OSError):
  513. isfile_strict(this_file)
  514. with mock.patch(
  515. 'psutil._common.os.stat', side_effect=FileNotFoundError
  516. ):
  517. assert not isfile_strict(this_file)
  518. with mock.patch('psutil._common.stat.S_ISREG', return_value=False):
  519. assert not isfile_strict(this_file)
  520. def test_debug(self):
  521. with mock.patch.object(psutil._common, "PSUTIL_DEBUG", True):
  522. with contextlib.redirect_stderr(io.StringIO()) as f:
  523. debug("hello")
  524. sys.stderr.flush()
  525. msg = f.getvalue()
  526. assert msg.startswith("psutil-debug"), msg
  527. assert "hello" in msg
  528. assert __file__.replace('.pyc', '.py') in msg
  529. # supposed to use repr(exc)
  530. with mock.patch.object(psutil._common, "PSUTIL_DEBUG", True):
  531. with contextlib.redirect_stderr(io.StringIO()) as f:
  532. debug(ValueError("this is an error"))
  533. msg = f.getvalue()
  534. assert "ignoring ValueError" in msg
  535. assert "'this is an error'" in msg
  536. # supposed to use str(exc), because of extra info about file name
  537. with mock.patch.object(psutil._common, "PSUTIL_DEBUG", True):
  538. with contextlib.redirect_stderr(io.StringIO()) as f:
  539. exc = OSError(2, "no such file")
  540. exc.filename = "/foo"
  541. debug(exc)
  542. msg = f.getvalue()
  543. assert "no such file" in msg
  544. assert "/foo" in msg
  545. def test_cat_bcat(self):
  546. testfn = self.get_testfn()
  547. with open(testfn, "w") as f:
  548. f.write("foo")
  549. assert cat(testfn) == "foo"
  550. assert bcat(testfn) == b"foo"
  551. with pytest.raises(FileNotFoundError):
  552. cat(testfn + '-invalid')
  553. with pytest.raises(FileNotFoundError):
  554. bcat(testfn + '-invalid')
  555. assert cat(testfn + '-invalid', fallback="bar") == "bar"
  556. assert bcat(testfn + '-invalid', fallback="bar") == "bar"
  557. # ===================================================================
  558. # --- Tests for wrap_numbers() function.
  559. # ===================================================================
  560. nt = collections.namedtuple('foo', 'a b c')
  561. class TestWrapNumbers(PsutilTestCase):
  562. def setUp(self):
  563. wrap_numbers.cache_clear()
  564. tearDown = setUp
  565. def test_first_call(self):
  566. input = {'disk1': nt(5, 5, 5)}
  567. assert wrap_numbers(input, 'disk_io') == input
  568. def test_input_hasnt_changed(self):
  569. input = {'disk1': nt(5, 5, 5)}
  570. assert wrap_numbers(input, 'disk_io') == input
  571. assert wrap_numbers(input, 'disk_io') == input
  572. def test_increase_but_no_wrap(self):
  573. input = {'disk1': nt(5, 5, 5)}
  574. assert wrap_numbers(input, 'disk_io') == input
  575. input = {'disk1': nt(10, 15, 20)}
  576. assert wrap_numbers(input, 'disk_io') == input
  577. input = {'disk1': nt(20, 25, 30)}
  578. assert wrap_numbers(input, 'disk_io') == input
  579. input = {'disk1': nt(20, 25, 30)}
  580. assert wrap_numbers(input, 'disk_io') == input
  581. def test_wrap(self):
  582. # let's say 100 is the threshold
  583. input = {'disk1': nt(100, 100, 100)}
  584. assert wrap_numbers(input, 'disk_io') == input
  585. # first wrap restarts from 10
  586. input = {'disk1': nt(100, 100, 10)}
  587. assert wrap_numbers(input, 'disk_io') == {'disk1': nt(100, 100, 110)}
  588. # then it remains the same
  589. input = {'disk1': nt(100, 100, 10)}
  590. assert wrap_numbers(input, 'disk_io') == {'disk1': nt(100, 100, 110)}
  591. # then it goes up
  592. input = {'disk1': nt(100, 100, 90)}
  593. assert wrap_numbers(input, 'disk_io') == {'disk1': nt(100, 100, 190)}
  594. # then it wraps again
  595. input = {'disk1': nt(100, 100, 20)}
  596. assert wrap_numbers(input, 'disk_io') == {'disk1': nt(100, 100, 210)}
  597. # and remains the same
  598. input = {'disk1': nt(100, 100, 20)}
  599. assert wrap_numbers(input, 'disk_io') == {'disk1': nt(100, 100, 210)}
  600. # now wrap another num
  601. input = {'disk1': nt(50, 100, 20)}
  602. assert wrap_numbers(input, 'disk_io') == {'disk1': nt(150, 100, 210)}
  603. # and again
  604. input = {'disk1': nt(40, 100, 20)}
  605. assert wrap_numbers(input, 'disk_io') == {'disk1': nt(190, 100, 210)}
  606. # keep it the same
  607. input = {'disk1': nt(40, 100, 20)}
  608. assert wrap_numbers(input, 'disk_io') == {'disk1': nt(190, 100, 210)}
  609. def test_changing_keys(self):
  610. # Emulate a case where the second call to disk_io()
  611. # (or whatever) provides a new disk, then the new disk
  612. # disappears on the third call.
  613. input = {'disk1': nt(5, 5, 5)}
  614. assert wrap_numbers(input, 'disk_io') == input
  615. input = {'disk1': nt(5, 5, 5), 'disk2': nt(7, 7, 7)}
  616. assert wrap_numbers(input, 'disk_io') == input
  617. input = {'disk1': nt(8, 8, 8)}
  618. assert wrap_numbers(input, 'disk_io') == input
  619. def test_changing_keys_w_wrap(self):
  620. input = {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 100)}
  621. assert wrap_numbers(input, 'disk_io') == input
  622. # disk 2 wraps
  623. input = {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 10)}
  624. assert wrap_numbers(input, 'disk_io') == {
  625. 'disk1': nt(50, 50, 50),
  626. 'disk2': nt(100, 100, 110),
  627. }
  628. # disk 2 disappears
  629. input = {'disk1': nt(50, 50, 50)}
  630. assert wrap_numbers(input, 'disk_io') == input
  631. # then it appears again; the old wrap is supposed to be
  632. # gone.
  633. input = {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 100)}
  634. assert wrap_numbers(input, 'disk_io') == input
  635. # remains the same
  636. input = {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 100)}
  637. assert wrap_numbers(input, 'disk_io') == input
  638. # and then wraps again
  639. input = {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 10)}
  640. assert wrap_numbers(input, 'disk_io') == {
  641. 'disk1': nt(50, 50, 50),
  642. 'disk2': nt(100, 100, 110),
  643. }
  644. def test_real_data(self):
  645. d = {
  646. 'nvme0n1': (300, 508, 640, 1571, 5970, 1987, 2049, 451751, 47048),
  647. 'nvme0n1p1': (1171, 2, 5600256, 1024, 516, 0, 0, 0, 8),
  648. 'nvme0n1p2': (54, 54, 2396160, 5165056, 4, 24, 30, 1207, 28),
  649. 'nvme0n1p3': (2389, 4539, 5154, 150, 4828, 1844, 2019, 398, 348),
  650. }
  651. assert wrap_numbers(d, 'disk_io') == d
  652. assert wrap_numbers(d, 'disk_io') == d
  653. # decrease this ↓
  654. d = {
  655. 'nvme0n1': (100, 508, 640, 1571, 5970, 1987, 2049, 451751, 47048),
  656. 'nvme0n1p1': (1171, 2, 5600256, 1024, 516, 0, 0, 0, 8),
  657. 'nvme0n1p2': (54, 54, 2396160, 5165056, 4, 24, 30, 1207, 28),
  658. 'nvme0n1p3': (2389, 4539, 5154, 150, 4828, 1844, 2019, 398, 348),
  659. }
  660. out = wrap_numbers(d, 'disk_io')
  661. assert out['nvme0n1'][0] == 400
  662. # --- cache tests
  663. def test_cache_first_call(self):
  664. input = {'disk1': nt(5, 5, 5)}
  665. wrap_numbers(input, 'disk_io')
  666. cache = wrap_numbers.cache_info()
  667. assert cache[0] == {'disk_io': input}
  668. assert cache[1] == {'disk_io': {}}
  669. assert cache[2] == {'disk_io': {}}
  670. def test_cache_call_twice(self):
  671. input = {'disk1': nt(5, 5, 5)}
  672. wrap_numbers(input, 'disk_io')
  673. input = {'disk1': nt(10, 10, 10)}
  674. wrap_numbers(input, 'disk_io')
  675. cache = wrap_numbers.cache_info()
  676. assert cache[0] == {'disk_io': input}
  677. assert cache[1] == {
  678. 'disk_io': {('disk1', 0): 0, ('disk1', 1): 0, ('disk1', 2): 0}
  679. }
  680. assert cache[2] == {'disk_io': {}}
  681. def test_cache_wrap(self):
  682. # let's say 100 is the threshold
  683. input = {'disk1': nt(100, 100, 100)}
  684. wrap_numbers(input, 'disk_io')
  685. # first wrap restarts from 10
  686. input = {'disk1': nt(100, 100, 10)}
  687. wrap_numbers(input, 'disk_io')
  688. cache = wrap_numbers.cache_info()
  689. assert cache[0] == {'disk_io': input}
  690. assert cache[1] == {
  691. 'disk_io': {('disk1', 0): 0, ('disk1', 1): 0, ('disk1', 2): 100}
  692. }
  693. assert cache[2] == {'disk_io': {'disk1': {('disk1', 2)}}}
  694. def check_cache_info():
  695. cache = wrap_numbers.cache_info()
  696. assert cache[1] == {
  697. 'disk_io': {
  698. ('disk1', 0): 0,
  699. ('disk1', 1): 0,
  700. ('disk1', 2): 100,
  701. }
  702. }
  703. assert cache[2] == {'disk_io': {'disk1': {('disk1', 2)}}}
  704. # then it remains the same
  705. input = {'disk1': nt(100, 100, 10)}
  706. wrap_numbers(input, 'disk_io')
  707. cache = wrap_numbers.cache_info()
  708. assert cache[0] == {'disk_io': input}
  709. check_cache_info()
  710. # then it goes up
  711. input = {'disk1': nt(100, 100, 90)}
  712. wrap_numbers(input, 'disk_io')
  713. cache = wrap_numbers.cache_info()
  714. assert cache[0] == {'disk_io': input}
  715. check_cache_info()
  716. # then it wraps again
  717. input = {'disk1': nt(100, 100, 20)}
  718. wrap_numbers(input, 'disk_io')
  719. cache = wrap_numbers.cache_info()
  720. assert cache[0] == {'disk_io': input}
  721. assert cache[1] == {
  722. 'disk_io': {('disk1', 0): 0, ('disk1', 1): 0, ('disk1', 2): 190}
  723. }
  724. assert cache[2] == {'disk_io': {'disk1': {('disk1', 2)}}}
  725. def test_cache_changing_keys(self):
  726. input = {'disk1': nt(5, 5, 5)}
  727. wrap_numbers(input, 'disk_io')
  728. input = {'disk1': nt(5, 5, 5), 'disk2': nt(7, 7, 7)}
  729. wrap_numbers(input, 'disk_io')
  730. cache = wrap_numbers.cache_info()
  731. assert cache[0] == {'disk_io': input}
  732. assert cache[1] == {
  733. 'disk_io': {('disk1', 0): 0, ('disk1', 1): 0, ('disk1', 2): 0}
  734. }
  735. assert cache[2] == {'disk_io': {}}
  736. def test_cache_clear(self):
  737. input = {'disk1': nt(5, 5, 5)}
  738. wrap_numbers(input, 'disk_io')
  739. wrap_numbers(input, 'disk_io')
  740. wrap_numbers.cache_clear('disk_io')
  741. assert wrap_numbers.cache_info() == ({}, {}, {})
  742. wrap_numbers.cache_clear('disk_io')
  743. wrap_numbers.cache_clear('?!?')
  744. @pytest.mark.skipif(not HAS_NET_IO_COUNTERS, reason="not supported")
  745. def test_cache_clear_public_apis(self):
  746. if not psutil.disk_io_counters() or not psutil.net_io_counters():
  747. raise pytest.skip("no disks or NICs available")
  748. psutil.disk_io_counters()
  749. psutil.net_io_counters()
  750. caches = wrap_numbers.cache_info()
  751. for cache in caches:
  752. assert 'psutil.disk_io_counters' in cache
  753. assert 'psutil.net_io_counters' in cache
  754. psutil.disk_io_counters.cache_clear()
  755. caches = wrap_numbers.cache_info()
  756. for cache in caches:
  757. assert 'psutil.net_io_counters' in cache
  758. assert 'psutil.disk_io_counters' not in cache
  759. psutil.net_io_counters.cache_clear()
  760. caches = wrap_numbers.cache_info()
  761. assert caches == ({}, {}, {})