def test_valid_complete_sample(self):
        """
        Create a mock port agent
        """
        mock_port_agent = Mock(spec=LoggerClient)

        """
        Instantiate the driver class directly (no driver client, no driver
        client, no zmq driver process, no driver process; just own the driver)
        """       
        test_driver = ooicoreInstrumentDriver(self.my_event_callback)
        
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverConnectionState.UNCONFIGURED)
        
        """
        Now configure the driver with the mock_port_agent, verifying
        that the driver transitions to the DISCONNECTED state
        """
        config = {'mock_port_agent' : mock_port_agent}
        test_driver.configure(config = config)
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverConnectionState.DISCONNECTED)
        
        """
        Invoke the connect method of the driver: should connect to mock
        port agent.  Verify that the connection FSM transitions to CONNECTED,
        (which means that the FSM should now be reporting the ProtocolState).
        """
        test_driver.connect()
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverProtocolState.UNKNOWN)

        """
        Force the driver into AUTOSAMPLE state so that it will parse and 
        publish samples
        """        
        test_driver.set_test_mode(True)
        test_driver.test_force_state(state = DriverProtocolState.AUTOSAMPLE)
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverProtocolState.AUTOSAMPLE)

        """
        - Reset test verification variables.
        - Construct a complete sample
        - Pass to got_data()
        - Verify that raw and parsed streams have been received
        """
        self.reset_test_vars()
        packet = PortAgentPacket()
        header = "\xa3\x9d\x7a\x02\x00\x1c\x0b\x2e\x00\x00\x00\x01\x80\x00\x00\x00" 
        packet.unpack_header(header) 
        packet.attach_data(FULL_SAMPLE) 

        test_driver._protocol.got_data(packet)
        
        self.assertTrue(self.raw_stream_received)
        self.assertTrue(self.parsed_stream_received)
    def test_valid_complete_sample(self):
        """
        Create a mock port agent
        """
        mock_port_agent = Mock(spec=LoggerClient)

        """
        Instantiate the driver class directly (no driver client, no driver
        client, no zmq driver process, no driver process; just own the driver)
        """
        test_driver = ooicoreInstrumentDriver(self.my_event_callback)

        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverConnectionState.UNCONFIGURED)

        """
        Now configure the driver with the mock_port_agent, verifying
        that the driver transitions to the DISCONNECTED state
        """
        config = {"mock_port_agent": mock_port_agent}
        test_driver.configure(config=config)
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverConnectionState.DISCONNECTED)

        """
        Invoke the connect method of the driver: should connect to mock
        port agent.  Verify that the connection FSM transitions to CONNECTED,
        (which means that the FSM should now be reporting the ProtocolState).
        """
        test_driver.connect()
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverProtocolState.UNKNOWN)

        """
        Force the driver into AUTOSAMPLE state so that it will parse and 
        publish samples
        """
        test_driver.set_test_mode(True)
        test_driver.test_force_state(state=DriverProtocolState.AUTOSAMPLE)
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverProtocolState.AUTOSAMPLE)

        """
        - Reset test verification variables.
        - Construct a complete sample
        - Pass to got_data()
        - Verify that raw and parsed streams have been received
        """
        self.reset_test_vars()
        packet = PortAgentPacket()
        header = "\xa3\x9d\x7a\x02\x00\x1c\x0b\x2e\x00\x00\x00\x01\x80\x00\x00\x00"
        packet.unpack_header(header)
        packet.attach_data(FULL_SAMPLE)

        test_driver._protocol.got_data(packet)

        self.assertTrue(self.raw_stream_received)
        self.assertTrue(self.parsed_stream_received)
