def test_expect(self):
        client = TcpClient()
        client.s = mock.Mock()
        client.s.recv.return_value = 'abcdefghi'

        # true if string is found
        self.assertTrue(client.expect('def'))
        # buf should contain only the text following the expected string
        self.assertEqual(client.buf, 'ghi')
예제 #2
0
    def test_expect(self):
        client = TcpClient()
        client.s = mock.Mock()
        client.s.recv.return_value = 'abcdefghi'

        # true if string is found
        self.assertTrue(client.expect('def'))
        # buf should contain only the text following the expected string
        self.assertEqual(client.buf, 'ghi')
예제 #3
0
    def test_pa_client_retry(self):
        """
        Test that the port agent client will not continually try to recover
        when the port agent closes the connection gracefully because it has
        another client connected.
        """

        exception_raised = False
        self.resetTestVars()

        self.init_instrument_simulator()
        self.startPortAgent()
        time.sleep(2)

        # Start a TCP client that will connect to the data port; this sets up the
        # situation where the Port Agent will immediately close the connection
        # because it already has one
        self.tcp_client = TcpClient("localhost", self.data_port)
        time.sleep(2)

        pa_client = PortAgentClient(self.ipaddr, self.data_port, self.cmd_port)

        try:
            pa_client.init_comms(self.myGotData, self.myGotRaw,
                                 self.myGotListenerError, self.myGotError)
        except InstrumentConnectionException:
            exception_raised = True

        # Give it some time to retry
        time.sleep(4)

        self.assertTrue(exception_raised)
    def test_expect_regex(self):
        client = TcpClient()
        client.s = mock.Mock()
        client.s.recv.return_value = 'abcdefghi'

        # un-compiled regex
        # expect_regex returns a match object
        self.assertEqual(client.expect_regex('[def]{3}').group(), 'def')
        # buf should contain only the text following the matched string
        self.assertEqual(client.buf, 'ghi')

        # pre-compiled regex with groups
        result = client.expect_regex(re.compile(r'(abc)(def)'))
        self.assertEqual(result.group(1), 'abc')
        self.assertEqual(result.group(2), 'def')
        self.assertEqual(client.buf, 'ghi')
예제 #5
0
    def test_expect_regex(self):
        client = TcpClient()
        client.s = mock.Mock()
        client.s.recv.return_value = 'abcdefghi'

        # un-compiled regex
        # expect_regex returns a match object
        self.assertEqual(client.expect_regex('[def]{3}').group(), 'def')
        # buf should contain only the text following the matched string
        self.assertEqual(client.buf, 'ghi')

        # pre-compiled regex with groups
        result = client.expect_regex(re.compile(r'(abc)(def)'))
        self.assertEqual(result.group(1), 'abc')
        self.assertEqual(result.group(2), 'def')
        self.assertEqual(client.buf, 'ghi')
