From 442f420d0189cc3e53a05b13d7633e0b624a20c1 Mon Sep 17 00:00:00 2001 From: chantra Date: Thu, 1 Sep 2022 23:29:29 +0000 Subject: [PATCH] [testing] add skipUnlessHasBinaries decorator 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) ``` --- tests/python/utils.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/python/utils.py b/tests/python/utils.py index b1a7d263801c..40e7b157dc33 100644 --- a/tests/python/utils.py +++ b/tests/python/utils.py @@ -2,6 +2,7 @@ from distutils.spawn import find_executable import traceback import distutils.version +import shutil import logging, os, sys @@ -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