示例#1
0
class NetnsMonitor(object):
    def __init__(self, project, vm_obj, vmi_obj, ifname, **kwargs):
        self.project = project
        self.vm = vm_obj
        self.vmi = vmi_obj
        self.ifname = ifname
        self.vmi_out = kwargs.get('vmi_out', None)
        self.ifname_out = kwargs.get('ifname_out', None)

        vrouter_semaphore = gevent.lock.Semaphore()
        self.vrouter_client = ContrailVRouterApi(doconnect=True,
                                                 semaphore=vrouter_semaphore)
        self.vrouter_client.connect()
        self.vrouter_agent_connection = self.vrouter_client._client

    def keepalive(self):
        self.vrouter_client.periodic_connection_check()
        current_agent_connection = self.vrouter_client._client
        if not current_agent_connection:
            # vrouter agent is down, Try checking...
            return
        if self.vrouter_agent_connection != current_agent_connection:
            # Initial connection object to vrouter agent is different from
            # the current object, vrouter agent is restarted, plug the netns
            # vif's again.
            interface_register(self.vm,
                               self.vmi,
                               self.ifname,
                               project=self.project)
            if self.vmi_out:
                interface_register(self.vm,
                                   self.vmi_out,
                                   self.ifname_out,
                                   project=self.project)
            self.vrouter_agent_connection = current_agent_connection

    def monitor(self):
        while True:
            gevent.sleep(INTERVAL)
            self.keepalive()
class NetnsMonitor(object):
    def __init__(self, project, vm_obj, vmi_obj, ifname, **kwargs):
        self.project = project
        self.vm = vm_obj
        self.vmi = vmi_obj
        self.ifname = ifname
        self.vmi_out = kwargs.get('vmi_out', None)
        self.ifname_out = kwargs.get('ifname_out', None)

        vrouter_semaphore = gevent.lock.Semaphore()
        self.vrouter_client = ContrailVRouterApi(doconnect=True,
                                                 semaphore=vrouter_semaphore)
        self.vrouter_client.connect()
        self.vrouter_agent_connection = self.vrouter_client._client

    def keepalive(self):
         self.vrouter_client.periodic_connection_check()
         current_agent_connection = self.vrouter_client._client
         if not current_agent_connection:
             # vrouter agent is down, Try checking...
             return
         if self.vrouter_agent_connection != current_agent_connection:
             # Initial connection object to vrouter agent is different from
             # the current object, vrouter agent is restarted, plug the netns
             # vif's again.
             interface_register(self.vm, self.vmi, self.ifname,
                                project=self.project)
             if self.vmi_out:
                 interface_register(self.vm, self.vmi_out, self.ifname_out,
                                    project=self.project)
             self.vrouter_agent_connection = current_agent_connection

    def monitor(self):
        while True:
            gevent.sleep(INTERVAL)
            self.keepalive()
class VRouterApiTest(unittest.TestCase):
    def setUp(self):
        self._api = ContrailVRouterApi()

    def test_create_port(self):
        mock_client = mock.Mock()
        self._api._rpc_client_instance = mock.MagicMock(
            name='rpc_client_instance')
        self._api._rpc_client_instance.return_value = mock_client
        self._api.add_port(str(uuid.uuid1()), str(uuid.uuid1()), 'tapX',
                           'aa:bb:cc:ee:ff:00')
        self.assertTrue(mock_client.AddPort.called)

    def test_delete_port(self):
        mock_client = mock.Mock()
        self._api._rpc_client_instance = mock.MagicMock(
            name='rpc_client_instance')
        self._api._rpc_client_instance.return_value = mock_client

        vm_uuid = uuid.uuid1()
        vif_uuid = uuid.uuid1()
        self._api.add_port(str(vm_uuid), str(vif_uuid), 'tapX',
                           'aa:bb:cc:ee:ff:00')
        self.assertTrue(mock_client.AddPort.called)
        self.assertTrue(self._api._ports[vif_uuid])

        self._api.delete_port(str(vif_uuid))
        self.assertTrue(mock_client.DeletePort.called)

    def test_resynchronize(self):
        self._api._rpc_client_instance = mock.MagicMock(
            name='rpc_client_instance')
        self._api._rpc_client_instance.return_value = None

        vm_uuid = str(uuid.uuid1())
        vif_uuid = str(uuid.uuid1())
        port1 = ttypes.Port(self._api._uuid_string_to_hex(vif_uuid),
                           self._api._uuid_string_to_hex(vm_uuid),
                           'tapX', '0.0.0.0', [0] * 16, 'aa:bb:cc:ee:ff:00')
        self._api.add_port(vm_uuid, vif_uuid, 'tapX', 'aa:bb:cc:ee:ff:00')

        mock_client = mock.Mock()
        self._api._rpc_client_instance.return_value = mock_client
        self._api.periodic_connection_check()
        mock_client.AddPort.assert_called_with([port1])

    def test_resynchronize_multi_ports(self):
        self._api._rpc_client_instance = mock.MagicMock(
            name='rpc_client_instance')
        self._api._rpc_client_instance.return_value = None

        vm_uuid = str(uuid.uuid1())
        vif_uuid = str(uuid.uuid1())
        port1 = ttypes.Port(self._api._uuid_string_to_hex(vif_uuid),
                            self._api._uuid_string_to_hex(vm_uuid),
                            'tapX', '0.0.0.0', [0] * 16, 'aa:bb:cc:ee:ff:00')
        self._api.add_port(vm_uuid, vif_uuid, 'tapX', 'aa:bb:cc:ee:ff:00')

        vm_uuid = str(uuid.uuid1())
        vif_uuid = str(uuid.uuid1())
        port2 = ttypes.Port(self._api._uuid_string_to_hex(vif_uuid),
                            self._api._uuid_string_to_hex(vm_uuid),
                            'tapY', '0.0.0.0', [0] * 16, '11:22:33:44:55:66')
        self._api.add_port(vm_uuid, vif_uuid, 'tapY', '11:22:33:44:55:66')

        mock_client = mock.Mock()
        self._api._rpc_client_instance.return_value = mock_client
        self._api.connect()
        self._api._resynchronize()
        mock_client.AddPort.assert_called_with([port1, port2])

    def test_additional_arguments(self):
        mock_client = mock.Mock()
        self._api._rpc_client_instance = mock.MagicMock(
            name='rpc_client_instance')
        self._api._rpc_client_instance.return_value = mock_client
        vif_uuid = uuid.uuid1()
        network_uuid = uuid.uuid1()
        project_id = uuid.uuid1().hex
        self._api.add_port(str(uuid.uuid1()), str(vif_uuid), 'tapX',
                           'aa:bb:cc:ee:ff:00',
                           network_uuid=str(network_uuid),
                           vm_project_id=project_id)
        self.assertTrue(mock_client.AddPort.called)
        port = self._api._ports[vif_uuid]
        self.assertEqual(self._api._uuid_to_hex(network_uuid),
                         port.vn_id)
        self.assertEqual(self._api._uuid_string_to_hex(project_id),
                         port.vm_project_id)
