]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
tests: Allow specifying multiple failure locations
authorBenjamin Berg <benjamin.berg@intel.com>
Mon, 20 Nov 2023 23:51:49 +0000 (01:51 +0200)
committerJouni Malinen <j@w1.fi>
Sun, 26 Nov 2023 10:56:03 +0000 (12:56 +0200)
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 <benjamin.berg@intel.com>
tests/hwsim/utils.py

index 769f824c7be6ed534fefe412a7916ef9d74b8275..d572bdc4c9acdcf2e6841edc6bfaef85c598bcb5 100644 (file)
@@ -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):