Commit 76fa6fd1 authored by Elisabetta Giani's avatar Elisabetta Giani
Browse files

CT-69: Added prvate methods to handle test setup.

parent ebf7518d
Loading
Loading
Loading
Loading
Loading
+55 −77
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ from ska.base.control_model import ObsState
# Device test case
@pytest.mark.usefixtures("midcsp_master", "midcsp_subarray01", "cbf_subarray01")


class TestBase(unittest.TestCase):
    fixture_names = ()
    @pytest.fixture(autouse=True)
@@ -40,73 +41,67 @@ class TestBase(unittest.TestCase):
class TestCspSubarrayConfiguration(TestBase):
    fixture_names =("midcsp_master", "midcsp_subarray01", "cbf_subarray01")
    
    def __is_csp_subarray_off(self):
        #state = midcsp_subarray01.State()
        state = self.midcsp_subarray01.State()
        if state == DevState.OFF:
            return True
        return False

    #def __set_csp_subarray_to_off(self,midcsp_subarray01):
    def __set_csp_subarray_to_off(self):
        if not self.__is_csp_subarray_off():
            self.midcsp_subarray01.RemoveAllReceptors()
            time.sleep(3)
            state = self.midcsp_subarray01.State()
            if state == DevState.OFF:
                return True
            return False

    def __set_csp_subarray_to_on(self):
        """
        Set the CSP Subarray to ON state.
        """
        # check if the subarray is in OFF-IDLE state
        if self.__is_csp_subarray_off():
        # read from the CSP Master the list of available receptors
        unassigned_receptors = self.midcsp_master.unassignedReceptorIDs
        # assign the first one to the subarray
        self.midcsp_subarray01.AddReceptors([unassigned_receptors[0],])
        # wait for the transition of the CSP subarray to ON
        time.sleep(3)
            # check the final subarray state
            state = self.midcsp_subarray01.State()
            if state == DevState.ON:
                return True
            return False

    def test_happy_path(self):
        """
        Test the happy path
        """
    def _setup_csp_subarray(self):
        # setup the CSP Subarray
        state = self.midcsp_subarray01.State()
        assert state in [DevState.DISABLE]
        assert state == DevState.DISABLE,"assuming that mid_csp_subarray_01 is disabled"

        #switch-on the CspMaster
        self.midcsp_master.On("")
        # wait for transition to OFF
        time.sleep(2)
        state = self.midcsp_subarray01.State()
        assert state in [DevState.OFF]
        assert state in [DevState.OFF],"assuming that mid_csp_subarray_01 is OFF"
        self.__set_csp_subarray_to_on()
        # load the json script from a file
        json_file = open("test_ConfigureScan_ADR4.json")

    def _reset_subarray_to_init_state(self):
        try:
            self.midcsp_subarray01.RemoveAllReceptors()
            time.sleep(3)
            # Set the CSP subarray to OFF issuing the Standby command
            # on CSP Master
            self.midcsp_master.Standby("")
            time.sleep(3)
        except tango.DevFailed as tango_err:
            logging.debug(f"Unable to reset subarray to init state")

    def _prepare_configuration_string(self,filename="test_ConfigureScan_ADR4.json"):
        """Create the config string for CSP-CBF"""
        try:
            json_file = open(filename)
            configuration_string = json_file.read().replace("\n", "")
            return configuration_string
        except Exception as e:
            logging.debug(f"Unable to locate file {filename}")

    def test_send_configure_to_cbf_and_json_stored(self):
        """
        Configure the CSP Subarray with a JSon string including
        the new ADR4 fields.
        """
        configuration_string = self._prepare_configuration_string()
        self._setup_csp_subarray()
        # exercise the device
        self.midcsp_subarray01.Configure(json_file.read().replace("\n", ""))
        self.midcsp_subarray01.Configure(configuration_string)
        time.sleep(3)

        # check
        # The CSP Subarray ObsState is READY
        obs_state = self.midcsp_subarray01.obsState
        assert obs_state == ObsState.READY
        
        # reset the device to initial state
        self.midcsp_subarray01.RemoveAllReceptors()
        time.sleep(3)
        self.midcsp_master.Standby("")
        time.sleep(3)
        state = self.midcsp_subarray01.State()
        assert state == DevState.DISABLE
        # stored json string equal to the sent one
        stored_json = self.midcsp_subarray01.validScanConfiguration
        assert stored_json == configuration_string 
        
    def test_json_without_configuration_id(self):
        """
@@ -114,27 +109,16 @@ class TestCspSubarrayConfiguration(TestBase):
        without the configID entry. 
        """
        # setup the test
        configuration_string = self._prepare_configuration_string("test_ConfigureScan_without_configID.json")
        self._setup_csp_subarray()
        # Set the CSP Subarray to ON-IDLE state
        if not self.__set_csp_subarray_to_on():
            assert 0
        obs_state = self.midcsp_subarray01.obsState
        assert obs_state == ObsState.IDLE
        # load the json configuration from a file
        json_file = open("test_ConfigureScan_without_configID.json")

        # exercise the device
        with pytest.raises(tango.DevFailed) as df:

            self.midcsp_subarray01.Configure(json_file.read().replace("\n", ""))
        if df:
            err_msg = str(df.value.args[0].desc)
            assert "No configID in json script" in err_msg
            self.midcsp_subarray01.Configure(configuration_string)
        # check
        # read the Subarray State and ObsState and check against
        # ON-IDLE
        # maybe we need only to check the obsState (?)
        state = self.midcsp_subarray01.State()
        assert state == DevState.ON
        obs_state = self.midcsp_subarray01.obsState
        assert obs_state == ObsState.IDLE

@@ -143,26 +127,20 @@ class TestCspSubarrayConfiguration(TestBase):
        Set the CSP Subarray in a wrong state and issue the
        Configure command on it
        """
        # setup the test 
        if not self.__set_csp_subarray_to_off():
            #state = self.midcsp_subarray01.State()
            #assert state == DevState.OFF
            assert 0
        configuration_string = self._prepare_configuration_string()
        logging.debug("configuration string:{}".format(configuration_string))
        self._setup_csp_subarray()
        self._reset_subarray_to_init_state()
        state = self.midcsp_subarray01.State()
        assert state == DevState.DISABLE 
        obs_state = self.midcsp_subarray01.obsState
        assert obs_state == ObsState.IDLE
        # the State-obsState value is OFF-IDLE. Wrong state to issue
        # configuration

        # load the json file
        json_file = open("test_ConfigureScan_ADR4.json")

        # exercise the device
        # Issue the Configure command
        with pytest.raises(tango.DevFailed) as df:
            self.midcsp_subarray01.Configure(json_file.read().replace("\n", ""))
            self.midcsp_subarray01.Configure(configuration_string)
        if df:
            err_msg = str(df.value.args[0].desc)
            assert "Command Configure not allowed" in err_msg
            logging.debug("Command configure failed with er {}".format(str(df.value.args[0].desc)))
        # check
        # Subarray final ObsState IDLE
        obs_state = self.midcsp_subarray01.obsState