示例#4
0
class VRouterApiTest(unittest.TestCase):
    def setUp(self):
        self._api = ContrailVRouterApi()

    def test_create_port(self):
        mock_client = mock.Mock()
        self._api._rpc_client_instance = mock.MagicMock(
            name='rpc_client_instance')
        self._api._rpc_client_instance.return_value = mock_client
        self._api.add_port(str(uuid.uuid1()), str(uuid.uuid1()), 'tapX',
                           'aa:bb:cc:ee:ff:00')
        self.assertTrue(mock_client.AddPort.called)

    def test_delete_port(self):
        mock_client = mock.Mock()
        self._api._rpc_client_instance = mock.MagicMock(
            name='rpc_client_instance')
        self._api._rpc_client_instance.return_value = mock_client

        vm_uuid = uuid.uuid1()
        vif_uuid = uuid.uuid1()
        self._api.add_port(str(vm_uuid), str(vif_uuid), 'tapX',
                           'aa:bb:cc:ee:ff:00')
        self.assertTrue(mock_client.AddPort.called)
        self.assertTrue(self._api._ports[vif_uuid])

        self._api.delete_port(str(vif_uuid))
        self.assertTrue(mock_client.DeletePort.called)

    def test_resynchronize(self):
        self._api._rpc_client_instance = mock.MagicMock(
            name='rpc_client_instance')
        self._api._rpc_client_instance.return_value = None

        vm_uuid = str(uuid.uuid1())
        vif_uuid = str(uuid.uuid1())
        port1 = ttypes.Port(self._api._uuid_string_to_hex(vif_uuid),
                            self._api._uuid_string_to_hex(vm_uuid), 'tapX',
                            '0.0.0.0', [0] * 16, 'aa:bb:cc:ee:ff:00')
        self._api.add_port(vm_uuid, vif_uuid, 'tapX', 'aa:bb:cc:ee:ff:00')

        mock_client = mock.Mock()
        self._api._rpc_client_instance.return_value = mock_client
        self._api.periodic_connection_check()
        mock_client.AddPort.assert_called_with([port1])

    def test_resynchronize_multi_ports(self):
        self._api._rpc_client_instance = mock.MagicMock(
            name='rpc_client_instance')
        self._api._rpc_client_instance.return_value = None

        vm_uuid = str(uuid.uuid1())
        vif_uuid = str(uuid.uuid1())
        port1 = ttypes.Port(self._api._uuid_string_to_hex(vif_uuid),
                            self._api._uuid_string_to_hex(vm_uuid), 'tapX',
                            '0.0.0.0', [0] * 16, 'aa:bb:cc:ee:ff:00')
        self._api.add_port(vm_uuid, vif_uuid, 'tapX', 'aa:bb:cc:ee:ff:00')

        vm_uuid = str(uuid.uuid1())
        vif_uuid = str(uuid.uuid1())
        port2 = ttypes.Port(self._api._uuid_string_to_hex(vif_uuid),
                            self._api._uuid_string_to_hex(vm_uuid), 'tapY',
                            '0.0.0.0', [0] * 16, '11:22:33:44:55:66')
        self._api.add_port(vm_uuid, vif_uuid, 'tapY', '11:22:33:44:55:66')

        mock_client = mock.Mock()
        self._api._rpc_client_instance.return_value = mock_client
        self._api.connect()
        self._api._resynchronize()
        mock_client.AddPort.assert_called_with([port1, port2])

    def test_additional_arguments(self):
        mock_client = mock.Mock()
        self._api._rpc_client_instance = mock.MagicMock(
            name='rpc_client_instance')
        self._api._rpc_client_instance.return_value = mock_client
        vif_uuid = uuid.uuid1()
        network_uuid = uuid.uuid1()
        project_id = uuid.uuid1().hex
        self._api.add_port(str(uuid.uuid1()),
                           str(vif_uuid),
                           'tapX',
                           'aa:bb:cc:ee:ff:00',
                           vn_id=str(network_uuid),
                           vm_project_id=project_id)
        self.assertTrue(mock_client.AddPort.called)
        port = self._api._ports[vif_uuid]
        self.assertEqual(self._api._uuid_to_hex(network_uuid), port.vn_id)
        self.assertEqual(self._api._uuid_string_to_hex(project_id),
                         port.vm_project_id)