Commit e1eebe62 authored by Elisabetta Giani's avatar Elisabetta Giani
Browse files

AT5-382: Updated configuration files and code files to run tests

on CspMaster and CspSubarray calsses.
parent c1370558
Loading
Loading
Loading
Loading
+40 −14
Original line number Diff line number Diff line
"""
A module defining a list of fixture functions.
"""
from __future__ import absolute_import
import mock
import pytest
import importlib
import pytest

import tango
from unittest import mock
from tango import DeviceProxy
from tango.test_context import DeviceTestContext

@pytest.fixture(scope="session", autouse=True)
@pytest.fixture(scope="class")
def tango_context(request):
    """Creates and returns a TANGO DeviceTestContext object.

@@ -18,24 +18,36 @@ def tango_context(request):
    request: _pytest.fixtures.SubRequest
        A request object gives access to the requesting test context.
    """
    test_properties = {
            'CspMaster': {'SkaLevel': '2', 
                          'CspCbf': 'mid_csp_cbf/sub_elt/master',
                          'CspPss': 'mid_csp_pss/sub_elt/master',
                          'CspPst': 'mid_csp_pst/sub_elt/master',
                          },
            'CspSubarray': {'CspMaster': 'common/elt/master',
                            'CbfSubarray': 'mid_csp_cbf/sub_elt/subarray_01',
                            'PssSubarray': 'mid_csp_pss/sub_elt/subarray_01',
                            'SubID' : 1,
                           },
            }
    # TODO: package_name and class_name can be used in future
    # fq_test_class_name = request.cls.__module__
    # fq_test_class_name_details = fq_test_class_name.split(".")
    # package_name = fq_test_class_name_details[1]
    # class_name = module_name = fq_test_class_name_details[1]
    module = importlib.import_module("{}.{}".format("csp_lmc_common", "CspMaster"))
    klass = getattr(module, "CspMaster")
    properties = {'SkaLevel': '2', 
                  'CspCbf': 'mid_csp_cbf/sub_elt/master',
                  'CspPss': 'mid_csp_pss/sub_elt/master',
                  'CspPst': 'mid_csp_pst/sub_elt/master',
                  }
    tango_context = DeviceTestContext(klass, properties=properties, process=False)
    test_class_name = request.cls.__name__
    class_name = test_class_name.split('Test', 1)[-1]
    module = importlib.import_module("{}.{}".format("csp_lmc_common", class_name))
    klass = getattr(module, class_name)
    tango_context = DeviceTestContext(klass, properties=test_properties.get(class_name), process=True)
    tango_context.start()
    klass.get_name = mock.Mock(side_effect=tango_context.get_device_access)
    yield tango_context
    tango_context.stop()

#
# NOTE: if initialize_device is called, the tests crash with error because during the
# CspMaster and CspMaster access to the TANGO DB to get the memorized attributes.
# Need to mock the call to the TANGO DB get_device_attribute
@pytest.fixture(scope="function")
def initialize_device(tango_context):
    """Re-initializes the device.
@@ -49,7 +61,7 @@ def initialize_device(tango_context):

@pytest.fixture(scope="class")
def cbf_master():
    """Create DeviceProxy for the CspMaster device
    """Create DeviceProxy for the CbfMaster device
       to test the device with the TANGO DB
    """
    database = tango.Database()
@@ -59,3 +71,17 @@ def cbf_master():
            return tango.DeviceProxy(instance)
        except tango.DevFailed:
            continue


@pytest.fixture(scope="class")
def csp_master():
    """Create DeviceProxy for the CspMaster device
       to test the device with the TANGO DB
    """
    database = tango.Database()
    instance_list = database.get_device_exported_for_class('CspMaster')
    for instance in instance_list.value_string:
        try:
            return tango.DeviceProxy(instance)
        except tango.DevFailed:
            continue
+16 −3
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
SHELL = /bin/bash
.SHELLFLAGS = -o pipefail -c

all: test 
all: test lint

# wait for the device to be available before beginning the test
# A temporary volume is mounted at /build when 'make test' is executing.
@@ -18,5 +18,18 @@ test:
	retry --max=10 -- tango_admin --ping-device common/elt/master
	retry --max=10 -- tango_admin --ping-device common/elt/subarray_01
	retry --max=10 -- tango_admin --ping-device common/elt/subarray_02
	cd /app && python setup.py test 
.PHONY: all test 
	cd /app && python setup.py test | tee setup_py_test.stdout
	mkdir -p /build/reports && \
        if [ -d /build ]; then \
                mv /app/setup_py_test.stdout /build/csp_lmc_common_setup_test.stdout; \
                mv /app/htmlcov /build/csp_lmc_common_htmlcov; \
                mv /app/coverage.xml /build/csp_lmc_common_coverage.xml; \
        fi;
