]> git.proxmox.com Git - mirror_frr.git/blobdiff - tests/topotests/lib/lutil.py
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / topotests / lib / lutil.py
index f8f580632e7175f78a0605086a9dc5861ebeef41..9aef41cd1cd7b5061a027f24c13407ea11dbd5cc 100644 (file)
@@ -1,32 +1,17 @@
 #!/usr/bin/env python
+# SPDX-License-Identifier: GPL-2.0-or-later
 
 # Copyright 2017, LabN Consulting, L.L.C.
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; see the file COPYING; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
 import os
 import re
 import sys
 import time
-import datetime
 import json
 import math
 import time
 from lib.topolog import logger
 from lib.topotest import json_cmp
-from mininet.net import Mininet
 
 
 # L utility functions
@@ -52,6 +37,7 @@ class lUtil:
     l_line = 0
     l_dotall_experiment = False
     l_last_nl = None
+    l_wait_strict = 1
 
     fout = ""
     fsum = ""
@@ -191,11 +177,8 @@ Total %-4d                                                           %-4d %d\n\
             self.log("unable to read: " + tstFile)
             sys.exit(1)
 
-    def command(self, target, command, regexp, op, result, returnJson):
+    def command(self, target, command, regexp, op, result, returnJson, startt=None, force_result=False):
         global net
-        if op != "wait":
-            self.l_line += 1
-
         if op == "jsoncmp_pass" or op == "jsoncmp_fail":
             returnJson = True
 
@@ -296,7 +279,11 @@ Total %-4d                                                           %-4d %d\n\
                     % (group_nl_converted, ret),
                     9,
                 )
-        if op == "pass" or op == "fail":
+        if startt != None:
+            if js != None or ret is not False or force_result is not False:
+                delta = time.time() - startt
+                self.result(target, success, "%s +%4.2f secs" % (result, delta))
+        elif op == "pass" or op == "fail":
             self.result(target, success, result)
         if js != None:
             return js
@@ -323,12 +310,23 @@ Total %-4d                                                           %-4d %d\n\
         n = 0
         startt = time.time()
 
+        if (op == "wait-strict") or ((op == "wait") and self.l_wait_strict):
+            strict = True
+        else:
+            strict = False
+
         # Calculate the amount of `sleep`s we are going to peform.
         wait_count = int(math.ceil(wait / wait_time)) + 1
 
+        force_result = False
         while wait_count > 0:
             n += 1
-            found = self.command(target, command, regexp, op, result, returnJson)
+
+            # log a failure on last iteration if we don't get desired regexp
+            if strict and (wait_count == 1):
+                force_result = True
+
+            found = self.command(target, command, regexp, op, result, returnJson, startt, force_result)
             if found is not False:
                 break
 
@@ -338,14 +336,6 @@ Total %-4d                                                           %-4d %d\n\
 
         delta = time.time() - startt
         self.log("Done after %d loops, time=%s, Found=%s" % (n, delta, found))
-        found = self.command(
-            target,
-            command,
-            regexp,
-            "pass",
-            "%s +%4.2f secs" % (result, delta),
-            returnJson,
-        )
         return found
 
 
@@ -387,12 +377,14 @@ def luCommand(
     returnJson=False,
     wait_time=0.5,
 ):
-    if op != "wait":
-        return LUtil.command(target, command, regexp, op, result, returnJson)
-    else:
+    waitops = ["wait", "wait-strict", "wait-nostrict"]
+
+    if op in waitops:
         return LUtil.wait(
             target, command, regexp, op, result, time, returnJson, wait_time
         )
+    else:
+        return LUtil.command(target, command, regexp, op, result, returnJson)
 
 
 def luLast(usenl=False):
@@ -463,6 +455,25 @@ def luShowFail():
     if printed > 0:
         logger.error("See %s for details of errors" % LUtil.fout_name)
 
+#
+# Sets default wait type for luCommand(op="wait) (may be overridden by
+# specifying luCommand(op="wait-strict") or luCommand(op="wait-nostrict")).
+#
+# "nostrict" is the historical default behavior, which is to ignore
+# failures to match the specified regexp in the specified time.
+#
+# "strict" means that failure to match the specified regexp in the
+# specified time yields an explicit, logged failure result
+#
+def luSetWaitType(waittype):
+    if waittype == "strict":
+        LUtil.l_wait_strict = 1
+    else:
+        if waittype == "nostrict":
+            LUtil.l_wait_strict = 0
+        else:
+            raise ValueError('waittype must be one of "strict" or "nostrict"')
+
 
 # for testing
 if __name__ == "__main__":