From 5a556a45fec47fa9218f7d5832a773ace63fbbdb Mon Sep 17 00:00:00 2001 From: lukken <lukken@astron.nl> Date: Tue, 1 Nov 2022 11:22:28 +0000 Subject: [PATCH] L2SS-938: Device server code coverage testing --- tangostationcontrol/requirements.txt | 1 + .../tangostationcontrol/common/constants.py | 1 + .../devices/lofar_device.py | 31 ++++++++++++++++++- .../tangostationcontrol/test/base.py | 9 ++++++ tangostationcontrol/test-requirements.txt | 1 - tangostationcontrol/tox.ini | 4 +++ 6 files changed, 45 insertions(+), 2 deletions(-) diff --git a/tangostationcontrol/requirements.txt b/tangostationcontrol/requirements.txt index 8d6285449..4788e3c46 100644 --- a/tangostationcontrol/requirements.txt +++ b/tangostationcontrol/requirements.txt @@ -4,6 +4,7 @@ importlib-metadata<2.0.0,>=0.12;python_version<"3.8" lofar-station-client@git+https://git.astron.nl/lofar2.0/lofar-station-client@0.9.2 +coverage>=6.0.2 # Apache-2.0 numpy mock asyncua >= 0.9.90 # LGPLv3 diff --git a/tangostationcontrol/tangostationcontrol/common/constants.py b/tangostationcontrol/tangostationcontrol/common/constants.py index a88e7151a..fbd4efb05 100644 --- a/tangostationcontrol/tangostationcontrol/common/constants.py +++ b/tangostationcontrol/tangostationcontrol/common/constants.py @@ -1,5 +1,6 @@ from tangostationcontrol.common.baselines import nr_baselines +DEBUG = True # number of FPGA processing nodes N_pn = 16 diff --git a/tangostationcontrol/tangostationcontrol/devices/lofar_device.py b/tangostationcontrol/tangostationcontrol/devices/lofar_device.py index 662c33032..524ea85f0 100644 --- a/tangostationcontrol/tangostationcontrol/devices/lofar_device.py +++ b/tangostationcontrol/tangostationcontrol/devices/lofar_device.py @@ -24,9 +24,9 @@ from tango import AttrWriteType, DevState, DebugIt, Attribute, DeviceProxy, Attr # Additional import from tangostationcontrol import __version__ as version from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper - from tangostationcontrol.common.lofar_logging import log_exceptions from tangostationcontrol.common.states import DEFAULT_COMMAND_STATES, INITIALISED_STATES +from tangostationcontrol.common import constants from tangostationcontrol.common.type_checking import sequence_not_str from tangostationcontrol.devices.device_decorators import only_in_states, fault_on_error from tangostationcontrol.toolkit.archiver import Archiver @@ -199,10 +199,39 @@ class lofar_device(Device, metaclass=DeviceMeta): elif not mask[idx]: merge_values[idx] = current_values[idx] + def store_coverage(self): + try: + self.cov.stop() + self.cov.save() + logger.info("Stopped code coverage measurements") + except Exception: + pass + def start_coverage(self): + try: + import os + import atexit + import coverage + + self.cov = coverage.Coverage( + data_file=os.getcwd() + "/tangostationcontrol/.coverage", + data_suffix=True, + branch=True, + source=os.getcwd() + "/tangostationcontrol/tangostationcontrol" + ) + self.cov.start() + logger.info("Started code coverage measurements: %s", os.getcwd()) + + atexit.register(self.store_coverage) + except Exception as e: + logger.warning("Could not configure for code coverage: %s", e) + @log_exceptions() def init_device(self): """ Instantiates the device in the OFF state. """ + if constants.DEBUG: + self.start_coverage() + # NOTE: Will delete_device first, if necessary Device.init_device(self) diff --git a/tangostationcontrol/tangostationcontrol/test/base.py b/tangostationcontrol/tangostationcontrol/test/base.py index 7cf3af7f8..d3d0955f9 100644 --- a/tangostationcontrol/tangostationcontrol/test/base.py +++ b/tangostationcontrol/tangostationcontrol/test/base.py @@ -16,6 +16,15 @@ import asynctest """Setup logging for unit tests""" configure_logger(debug=True) +import logging +logger = logging.getLogger() + + +# def tearDownModule(): +# import pdb; pdb.set_trace() +# logger.fatal("HELLO TEARDOWN?") +# +# unittest.addModuleCleanup(tearDownModule) class BaseTestCase(testscenarios.WithScenarios, unittest.TestCase): """Test base class.""" diff --git a/tangostationcontrol/test-requirements.txt b/tangostationcontrol/test-requirements.txt index 03aa93626..603cc7d4c 100644 --- a/tangostationcontrol/test-requirements.txt +++ b/tangostationcontrol/test-requirements.txt @@ -7,7 +7,6 @@ asynctest>=0.13.0 # Apache-2.0 bandit>=1.6.0 # Apache-2.0 virtualenv>=20.16.0 # MIT build>=0.8.0 # MIT -coverage>=5.2.0 # Apache-2.0 doc8>=0.8.0 # Apache-2.0 flake8>=3.8.0 # MIT flake8-bugbear>=22.1.11 # MIT diff --git a/tangostationcontrol/tox.ini b/tangostationcontrol/tox.ini index 332d3777f..73fe8ba9f 100644 --- a/tangostationcontrol/tox.ini +++ b/tangostationcontrol/tox.ini @@ -42,6 +42,10 @@ envdir = {toxworkdir}/testenvpy39 [testenv:py310] envdir = {toxworkdir}/testenvpy310 +[testenv:debug] +basepython=python3.7 +commands = {envpython} -m testtools.run {posargs} + [testenv:integration] allowlist_externals = echo passenv = TANGO_HOST -- GitLab