lint:
	pip3 install pylint2junit; \
        mkdir -p /build/reports; \
        cd /app && pylint --output-format=parseable csp_lmc_common | tee /build/csp_lmc_common_code_analysis.stdout; \
        cd /app && pylint --output-format=pylint2junit.JunitReporter csp_lmc_common > /build/reports/cspl_lmc_common_linting.xml;

.PHONY: all test lint
+3 −2
Original line number Diff line number Diff line
@@ -11,14 +11,15 @@ source = csp-lmc-common

[tool:pytest]
testpaths = tests
addopts = --cov=csp_lmc_common
addopts = --verbose
	  --cov=csp_lmc_common
          --json-report
          --json-report-file=htmlcov/report.json 
          --cov-report=term 
          --cov-report=html 
          --cov-report=xml 
	  --junitxml=/build/reports/unit-tests.xml
          -v
console_output_style = progress
junit_family=legacy
filterwarnings =
    ignore::DeprecationWarning
+7 −7
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ setup(
        description=INFO['description'],
        author=INFO['author'],
        author_email=INFO['author_email'],
        packages=find_packages(),
        packages=find_packages(where="csp-lmc-common"),
        license=INFO['license'],
        url=INFO['url'],
        long_description=long_description,
@@ -38,9 +38,7 @@ setup(
        install_requires = [
            'pytango >=9.3.1',
            'future',
            'ska_logging >=0.2.1',
            'lmcbaseclasses >=0.5.0',
            'mock'
        ],
        setup_requires=[
            'pytest-runner',
@@ -48,7 +46,9 @@ setup(
            'recommonmark'
        ],
        test_suite="test",
        entry_points={'console_scripts':['CspMaster = CspMaster:main']},
        entry_points={'console_scripts':[
            'CspMaster = csp_lmc_common.CspMaster:main',
            'CspSubarray=csp_lmc_common.CspSubarray:main']},
        classifiers=[
            "Development Status :: 3 - Alpha",
            "Programming Language :: Python :: 3",
@@ -59,9 +59,9 @@ setup(
            ],
        tests_require=[
            'pytest',
            'pytest-cov',
            'coverage',
            'pytest-json-report',
            'pycodestyle',
            'mock'
        ],
        extras_require={
      })
      )
+126 −8
Original line number Diff line number Diff line
@@ -22,10 +22,10 @@ import pytest

#Local imports
from csp_lmc_common.CspMaster import CspMaster
from ska.base.control_model import AdminMode
from ska.base.control_model import AdminMode, HealthState

# Device test case
@pytest.mark.usefixtures("cbf_master", "tango_context")
@pytest.mark.usefixtures("tango_context", "cbf_master")

class TestCspMaster(object):
    properties = {'SkaLevel': '2', 
@@ -35,7 +35,10 @@ class TestCspMaster(object):
                  }

    def test_State(self, cbf_master, tango_context):
        """Test for State after initialization """
        """Test for State after initialization 
           NOTE: the CspMaster device state is ALARM because
           this device relies on several forwarded attributes.
        """
        # reinitalize Csp Master and CbfMaster devices
        cbf_master.Init()
        time.sleep(2)
@@ -54,18 +57,90 @@ class TestCspMaster(object):
        time.sleep(3)
        assert tango_context.device.adminMode.value == AdminMode.ONLINE

    def test_write_invalid_admin_mode(self, tango_context):

        with pytest.raises(tango.DevFailed) as df:
            tango_context.device.adminMode = 7
        assert "Set value for attribute adminMode is negative or above the maximun authorized" in str(df.value.args[0].desc)

    def test_cspHealthState(self, tango_context):
        """ Test the CSP initial healthState.
            NOTE: to build the CSP healthState, the sub-elements
            not ONLINE or MAINTENACE are not taken into account.
        """
        assert tango_context.device.healthState == HealthState.OK

    def test_cbfAdminMode(self, tango_context):
        """ Test the CBF adminMode attribute w/r"""
        tango_context.device.cbfAdminMode = AdminMode.ONLINE
        tango_context.device.cspCbfAdminMode = AdminMode.ONLINE
        time.sleep(3)
        assert tango_context.device.cbfAdminMode.value == AdminMode.ONLINE
        assert tango_context.device.cspCbfAdminMode.value == AdminMode.ONLINE

    def test_cbfHealthState(self, tango_context):
        """ Test the CBF initial healthState          """
        tango_context.device.cspCbfHealthState == HealthState.OK

    def test_pssState(self, tango_context):
        """ Test the PSS initial State          """
        assert tango_context.device.cspPssState == DevState.DISABLE

    def test_pssHealthState(self, tango_context):
        """ Test the PSS initial healthState          """
        assert tango_context.device.cspPssHealthState == HealthState.UNKNOWN

    def test_subelement_address(self, tango_context):
        """Test for report state of SearchBeam Capabilitities"""
    def test_pssAdminMode(self, tango_context):
        """ Test the PSS initial adminMode          """
        pss_admin = tango_context.device.cspPssAdminMode
        assert pss_admin == AdminMode.NOT_FITTED

    def test_write_adminMode_of_not_running_device(self, tango_context):
        assert tango_context.device.cspPssAdminMode == AdminMode.NOT_FITTED
        tango_context.device.cspPssAdminMode == AdminMode.ONLINE
        time.sleep(3)
        assert tango_context.device.cspPssAdminMode == AdminMode.NOT_FITTED

    def test_pstState(self, tango_context):
        """ Test the PST initial State          """
        assert tango_context.device.cspPstState == DevState.DISABLE

    def test_pstHealthState(self, tango_context):
        """ Test the PST initial healthState          """
        assert tango_context.device.cspPstHealthState == HealthState.UNKNOWN

    def test_pstAdminMode(self, tango_context):
        """ Test the PST initial adminMode          """
        assert tango_context.device.cspPstAdminMode == AdminMode.NOT_FITTED

    def test_subelement_cbf_address(self, tango_context):
        """Test the cbfMasterAdress value"""
        cbf_addr = tango_context.device.cbfMasterAddress
        cbf_addr_property = self.properties['CspCbf']
        assert cbf_addr == cbf_addr_property

    def test_subelement_pss_address(self, tango_context):
        """Test the pssMasterAdress value"""
        pss_addr = tango_context.device.pssMasterAddress
        pss_addr_property = self.properties['CspPss']
        assert pss_addr == pss_addr_property

    def test_subelement_pst_address(self, tango_context):
        """Test the pstMasterAdress value"""
        pst_addr = tango_context.device.pstMasterAddress
        pst_addr_property = self.properties['CspPst']
        assert pst_addr == pst_addr_property

    def test_configure_On_command_duration_time(self, tango_context):
        tango_context.device.onCmdDurationExpected = 3
        assert tango_context.device.onCmdDurationExpected == 3

    def test_configure_Off_command_duration_time(self, tango_context):
        tango_context.device.offCmdDurationExpected = 3
        assert tango_context.device.offCmdDurationExpected == 3

    def test_configure_Standby_command_duration_time(self, tango_context):
        tango_context.device.standbyCmdDurationExpected = 3
        assert tango_context.device.standbyCmdDurationExpected == 3

    def test_On_valid_state(self, tango_context, cbf_master):
        """
        Test for execution of On command when the CbfTestMaster is in the right state
@@ -82,6 +157,44 @@ class TestCspMaster(object):
        time.sleep(3)
        assert tango_context.device.cspCbfState == DevState.ON

    def test_on_command_duration(self, tango_context):
        time_measured = tango_context.device.onCmdDurationMeasured
        assert time_measured > 0

    def test_on_execution_timeout(self, tango_context):
        on_timeout = tango_context.device.onCmdTimeoutExpired
        assert not on_timeout 
        time.sleep(2.5)
        assert tango_context.device.onCommandProgress == 100

    def test_read_list_of_completed_task(self, tango_context):
        list_of_task = tango_context.device.listOfDevCompletedTask
        print("list_of_task:", list_of_task)
        assert tango_context.device.cbfMasterAddress in list_of_task

    def test_Standby_valid_state(self, tango_context, cbf_master):
        assert tango_context.device.cspCbfState == DevState.ON
        # issue the "On" command on CbfTestMaster device
        argin = [tango_context.device.cbfMasterAddress,]
        tango_context.device.Standby(argin)
        time.sleep(3)
        assert tango_context.device.cspCbfState == DevState.STANDBY

    def test_standby_command_duration(self, tango_context):
        time_measured = tango_context.device.standbyCmdDurationMeasured
        assert time_measured > 0

    def test_standby_execution_timeout(self, tango_context):
        on_timeout = tango_context.device.standbyCmdTimeoutExpired
        assert not on_timeout 
        time.sleep(2.5)
        assert tango_context.device.standbyCommandProgress == 100

    def test_num_of_completed_task(self, tango_context):
        time.sleep(3)
        num_of_task = tango_context.device.numOfDevCompletedTask
        assert num_of_task == 1

    def test_On_invalid_state(self, tango_context, cbf_master):
        """
        Test for the execution of the On command when the CbfTestMaster 
@@ -104,5 +217,10 @@ class TestCspMaster(object):
        with pytest.raises(tango.DevFailed) as df:
            argin = ["mid_csp_cbf/sub_elt/master", ]
            tango_context.device.On(argin)
        time.sleep(20)
        assert "Command On not allowed when the device is in OFF state" in str(df.value.args[0].desc)

    def test_off_execution_timeout(self, tango_context):
        off_timeout = tango_context.device.offCmdTimeoutExpired
        assert not off_timeout 
        time_measured = tango_context.device.offCmdDurationMeasured
        assert time_measured > 0
Loading