From a5d5b63d61a9164fa7bd5037a6ea0c7d9b02a4de Mon Sep 17 00:00:00 2001 From: Benjamin Berg Date: Tue, 21 Nov 2023 01:51:49 +0200 Subject: [PATCH] tests: Allow specifying multiple failure locations Having the ability to trigger multiple failures in one test can be useful. Add support to the test infrastructure to do this. Signed-off-by: Benjamin Berg --- tests/hwsim/utils.py | 45 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/tests/hwsim/utils.py b/tests/hwsim/utils.py index 769f824c7..d572bdc4c 100644 --- a/tests/hwsim/utils.py +++ b/tests/hwsim/utils.py @@ -35,33 +35,38 @@ def long_duration_test(func): func.long_duration_test = True return func -class alloc_fail(object): - def __init__(self, dev, count, funcs): - self._dev = dev - self._count = count - self._funcs = funcs - def __enter__(self): - cmd = "TEST_ALLOC_FAIL %d:%s" % (self._count, self._funcs) - if "OK" not in self._dev.request(cmd): - raise HwsimSkip("TEST_ALLOC_FAIL not supported") - def __exit__(self, type, value, traceback): - if type is None: - if self._dev.request("GET_ALLOC_FAIL") != "0:%s" % self._funcs: - raise Exception("Allocation failure did not trigger") - class fail_test(object): - def __init__(self, dev, count, funcs): + _test_fail = 'TEST_FAIL' + _get_fail = 'GET_FAIL' + + def __init__(self, dev, count, funcs, *args): self._dev = dev - self._count = count - self._funcs = funcs + self._funcs = [(count, funcs)] + + args = list(args) + while args: + count = args.pop(0) + funcs = args.pop(0) + self._funcs.append((count, funcs)) def __enter__(self): - cmd = "TEST_FAIL %d:%s" % (self._count, self._funcs) + patterns = ' '.join(['%d:%s' % (c, f) for c, f in self._funcs]) + cmd = '%s %s' % (self._test_fail, patterns) if "OK" not in self._dev.request(cmd): raise HwsimSkip("TEST_FAIL not supported") def __exit__(self, type, value, traceback): + pending = self._dev.request(self._get_fail) if type is None: - if self._dev.request("GET_FAIL") != "0:%s" % self._funcs: - raise Exception("Test failure did not trigger") + expected = ' '.join(['0:%s' % f for c, f in self._funcs]) + if pending != expected: + # Ensure the failure cannot trigger in the future + self._dev.request('%s 0:' % self._test_fail) + raise Exception("Not all failures triggered (pending: %s)" % pending) + else: + logger.info("Pending failures at time of exception: %s" % pending) + +class alloc_fail(fail_test): + _test_fail = 'TEST_ALLOC_FAIL' + _get_fail = 'GET_ALLOC_FAIL' def wait_fail_trigger(dev, cmd, note="Failure not triggered", max_iter=40, timeout=0.05):