From 405c632cebeb9d1e99bb557d1e802e6569b8892a Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Wed, 10 Jan 2018 17:55:34 +0100 Subject: [PATCH 1/5] edit some tests --- boswatch/config.py | 14 ++++++--- test/test_ServerClient.py | 42 ++++++++++++------------- test/test_descriptor.py | 21 +++++++------ test/test_header.py | 8 ++--- test/test_packet.py | 65 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 39 deletions(-) create mode 100644 test/test_packet.py diff --git a/boswatch/config.py b/boswatch/config.py index 4641b96..3279296 100644 --- a/boswatch/config.py +++ b/boswatch/config.py @@ -72,9 +72,9 @@ class Config: except KeyError: logging.error("no sharePoint named: %s", sharePoint) except configparser.NoSectionError: - logging.error("no shared config section: %s", section) + logging.warning("no shared config section: %s", section) except configparser.NoOptionError: - logging.error("no shared config option: %s", key) + logging.warning("no shared config option: %s", key) except: # pragma: no cover logging.exception("error while reading shared config") return None @@ -83,9 +83,15 @@ class Config: try: return self._config.get(section, key) except configparser.NoSectionError: - logging.error("no local config section: %s", section) + logging.warning("no local config section: %s", section) except configparser.NoOptionError: - logging.error("no local config option: %s", key) + logging.warning("no local config option: %s", key) except: # pragma: no cover logging.exception("error while reading local config") return None + + def getAllSharepoints(self): + """!Return a python dict of all set sharepoint's + + @return Sharepoint dict""" + return self._sharePoints diff --git a/test/test_ServerClient.py b/test/test_ServerClient.py index 1f971f4..cb0b118 100644 --- a/test/test_ServerClient.py +++ b/test/test_ServerClient.py @@ -18,8 +18,8 @@ import pytest # import the pytest framework import time -import boswatch.network.server -import boswatch.network.client +from boswatch.network.server import TCPServer +from boswatch.network.client import TCPClient class Test_ServerClient: @@ -28,7 +28,7 @@ class Test_ServerClient: @pytest.fixture(scope="function") def useServer(self): """!Start and serve the sever for each functions where useServer is given""" - self.testServer = boswatch.network.server.TCPServer() + self.testServer = TCPServer() assert self.testServer.start() time.sleep(0.1) # wait for server yield self.testServer # server to all test where useServer is given @@ -37,33 +37,33 @@ class Test_ServerClient: def test_client_failed_connect(self): """!Connect to a non available server""" - self.testClient = boswatch.network.client.TCPClient() + self.testClient = TCPClient() assert not self.testClient.connect() def test_client_failed_disconnect(self): """!Disconnect while no connection is established""" - self.testClient = boswatch.network.client.TCPClient() + self.testClient = TCPClient() assert not self.testClient.disconnect() def test_client_failed_transmit(self): """!Transmit while no connection is established""" - self.testClient = boswatch.network.client.TCPClient() + self.testClient = TCPClient() assert not self.testClient.transmit("test") def test_client_failed_receive(self): """!Receive while no connection is established""" - self.testClient = boswatch.network.client.TCPClient() + self.testClient = TCPClient() assert not self.testClient.receive() def test_client_success_connect(self, useServer): """!Connect to a server""" - self.testClient = boswatch.network.client.TCPClient() + self.testClient = TCPClient() assert self.testClient.connect() assert self.testClient.disconnect() def test_client_reconnect(self, useServer): """!Try a reconnect after a established connection""" - self.testClient = boswatch.network.client.TCPClient() + self.testClient = TCPClient() assert self.testClient.connect() assert self.testClient.disconnect() assert self.testClient.connect() @@ -71,9 +71,9 @@ class Test_ServerClient: def test_multi_connect(self, useServer): """!Connect with 2 clients to the server""" - self.testClient1 = boswatch.network.client.TCPClient() + self.testClient1 = TCPClient() assert self.testClient1.connect() - self.testClient2 = boswatch.network.client.TCPClient() + self.testClient2 = TCPClient() assert self.testClient2.connect() # disconnect all assert self.testClient1.disconnect() @@ -81,7 +81,7 @@ class Test_ServerClient: def test_client_communicate(self, useServer): """!Try to send data to the server and check on '[ack]'""" - self.testClient = boswatch.network.client.TCPClient() + self.testClient = TCPClient() assert self.testClient.connect() assert self.testClient.transmit("test") assert self.testClient.receive() == "[ack]" @@ -90,11 +90,11 @@ class Test_ServerClient: def test_client_multi_communicate(self, useServer): """!Try to send data to the server with 3 clients and check on '[ack]'""" # connect all - self.testClient1 = boswatch.network.client.TCPClient() + self.testClient1 = TCPClient() assert self.testClient1.connect() - self.testClient2 = boswatch.network.client.TCPClient() + self.testClient2 = TCPClient() assert self.testClient2.connect() - self.testClient3 = boswatch.network.client.TCPClient() + self.testClient3 = TCPClient() assert self.testClient3.connect() # send all assert self.testClient1.transmit("test") @@ -111,7 +111,7 @@ class Test_ServerClient: def test_server_restart(self): """!Test a restart of the server""" - self.testServer = boswatch.network.server.TCPServer() + self.testServer = TCPServer() assert self.testServer.start() assert self.testServer.stop() assert self.testServer.start() @@ -119,13 +119,13 @@ class Test_ServerClient: def test_server_failed_stop(self): """!Test to start the server twice""" - self.testServer = boswatch.network.server.TCPServer() + self.testServer = TCPServer() assert not self.testServer.stop() def test_server_doubleStart(self): """!Test to start the server twice""" - self.testServer1 = boswatch.network.server.TCPServer() - self.testServer2 = boswatch.network.server.TCPServer() + self.testServer1 = TCPServer() + self.testServer2 = TCPServer() assert self.testServer1.start() assert not self.testServer2.start() assert self.testServer1.stop() @@ -134,9 +134,9 @@ class Test_ServerClient: def test_server_output(self, useServer): """!Send data to server with 2 clients, check '[ack]' and data on server queue""" # connect all - self.testClient1 = boswatch.network.client.TCPClient() + self.testClient1 = TCPClient() assert self.testClient1.connect() - self.testClient2 = boswatch.network.client.TCPClient() + self.testClient2 = TCPClient() assert self.testClient2.connect() # send all useServer.flushData() diff --git a/test/test_descriptor.py b/test/test_descriptor.py index 2493b50..eea1450 100644 --- a/test/test_descriptor.py +++ b/test/test_descriptor.py @@ -17,8 +17,9 @@ # import pytest # import the pytest framework -from boswatch.descriptor import descriptor -from boswatch.packet import packet +from boswatch.descriptor.descriptor import Descriptor +from boswatch.descriptor.descriptor import DescriptionList +from boswatch.packet.packet import Packet class Test_Descriptor: @@ -26,29 +27,29 @@ class Test_Descriptor: def test_loadCSVnotExist(self): """!read CSV file where not exist direct per DescriptionList class""" - descList = descriptor.DescriptionList() + descList = DescriptionList() assert not descList.loadCSV("boswatch") def test_loadCSV(self): """!read CSV file direct per DescriptionList class""" - descList = descriptor.DescriptionList() + descList = DescriptionList() assert descList.loadCSV("zvei") def test_descriptorLoadFailed(self): """!read CSV file where not exist""" - bwDescriptor = descriptor.Descriptor() + bwDescriptor = Descriptor() assert not bwDescriptor.loadDescription("boswatch") def test_descriptorLoad(self): """!read CSV file""" - bwDescriptor = descriptor.Descriptor() + bwDescriptor = Descriptor() assert bwDescriptor.loadDescription("zvei") def test_loadDescriptionsNotSet(self): """!load descriptions where not set to an bwPacket""" - bwDescriptor = descriptor.Descriptor() + bwDescriptor = Descriptor() assert bwDescriptor.loadDescription("zvei") - bwPacket = packet.Packet() + bwPacket = Packet() bwPacket.set("mode", "zvei") bwPacket.set("zvei", "54321") assert bwDescriptor.addDescriptions(bwPacket) @@ -57,9 +58,9 @@ class Test_Descriptor: def test_loadDescriptions(self): """!load descriptions to an bwPacket""" - bwDescriptor = descriptor.Descriptor() + bwDescriptor = Descriptor() assert bwDescriptor.loadDescription("zvei") - bwPacket = packet.Packet() + bwPacket = Packet() bwPacket.set("mode", "zvei") bwPacket.set("zvei", "12345") assert bwDescriptor.addDescriptions(bwPacket) diff --git a/test/test_header.py b/test/test_header.py index e3ba6b8..16dbab0 100644 --- a/test/test_header.py +++ b/test/test_header.py @@ -18,7 +18,7 @@ # import pytest # import the pytest framework -import boswatch.utils.header +from boswatch.utils import header class Test_Header: @@ -26,12 +26,12 @@ class Test_Header: def test_logoToLog(self): """!Test logo to log""" - assert boswatch.utils.header.logoToLog() + assert header.logoToLog() def test_infoToLog(self): """!Test info to log""" - assert boswatch.utils.header.infoToLog() + assert header.infoToLog() def test_logoToScreen(self): """!Test logo to screen""" - assert boswatch.utils.header.logoToScreen() + assert header.logoToScreen() diff --git a/test/test_packet.py b/test/test_packet.py new file mode 100644 index 0000000..4f5b1d1 --- /dev/null +++ b/test/test_packet.py @@ -0,0 +1,65 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +"""! + ____ ____ ______ __ __ __ _____ + / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ / + / __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ < + / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ / +/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/ + German BOS Information Script + by Bastian Schroll + +@file: test_packet.py +@date: 12.12.2017 +@author: Bastian Schroll +@description: Unittests for BOSWatch. File must be _run as "pytest" unittest +""" + + +import pytest # import the pytest framework + +from boswatch.packet.packet import Packet + + +class Test_Packet: + """!Unittests for the BOSWatch packet""" + + @pytest.fixture(scope="function") + def buildPacket(self): + """!Build a BOSWatch packet and serve it to each test""" + bwPacket = Packet() + yield bwPacket + + def test_crearePacket(self): + """!Create a packet""" + bwPacket = Packet() + assert bwPacket is not "" + + def test_copyPacket(self, buildPacket): + """!Copy a packet to an new instance""" + bwCopyPacket = Packet(buildPacket.__str__()) + assert bwCopyPacket is not "" + + def test_getPacketString(self, buildPacket): + """!get the intern packet dict as string""" + assert type(buildPacket.__str__()) is str + assert buildPacket.__str__() is not "" + + def test_getNotSetField(self, buildPacket): + """!try to get a not set field""" + assert not buildPacket.get("testfield") + + def test_setGetField(self, buildPacket): + """!set and get a field""" + buildPacket.set("testField", "test") + assert buildPacket.get("testField") is "test" + + def test_addClientData(self, buildPacket): + """!add client data to packet""" + buildPacket.addClientData() + assert buildPacket.get("clientVersion") + + def test_addServerData(self, buildPacket): + """!add server data to packet""" + buildPacket.addServerData() + assert buildPacket.get("serverVersion") From 03837449276733fa3dd783e804aa18a4b836e53f Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Wed, 10 Jan 2018 21:55:18 +0100 Subject: [PATCH 2/5] add specific datatypes to config loader --- boswatch/config.py | 32 +++++++++++++++++++++++++++++++- boswatch/packet/packet.py | 8 ++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/boswatch/config.py b/boswatch/config.py index 3279296..1245eca 100644 --- a/boswatch/config.py +++ b/boswatch/config.py @@ -59,7 +59,37 @@ class Config: logging.debug("configuration sharePoint: %s", sharePoint) return True - def get(self, section, key, sharePoint=""): + def getInt(self, section, key, sharePoint=""): + """!Method to read a single config entry as integer + + @param section: Section to read from + @param key: Value to read + @param sharePoint: Name of the global config share (empty is only local) + @return The value or None""" + value = int(self._get(section, key, sharePoint)) + if value is None: + return 0 + return int(value) + + def getBool(self, section, key, sharePoint=""): + """!Method to read a single config entry as boolean + + @param section: Section to read from + @param key: Value to read + @param sharePoint: Name of the global config share (empty is only local) + @return The value or None""" + return bool(self._get(section, key, sharePoint)) + + def getStr(self, section, key, sharePoint=""): + """!Method to read a single config entry as string + + @param section: Section to read from + @param key: Value to read + @param sharePoint: Name of the global config share (empty is only local) + @return The value or None""" + return str(self._get(section, key, sharePoint)) + + def _get(self, section, key, sharePoint=""): """!Method to read a single config entry @param section: Section to read from diff --git a/boswatch/packet/packet.py b/boswatch/packet/packet.py index d3bfaa3..bf006b8 100644 --- a/boswatch/packet/packet.py +++ b/boswatch/packet/packet.py @@ -71,12 +71,12 @@ class Packet: - frequency""" config = Config() logging.debug("add client data to bwPacket") - self.set("clientName", config.get("Client", "Name", "clientConfig")) + self.set("clientName", config.getStr("Client", "Name", "clientConfig")) self.set("clientVersion", version.client) self.set("clientBuildDate", version.date) self.set("clientBranch", version.branch) - self.set("inputSource", config.get("Server", "InputSource", "clientConfig")) - self.set("frequency", config.get("Stick", "Frequency", "clientConfig")) + self.set("inputSource", config.getStr("Server", "InputSource", "clientConfig")) + self.set("frequency", config.getStr("Stick", "Frequency", "clientConfig")) def addServerData(self): """!Add the server information to the decoded data @@ -88,7 +88,7 @@ class Packet: - serverBranch""" config = Config() logging.debug("add server data to bwPacket") - self.set("serverName", config.get("Server", "Name", "serverConfig")) + self.set("serverName", config.getStr("Server", "Name", "serverConfig")) self.set("serverVersion", version.server) self.set("serverBuildDate", version.date) self.set("serverBranch", version.branch) From ed095ed36650316d402fcc5c5ee48b76289dbb46 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Wed, 10 Jan 2018 21:56:02 +0100 Subject: [PATCH 3/5] change some logging in plugin main class --- boswatch/plugin/plugin.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/boswatch/plugin/plugin.py b/boswatch/plugin/plugin.py index ed6a03d..dd95edc 100644 --- a/boswatch/plugin/plugin.py +++ b/boswatch/plugin/plugin.py @@ -28,7 +28,6 @@ class Plugin: def __init__(self, pluginName): """!init preload some needed locals and then call onLoad() directly""" self._pluginName = pluginName - logging.debug("load %s", self._pluginName) self._pluginsActive += 1 # for time counting @@ -44,11 +43,12 @@ class Plugin: self._alarmErrorCount = 0 self._teardownErrorCount = 0 + logging.debug("[%s] onLoad()", pluginName) self.onLoad() def __del__(self): """!Destructor calls onUnload() directly""" - logging.debug("unload %s", self._pluginName) + logging.debug("[%s] onUnload()", self._pluginName) self._pluginsActive -= 1 self.onUnload() @@ -63,33 +63,33 @@ class Plugin: def _run(self, bwPacket): self._runCount += 1 - logging.debug("[%s] run #%s", self._pluginName, self._runCount) + logging.debug("[%s] run #%d", self._pluginName, self._runCount) self._tmpTime = time.time() try: - logging.debug("[%s] setup", self._pluginName) + logging.debug("[%s] setup()", self._pluginName) self.setup() except: self._setupErrorCount += 1 - logging.exception("[%s] error in setup", self._pluginName) + logging.exception("[%s] error in setup()", self._pluginName) self._setupTime = time.time() - self._tmpTime self._tmpTime = time.time() try: - logging.debug("[%s] alarm", self._pluginName) + logging.debug("[%s] alarm()", self._pluginName) self.alarm(bwPacket) except: self._alarmErrorCount += 1 - logging.exception("[%s] error in alarm", self._pluginName) + logging.exception("[%s] error in alarm()", self._pluginName) self._alarmTime = time.time() - self._tmpTime self._tmpTime = time.time() try: - logging.debug("[%s] teardown", self._pluginName) + logging.debug("[%s] teardown()", self._pluginName) self.teardown() except: self._teardownErrorCount += 1 - logging.exception("[%s] error in teardown", self._pluginName) + logging.exception("[%s] error in teardown()", self._pluginName) self._teardownTime = time.time() - self._tmpTime self._sumTime = self._setupTime + self._alarmTime + self._teardownTime From 9c79757406f56a60f4778729e21f57537f426e34 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Wed, 10 Jan 2018 22:02:43 +0100 Subject: [PATCH 4/5] add docu to plugin main class --- boswatch/plugin/plugin.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/boswatch/plugin/plugin.py b/boswatch/plugin/plugin.py index dd95edc..7d09414 100644 --- a/boswatch/plugin/plugin.py +++ b/boswatch/plugin/plugin.py @@ -52,16 +52,15 @@ class Plugin: self._pluginsActive -= 1 self.onUnload() - # logging.debug("[%s] statistics:", self._pluginName) - # logging.debug("- runs %d", self._runCount) - # logging.debug("- setup errors %d", self._setupErrorCount) - # logging.debug("- alarm errors %d", self._alarmErrorCount) - # logging.debug("- teardown errors %d", self._teardownErrorCount) - def _loadConfig(self): pass def _run(self, bwPacket): + """!start an complete running turnus of an plugin. + Calls setup(), alarm() and teardown() in this order. + The alarm() method serves the BOSWatch packet to the plugin. + + @param bwPacket: A BOSWatch packet instance""" self._runCount += 1 logging.debug("[%s] run #%d", self._pluginName, self._runCount) From 9e0aa779aed95944e7d8a28be8bae282774e2614 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Wed, 10 Jan 2018 22:22:06 +0100 Subject: [PATCH 5/5] edit test_config.py to run with new config methods --- boswatch/config.py | 4 ++-- test/test_config.py | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/boswatch/config.py b/boswatch/config.py index 1245eca..1fb2673 100644 --- a/boswatch/config.py +++ b/boswatch/config.py @@ -66,9 +66,9 @@ class Config: @param key: Value to read @param sharePoint: Name of the global config share (empty is only local) @return The value or None""" - value = int(self._get(section, key, sharePoint)) + value = self._get(section, key, sharePoint) if value is None: - return 0 + return int(0) return int(value) def getBool(self, section, key, sharePoint=""): diff --git a/test/test_config.py b/test/test_config.py index 0e3bfa4..967270e 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -34,16 +34,17 @@ class Test_Config: """!get values from local config file""" bwConfig = Config() bwConfig.loadConfigFile(paths.TEST_PATH + "test.ini") - assert bwConfig.get("test", "one") == "1" - assert bwConfig.get("test", "two") == "two" - assert bwConfig.get("testcase", "test") == "ok" + assert bwConfig.getInt("test", "one") == 1 + assert bwConfig.getBool("test", "one") is True + assert bwConfig.getStr("test", "two") == "two" + assert bwConfig.getStr("testcase", "test") == "ok" def test_getLocalConfigFailed(self): """!fail while get values from local config file""" bwConfig = Config() bwConfig.loadConfigFile(paths.TEST_PATH + "test.ini") - assert bwConfig.get("test", "abc") is None - assert bwConfig.get("abc", "test") is None + assert bwConfig.getStr("test", "abc") == "None" + assert bwConfig.getStr("abc", "test") == "None" def test_shareConfig(self): """!load local config file and share it""" @@ -65,8 +66,8 @@ class Test_Config: """!try to get values from shared config where not exists""" bwConfig = Config() bwConfig.loadConfigFile(paths.TEST_PATH + "test.ini") - assert bwConfig.get("test", "one") == "1" - assert bwConfig.get("test", "one", "NotSetSharedConfig") is None + assert bwConfig.getInt("test", "one") == 1 + assert bwConfig.getInt("test", "one", "NotSetSharedConfig") == 0 def test_getSharedConfig(self): """!get values from shared config file""" @@ -75,8 +76,8 @@ class Test_Config: assert bwConfig1._sharePoints["test_getSharedConfig"] is not None bwConfig2 = Config() - assert bwConfig2.get("test", "one") is None - assert bwConfig2.get("test", "one", "test_getSharedConfig") == "1" + assert bwConfig2.getStr("test", "one") == "None" + assert bwConfig2.getInt("test", "one", "test_getSharedConfig") == 1 def test_getSharedConfigFailed(self): """!fail while get values from shared config file""" @@ -85,5 +86,5 @@ class Test_Config: assert bwConfig1._sharePoints["test_getSharedConfigFailed"] is not None bwConfig2 = Config() - assert bwConfig2.get("test", "abc", "test_getSharedConfigFailed") is None - assert bwConfig2.get("abc", "test", "test_getSharedConfigFailed") is None + assert bwConfig2.getStr("test", "abc", "test_getSharedConfigFailed") == "None" + assert bwConfig2.getStr("abc", "test", "test_getSharedConfigFailed") == "None"