Skip to content

Commit

Permalink
Fix ifconfig flags tests, run in Python 3 CI
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
ben9923 committed Sep 15, 2022
1 parent 5c55456 commit 9ae8920
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions psutil/tests/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500"
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 9ae8920

Please sign in to comment.