From: Eugene Crosser Date: Sat, 18 Jun 2022 21:16:56 +0000 (+0200) Subject: test: skeleton for an end-to-end unittest X-Git-Tag: 0.99~8 X-Git-Url: http://www.average.org/gitweb/?p=loctrkd.git;a=commitdiff_plain;h=7ff26a2079e56969c65937b1bae7f6d908aa0664 test: skeleton for an end-to-end unittest --- diff --git a/gps303/googlemaps.py b/gps303/googlemaps.py index 2ea0103..d4deea2 100644 --- a/gps303/googlemaps.py +++ b/gps303/googlemaps.py @@ -11,6 +11,10 @@ def init(conf: Dict[str, Any]) -> None: gclient = gmaps.Client(key=token) +def shut() -> None: + return + + def lookup( mcc: int, mnc: int, diff --git a/gps303/gps303proto.py b/gps303/gps303proto.py index 2d777d9..5613bea 100755 --- a/gps303/gps303proto.py +++ b/gps303/gps303proto.py @@ -313,8 +313,8 @@ class LOGIN(GPS303Pkt): # Default response for ACK, can also respond with STOP_UPLOAD def in_decode(self, length: int, payload: bytes) -> None: - self.imei = payload[:16].hex() - self.ver = payload[16] + self.imei = payload[:8].hex() + self.ver = payload[8] class SUPERVISION(GPS303Pkt): diff --git a/gps303/lookaside.py b/gps303/lookaside.py index d05e769..1d21489 100644 --- a/gps303/lookaside.py +++ b/gps303/lookaside.py @@ -52,7 +52,10 @@ def runserver(conf: ConfigParser) -> None: log.warning("Lookup for %s resulted in %s", msg, e) except KeyboardInterrupt: - pass + zsub.close() + zpush.close() + zctx.destroy() # type: ignore + qry.shut() if __name__.endswith("__main__"): diff --git a/gps303/opencellid.py b/gps303/opencellid.py index 1d60bfe..90a8a74 100644 --- a/gps303/opencellid.py +++ b/gps303/opencellid.py @@ -15,6 +15,11 @@ def init(conf: Dict[str, Any]) -> None: ldb = connect(conf["opencellid"]["dbfn"]) +def shut() -> None: + if ldb is not None: + ldb.close() + + def lookup( mcc: int, mnc: int, gsm_cells: List[Tuple[int, int, int]], __: Any ) -> Tuple[float, float]: diff --git a/gps303/storage.py b/gps303/storage.py index b6f84ce..c0fc89b 100644 --- a/gps303/storage.py +++ b/gps303/storage.py @@ -43,7 +43,8 @@ def runserver(conf: ConfigParser) -> None: packet=zmsg.packet, ) except KeyboardInterrupt: - pass + zsub.close() + zctx.destroy() # type: ignore if __name__.endswith("__main__"): diff --git a/gps303/termconfig.py b/gps303/termconfig.py index 953e6ae..8313972 100644 --- a/gps303/termconfig.py +++ b/gps303/termconfig.py @@ -74,7 +74,9 @@ def runserver(conf: ConfigParser) -> None: zpush.send(resp.packed) except KeyboardInterrupt: - pass + zsub.close() + zpush.close() + zctx.destroy() # type: ignore if __name__.endswith("__main__"): diff --git a/gps303/wsgateway.py b/gps303/wsgateway.py index c344e88..381a855 100644 --- a/gps303/wsgateway.py +++ b/gps303/wsgateway.py @@ -259,7 +259,7 @@ def runserver(conf: ConfigParser) -> None: global htmlfile initdb(conf.get("storage", "dbfn")) - htmlfile = conf.get("wsgateway", "htmlfile") + htmlfile = conf.get("wsgateway", "htmlfile", fallback=None) # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?! zctx = zmq.Context() # type: ignore zsub = zctx.socket(zmq.SUB) # type: ignore @@ -402,7 +402,9 @@ def runserver(conf: ConfigParser) -> None: towait &= trywrite towait |= morewait except KeyboardInterrupt: - pass + zsub.close() + zctx.destroy() # type: ignore + tcpl.close() if __name__.endswith("__main__"): diff --git a/test/common.py b/test/common.py index 5f6c753..1388585 100644 --- a/test/common.py +++ b/test/common.py @@ -23,17 +23,33 @@ from unittest import TestCase class TestWithServers(TestCase): def setUp(self, *args: str) -> None: - with closing(socket(AF_INET6, SOCK_DGRAM)) as sock: - sock.bind(("", 0)) - sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) - freeport = sock.getsockname()[1] + with closing(socket(AF_INET6, SOCK_DGRAM)) as sock1, closing( + socket(AF_INET6, SOCK_DGRAM) + ) as sock2: + freeports = [] + for sock in sock1, sock2: + sock.bind(("", 0)) + sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) + freeports.append(sock.getsockname()[1]) _, self.tmpfilebase = mkstemp() self.conf = ConfigParser() self.conf["collector"] = { - "port": str(freeport), + "port": str(freeports[0]), "publishurl": "ipc://" + self.tmpfilebase + ".pub", "listenurl": "ipc://" + self.tmpfilebase + ".pul", } + self.conf["storage"] = { + "dbfn": self.tmpfilebase + ".storage.sqlite", + } + self.conf["opencellid"] = { + "dbfn": self.tmpfilebase + ".opencellid.sqlite", + } + self.conf["lookaside"] = { + "backend": "opencellid", + } + self.conf["wsgateway"] = { + "port": str(freeports[1]), + } self.children = [] for srvname in args: if srvname == "collector": @@ -57,8 +73,17 @@ class TestWithServers(TestCase): 0, srvname + " terminated with non-zero return code", ) - for sfx in (".pub", ".pul"): - unlink(self.tmpfilebase + sfx) + for sfx in ( + "", + ".pub", + ".pul", + ".storage.sqlite", + ".opencellid.sqlite", + ): + try: + unlink(self.tmpfilebase + sfx) + except OSError: + pass def send_and_drain(sock: SocketType, buf: Optional[bytes]) -> None: diff --git a/test/test_storage.py b/test/test_storage.py new file mode 100644 index 0000000..0c5e260 --- /dev/null +++ b/test/test_storage.py @@ -0,0 +1,42 @@ +""" Send junk to the collector """ + +from random import Random +from socket import getaddrinfo, socket, AF_INET6, SOCK_STREAM +from sqlite3 import connect +from time import sleep +import unittest +from .common import send_and_drain, TestWithServers + + +class Storage(TestWithServers): + def setUp(self, *args: str) -> None: + super().setUp("collector", "storage", "lookaside", "termconfig") + for fam, typ, pro, cnm, skadr in getaddrinfo( + "::1", + self.conf.getint("collector", "port"), + family=AF_INET6, + type=SOCK_STREAM, + ): + break # Just take the first element + self.sock = socket(AF_INET6, SOCK_STREAM) + self.sock.connect(skadr) + + def tearDown(self) -> None: + sleep(1) # give collector some time + send_and_drain(self.sock, None) + self.sock.close() + super().tearDown() + + def test_storage(self) -> None: + buf = b"xx\r\x01\x03Y3\x90w\x19q\x85\x05\r\n" + send_and_drain(self.sock, buf) + # TODO: make a proper sequence + with connect(self.conf.get("storage", "dbfn")) as db: + c = db.cursor() + c.execute("select * from events") + events = [dict(row) for row in c] + print(events) + + +if __name__ == "__main__": + unittest.main()