Exemple #1
0
class TestInstrumentAgent(IonTestCase):

    @defer.inlineCallbacks
    def setUp(self):
        yield self._start_container()

        # Start an instrument agent
        processes = [
            {'name':'pubsub_registry','module':'ion.services.dm.distribution.pubsub_registry','class':'DataPubSubRegistryService'},
            {'name':'pubsub_service','module':'ion.services.dm.distribution.pubsub_service','class':'DataPubsubService'},
            {'name':'agent_registry',
             'module':'ion.services.coi.agent_registry',
             'class':'ResourceRegistryService'},
            {'name':'testSBE49IA',
             'module':'ion.agents.instrumentagents.SBE49_IA',
             'class':'SBE49InstrumentAgent'},
        ]
        self.sup = yield self._spawn_processes(processes)
        self.svc_id = yield self.sup.get_child_id('testSBE49IA')
        self.reg_id = yield self.sup.get_child_id('agent_registry')

        # Start a client (for the RPC)
        self.IAClient = IA.InstrumentAgentClient(proc=self.sup,
                                                 target=self.svc_id)

        # Start an Agent Registry to test against
        self.reg_client = AgentRegistryClient(proc=self.sup)
        yield self.reg_client.clear_registry()

    @defer.inlineCallbacks
    def tearDown(self):
        yield self._stop_container()

    @defer.inlineCallbacks
    def test_get_SBE49_capabilities(self):
        """
        Test the ability to gather capabilities from the SBE49 instrument
        capabilities
        """
        result = yield self.IAClient.get_capabilities()
        #logging.info("getCapabilities result: "+ str(result))
        self.assert_(set(IACIParameters).issubset(set(result[IA.ci_parameters])))
        self.assert_(IA.driver_address in
                     result[IA.ci_parameters])
        self.assert_(list(IACICommands) == result[IA.ci_commands])
        self.assert_(list(IAInstCommands) ==
                     result[IA.instrument_commands])
        self.assert_(list(IAInstParameters) ==
                     result[IA.instrument_parameters])

    @defer.inlineCallbacks
    def test_get_set_SBE49_params(self):
        """
        Test the ability of the SBE49 driver to send and receive get, set,
        and other messages. Best called as RPC message pairs.
        """

        self.simulator = Simulator("123", 9000)
        self.simulator.start()

        # Sleep for a while to allow simlator to get set up.
        yield pu.asleep(1)

        try:
            
            response = yield self.IAClient.get_from_instrument(['baudrate',
                                                                'outputformat'])
            self.assert_(response['status'] == 'OK')
            self.assertEqual(response['baudrate'], 9600)
            self.assertEqual(response['outputformat'], 0)
    
            response = yield self.IAClient.set_to_instrument({'baudrate': 19200,
                                                'outputformat': 1})
            self.assert_(response['status'] == 'OK')
            self.assertEqual(response['baudrate'], 19200)
            self.assertEqual(response['outputformat'], 1)
            
            response = yield self.IAClient.get_from_instrument(['baudrate',
                                                                'outputformat'])
            self.assert_(response['status'] == 'OK')
            self.assertEqual(response['baudrate'], 19200)
            self.assertEqual(response['outputformat'], 1)
    
            response = yield self.IAClient.set_to_instrument({'outputformat': 2})
            self.assert_(response['status'] == 'OK')
            self.assertEqual(response['outputformat'], 2)
            
            # Try setting something bad
            response = yield self.IAClient.set_to_instrument({'baudrate': 19200,
                                                'badvalue': 1})
            self.assert_(response['status'] == 'ERROR')
            self.assert_('baudrate' not in response)

        finally:
            yield self.simulator.stop()

    @defer.inlineCallbacks
    def test_registration(self):
        """
        Tests the ability of an instrument agent to successfully register
        ifself with the resource registry.
        """
        reg_ref = yield self.IAClient.register_resource("123")

        result = yield self.IAClient.get_resource_instance()
        self.assertNotEqual(result, None)

        self.assert_(isinstance(result, InstrumentAgentResourceInstance))
        self.assertNotEqual(result.driver_process_id, None)
        self.assertEqual(result.instrument_ref.RegistryIdentity, "123")

        self.assertEqual(reg_ref.RegistryCommit, '')
        self.assertNotEqual(result.RegistryCommit, reg_ref.RegistryCommit)
        self.assertEqual(reg_ref.RegistryIdentity, result.RegistryIdentity)

        # Verify the reference is the same
        result = yield self.IAClient.get_resource_ref()

        self.assertEqual(result, reg_ref)

    @defer.inlineCallbacks
    def test_lifecycle_states(self):
        """
        Test the resource lifecycle management
        """
        yield self.IAClient.register_resource("123")

        response = yield self.IAClient.set_lifecycle_state(LCS.inactive)
        self.assertEqual(response, LCS.inactive)

        response = yield self.IAClient.get_lifecycle_state()
        self.assertEqual(response, LCS.inactive)
        self.assertNotEqual(response, LCS.active)

        response = yield self.IAClient.set_lifecycle_state(LCS.active)
        self.assertEqual(response, LCS.active)

        response = yield self.IAClient.get_lifecycle_state()
        self.assertEqual(response, LCS.active)

    @defer.inlineCallbacks
    def test_execute(self):
        """
        Test the ability of the SBE49 driver to execute commands through the
        InstrumentAgentClient class
        """
        self.simulator = Simulator("123", 9000)
        self.simulator.start()

        try:

            response = yield self.IAClient.execute_instrument([['start','now', 1],
                                                               ['stop']])
            print "response ", response
            self.assert_(isinstance(response, dict))
            self.assert_('status' in response.keys())
            self.assertEqual(response['status'], 'OK')
            self.assert_('start' in response['value'])
            self.assert_('stop' in response['value'])
            self.assert_(response['status'] == 'OK')

            response = yield self.IAClient.execute_instrument([['badcommand',
                                                                'now','1']])
            self.assert_(isinstance(response, dict))
            self.assertEqual(response['status'], 'ERROR')

            response = yield self.IAClient.execute_instrument([])
            self.assert_(isinstance(response, dict))
            self.assertEqual(response['status'], 'ERROR')

        finally:
            yield self.simulator.stop()

    @defer.inlineCallbacks
    def test_get_driver_proc(self):
        """
        Test the methods for retreiving the driver process directly from
        the instrument agent.
        """
        response = yield self.IAClient.get_from_CI([IA.driver_address])
        self.assertNotEqual(response, None)
        """
        Not the best test or logic, but see if the format is at least close
        Need a better way to get at the process id of the driver...maybe
        out of the registry?
        """
        self.assertEqual(str(response[IA.driver_address]).rsplit('.', 1)[0],
                         str(self.svc_id).rsplit('.', 1)[0])

    @defer.inlineCallbacks
    def test_status(self):
        """
        Test to see if the status response is correct
        @todo Do we even need this function?
        """
        response = yield self.IAClient.get_status(['some_arg'])
        self.assert_(isinstance(response, dict))
        self.assertEqual(response['status'], "OK")
        self.assertEqual(response['value'], 'a-ok')

    @defer.inlineCallbacks
    def test_translator(self):
        """
        Test to see if the translator function is coming back cleanly
        @todo make this not a stub when we can pass functions through AMQP
        """
        raise unittest.SkipTest('Needs Refactor of LifeCycle State and Resource Descriptions')

        yield
