]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add LogFile helper
authorTom Krizek <tkrizek@isc.org>
Fri, 22 Dec 2023 09:55:17 +0000 (10:55 +0100)
committerMichał Kępień <michal@isc.org>
Fri, 22 Dec 2023 14:02:58 +0000 (15:02 +0100)
LogFile class contains a log path and has means to find a string in the
log file.

bin/tests/system/isctest/__init__.py
bin/tests/system/isctest/instance.py
bin/tests/system/isctest/log.py [moved from bin/tests/system/isctest/watchlog.py with 91% similarity]

index a55c1280079977922b7a90f6a0058aacc0d6e67d..3ea82a3f455c0bd75f2211f3208a17682fe75720 100644 (file)
@@ -13,4 +13,4 @@ from . import check
 from . import instance
 from . import query
 from . import rndc
-from . import watchlog
+from . import log
index fdac94c7d2fd0f7ba3237cee7718ff66cc453225..ede48c095c531e570e4530a3c50d23703d76ee9f 100644 (file)
@@ -18,7 +18,7 @@ import os
 import re
 
 from .rndc import RNDCBinaryExecutor, RNDCException, RNDCExecutor
-from .watchlog import WatchLogFromStart, WatchLogFromHere
+from .log import LogFile, WatchLogFromStart, WatchLogFromHere
 
 
 class NamedPorts(NamedTuple):
@@ -63,7 +63,7 @@ class NamedInstance:
         """
         self.ip = self._identifier_to_ip(identifier)
         self.ports = ports
-        self._log_file = os.path.join(identifier, "named.run")
+        self.log = LogFile(os.path.join(identifier, "named.run"))
         self._rndc_executor = rndc_executor or RNDCBinaryExecutor()
         self._rndc_logger = rndc_logger or logging.getLogger()
 
@@ -133,14 +133,14 @@ class NamedInstance:
         Return an instance of the `WatchLogFromStart` context manager for this
         `named` instance's log file.
         """
-        return WatchLogFromStart(self._log_file)
+        return WatchLogFromStart(self.log.path)
 
     def watch_log_from_here(self) -> WatchLogFromHere:
         """
         Return an instance of the `WatchLogFromHere` context manager for this
         `named` instance's log file.
         """
-        return WatchLogFromHere(self._log_file)
+        return WatchLogFromHere(self.log.path)
 
     def reconfigure(self) -> None:
         """
similarity index 91%
rename from bin/tests/system/isctest/watchlog.py
rename to bin/tests/system/isctest/log.py
index 1456c535c93ddee8a9e1a03ca1bb85d97343d627..38c11438217046d12c501d46374097e5924c3e18 100644 (file)
@@ -9,7 +9,7 @@
 # See the COPYRIGHT file distributed with this work for additional
 # information regarding copyright ownership.
 
-from typing import Optional, TextIO, Dict, Any, overload, List, Union
+from typing import Iterator, Optional, TextIO, Dict, Any, overload, List, Union
 
 import abc
 import os
@@ -22,6 +22,40 @@ class WatchLogException(Exception):
     pass
 
 
+class LogFile:
+    """
+    Log file wrapper with a path and means to find a string in its contents.
+    """
+
+    def __init__(self, path: str):
+        self.path = path
+
+    @property
+    def _lines(self) -> Iterator[str]:
+        with open(self.path, encoding="utf-8") as f:
+            yield from f
+
+    def __contains__(self, substring: str) -> bool:
+        """
+        Return whether any of the lines in the log contains a given string.
+        """
+        for line in self._lines:
+            if substring in line:
+                return True
+        return False
+
+    def expect(self, msg: str):
+        """Check the string is present anywhere in the log file."""
+        if msg in self:
+            return
+        assert False, f"log message not found in log {self.path}: {msg}"
+
+    def prohibit(self, msg: str):
+        """Check the string is not present in the entire log file."""
+        if msg in self:
+            assert False, f"forbidden message appeared in log {self.path}: {msg}"
+
+
 class WatchLog(abc.ABC):
 
     """