Skip to content

Commit

Permalink
[testing] add skipUnlessHasBinaries decorator
Browse files Browse the repository at this point in the history
Some tests are currently allowed to fail with @mayFail because some binaries may be missing.
The problem is that we cannot differenciate between a test failing because some tooling is missing,
or tests legitimately failing.

This diff creates a new decorator that will skip a test if some binaries are not
present, therefore, if the binaries are there and there is any issue, CI will fail.

It will generate a message similar to:
```
docker run -ti \
                    --privileged \
                    --network=host \
                    --pid=host \
                    -v $(pwd):/bcc \
                    -v /sys/kernel/debug:/sys/kernel/debug:rw \
                    -v /lib/modules:/lib/modules:ro \
                    -v /usr/src:/usr/src:ro \
                    -e CTEST_OUTPUT_ON_FAILURE=1 \
                    u34 \
                    /bin/bash -c \
                    '/bcc/build/tests/wrapper.sh "py_test_percpu" "sudo" "/bcc/tests/python/test_brb.py" test_brb.c -v'
test_brb (__main__.TestBPFSocket) ... skipped 'Missing binaries: neperf, neterver. iperf and netperf packages must be installed.'

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK (skipped=1)
```
  • Loading branch information
chantra authored and yonghong-song committed Sep 11, 2022
1 parent ad4c8e3 commit 442f420
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from distutils.spawn import find_executable
import traceback
import distutils.version
import shutil

import logging, os, sys

Expand Down Expand Up @@ -51,6 +52,23 @@ def wrapper(*args, **kwargs):
return wrapper
return decorator

# This is a decorator that will skip tests if any binary in the list is not in PATH.
def skipUnlessHasBinaries(binaries, message):
def decorator(func):
def wrapper(self, *args, **kwargs):
missing = []
for binary in binaries:
if shutil.which(binary) is None:
missing.append(binary)

if len(missing):
missing_binaries = ", ".join(missing)
self.skipTest(f"Missing binaries: {missing_binaries}. {message}")
else:
func(self, *args, **kwargs)
return wrapper
return decorator

class NSPopenWithCheck(NSPopen):
"""
A wrapper for NSPopen that additionally checks if the program
Expand Down

0 comments on commit 442f420

Please sign in to comment.