From 3cec63006e0f3f3498862310268d2ca6e21af39d Mon Sep 17 00:00:00 2001 From: Adrian Page Date: Sat, 28 Oct 2017 18:37:07 +0100 Subject: Fix network tests for newer ifconfig versions. (#1160) Arch Linux and Ubuntu 17.10 use a newer ifconfig version than other distributions and that changes the statistics output text formatting, causing the following tests to fail: psutil.tests.test_linux.TestSystemNetwork.test_net_if_stats psutil.tests.test_linux.TestSystemNetwork.test_net_io_counters MTU becomes lower case, colons are replaced with spaces, and packets and bytes are on the same line. Example ifconfig output: enp2s0f0: flags=4163 mtu 1500 ether a8:20:66:04:4c:45 txqueuelen 1000 (Ethernet) RX packets 1396351 bytes 1928499072 (1.7 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 750492 bytes 185338978 (176.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device interrupt 16 --- psutil/tests/test_linux.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'psutil/tests/test_linux.py') diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 468b3c66..7bb37a8d 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -762,21 +762,25 @@ class TestSystemNetwork(unittest.TestCase): # Not always reliable. # self.assertEqual(stats.isup, 'RUNNING' in out, msg=out) self.assertEqual(stats.mtu, - int(re.findall(r'MTU:(\d+)', out)[0])) + int(re.findall(r'(?i)MTU[: ](\d+)', out)[0])) @retry_before_failing() def test_net_io_counters(self): def ifconfig(nic): ret = {} out = sh("ifconfig %s" % name) - ret['packets_recv'] = int(re.findall(r'RX packets:(\d+)', out)[0]) - ret['packets_sent'] = int(re.findall(r'TX packets:(\d+)', out)[0]) - ret['errin'] = int(re.findall(r'errors:(\d+)', out)[0]) - ret['errout'] = int(re.findall(r'errors:(\d+)', out)[1]) - ret['dropin'] = int(re.findall(r'dropped:(\d+)', out)[0]) - ret['dropout'] = int(re.findall(r'dropped:(\d+)', out)[1]) - ret['bytes_recv'] = int(re.findall(r'RX bytes:(\d+)', out)[0]) - ret['bytes_sent'] = int(re.findall(r'TX bytes:(\d+)', out)[0]) + ret['packets_recv'] = int( + re.findall(r'RX packets[: ](\d+)', out)[0]) + ret['packets_sent'] = int( + re.findall(r'TX packets[: ](\d+)', out)[0]) + ret['errin'] = int(re.findall(r'errors[: ](\d+)', out)[0]) + ret['errout'] = int(re.findall(r'errors[: ](\d+)', out)[1]) + ret['dropin'] = int(re.findall(r'dropped[: ](\d+)', out)[0]) + ret['dropout'] = int(re.findall(r'dropped[: ](\d+)', out)[1]) + ret['bytes_recv'] = int( + re.findall(r'RX (?:packets \d+ +)?bytes[: ](\d+)', out)[0]) + ret['bytes_sent'] = int( + re.findall(r'TX (?:packets \d+ +)?bytes[: ](\d+)', out)[0]) return ret nio = psutil.net_io_counters(pernic=True, nowrap=False) -- cgit v1.2.1 From fe53d1a582a8174204ae08b5e301a3dcfdf09901 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 29 Oct 2017 15:51:44 -0700 Subject: Fix test_emulate_energy_full_not_avail (#1163) The value may come from two different files, must mock both. --- psutil/tests/test_linux.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'psutil/tests/test_linux.py') diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 7bb37a8d..4658dd21 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -1326,7 +1326,9 @@ class TestSensorsBattery(unittest.TestCase): # Emulate a case where energy_full file does not exist. # Expected fallback on /capacity. def open_mock(name, *args, **kwargs): - if name.startswith("/sys/class/power_supply/BAT0/energy_full"): + energy_full = "/sys/class/power_supply/BAT0/energy_full" + charge_full = "/sys/class/power_supply/BAT0/charge_full" + if name.startswith(energy_full) or name.startswith(charge_full): raise IOError(errno.ENOENT, "") elif name.startswith("/sys/class/power_supply/BAT0/capacity"): return io.BytesIO(b"88") -- cgit v1.2.1 From 02e25d765040d3f36ecc66249700a8777b175c7a Mon Sep 17 00:00:00 2001 From: wiggin15 Date: Sat, 11 Nov 2017 03:34:22 +0200 Subject: skip cpu_freq tests if not available (#1170) cpu_freq is not always available on Linux --- psutil/tests/test_linux.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'psutil/tests/test_linux.py') diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 4658dd21..71d428c3 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -29,6 +29,7 @@ from psutil._compat import PY3 from psutil._compat import u from psutil.tests import call_until from psutil.tests import HAS_BATTERY +from psutil.tests import HAS_CPU_FREQ from psutil.tests import HAS_RLIMIT from psutil.tests import MEMORY_TOLERANCE from psutil.tests import mock @@ -607,11 +608,13 @@ class TestSystemCPU(unittest.TestCase): self.assertIsNone(psutil._pslinux.cpu_count_physical()) assert m.called + @unittest.skipIf(not HAS_CPU_FREQ, "not supported") def test_cpu_freq_no_result(self): with mock.patch("psutil._pslinux.glob.glob", return_value=[]): self.assertIsNone(psutil.cpu_freq()) @unittest.skipIf(TRAVIS, "fails on Travis") + @unittest.skipIf(not HAS_CPU_FREQ, "not supported") def test_cpu_freq_use_second_file(self): # https://github.com/giampaolo/psutil/issues/981 def glob_mock(pattern): @@ -629,6 +632,7 @@ class TestSystemCPU(unittest.TestCase): assert psutil.cpu_freq() self.assertEqual(len(flags), 2) + @unittest.skipIf(not HAS_CPU_FREQ, "not supported") def test_cpu_freq_emulate_data(self): def open_mock(name, *args, **kwargs): if name.endswith('/scaling_cur_freq'): @@ -651,6 +655,7 @@ class TestSystemCPU(unittest.TestCase): self.assertEqual(freq.min, 600.0) self.assertEqual(freq.max, 700.0) + @unittest.skipIf(not HAS_CPU_FREQ, "not supported") def test_cpu_freq_emulate_multi_cpu(self): def open_mock(name, *args, **kwargs): if name.endswith('/scaling_cur_freq'): @@ -675,6 +680,7 @@ class TestSystemCPU(unittest.TestCase): self.assertEqual(freq.max, 300.0) @unittest.skipIf(TRAVIS, "fails on Travis") + @unittest.skipIf(not HAS_CPU_FREQ, "not supported") def test_cpu_freq_no_scaling_cur_freq_file(self): # See: https://github.com/giampaolo/psutil/issues/1071 def open_mock(name, *args, **kwargs): -- cgit v1.2.1 From 7c6b6c2a6d89a1a270d6357be3b77fd8e0e2cbe7 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Tue, 28 Nov 2017 11:10:42 +0100 Subject: fix #1179 / linux / cmdline: handle processes erroneously overwriting /proc/pid/cmdline by using spaces instead of null bytes as args separator --- psutil/tests/test_linux.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'psutil/tests/test_linux.py') diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 71d428c3..6ba17b25 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -1585,6 +1585,20 @@ class TestProcess(unittest.TestCase): self.assertEqual(p.cmdline(), ['foo', 'bar', '']) assert m.called + def test_cmdline_spaces_mocked(self): + # see: https://github.com/giampaolo/psutil/issues/1179 + p = psutil.Process() + fake_file = io.StringIO(u('foo bar ')) + with mock.patch('psutil._pslinux.open', + return_value=fake_file, create=True) as m: + self.assertEqual(p.cmdline(), ['foo', 'bar']) + assert m.called + fake_file = io.StringIO(u('foo bar ')) + with mock.patch('psutil._pslinux.open', + return_value=fake_file, create=True) as m: + self.assertEqual(p.cmdline(), ['foo', 'bar', '']) + assert m.called + def test_readlink_path_deleted_mocked(self): with mock.patch('psutil._pslinux.os.readlink', return_value='/home/foo (deleted)'): -- cgit v1.2.1