def test_set_call(self): base = mock.Mock() base.receive_sentence.return_value = [b'!done', b'.tag=1'] communicator = api_communicator.ApiCommunicator(base) communicator.call('/interface/', 'set', {'x': b'y'}) base.send_sentence.assert_called_once_with( [b'/interface/set', b'=x=y', b'.tag=1'])
def test_normal_call(self): base = mock.Mock() base.receive_sentence.side_effect = [[b'!re', b'=x=y', b'.tag=1'], [b'!done', b'.tag=1']] communicator = api_communicator.ApiCommunicator(base) response = communicator.call('/interface/', 'print').get() self.assertEqual(response, [{'x': b'y'}])
def test_query_call(self): base = mock.Mock() base.receive_sentence.return_value = [b'!done', b'.tag=1'] communicator = api_communicator.ApiCommunicator(base) communicator.call('/interface/', 'print', queries={'x': 'y'}).get() base.send_sentence.assert_called_once_with( [b'/interface/print', b'?x=y', b'.tag=1'])
def test_boolean_resource_get(self): communicator = mock.Mock() response = base.AsynchronousResponse([{'boolean': b'yes'}]) communicator.call.return_value.get.return_value = response some_resource = resource.RouterOsResource(communicator, '/boolean', BOOLEAN_STRUCTURE) result = some_resource.get() self.assertEqual(result, [{'boolean': True}])
def test_login_call(self): base = mock.Mock() base.receive_sentence.return_value = [ b'!done', b'=ret=some-hex', b'.tag=1' ] communicator = api_communicator.ApiCommunicator(base) response = communicator.call('/', 'login').get() self.assertEqual(response.done_message['ret'], b'some-hex')
def test_bytes_resource_get(self): communicator = mock.Mock() response = base.AsynchronousResponse([{'bytes': b'y'}]) communicator.call.return_value.get.return_value = response some_resource = resource.RouterOsResource(communicator, '/bytes', BYTES_STRUCTURE) result = some_resource.get() self.assertEqual(result, [{'bytes': b'y'}])
def test_unknown_resource_get(self): communicator = mock.Mock() response = base.AsynchronousResponse([{'x': b'y'}]) communicator.call.return_value.get.return_value = response some_resource = resource.RouterOsResource(communicator, '/unknown', structure.default_structure) result = some_resource.get() self.assertEqual(result, [{'x': 'y'}])
def test_error_call(self): base = mock.Mock() base.receive_sentence.side_effect = [[ b'!trap', b'=message=y', b'.tag=1' ], [b'!done', b'.tag=1']] communicator = api_communicator.ApiCommunicator(base) promise = communicator.call('/file/', 'print') self.assertRaises(exceptions.RouterOsApiCommunicationError, promise.get)
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)
def test_async_error_raises_when_synchronizing(self): base = mock.Mock() base.receive_sentence.side_effect = [[ b'!trap', b'=message=m', b'.tag=1' ], [b'!done', b'.tag=2'], [b'!done', b'.tag=1']] communicator = api_communicator.ApiCommunicator(base) promise = communicator.call('/interface/', 'print') communicator.call('/interface/', 'print').get() self.assertRaises(exceptions.RouterOsApiCommunicationError, promise.get)
def test_boolean_resource_set(self): communicator = mock.Mock() some_resource = resource.RouterOsResource(communicator, '/boolean', BOOLEAN_STRUCTURE) some_resource.set(boolean=True) communicator.call.assert_called_with('/boolean/', 'set', arguments={'boolean': b'yes'}, queries={}, additional_queries=())
def test_bytes_resource_set(self): communicator = mock.Mock() some_resource = resource.RouterOsResource(communicator, '/bytes', BYTES_STRUCTURE) some_resource.set(bytes=b'y') communicator.call.assert_called_with('/bytes/', 'set', arguments={'bytes': b'y'}, queries={}, additional_queries=())
def test_unknown_resource_set(self): communicator = mock.Mock() some_resource = resource.RouterOsResource(communicator, '/unknown', structure.default_structure) some_resource.set(x='y') communicator.call.assert_called_with('/unknown/', 'set', arguments={'x': b'y'}, queries={}, additional_queries=())
def test_mixed_calls(self): base = mock.Mock() base.receive_sentence.side_effect = [[b'!re', b'=x1=y1', b'.tag=1'], [b'!re', b'=x2=y2', b'.tag=2'], [b'!done', b'.tag=1'], [b'!done', b'.tag=2']] communicator = api_communicator.ApiCommunicator(base) promise = communicator.call('/interface/', 'print') response2 = communicator.call('/interface/', 'print').get() response1 = promise.get() self.assertEqual(response1, [{'x1': b'y1'}]) self.assertEqual(response2, [{'x2': b'y2'}])
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)
def test_socket(self): inner = mock.Mock() wrapper = api_socket.SocketWrapper(inner) inner.recv.side_effect = [socket.error(api_socket.EINTR), 'bytes'] self.assertEqual(wrapper.receive(5), 'bytes')