class TestPortAgentPacket(MiUnitTest):
    # time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(time.time()))
    #

    @staticmethod
    def ntp_to_system_time(date):
        """convert a NTP time to system time"""
        return date - NTP_DELTA

    @staticmethod
    def system_to_ntp_time(date):
        """convert a system time to a NTP time"""
        return date + NTP_DELTA

    def setUp(self):
        self.pap = PortAgentPacket()
        #self.test_time = time.time()
        #self.ntp_time = self.system_to_ntp_time(self.test_time)

        #self.pap.set_timestamp(self.ntp_time)


    def test_pack_header(self):
        self.pap.attach_data("Only the length of this matters?") # 32 chars
        #self.pap.set_timestamp(3564425404.85)
        self.pap.pack_header()
        header = self.pap.get_header()
        self.assertEqual(header, array.array('B', [163, 157, 122, 2, 0, 48, 14, 145, 65, 234, 142, 154, 23, 155, 51, 51]))
        pass

    def test_unpack_header(self):
        self.pap = PortAgentPacket()
        data = self.pap.unpack_header(array.array('B', [163, 157, 122, 2, 0, 48, 14, 145, 65, 234, 142, 154, 23, 155, 51, 51]))

        self.assertEqual(self.pap.get_header_type(), 2)
        self.assertEqual(self.pap.get_header_length(), 32)
        self.assertEqual(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(self.ntp_to_system_time(self.pap.get_timestamp()))), "Thu, 13 Dec 2012 14:10:04 +0000")
        self.assertEqual(self.pap.get_header_recv_checksum(), 3729) #@TODO Probably should wire in one of these checksums.
        self.assertEqual(self.pap.get_header_checksum(), None)
        pass


    def test_get_time_stamp(self):
        result = self.pap.get_timestamp()
        self.assertEqual(self.ntp_time, result)
        pass

    def test_pack_unpack_header_timestamp(self):
        self.pap.attach_data("sweet polly purebread")
        self.pap.pack_header()
        header = self.pap.get_header()
        self.pap.unpack_header(header)

        result = self.pap.get_timestamp()
        self.assertEqual(self.ntp_time, result)
        pass
def _get_header(buffer):
    packet = PortAgentPacket()
    if(len(buffer) < HEADER_SIZE): return None

    header = buffer[0:HEADER_SIZE]
    packet.unpack_header(header)

    if(packet.get_data_length() < 0): return None

    return packet
Beispiel #5
0
def _get_header(buffer):
    packet = PortAgentPacket()
    if (len(buffer) < HEADER_SIZE): return None

    header = buffer[0:HEADER_SIZE]
    packet.unpack_header(header)

    if (packet.get_data_length() < 0): return None

    return packet
Beispiel #6
0
def _get_header(buffer):
    packet = PortAgentPacket()
    if (len(buffer) < HEADER_SIZE): return None

    header = buffer[0:HEADER_SIZE]
    packet.unpack_header(header)

    if (packet.get_data_length() < 0): return None

    print "time: %f" % packet.get_timestamp()

    return packet
def _get_header(buffer):
    packet = PortAgentPacket()
    if(len(buffer) < HEADER_SIZE): return None

    header = buffer[0:HEADER_SIZE]
    packet.unpack_header(header)

    if(packet.get_data_length() < 0): return None

    print "time: %f" % packet.get_timestamp()

    return packet
class PAClientTestPortAgentPacket(MiUnitTest):

    @staticmethod
    def ntp_to_system_time(date):
        """convert a NTP time to system time"""
        return date - NTP_DELTA

    @staticmethod
    def system_to_ntp_time(date):
        """convert a system time to a NTP time"""
        return date + NTP_DELTA

    def setUp(self):
        self.pap = PortAgentPacket()
        # self.test_time = time.time()
        # self.ntp_time = self.system_to_ntp_time(self.test_time)

        # self.pap.set_timestamp(self.ntp_time)

    def test_pack_header(self):
        test_data = "Only the length of this matters?"
        test_data_length = len(test_data)
        self.pap.attach_data(test_data)
        self.pap.pack_header()
        self.assertEqual(self.pap.get_data_length(), test_data_length)

    def test_get_length(self):
        test_length = 100
        self.pap.set_data_length(test_length)
        got_length = self.pap.get_data_length()
        self.assertEqual(got_length, test_length)

    def test_checksum(self):
        """
        This tests the checksum algorithm; if somebody changes the algorithm
        this test should catch it.  Had to jump through some hoops to do this;
        needed to add set_data_length and set_header because we're building our
        own header here (the one in PortAgentPacket includes the timestamp
        so the checksum is not consistent).
        """
        test_data = "This tests the checksum algorithm."
        test_length = len(test_data)
        self.pap.attach_data(test_data)

        # Now build a header
        variable_tuple = (0xa3, 0x9d, 0x7a, self.pap.DATA_FROM_DRIVER,
                          test_length + HEADER_SIZE, 0x0000,
                          0)
        self.pap.set_data_length(test_length)

        header_format = '>BBBBHHd'
        size = struct.calcsize(header_format)
        temp_header = ctypes.create_string_buffer(size)
        struct.pack_into(header_format, temp_header, 0, *variable_tuple)

        # Now set the header member in PortAgentPacket to the header
        # we built
        self.pap.set_header(temp_header.raw)

        # Now get the checksum and verify it is what we expect it to be.
        checksum = self.pap.calculate_checksum()
        self.assertEqual(checksum, 2)

    def test_unpack_header(self):
        self.pap = PortAgentPacket()
        data_length = 32
        data = self.pap.unpack_header(array.array('B',
                                                  [163, 157, 122, 2, 0, data_length + HEADER_SIZE, 14, 145, 65, 234,
                                                   142, 154, 23, 155, 51, 51]))
        got_timestamp = self.pap.get_timestamp()

        self.assertEqual(self.pap.get_header_type(), self.pap.DATA_FROM_DRIVER)
        self.assertEqual(self.pap.get_data_length(), data_length)
        self.assertEqual(got_timestamp, 1105890970.092212)
        self.assertEqual(self.pap.get_header_recv_checksum(), 3729)
