Пример #1
0
    def test_dhcp_service_with_create_dhcp_subnet_bulk(self):
        # Test if DHCP service is enabled on all networks after a
        # create_subnet_bulk operation.
        with self.network() as network1, self.network() as network2:
            subnet1 = self._make_subnet_data(
                network_id=network1['network']['id'],
                cidr='10.0.0.0/24',
                tenant_id=network1['network']['tenant_id'])
            subnet2 = self._make_subnet_data(
                network_id=network2['network']['id'],
                cidr='20.0.0.0/24',
                tenant_id=network2['network']['tenant_id'])
            subnets = {'subnets': [subnet1, subnet2]}

            with mock.patch.object(
                    self.plugin, '_post_create_subnet') as post_create_subnet:
                self.plugin.create_subnet_bulk(context.get_admin_context(),
                                               subnets)
                # Check if post_create function has been called for
                # both subnets.
                self.assertEqual(len(subnets['subnets']),
                                 post_create_subnet.call_count)

            # Check if the bindings to backend DHCP entries are created.
            dhcp_service = nsx_db.get_nsx_service_binding(
                context.get_admin_context().session, network1['network']['id'],
                nsx_constants.SERVICE_DHCP)
            self.assertTrue(dhcp_service)
            dhcp_service = nsx_db.get_nsx_service_binding(
                context.get_admin_context().session, network2['network']['id'],
                nsx_constants.SERVICE_DHCP)
            self.assertTrue(dhcp_service)
Пример #2
0
    def test_dhcp_service_with_create_dhcp_subnet_bulk_failure(self):
        # Test if user-provided rollback function is invoked when
        # exception occurred during a create_subnet_bulk operation.
        with self.network() as network1, self.network() as network2:
            subnet1 = self._make_subnet_data(
                network_id=network1['network']['id'],
                cidr='10.0.0.0/24',
                tenant_id=network1['network']['tenant_id'])
            subnet2 = self._make_subnet_data(
                network_id=network2['network']['id'],
                cidr='20.0.0.0/24',
                tenant_id=network2['network']['tenant_id'])
            subnets = {'subnets': [subnet1, subnet2]}

            # Inject an exception on the second create_subnet call.
            orig_create_subnet = self.plugin.create_subnet
            with mock.patch.object(self.plugin,
                                   'create_subnet') as create_subnet:

                def side_effect(*args, **kwargs):
                    return self._fail_second_call(create_subnet,
                                                  orig_create_subnet, *args,
                                                  **kwargs)

                create_subnet.side_effect = side_effect

                with mock.patch.object(self.plugin,
                                       '_rollback_subnet') as rollback_subnet:
                    try:
                        self.plugin.create_subnet_bulk(
                            context.get_admin_context(), subnets)
                    except Exception:
                        pass
                    # Check if rollback function has been called for
                    # the subnet in the first network.
                    rollback_subnet.assert_called_once_with(mock.ANY, mock.ANY)
                    subnet_arg = rollback_subnet.call_args[0][0]
                    self.assertEqual(network1['network']['id'],
                                     subnet_arg['network_id'])

                # Check if the bindings to backend DHCP entries are removed.
                dhcp_service = nsx_db.get_nsx_service_binding(
                    context.get_admin_context().session,
                    network1['network']['id'], nsx_constants.SERVICE_DHCP)
                self.assertFalse(dhcp_service)
                dhcp_service = nsx_db.get_nsx_service_binding(
                    context.get_admin_context().session,
                    network2['network']['id'], nsx_constants.SERVICE_DHCP)
                self.assertFalse(dhcp_service)
Пример #3
0
 def test_dhcp_binding_with_create_port(self):
     # Test if DHCP binding is added when a compute port is created.
     with mock.patch.object(nsx_resources.LogicalDhcpServer,
                            'create_binding',
                            return_value={"id": uuidutils.generate_uuid()
                                          }) as create_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_service = nsx_db.get_nsx_service_binding(
                     context.get_admin_context().session,
                     subnet['subnet']['network_id'],
                     nsx_constants.SERVICE_DHCP)
                 ip = port['port']['fixed_ips'][0]['ip_address']
                 hostname = 'host-%s' % ip.replace('.', '-')
                 options = {
                     'option121': {
                         'static_routes': [{
                             'network':
                             '%s' % cfg.CONF.nsx_v3.native_metadata_route,
                             'next_hop':
                             ip
                         }]
                     }
                 }
                 create_dhcp_binding.assert_called_once_with(
                     dhcp_service['nsx_service_id'],
                     port['port']['mac_address'], ip, hostname,
                     cfg.CONF.nsx_v3.dhcp_lease_time, options)
Пример #4
0
 def test_dhcp_service_with_update_dhcp_port(self):
     # Test if DHCP server IP is updated when the corresponding DHCP port
     # IP is changed.
     with mock.patch.object(nsx_resources.LogicalDhcpServer,
                            'update') as update_logical_dhcp_server:
         with self.subnet(cidr='10.0.0.0/24', enable_dhcp=True) as subnet:
             dhcp_service = nsx_db.get_nsx_service_binding(
                 context.get_admin_context().session,
                 subnet['subnet']['network_id'], nsx_constants.SERVICE_DHCP)
             port = self.plugin.get_port(context.get_admin_context(),
                                         dhcp_service['port_id'])
             old_ip = port['fixed_ips'][0]['ip_address']
             new_ip = str(netaddr.IPAddress(old_ip) + 1)
             data = {
                 'port': {
                     'fixed_ips': [{
                         'subnet_id': subnet['subnet']['id'],
                         'ip_address': new_ip
                     }]
                 }
             }
             self.plugin.update_port(context.get_admin_context(),
                                     dhcp_service['port_id'], data)
             update_logical_dhcp_server.assert_called_once_with(
                 dhcp_service['nsx_service_id'], server_ip=new_ip)