class TestResourceAgent(IonTestCase):
    @defer.inlineCallbacks
    def setUp(self):
        yield self._start_container()

        # use some sort of agent...doesnt really matter what kind, does it
        processes = [{
            'name': 'testAgent',
            'module': 'ion.agents.instrumentagents.SBE49_IA',
            'class': 'SBE49InstrumentAgent'
        }, {
            'name': 'agent_registry',
            'module': 'ion.services.coi.agent_registry',
            'class': 'AgentRegistryService'
        }]
        self.sup = yield self._spawn_processes(processes)
        self.svc_id = self.sup.get_child_id('testAgent')
        self.rr_id = self.sup.get_child_id('agent_registry')

        # Start a client (for the RPC)
        self.RAClient = ResourceAgentClient(proc=self.sup, target=self.svc_id)

        # RR Client is ,,,,,desired only for testing
        self.reg_client = AgentRegistryClient(proc=self.sup)
        yield self.reg_client.clear_registry()

        # need any non-generic resource...use an instrument agent one for now
        self.res_desc = \
            InstrumentAgentResourceDescription.create_new_resource()
        #self.res_desc.version = '1.23'
        self.res_desc.name = 'I am a test IA resource description'

        self.res_inst = InstrumentAgentResourceInstance.create_new_resource()
        self.res_inst.driver_process_id = 'something_for_now.1'
        self.res_inst.name = 'I am an instantiation of a test IA resource'

    @defer.inlineCallbacks
    def tearDown(self):
        yield self._stop_container()

    @defer.inlineCallbacks
    def test_reg_direct(self):
        registered_agent_desc = \
           yield self.reg_client.register_agent_definition(self.res_desc)
        # Not committed, so not equal, but parts should be
        self.assertNotEqual(registered_agent_desc, self.res_desc)
        self.assertEqual(registered_agent_desc.RegistryIdentity,
                         self.res_desc.RegistryIdentity)

        registered_agent_instance = \
            yield self.reg_client.register_agent_instance(self.res_inst)
        # Not committed, so not equal, but parts should be
        self.assertNotEqual(registered_agent_instance, self.res_inst)
        self.assertEqual(registered_agent_instance.RegistryIdentity,
                         self.res_inst.RegistryIdentity)

        recv_agent_desc = \
            yield self.reg_client.get_agent_definition(self.res_desc)
        self.assertEqual(recv_agent_desc, registered_agent_desc)
        self.assertEqual(recv_agent_desc.RegistryIdentity,
                         self.res_desc.RegistryIdentity)
        # Not committed, so not equal
        self.assertNotEqual(recv_agent_desc, self.res_desc)

        recv_agent_inst = \
            yield self.reg_client.get_agent_instance(self.res_inst)
        self.assertEqual(recv_agent_inst, registered_agent_instance)
        # Not commiteed, so not equal, but parts should be
        self.assertNotEqual(recv_agent_inst, self.res_inst)
        self.assertEqual(recv_agent_inst.RegistryIdentity,
                         self.res_inst.RegistryIdentity)

    @defer.inlineCallbacks
    def test_registration_with_definitions(self):
        reg_id = yield self.RAClient.register_resource(self.res_inst)
        orig_result = yield self.RAClient.get_resource_instance()
        self.assertNotEqual(orig_result, None)
        self.assertNotEqual(orig_result, self.res_inst)
        self.assertNotEqual(orig_result.RegistryCommit,
                            self.res_inst.RegistryCommit)
        self.assertEqual(reg_id.RegistryCommit, '')
        self.assertNotEqual(orig_result.RegistryCommit, reg_id.RegistryCommit)
        self.assertEqual(reg_id.RegistryIdentity, orig_result.RegistryIdentity)

        # Verify the reference is the same
        result = yield self.RAClient.get_resource_ref()
        self.assertEqual(result, reg_id)

        # test update/repeat reg if a different instance
        new_res_inst = yield self.RAClient.get_resource_instance()
        new_res_inst.name = "REPLACED TestAgentInstance"
        new_res_inst.driver_process_id = 'something_else.1'

        # Even pulling it, modifiying it, then re-registering makes a new one
        new_result = yield self.RAClient.get_resource_instance()
        new_result.name = "UPDATED TestAgentInstance"
        new_result.driver_process_id = 'something_else.2'
        new_reg_ref = yield self.RAClient.register_resource(new_res_inst)
        self.assertNotEqual(reg_id, new_reg_ref)
        new_result2 = yield self.RAClient.get_resource_instance()
        self.assertNotEqual(new_result2.RegistryIdentity,
                            orig_result.RegistryIdentity)
        self.assertNotEqual(new_result2.RegistryCommit,
                            orig_result.RegistryCommit)
        self.assertNotEqual(new_result2.name, orig_result.name)
        self.assertEqual(new_result2.name, new_res_inst.name)
        self.assertEqual(new_result2.driver_process_id,
                         new_res_inst.driver_process_id)
        self.assertNotEqual(new_result2.name, new_result.name)

    @defer.inlineCallbacks
    def test_agent_self_registration(self):
        reg_id = yield self.RAClient.register_resource()
        result = yield self.RAClient.get_resource_instance()
        self.assertNotEqual(result, None)
        self.assertEqual(reg_id.RegistryCommit, '')
        self.assertNotEqual(result.RegistryCommit, reg_id.RegistryCommit)
        self.assertEqual(reg_id.RegistryIdentity, result.RegistryIdentity)

        # Verify the reference is the same
        result = yield self.RAClient.get_resource_ref()
        self.assertEqual(result, reg_id)

        # Make A new agent to verify we have 2
        processes = [{
            'name': 'testAgent2',
            'module': 'ion.agents.instrumentagents.SBE49_IA',
            'class': 'SBE49InstrumentAgent'
        }]

        sup2 = yield self._spawn_processes(processes)
        svc_id2 = self.sup.get_child_id('testAgent2')

        # Start a client (for the RPC)
        RAClient2 = ResourceAgentClient(proc=sup2, target=svc_id2)

        # test update/repeat reg if a different instance
        yield RAClient2.register_resource()
        refinst2 = yield RAClient2.get_resource_instance()
        refinst1 = yield self.RAClient.get_resource_ref()

        self.assertNotEqual(refinst1.RegistryCommit, refinst2.RegistryCommit)
        self.assertNotEqual(refinst1.RegistryIdentity,
                            refinst2.RegistryIdentity)
        self.assertEqual(refinst1.RegistryCommit, result.RegistryCommit)
        self.assertEqual(refinst1.RegistryIdentity, result.RegistryIdentity)

    @defer.inlineCallbacks
    def test_lifecycle(self):
        registered_res = yield self.RAClient.register_resource(self.res_inst)
        self.assertNotEqual(registered_res, None)
        result = yield self.RAClient.get_resource_ref()
        self.assertEqual(result, registered_res)

        active_state = LCState('active')
        inactive_state = LCState('inactive')

        result = yield self.RAClient.set_lifecycle_state(active_state)
        self.assertEqual(result, active_state)
        result = yield self.RAClient.get_lifecycle_state()
        self.assertEqual(result, active_state)

        result = yield self.RAClient.set_lifecycle_state(inactive_state)
        ref = yield self.RAClient.get_resource_ref()
        self.assertNotEqual(ref, None)
        self.assertEqual(ref, registered_res)
        result = yield self.RAClient.get_lifecycle_state()
        self.assertEqual(result, inactive_state)
