def get_api(self):
     socket = mock.MagicMock()
     base = base_api.Connection(socket)
     base.receive_sentence = mock.Mock(return_value=[b'!done', b'.tag=1'])
     communicator = api_communicator.ApiCommunicator(base)
     routeros_api = api.RouterOsApi(communicator)
     return routeros_api
示例#2
0
 def test_sending(self, ):
     socket = mock.Mock()
     connection = base_api.Connection(socket)
     connection.send_sentence([b'foo', b'bar'])
     expected = [
         mock.call(b'\x03foo'),
         mock.call(b'\x03bar'),
         mock.call(b'\x00')
     ]
     self.assertEqual(expected, socket.send.mock_calls)
示例#3
0
 def get_api(self):
     if not self.connected:
         self.socket = api_socket.get_socket(self.host,
                                             self.port,
                                             timeout=self.socket_timeout)
         base = base_api.Connection(self.socket)
         communicator = api_communicator.ApiCommunicator(base)
         self.api = RouterOsApi(communicator)
         for handler in self._get_exception_handlers():
             communicator.add_exception_handler(handler)
         self.api.login(self.username, self.password)
         self.connected = True
     return self.api
示例#4
0
 def test_receiving(self):
     socket = mock.Mock()
     socket.receive.side_effect = [b'\x03', b'foo', b'\x03', b'bar',
                                   b'\x00']
     connection = base_api.Connection(socket)
     result = connection.receive_sentence()
     self.assertEqual([b'foo', b'bar'], result)
     expected = [
         mock.call(1),
         mock.call(3),
         mock.call(1),
         mock.call(3),
         mock.call(1),
     ]
     self.assertEqual(expected, socket.receive.mock_calls)