Beispiel #9
0
class PAClientTestPortAgentPacket(MiUnitTest):
    @staticmethod
    def ntp_to_system_time(date):
        """convert a NTP time to system time"""
        return date - NTP_DELTA

    @staticmethod
    def system_to_ntp_time(date):
        """convert a system time to a NTP time"""
        return date + NTP_DELTA

    def setUp(self):
        self.pap = PortAgentPacket()
        # self.test_time = time.time()
        # self.ntp_time = self.system_to_ntp_time(self.test_time)

        # self.pap.set_timestamp(self.ntp_time)

    def test_pack_header(self):
        test_data = "Only the length of this matters?"
        test_data_length = len(test_data)
        self.pap.attach_data(test_data)
        self.pap.pack_header()
        self.assertEqual(self.pap.get_data_length(), test_data_length)

    def test_get_length(self):
        test_length = 100
        self.pap.set_data_length(test_length)
        got_length = self.pap.get_data_length()
        self.assertEqual(got_length, test_length)

    def test_checksum(self):
        """
        This tests the checksum algorithm; if somebody changes the algorithm
        this test should catch it.  Had to jump through some hoops to do this;
        needed to add set_data_length and set_header because we're building our
        own header here (the one in PortAgentPacket includes the timestamp
        so the checksum is not consistent).
        """
        test_data = "This tests the checksum algorithm."
        test_length = len(test_data)
        self.pap.attach_data(test_data)

        # Now build a header
        variable_tuple = (0xa3, 0x9d, 0x7a, self.pap.DATA_FROM_DRIVER,
                          test_length + HEADER_SIZE, 0x0000, 0)
        self.pap.set_data_length(test_length)

        header_format = '>BBBBHHd'
        size = struct.calcsize(header_format)
        temp_header = ctypes.create_string_buffer(size)
        struct.pack_into(header_format, temp_header, 0, *variable_tuple)

        # Now set the header member in PortAgentPacket to the header
        # we built
        self.pap.set_header(temp_header.raw)

        # Now get the checksum and verify it is what we expect it to be.
        checksum = self.pap.calculate_checksum()
        self.assertEqual(checksum, 2)

    def test_unpack_header(self):
        self.pap = PortAgentPacket()
        data_length = 32
        data = self.pap.unpack_header(
            array.array('B', [
                163, 157, 122, 2, 0, data_length + HEADER_SIZE, 14, 145, 65,
                234, 142, 154, 23, 155, 51, 51
            ]))
        got_timestamp = self.pap.get_timestamp()

        self.assertEqual(self.pap.get_header_type(), self.pap.DATA_FROM_DRIVER)
        self.assertEqual(self.pap.get_data_length(), data_length)
        self.assertEqual(got_timestamp, 1105890970.092212)
        self.assertEqual(self.pap.get_header_recv_checksum(), 3729)
    def test_concatenated_fragmented_sample(self):
        """
        Simulate a complete sample that arrives in with a fragment concatenated.  The concatenated fragment
        should have have a terminator.  A separate invocations of got_data() will have the remainder;
        result should be a complete sample published 
        """
        
        """
        Create a mock port agent
        """
        mock_port_agent = Mock(spec=LoggerClient)

        """
        Instantiate the driver class directly (no driver client, no driver
        client, no zmq driver process, no driver process; just own the driver)
        """                  
        test_driver = ooicoreInstrumentDriver(self.my_event_callback)
        
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverConnectionState.UNCONFIGURED)
        
        """
        Now configure the driver with the mock_port_agent, verifying
        that the driver transitions to that state
        """
        config = {'mock_port_agent' : mock_port_agent}
        test_driver.configure(config = config)
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverConnectionState.DISCONNECTED)
        
        """
        Invoke the connect method of the driver: should connect to mock
        port agent.  Verify that the connection FSM transitions to CONNECTED,
        (which means that the FSM should now be reporting the ProtocolState).
        """
        test_driver.connect()
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverProtocolState.UNKNOWN)

        """
        Force the driver into AUTOSAMPLE state so that it will parse and 
        publish samples
        """        
        test_driver.set_test_mode(True)
        test_driver.test_force_state(state = DriverProtocolState.AUTOSAMPLE)
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverProtocolState.AUTOSAMPLE)

        """
        - Reset test verification variables.
        - Construct a sample stream with a concatenated fragment
        - Pass to got_data()
        - Verify that raw and parsed streams have been received
        - Later, when the final fragment has been send, verify that raw and
          parsed streams have been received.
        """
        self.reset_test_vars()

        """
        - Add the beginning of another sample stream
        - Pass to got_data()
        """
	packet = PortAgentPacket() 
        header = "\xa3\x9d\x7a\x02\x00\x1c\x0b\x2e\x00\x00\x00\x01\x80\x00\x00\x00" 
        packet.unpack_header(header) 
        packet.attach_data(FULL_SAMPLE+SAMPLE_FRAGMENT_1) 
	test_driver._protocol.got_data(packet)                
        self.assertTrue(self.raw_stream_received)
        self.assertTrue(self.parsed_stream_received)

        """
        - Reset test verification variables
        - Construct the final fragment of a sample stream
        - Pass to got_data()
        - Verify that raw and parsed streams have been received
        """
        self.reset_test_vars()
	packet = PortAgentPacket() 
	header = "\xa3\x9d\x7a\x02\x00\x1c\x0b\x2e\x00\x00\x00\x01\x80\x00\x00\x00" 
        packet.unpack_header(header) 
        packet.attach_data(SAMPLE_FRAGMENT_2) 
	test_driver._protocol.got_data(packet)                

        self.assertTrue(self.raw_stream_received)
        self.assertTrue(self.parsed_stream_received)
    def test_concatenated_fragmented_sample(self):
        """
        Simulate a complete sample that arrives in with a fragment concatenated.  The concatenated fragment
        should have have a terminator.  A separate invocations of got_data() will have the remainder;
        result should be a complete sample published 
        """

        """
        Create a mock port agent
        """
        mock_port_agent = Mock(spec=LoggerClient)

        """
        Instantiate the driver class directly (no driver client, no driver
        client, no zmq driver process, no driver process; just own the driver)
        """
        test_driver = ooicoreInstrumentDriver(self.my_event_callback)

        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverConnectionState.UNCONFIGURED)

        """
        Now configure the driver with the mock_port_agent, verifying
        that the driver transitions to that state
        """
        config = {"mock_port_agent": mock_port_agent}
        test_driver.configure(config=config)
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverConnectionState.DISCONNECTED)

        """
        Invoke the connect method of the driver: should connect to mock
        port agent.  Verify that the connection FSM transitions to CONNECTED,
        (which means that the FSM should now be reporting the ProtocolState).
        """
        test_driver.connect()
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverProtocolState.UNKNOWN)

        """
        Force the driver into AUTOSAMPLE state so that it will parse and 
        publish samples
        """
        test_driver.set_test_mode(True)
        test_driver.test_force_state(state=DriverProtocolState.AUTOSAMPLE)
        current_state = test_driver.get_resource_state()
        self.assertEqual(current_state, DriverProtocolState.AUTOSAMPLE)

        """
        - Reset test verification variables.
        - Construct a sample stream with a concatenated fragment
        - Pass to got_data()
        - Verify that raw and parsed streams have been received
        - Later, when the final fragment has been send, verify that raw and
          parsed streams have been received.
        """
        self.reset_test_vars()

        """
        - Add the beginning of another sample stream
        - Pass to got_data()
        """
        packet = PortAgentPacket()
        header = "\xa3\x9d\x7a\x02\x00\x1c\x0b\x2e\x00\x00\x00\x01\x80\x00\x00\x00"
        packet.unpack_header(header)
        packet.attach_data(FULL_SAMPLE + SAMPLE_FRAGMENT_1)
        test_driver._protocol.got_data(packet)
        self.assertTrue(self.raw_stream_received)
        self.assertTrue(self.parsed_stream_received)

        """
        - Reset test verification variables
        - Construct the final fragment of a sample stream
        - Pass to got_data()
        - Verify that raw and parsed streams have been received
        """
        self.reset_test_vars()
        packet = PortAgentPacket()
        header = "\xa3\x9d\x7a\x02\x00\x1c\x0b\x2e\x00\x00\x00\x01\x80\x00\x00\x00"
        packet.unpack_header(header)
        packet.attach_data(SAMPLE_FRAGMENT_2)
        test_driver._protocol.got_data(packet)

        self.assertTrue(self.raw_stream_received)
        self.assertTrue(self.parsed_stream_received)
    def test_sample(self):
        """
        Create a mock port agent
        """
        mock_port_agent = Mock(spec=PortAgentClient)

        """
        Instantiate the driver class directly (no driver client, no driver
        client, no zmq driver process, no driver process; just own the driver)
        """                  
        test_driver = InstrumentDriver(self.my_event_callback)
        
        current_state = test_driver.get_resource_state()
        print "DHE: DriverConnectionState: " + str(current_state)
        self.assertEqual(current_state, DriverConnectionState.UNCONFIGURED)
        
        """
        Now configure the driver with the mock_port_agent, verifying
        that the driver transitions to that state
        """
        config = {'mock_port_agent' : mock_port_agent}
        # commented below out; trying real port agent
        test_driver.configure(config = config)

        """
        DHE: trying this; want to invoke the driver methods themselves, but 
        with the driver talking to the port_agent (through the client).
        Will it work? Answer: It does, but I can't step through it.  Let
        me try just running this.
        """
        #test_driver.configure(config = self.port_agent_comm_config())
        current_state = test_driver.get_resource_state()
        print "DHE: DriverConnectionState: " + str(current_state)
        self.assertEqual(current_state, DriverConnectionState.DISCONNECTED)
        
        """
        Invoke the connect method of the driver: should connect to mock
        port agent.  Verify that the connection FSM transitions to CONNECTED,
        (which means that the FSM should now be reporting the ProtocolState).
        """
        
        """
        TODO: DHE: Shouldn't have to put a sleep here; it's like the
        port_agent isn't up yet; I get a connection refused without this.
        """
        gevent.sleep(2)

        test_driver.connect()
        current_state = test_driver.get_resource_state()
        print "DHE: DriverConnectionState: " + str(current_state)
        self.assertEqual(current_state, DriverProtocolState.UNKNOWN)

        """
        Invoke the discover_state method of the driver
        """
        # commenting out because it will try wakeup
        #test_driver.discover_state()
        #current_state = test_driver.get_resource_state()
        #print "DHE: DriverConnectionState: " + str(current_state)

        """
        Force state to command state.
        """
        
        """
        This won't work right now because the SBE16 does an _update_params upon
        entry to the command_handler; since this is a unit test there is nothing
        to respond to the ds and dc commands.  Setting up mock to do it would
        be really handy.
        """
        test_driver.execute_force_state(state = DriverProtocolState.AUTOSAMPLE)
        current_state = test_driver.get_resource_state()
        print "DHE: DriverConnectionState: " + str(current_state)
        self.assertEqual(current_state, DriverProtocolState.AUTOSAMPLE)

        self.reset_test_vars()
        test_sample = "#  24.0088,  0.00001,   -0.000,   0.0117, 03 Oct 2012 20:59:04\r\n"
        
        """
        use for test particle
        test_sample = "#  24.0088,  0.00001,   -0.000,   0.0117, 03 Oct 2012 20:59:04\r\n"
        test_sample = "# 24. 24.0088,  0.00001,    0.000,   0.0117, 03 Oct 2012 20:58:54\r\n"
        """

        paPacket = PortAgentPacket()         
        paHeader = "\xa3\x9d\x7a\x02\x00\x50\x0b\x2e\x00\x00\x00\x01\x80\x00\x00\x00"
        paPacket.unpack_header(paHeader)
        
        paPacket.attach_data(test_sample)

        test_driver._protocol.got_data(paPacket)
        
        """