]> www.average.org Git - loctrkd.git/commitdiff
test: skeleton for an end-to-end unittest
authorEugene Crosser <crosser@average.org>
Sat, 18 Jun 2022 21:16:56 +0000 (23:16 +0200)
committerEugene Crosser <crosser@average.org>
Sat, 18 Jun 2022 21:16:56 +0000 (23:16 +0200)
gps303/googlemaps.py
gps303/gps303proto.py
gps303/lookaside.py
gps303/opencellid.py
gps303/storage.py
gps303/termconfig.py
gps303/wsgateway.py
test/common.py
test/test_storage.py [new file with mode: 0644]

index 2ea0103932384ca86b34daeb1dc1d15a1411c29c..d4deea2438dcae736ba2685b62d8f1e13c53b965 100644 (file)
@@ -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,
index 2d777d94fa5bfbec0b4c4b15192a0a56f9427954..5613bea455e3b2e9d66be24b173a10fddc325b35 100755 (executable)
@@ -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):
index d05e76990f0229e0ab7b862c620181bbb88a8cfa..1d214896b81fc5e3c73db89fa6a68c9a10412d88 100644 (file)
@@ -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__"):
index 1d60bfee1671defc9f8ed6fe9e994e787408ac0f..90a8a74bc78d7c426bb75e9a92d79b37a8babb1a 100644 (file)
@@ -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]:
index b6f84ce993e4356c4e8f32e43946f27bf12ce846..c0fc89be0ad3d41029faacbce17be5c76cafd207 100644 (file)
@@ -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__"):
index 953e6aec75f67ae380732d7f80dcfcc4cfa845ed..831397201a4521c9c3fd160c0fdbb3bb8eb0b39a 100644 (file)
@@ -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__"):
index c344e883bacd2c567b3966ac4b052123edc7255b..381a8558a4218f9d56489425ece81d4c4406e6d8 100644 (file)
@@ -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__"):
index 5f6c753fd0ae6f5863b29d41f1e76d489b8e865e..138858530cc134baaf2e41033ce7f3363f08c074 100644 (file)
@@ -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 (file)
index 0000000..0c5e260
--- /dev/null
@@ -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()