예제 #6
0
    def test_remove_from_buffer(self):
        """
        Test the remove from buffer method.  Verify that all bytes before the match string are removed.
        """
        client = TcpClient()
        target = "MATCH"

        # Remove with an None target
        self.assertFalse(client.remove_from_buffer(None))

        # Remove with a zero length target
        self.assertFalse(client.remove_from_buffer(""))

        # Remove from buffer when buffer empty
        client.buf = None
        self.assertFalse(client.remove_from_buffer(target))
        self.assertIsNone(client.buf)

        # Remove from buffer when target the only thing in the list
        client.buf = target
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(len(client.buf), 0)

        # Remove from buffer when target the last thing in the list
        client.buf = "foo" + target
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(len(client.buf), 0)

        # Remove from buffer when target in the middle
        client.buf = "foo" + target + "remains"
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(client.buf, "remains")

        # Remove from buffer when target not in the buff
        client.buf = "target not in the list"
        self.assertFalse(client.remove_from_buffer(target))
        self.assertEqual(client.buf, "target not in the list")

        # Remove from buffer when target in the list more than once        client.buf = "foo" + target + "remains"
        client.buf = "foo" + target + "remains" + target
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(client.buf, "remains" + target)
예제 #7
0
    def test_direct_access_telnet_mode(self):
        """
        @brief This test verifies that the Instrument Driver properly supports direct access to the physical instrument. (telnet mode)
        """
        cmd = AgentCommand(command='power_down')
        retval = self.instrument_agent_client.execute_agent(cmd)
        cmd = AgentCommand(command='get_agent_state')
        retval = self.instrument_agent_client.execute_agent(cmd)
        state = retval.result
        self.assertEqual(state, InstrumentAgentState.POWERED_DOWN)

        cmd = AgentCommand(command='power_up')
        retval = self.instrument_agent_client.execute_agent(cmd)
        cmd = AgentCommand(command='get_agent_state')
        retval = self.instrument_agent_client.execute_agent(cmd)
        state = retval.result
        self.assertEqual(state, InstrumentAgentState.UNINITIALIZED)

        cmd = AgentCommand(command='initialize')
        retval = self.instrument_agent_client.execute_agent(cmd)
        cmd = AgentCommand(command='get_agent_state')
        retval = self.instrument_agent_client.execute_agent(cmd)
        state = retval.result
        self.assertEqual(state, InstrumentAgentState.INACTIVE)

        cmd = AgentCommand(command='go_active')
        retval = self.instrument_agent_client.execute_agent(cmd)
        cmd = AgentCommand(command='get_agent_state')
        retval = self.instrument_agent_client.execute_agent(cmd)
        state = retval.result
        self.assertEqual(state, InstrumentAgentState.IDLE)

        cmd = AgentCommand(command='run')
        retval = self.instrument_agent_client.execute_agent(cmd)
        cmd = AgentCommand(command='get_agent_state')
        retval = self.instrument_agent_client.execute_agent(cmd)
        state = retval.result
        self.assertEqual(state, InstrumentAgentState.OBSERVATORY)

        gevent.sleep(5)  # wait for mavs4 to go back to sleep if it was sleeping
        
        # go direct access
        cmd = AgentCommand(command='go_direct_access',
                           kwargs={'session_type': DirectAccessTypes.telnet,
                                   #kwargs={'session_type':DirectAccessTypes.vsp,
                                   'session_timeout':600,
                                   'inactivity_timeout':600})
        retval = self.instrument_agent_client.execute_agent(cmd)
        log.warn("go_direct_access retval=" + str(retval.result))

        # start 'telnet' client with returned address and port
        s = TcpClient(retval.result['ip_address'], retval.result['port'])

        # look for and swallow 'Username' prompt
        try_count = 0
        while s.peek_at_buffer().find("Username: "******"WANT 'Username:'******'It took longer than 10 seconds to get a Username: prompt')
        s.remove_from_buffer("Username: "******"bob\r\n", "1")
        
        # look for and swallow 'token' prompt
        try_count = 0
        while s.peek_at_buffer().find("token: ") == -1:
            log.debug("WANT 'token: ' READ ==>" + str(s.peek_at_buffer()))
            gevent.sleep(1)
            try_count += 1
            if try_count > 10:
                raise Timeout('It took longer than 10 seconds to get a token: prompt')
        s.remove_from_buffer("token: ")
        # send the returned token
        s.send_data(retval.result['token'] + "\r\n", "1")
        
        # look for and swallow telnet negotiation string
        try_count = 0
        while s.peek_at_buffer().find(WILL_ECHO_CMD) == -1:
            log.debug("WANT %s READ ==> %s" %(WILL_ECHO_CMD, str(s.peek_at_buffer())))
            gevent.sleep(1)
            try_count += 1
            if try_count > 10:
                raise Timeout('It took longer than 10 seconds to get the telnet negotiation string')
        s.remove_from_buffer(WILL_ECHO_CMD)
        # send the telnet negotiation response string
        s.send_data(DO_ECHO_CMD, "1")

        # look for and swallow 'connected' indicator
        try_count = 0
        while s.peek_at_buffer().find("connected\r\n") == -1:
            log.debug("WANT 'connected\n' READ ==>" + str(s.peek_at_buffer()))
            gevent.sleep(1)
            try_count += 1
            if try_count > 10:
                raise Timeout('It took longer than 10 seconds to get a connected prompt')
        s.remove_from_buffer("connected\r\n")
        
        # try to interact with the instrument 
        try_count = 0
        while ((s.peek_at_buffer().find("Enter <CTRL>-<C> now to wake up") == -1) and
              (s.peek_at_buffer().find("Main Menu") == -1)):
            self.assertNotEqual(try_count, 5)
            try_count += 1
            log.debug("WANT %s or %s; READ ==> %s" %("'Enter <CTRL>-<C> now to wake up'", "'Main Menu'", str(s.peek_at_buffer())))
            s.send_data("\r\n\r\n", "1")
            gevent.sleep(2)
    def test_remove_from_buffer(self):
        """
        Test the remove from buffer method.  Verify that all bytes before the match string are removed.
        """
        client = TcpClient()
        target = "MATCH"

        # Remove with an None target
        self.assertFalse(client.remove_from_buffer(None))

        # Remove with a zero length target
        self.assertFalse(client.remove_from_buffer(""))

        # Remove from buffer when buffer empty
        client.buf = None
        self.assertFalse(client.remove_from_buffer(target))
        self.assertIsNone(client.buf)

        # Remove from buffer when target the only thing in the list
        client.buf = target
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(len(client.buf), 0)

        # Remove from buffer when target the last thing in the list
        client.buf = "foo" + target
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(len(client.buf), 0)

        # Remove from buffer when target in the middle
        client.buf = "foo" + target + "remains"
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(client.buf, "remains")

        # Remove from buffer when target not in the buff
        client.buf = "target not in the list"
        self.assertFalse(client.remove_from_buffer(target))
        self.assertEqual(client.buf, "target not in the list")

        # Remove from buffer when target in the list more than once        client.buf = "foo" + target + "remains"
        client.buf = "foo" + target + "remains" + target
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(client.buf, "remains" + target)