@@ -336,18 +336,48 @@ def get_checklogs(self):
336336 return {"all" : [], "none" : []}
337337
338338 def check_logs (self ):
339+ logs = self .container .logs ().decode ("UTF-8" )
339340 self .log .info (self .checklogs )
340- for regex in self .checklogs ["all" ]:
341+ for rule in self .checklogs ["all" ]:
341342 try :
342- if not re .search (regex , self .container .logs ().decode ('UTF-8' )):
343- self .log .error (f"logs check failed for { regex } ; should match" )
344- return False
343+ if isinstance (rule , dict ):
344+ pattern = rule .get ("expr" , "" )
345+ expected = rule .get ("cnt" , None )
346+
347+ if not pattern :
348+ self .log .error ("logs check: empty pattern in rule" )
349+ return False
350+
351+ matches = re .findall (pattern , logs )
352+ count = len (matches )
353+
354+ if expected is not None and count != expected :
355+ self .log .error (
356+ f"logs check failed for { pattern } ; "
357+ f"expected { expected } matches, got { count } "
358+ )
359+ return False
360+ elif expected is None and count == 0 :
361+ self .log .error (
362+ f"logs check failed for { pattern } ; should match"
363+ )
364+ return False
365+ else :
366+ pattern = rule
367+ if not re .search (pattern , logs ):
368+ self .log .error (
369+ f"logs check failed for { pattern } ; should match"
370+ )
371+ return False
372+
345373 except Exception as e :
346374 self .log .error (f"error while checking logs: { e } " )
347375 return False
376+
377+
348378 for regex in self .checklogs ["none" ]:
349379 try :
350- if re .search (regex , self . container . logs (). decode ( 'UTF-8' ) ):
380+ if re .search (regex , logs ):
351381 self .log .error (f"logs check failed for { regex } ; shouldn't match" )
352382 return False
353383 except Exception as e :
0 commit comments