class TestResourceAgent(IonTestCase):

    @defer.inlineCallbacks
    def setUp(self):
        yield self._start_container()

        # use some sort of agent...doesnt really matter what kind, does it
        processes = [
            {'name':'testAgent',
             'module':'ion.agents.instrumentagents.SBE49_IA',
             'class':'SBE49InstrumentAgent'},
            {'name':'agent_registry',
             'module':'ion.services.coi.agent_registry',
             'class':'AgentRegistryService'}
        ]
        self.sup = yield self._spawn_processes(processes)
        self.svc_id = self.sup.get_child_id('testAgent')
        self.rr_id = self.sup.get_child_id('agent_registry')

        # Start a client (for the RPC)
        self.RAClient = ResourceAgentClient(proc=self.sup, target=self.svc_id)

        # RR Client is ,,,,,desired only for testing
        self.reg_client = AgentRegistryClient(proc=self.sup)
        yield self.reg_client.clear_registry()

        # need any non-generic resource...use an instrument agent one for now
        self.res_desc = \
            InstrumentAgentResourceDescription.create_new_resource()
        #self.res_desc.version = '1.23'
        self.res_desc.name = 'I am a test IA resource description'

        self.res_inst = InstrumentAgentResourceInstance.create_new_resource()
        self.res_inst.driver_process_id = 'something_for_now.1'
        self.res_inst.name = 'I am an instantiation of a test IA resource'

    @defer.inlineCallbacks
    def tearDown(self):
        yield self._stop_container()

    @defer.inlineCallbacks
    def test_reg_direct(self):
        registered_agent_desc = \
           yield self.reg_client.register_agent_definition(self.res_desc)
        # Not committed, so not equal, but parts should be
        self.assertNotEqual(registered_agent_desc, self.res_desc)
        self.assertEqual(registered_agent_desc.RegistryIdentity,
                         self.res_desc.RegistryIdentity)

        registered_agent_instance = \
            yield self.reg_client.register_agent_instance(self.res_inst)
        # Not committed, so not equal, but parts should be
        self.assertNotEqual(registered_agent_instance, self.res_inst)
        self.assertEqual(registered_agent_instance.RegistryIdentity,
                         self.res_inst.RegistryIdentity)

        recv_agent_desc = \
            yield self.reg_client.get_agent_definition(self.res_desc)
        self.assertEqual(recv_agent_desc, registered_agent_desc)
        self.assertEqual(recv_agent_desc.RegistryIdentity,
                         self.res_desc.RegistryIdentity)
        # Not committed, so not equal
        self.assertNotEqual(recv_agent_desc, self.res_desc)

        recv_agent_inst = \
            yield self.reg_client.get_agent_instance(self.res_inst)
        self.assertEqual(recv_agent_inst, registered_agent_instance)
        # Not commiteed, so not equal, but parts should be
        self.assertNotEqual(recv_agent_inst, self.res_inst)
        self.assertEqual(recv_agent_inst.RegistryIdentity,
                         self.res_inst.RegistryIdentity)

    @defer.inlineCallbacks
    def test_registration_with_definitions(self):
        reg_id = yield self.RAClient.register_resource(self.res_inst)
        orig_result = yield self.RAClient.get_resource_instance()
        self.assertNotEqual(orig_result, None)
        self.assertNotEqual(orig_result, self.res_inst)
        self.assertNotEqual(orig_result.RegistryCommit,
                            self.res_inst.RegistryCommit)
        self.assertEqual(reg_id.RegistryCommit, '')
        self.assertNotEqual(orig_result.RegistryCommit, reg_id.RegistryCommit)
        self.assertEqual(reg_id.RegistryIdentity, orig_result.RegistryIdentity)

        # Verify the reference is the same
        result = yield self.RAClient.get_resource_ref()
        self.assertEqual(result, reg_id)

        # test update/repeat reg if a different instance
        new_res_inst = yield self.RAClient.get_resource_instance()
        new_res_inst.name = "REPLACED TestAgentInstance"
        new_res_inst.driver_process_id = 'something_else.1'

        # Even pulling it, modifiying it, then re-registering makes a new one
        new_result = yield self.RAClient.get_resource_instance()
        new_result.name = "UPDATED TestAgentInstance"
        new_result.driver_process_id = 'something_else.2'
        new_reg_ref = yield self.RAClient.register_resource(new_res_inst)
        self.assertNotEqual(reg_id, new_reg_ref)
        new_result2 = yield self.RAClient.get_resource_instance()
        self.assertNotEqual(new_result2.RegistryIdentity,
                            orig_result.RegistryIdentity)
        self.assertNotEqual(new_result2.RegistryCommit,
                            orig_result.RegistryCommit)
        self.assertNotEqual(new_result2.name, orig_result.name)
        self.assertEqual(new_result2.name, new_res_inst.name)
        self.assertEqual(new_result2.driver_process_id,
                         new_res_inst.driver_process_id)
        self.assertNotEqual(new_result2.name, new_result.name)

    @defer.inlineCallbacks
    def test_agent_self_registration(self):
        reg_id = yield self.RAClient.register_resource()
        result = yield self.RAClient.get_resource_instance()
        self.assertNotEqual(result, None)
        self.assertEqual(reg_id.RegistryCommit, '')
        self.assertNotEqual(result.RegistryCommit, reg_id.RegistryCommit)
        self.assertEqual(reg_id.RegistryIdentity, result.RegistryIdentity)

        # Verify the reference is the same
        result = yield self.RAClient.get_resource_ref()
        self.assertEqual(result, reg_id)

        # Make A new agent to verify we have 2
        processes = [{'name':'testAgent2',
                      'module':'ion.agents.instrumentagents.SBE49_IA',
                      'class':'SBE49InstrumentAgent'}]

        sup2 = yield self._spawn_processes(processes)
        svc_id2 = self.sup.get_child_id('testAgent2')

        # Start a client (for the RPC)
        RAClient2 = ResourceAgentClient(proc=sup2, target=svc_id2)

        # test update/repeat reg if a different instance
        yield RAClient2.register_resource()
        refinst2 = yield RAClient2.get_resource_instance()
        refinst1 = yield self.RAClient.get_resource_ref()

        self.assertNotEqual(refinst1.RegistryCommit, refinst2.RegistryCommit)
        self.assertNotEqual(refinst1.RegistryIdentity,
                            refinst2.RegistryIdentity)
        self.assertEqual(refinst1.RegistryCommit, result.RegistryCommit)
        self.assertEqual(refinst1.RegistryIdentity, result.RegistryIdentity)


    @defer.inlineCallbacks
    def test_lifecycle(self):
        registered_res = yield self.RAClient.register_resource(self.res_inst)
        self.assertNotEqual(registered_res, None)
        result = yield self.RAClient.get_resource_ref()
        self.assertEqual(result, registered_res)

        active_state = LCState('active')
        inactive_state = LCState('inactive')

        result = yield self.RAClient.set_lifecycle_state(active_state)
        self.assertEqual(result, active_state)
        result = yield self.RAClient.get_lifecycle_state()
        self.assertEqual(result, active_state)

        result = yield self.RAClient.set_lifecycle_state(inactive_state)
        ref = yield self.RAClient.get_resource_ref()
        self.assertNotEqual(ref, None)
        self.assertEqual(ref, registered_res)
        result = yield self.RAClient.get_lifecycle_state()
        self.assertEqual(result, inactive_state)