diff --git a/boswatch/network/client.py b/boswatch/network/client.py index beff672..9e719ce 100644 --- a/boswatch/network/client.py +++ b/boswatch/network/client.py @@ -47,11 +47,12 @@ class TCPClient: logging.debug("connected to " + str(host) + ":" + str(port)) return True - except socket.timeout: - logging.warning("cannot connect to %s:%s - timeout after %s sec", str(host), str(port), self._timeout) except ConnectionRefusedError: logging.error("cannot connect to %s:%s - connection refused", str(host), str(port)) return False + except socket.timeout: # pragma: no cover + logging.warning("cannot connect to %s:%s - timeout after %s sec", str(host), str(port), self._timeout) + return False except: # pragma: no cover logging.exception("cannot connect to %s:%s", str(host), str(port)) return False @@ -99,14 +100,15 @@ class TCPClient: received = str(self._sock.recv(1024), "utf-8") logging.debug("received: " + received) return received - except socket.timeout: - logging.warning("cannot receive - timeout after %s sec", self._timeout) except AttributeError: logging.error("cannot receive - no connection established") return False except ConnectionResetError: logging.error("cannot receive - host closed connection") return False + except socket.timeout: # pragma: no cover + logging.warning("cannot receive - timeout after %s sec", self._timeout) + return False except: # pragma: no cover logging.exception("error while receiving") return False diff --git a/boswatch/plugin/plugin.py b/boswatch/plugin/plugin.py index 0da24e2..413a96c 100644 --- a/boswatch/plugin/plugin.py +++ b/boswatch/plugin/plugin.py @@ -47,7 +47,7 @@ class Plugin: self._alarmErrorCount = 0 self._teardownErrorCount = 0 - if paths.FileExist(paths.PLUGIN_PATH + pluginName + "/" + pluginName + ".ini"): + if paths.fileExist(paths.PLUGIN_PATH + pluginName + "/" + pluginName + ".ini"): self.config = Config() self.config.loadConfigFile(paths.PLUGIN_PATH + pluginName + "/" + pluginName + ".ini") else: diff --git a/boswatch/utils/paths.py b/boswatch/utils/paths.py index 486bee2..eac59b2 100644 --- a/boswatch/utils/paths.py +++ b/boswatch/utils/paths.py @@ -30,7 +30,7 @@ BIN_PATH = ROOT_PATH + "_bin/" TEST_PATH = ROOT_PATH + "test/" -def FileExist(filePath): +def fileExist(filePath): return os.path.exists(filePath) @@ -44,6 +44,6 @@ def makeDirIfNotExist(dirPath): os.mkdir(dirPath) logging.debug("directory created: %s", dirPath) return dirPath - except: + except: # pragma: no cover logging.exception("error by creating a directory: %s", dirPath) return False diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..5470b24 --- /dev/null +++ b/install.sh @@ -0,0 +1,240 @@ +#!/bin/bash + +function exitcodefunction { + errorcode=$1 + action=$2 + module=$3 + + if [ $errorcode -ne "0" ]; then + echo "Action: $action on $module failed." >> $boswatchpath/install/setup_log.txt + echo "Exitcode: $errorcode" >> $boswatchpath/install/setup_log.txt + echo "" + echo "Action: $action on $module failed." + echo "Exitcode: $errorcode" + echo "" + echo " -> If you want to open an issue at https://github.com/Schrolli91/BOSWatch/issues" + echo " please post the logfile, located at $boswatchpath/install/setup_log.txt" + exit 1 + else + echo "Action: $action on $module ok." >> $boswatchpath/install/setup_log.txt + fi + } + + +tput clear +tput civis +echo " ____ ____ ______ __ __ __ " +echo " / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ " +echo " / __ / / / /\__ \| | /| / / __ / __/ ___/ __ \ " +echo " / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / " +echo " /_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ " +echo " German BOS Information Script " +echo " by Bastian Schroll " +echo "" + +# Make sure only root can run our script +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root!" 1>&2 + exit 1 +fi + +echo "This may take several minutes... Don't panic!" +echo "" +echo "Caution, script does not install a webserver with PHP and MySQL" +echo "So you have to make up manually if you want to use MySQL support" + +boswatchpath=/opt/boswatch +reboot=false +didBackup=false + +# check for old version (for the old ones...) +if [ -f $boswatchpath/BOSWatch/boswatch.py ]; then + echo "Old installation found!" + echo "A backup will be copied to $boswatchpath/old" + + mkdir /tmp/boswatch + mv $boswatchpath/BOSWatch/* /tmp/boswatch/ + didBackup=true +fi + +#and the future... +if [ -f $boswatchpath/boswatch.py ]; then + echo "Old installation found!" + echo "A backup will be copied to $boswatchpath/old" + + mkdir /tmp/boswatch + mv $boswatchpath/* /tmp/boswatch/ + didBackup=true +fi + +for (( i=1; i<=$#; i=$i+2 )); do + t=$((i + 1)) + eval arg=\$$i + eval arg2=\$$t + + case $arg in + -r|--reboot) reboot=true ;; + + -b|--branch) + case $arg2 in + dev|develop) echo " !!! WARNING: you are using the DEV BRANCH !!! "; branch=dev ;; + *) branch=master ;; + esac ;; + + -p|--path) echo " !!! WARNING: you'll install BOSWATCH to alternative path !!! "; boswatchpath=$arg2 ;; + + *) echo "Internal error!" ; exit 1 ;; + esac +done + +mkdir -p $boswatchpath +mkdir -p $boswatchpath/install + +echo "" + +tput cup 13 15 +echo "[ 1/10] [#---------]" +tput cup 15 5 +echo "-> make an apt-get update................" +apt-get update -y > $boswatchpath/install/setup_log.txt 2>&1 + +tput cup 13 15 +echo "[ 2/10] [##--------]" +tput cup 15 5 +echo "-> download GIT and other stuff.........." +apt-get -y install git cmake build-essential libusb-1.0 qt4-qmake qt4-default libpulse-dev libx11-dev sox >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? download stuff + +tput cup 13 15 +echo "[ 3/10] [###-------]" +tput cup 15 5 +echo "-> download rtl_fm......................" +cd $boswatchpath/install +git clone https://github.com/Schrolli91/rtl-sdr.git >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? git-clone rtl-sdr +cd rtl-sdr/ + +tput cup 13 15 +echo "[ 4/10] [####------]" +tput cup 15 5 +echo "-> compile rtl_fm......................" +mkdir -p build && cd build +cmake ../ -DINSTALL_UDEV_RULES=ON >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? cmake rtl-sdr + +make >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? make rtl-sdr + +make install >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? make-install rtl-sdr + +ldconfig >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? ldconfig rtl-sdr + + + +tput cup 13 15 +echo "[ 5/10] [#####-----]" +tput cup 15 5 +echo "-> download multimon-ng................" +cd $boswatchpath/install +git clone https://github.com/Schrolli91/multimon-ng.git multimonNG >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? git-clone multimonNG + + +cd $boswatchpath/install/multimonNG/ + +tput cup 13 15 +echo "[ 6/10] [######----]" +tput cup 15 5 +echo "-> compile multimon-ng................." +mkdir -p build +cd build +qmake ../multimon-ng.pro >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? qmake multimonNG + +make >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? make multimonNG + + +make install >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? qmakeinstall multimonNG + + +tput cup 13 15 +echo "[ 7/10] [#######---]" +tput cup 15 5 +echo "-> download MySQL connector for Python." +cd $boswatchpath/install +wget "http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-1.0.9.tar.gz/from/http://cdn.mysql.com/" -O mysql-connector.tar >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? download mysql-connector + +tar xfv mysql-connector.tar >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? untar mysql-connector + +cd $boswatchpath/install/mysql-connector-python* + +tput cup 13 15 +echo "[ 8/10] [########--]" +tput cup 15 5 +echo "-> install MySQL connector for Python.." +chmod +x ./setup.py +./setup.py install >> $boswatchpath/install/setup_log.txt 2>&1 +exitcodefunction $? setup mysql-connector + + +tput cup 13 15 +echo "[ 9/10] [#########-]" +tput cup 15 5 +echo "-> download BOSWatch..................." +cd $boswatchpath/ + +case $branch in + "dev") git clone -b develop https://github.com/Schrolli91/BOSWatch >> $boswatchpath/install/setup_log.txt 2>&1 && \ + exitcodefunction $? git-clone BOSWatch-develop ;; + "beta") git clone -b beta https://github.com/Schrolli91/BOSWatch >> $boswatchpath/install/setup_log.txt 2>&1 && \ + exitcodefunction $? git-clone BOSWatch-beta ;; + *) git clone -b master https://github.com/Schrolli91/BOSWatch >> $boswatchpath/install/setup_log.txt 2>&1 && \ + exitcodefunction $? git-clone BOSWatch ;; +esac + +tput cup 13 15 +echo "[10/10] [##########]" +tput cup 15 5 +echo "-> configure..........................." +cd $boswatchpath/ +chmod +x * +echo $'# BOSWatch - blacklist the DVB drivers to avoid conflicts with the SDR driver\n blacklist dvb_usb_rtl28xxu \n blacklist rtl2830\n blacklist dvb_usb_v2\n blacklist dvb_core' >> /etc/modprobe.d/boswatch_blacklist_sdr.conf + +tput cup 17 1 +echo "BOSWatch is now installed in $boswatchpath/" +echo "Installation ready!" +tput cup 19 3 +echo "Watch out: to run BOSWatch you have to modify the config.ini!" +echo "Do the following step to do so:" +echo "sudo nano $boswatchpath/config/config.ini" +echo "and modify the config as you need. This step is optional if you are upgrading an old version of BOSWatch. " + +tput cnorm + +# cleanup +mkdir $boswatchpath/log/install -p +mv $boswatchpath/install/setup_log.txt $boswatchpath/log/install/ +rm $boswatchpath/install/ -R + +mv $boswatchpath/BOSWatch/* $boswatchpath/ +rm $boswatchpath/BOSWatch -R + +#copy the template config to run boswatch +cp $boswatchpath/config/config.template.ini $boswatchpath/config/config.ini + + +#replay the backup +if [ $didBackup = "true" ]; then + mkdir $boswatchpath/old/ + mv /tmp/boswatch/* $boswatchpath/old/ +fi + +if [ $reboot = "true" ]; then + /sbin/reboot +fi diff --git a/test/test_ServerClient.py b/test/test_ServerClient.py index 828d9f9..44080b2 100644 --- a/test/test_ServerClient.py +++ b/test/test_ServerClient.py @@ -78,6 +78,9 @@ class Test_ServerClient: assert self.testClient1.connect() self.testClient2 = TCPClient() assert self.testClient2.connect() + time.sleep(0.1) # wait for all clients connected + # check connected clients + assert useServer.countClientsConnected() == 2 # disconnect all assert self.testClient1.disconnect() assert self.testClient2.disconnect() @@ -107,6 +110,8 @@ class Test_ServerClient: assert self.testClient3.receive() == "[ack]" assert self.testClient2.receive() == "[ack]" assert self.testClient1.receive() == "[ack]" + # check server msg queue + assert useServer.countPacketsInQueue() == 3 # disconnect all assert self.testClient1.disconnect() assert self.testClient2.disconnect() @@ -150,6 +155,7 @@ class Test_ServerClient: assert self.testClient1.receive() == "[ack]" assert self.testClient2.receive() == "[ack]" # _check server output data + assert useServer.countPacketsInQueue() == 2 assert useServer.getDataFromQueue()[1] == "test1" assert useServer.getDataFromQueue()[1] == "test2" assert useServer.getDataFromQueue() is None # Last _check must be None diff --git a/test/test_paths.py b/test/test_paths.py new file mode 100644 index 0000000..38850a2 --- /dev/null +++ b/test/test_paths.py @@ -0,0 +1,46 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +"""! + ____ ____ ______ __ __ __ _____ + / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ / + / __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ < + / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ / +/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/ + German BOS Information Script + by Bastian Schroll + +@file: test_paths.py +@date: 22.02.2017 +@author: Bastian Schroll +@description: Unittests for BOSWatch. File must be _run as "pytest" unittest +""" +import logging +import os + +from boswatch.utils import paths + + +class Test_Config: + """!Unittests for the paths""" + + def setup_method(self, method): + logging.debug("[TEST] %s.%s" % (type(self).__name__, method.__name__)) + + def test_fileExists(self): + """!load a local config file""" + assert paths.fileExist("README.md") + + def test_fileNotExists(self): + """!load a local config file""" + assert not paths.fileExist("notFound.txt") + + def test_makeDirNotExisting(self): + """!load a local config file""" + assert paths.makeDirIfNotExist("UnItTeSt") + os.removedirs("UnItTeSt") + + def test_makeDirExisting(self): + """!load a local config file""" + paths.makeDirIfNotExist("UnItTeSt") + assert paths.makeDirIfNotExist("UnItTeSt") + os.removedirs("UnItTeSt")