Beispiel #1
0
 def test_dhcp_binding_with_disable_enable_dhcp(self):
     # Test if DHCP binding is preserved after DHCP is disabled and
     # re-enabled on a subnet.
     with self.subnet(enable_dhcp=True) as subnet:
         device_owner = constants.DEVICE_OWNER_COMPUTE_PREFIX + 'None'
         device_id = uuidutils.generate_uuid()
         with self.port(subnet=subnet,
                        device_owner=device_owner,
                        device_id=device_id) as port:
             ip = port['port']['fixed_ips'][0]['ip_address']
             dhcp_bindings = nsx_db.get_nsx_dhcp_bindings(
                 context.get_admin_context().session, port['port']['id'])
             dhcp_service = dhcp_bindings[0]['nsx_service_id']
             self.assertEqual(1, len(dhcp_bindings))
             self.assertEqual(ip, dhcp_bindings[0]['ip_address'])
             # Disable DHCP on subnet.
             data = {'subnet': {'enable_dhcp': False}}
             self.plugin.update_subnet(context.get_admin_context(),
                                       subnet['subnet']['id'], data)
             dhcp_bindings = nsx_db.get_nsx_dhcp_bindings(
                 context.get_admin_context().session, port['port']['id'])
             self.assertEqual([], dhcp_bindings)
             # Re-enable DHCP on subnet.
             data = {'subnet': {'enable_dhcp': True}}
             self.plugin.update_subnet(context.get_admin_context(),
                                       subnet['subnet']['id'], data)
             dhcp_bindings = nsx_db.get_nsx_dhcp_bindings(
                 context.get_admin_context().session, port['port']['id'])
             self.assertEqual(1, len(dhcp_bindings))
             self.assertEqual(ip, dhcp_bindings[0]['ip_address'])
             # The DHCP service ID should be different because a new
             # logical DHCP server is created for re-enabling DHCP.
             self.assertNotEqual(dhcp_service,
                                 dhcp_bindings[0]['nsx_service_id'])
    def _verify_dhcp_binding(self, subnet, port_data, update_data,
                             assert_data):
        # Verify if DHCP binding is updated.

        with mock.patch(
                'vmware_nsx.nsxlib.v3.resources.LogicalDhcpServer.update_binding'
        ) as update_dhcp_binding:
            device_owner = constants.DEVICE_OWNER_COMPUTE_PREFIX + 'None'
            device_id = uuidutils.generate_uuid()
            with self.port(subnet=subnet,
                           device_owner=device_owner,
                           device_id=device_id,
                           **port_data) as port:
                # Retrieve the DHCP binding info created in the DB for the
                # new port.
                dhcp_binding = nsx_db.get_nsx_dhcp_bindings(
                    context.get_admin_context().session, port['port']['id'])[0]
                # Update the port with provided data.
                self.plugin.update_port(context.get_admin_context(),
                                        port['port']['id'], update_data)
                binding_data = {
                    'mac_address': port['port']['mac_address'],
                    'ip_address': port['port']['fixed_ips'][0]['ip_address']
                }
                # Extend basic binding data with to-be-asserted data.
                binding_data.update(assert_data)
                # Verify the update call.
                update_dhcp_binding.assert_called_once_with(
                    dhcp_binding['nsx_service_id'],
                    dhcp_binding['nsx_binding_id'], **binding_data)
 def test_dhcp_binding_with_update_port_delete_ip(self):
     # Test if DHCP binding is deleted when the IP of the associated
     # compute port is deleted.
     with mock.patch.object(nsx_resources.LogicalDhcpServer,
                            'delete_binding') as delete_dhcp_binding:
         with self.subnet(enable_dhcp=True) as subnet:
             device_owner = constants.DEVICE_OWNER_COMPUTE_PREFIX + 'None'
             device_id = uuidutils.generate_uuid()
             with self.port(subnet=subnet,
                            device_owner=device_owner,
                            device_id=device_id) as port:
                 dhcp_binding = nsx_db.get_nsx_dhcp_bindings(
                     context.get_admin_context().session,
                     port['port']['id'])[0]
                 data = {
                     'port': {
                         'fixed_ips': [],
                         'admin_state_up': False,
                         secgrp.SECURITYGROUPS: []
                     }
                 }
                 self.plugin.update_port(context.get_admin_context(),
                                         port['port']['id'], data)
                 delete_dhcp_binding.assert_called_once_with(
                     dhcp_binding['nsx_service_id'],
                     dhcp_binding['nsx_binding_id'])
 def test_dhcp_binding_with_delete_port(self):
     # Test if DHCP binding is removed when the associated compute port
     # is deleted.
     with mock.patch.object(nsx_resources.LogicalDhcpServer,
                            'delete_binding') as delete_dhcp_binding:
         with self.subnet(enable_dhcp=True) as subnet:
             device_owner = constants.DEVICE_OWNER_COMPUTE_PREFIX + 'None'
             device_id = uuidutils.generate_uuid()
             with self.port(subnet=subnet,
                            device_owner=device_owner,
                            device_id=device_id) as port:
                 dhcp_binding = nsx_db.get_nsx_dhcp_bindings(
                     context.get_admin_context().session,
                     port['port']['id'])[0]
                 self.plugin.delete_port(context.get_admin_context(),
                                         port['port']['id'])
                 delete_dhcp_binding.assert_called_once_with(
                     dhcp_binding['nsx_service_id'],
                     dhcp_binding['nsx_binding_id'])