From f92b2ee3646fe2f2c2ec5decfb98ff391faf092f Mon Sep 17 00:00:00 2001 From: Hemna Date: Mon, 1 Apr 2024 21:46:23 -0400 Subject: [PATCH] Got unit tests working again --- aprsd/cmds/list_plugins.py | 2 +- aprsd/packets/watch_list.py | 6 +++++- aprsd/plugins/version.py | 7 ++++--- aprsd/stats/app.py | 16 +++++++++++++--- tests/plugins/test_version.py | 13 +++++-------- tests/test_plugin.py | 3 +-- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/aprsd/cmds/list_plugins.py b/aprsd/cmds/list_plugins.py index 6c4dc08..5ff3f4d 100644 --- a/aprsd/cmds/list_plugins.py +++ b/aprsd/cmds/list_plugins.py @@ -122,7 +122,7 @@ def get_installed_extensions(): def show_built_in_plugins(console): - modules = [email, fortune, location, notify, ping, query, time, version, weather] + modules = [email, fortune, location, notify, ping, time, version, weather] plugins = [] for module in modules: diff --git a/aprsd/packets/watch_list.py b/aprsd/packets/watch_list.py index 05ba8db..2b17d61 100644 --- a/aprsd/packets/watch_list.py +++ b/aprsd/packets/watch_list.py @@ -76,7 +76,11 @@ class WatchList(objectstore.ObjectStoreMixin): def age(self, callsign): now = datetime.datetime.now() - return str(now - self.last_seen(callsign)) + last_seen_time = self.last_seen(callsign) + if last_seen_time: + return str(now - last_seen_time) + else: + return None def max_delta(self, seconds=None): if not seconds: diff --git a/aprsd/plugins/version.py b/aprsd/plugins/version.py index 32037a0..7dce7cb 100644 --- a/aprsd/plugins/version.py +++ b/aprsd/plugins/version.py @@ -1,7 +1,8 @@ import logging import aprsd -from aprsd import plugin, stats +from aprsd import plugin +from aprsd.stats import collector LOG = logging.getLogger("APRSD") @@ -23,8 +24,8 @@ class VersionPlugin(plugin.APRSDRegexCommandPluginBase): # fromcall = packet.get("from") # message = packet.get("message_text", None) # ack = packet.get("msgNo", "0") - s = stats.APRSDStats().stats() + s = collector.Collector().collect() return "APRSD ver:{} uptime:{}".format( aprsd.__version__, - s["aprsd"]["uptime"], + s["APRSDStats"]["uptime"], ) diff --git a/aprsd/stats/app.py b/aprsd/stats/app.py index 5382818..1db8a32 100644 --- a/aprsd/stats/app.py +++ b/aprsd/stats/app.py @@ -10,13 +10,23 @@ from aprsd import utils CONF = cfg.CONF -@utils.singleton class APRSDStats: """The AppStats class is used to collect stats from the application.""" + + _instance = None + + def __new__(cls, *args, **kwargs): + """Have to override the new method to make this a singleton + + instead of using @singletone decorator so the unit tests work. + """ + if not cls._instance: + cls._instance = super().__new__(cls) + return cls._instance + def __init__(self): self.start_time = datetime.datetime.now() - @property def uptime(self): return datetime.datetime.now() - self.start_time @@ -24,7 +34,7 @@ class APRSDStats: current, peak = tracemalloc.get_traced_memory() stats = { "version": aprsd.__version__, - "uptime": self.uptime, + "uptime": self.uptime(), "callsign": CONF.callsign, "memory_current": int(current), "memory_current_str": utils.human_size(current), diff --git a/tests/plugins/test_version.py b/tests/plugins/test_version.py index 32d66e2..8cf4f69 100644 --- a/tests/plugins/test_version.py +++ b/tests/plugins/test_version.py @@ -1,3 +1,5 @@ +from unittest import mock + from oslo_config import cfg import aprsd @@ -11,7 +13,9 @@ CONF = cfg.CONF class TestVersionPlugin(test_plugin.TestPlugin): - def test_version(self): + @mock.patch("aprsd.stats.app.APRSDStats.uptime") + def test_version(self, mock_stats): + mock_stats.return_value = "00:00:00" expected = f"APRSD ver:{aprsd.__version__} uptime:00:00:00" CONF.callsign = fake.FAKE_TO_CALLSIGN version = version_plugin.VersionPlugin() @@ -31,10 +35,3 @@ class TestVersionPlugin(test_plugin.TestPlugin): ) actual = version.filter(packet) self.assertEqual(expected, actual) - - packet = fake.fake_packet( - message="Version", - msg_number=1, - ) - actual = version.filter(packet) - self.assertEqual(expected, actual) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index e9a17eb..7e2cdab 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -6,7 +6,7 @@ from oslo_config import cfg from aprsd import conf # noqa: F401 from aprsd import packets from aprsd import plugin as aprsd_plugin -from aprsd import plugins, stats +from aprsd import plugins from aprsd.packets import core from . import fake @@ -89,7 +89,6 @@ class TestPlugin(unittest.TestCase): self.config_and_init() def tearDown(self) -> None: - stats.APRSDStats._instance = None packets.WatchList._instance = None packets.SeenList._instance = None packets.PacketTrack._instance = None