From 9ae8920303321da416ba1570d2a386188e93a271 Mon Sep 17 00:00:00 2001 From: Ben Raz Date: Thu, 15 Sep 2022 21:07:50 +0300 Subject: [PATCH] Fix ifconfig flags tests, run in Python 3 CI - Python 2 CI image has an old net-tools (hence ifconfig) version with a non-easily-parsable flags format, making tests fail. - Python 3 musllinux CI image has a busybox-provided ifconfig with the same format as old net-tools. - Python 3 manylinux CI image does not have net-tools (hence ifconfig) pre-installed, so tests using it were skippied altogether. Signed-off-by: Ben Raz --- .github/workflows/build.yml | 3 ++- psutil/tests/test_linux.py | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cb4bf4c4f4..696dea1425 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,7 +33,8 @@ jobs: include: - {name: Linux, python: '3.9', os: ubuntu-latest} env: - CIBW_BEFORE_ALL_LINUX: command -v apk && apk --no-cache add coreutils procps || true + CIBW_BEFORE_ALL_LINUX: command -v apk && apk --no-cache add coreutils procps || + (yum install -y net-tools && yum clean all) CIBW_TEST_COMMAND: PYTHONWARNINGS=always PYTHONUNBUFFERED=1 PSUTIL_DEBUG=1 python {project}/psutil/tests/runner.py && PYTHONWARNINGS=always PYTHONUNBUFFERED=1 PSUTIL_DEBUG=1 python {project}/psutil/tests/test_memleaks.py diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index feec0a59f7..12536bc9a2 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -204,6 +204,18 @@ def get_free_version_info(): return tuple(map(int, out.split()[-1].split('.'))) +def ifconfig_has_flags(): + """ + Parse 'ifconfig' command output and determine whether + it exposes easily-parsable flags for interfaces or not. + """ + if not which('ifconfig'): + return False + + out = sh(['ifconfig']).strip() + return 'flags=' in out + + @contextlib.contextmanager def mock_open_content(for_path, content): """Mock open() builtin and forces it to return a certain `content` @@ -993,7 +1005,10 @@ def test_mtu(self): with open("/sys/class/net/%s/mtu" % name, "rt") as f: self.assertEqual(stats.mtu, int(f.read().strip())) - @unittest.skipIf(not which("ifconfig"), "ifconfig utility not available") + @unittest.skipIf(not which("ifconfig"), + "ifconfig utility not available") + @unittest.skipIf(not ifconfig_has_flags(), + "ifconfig utility doesn't expose flags in a known format") def test_flags(self): # first line looks like this: # "eth0: flags=4163 mtu 1500" @@ -1023,7 +1038,7 @@ class TestSystemNetIOCounters(PsutilTestCase): def test_against_ifconfig(self): def ifconfig(nic): ret = {} - out = sh("ifconfig %s" % name) + out = sh("ifconfig %s" % nic) ret['packets_recv'] = int( re.findall(r'RX packets[: ](\d+)', out)[0]) ret['packets_sent'] = int(