コード例 #1
0
 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'])
コード例 #2
0
 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'}])
コード例 #3
0
 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'])
コード例 #4
0
 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}])
コード例 #5
0
 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')
コード例 #6
0
 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'}])
コード例 #7
0
 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'}])
コード例 #8
0
 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)
コード例 #9
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)
コード例 #10
0
 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)
コード例 #11
0
 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=())
コード例 #12
0
 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=())
コード例 #13
0
 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=())
コード例 #14
0
 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'}])
コード例 #15
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)
コード例 #16
0
 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')