"Fossies" - the Fresh Open Source Software Archive

Member "neutron-14.0.3/neutron/tests/fullstack/resources/config.py" (22 Oct 2019, 16143 Bytes) of package /linux/misc/openstack/neutron-14.0.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "config.py": 14.0.2_vs_14.0.3.

    1 # Copyright 2015 Red Hat, Inc.
    2 #
    3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 #    not use this file except in compliance with the License. You may obtain
    5 #    a copy of the License at
    6 #
    7 #         http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 #    Unless required by applicable law or agreed to in writing, software
   10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 #    License for the specific language governing permissions and limitations
   13 #    under the License.
   14 
   15 import os
   16 import shutil
   17 import tempfile
   18 
   19 from neutron_lib import constants
   20 
   21 from neutron.common import constants as c_const
   22 from neutron.common import utils
   23 from neutron.plugins.ml2.extensions import qos as qos_ext
   24 from neutron.tests import base
   25 from neutron.tests.common import config_fixtures
   26 from neutron.tests.common.exclusive_resources import port
   27 from neutron.tests.common import helpers as c_helpers
   28 from neutron.tests.fullstack import base as fullstack_base
   29 
   30 PHYSICAL_NETWORK_NAME = "physnet1"
   31 MINIMUM_BANDWIDTH_INGRESS_KBPS = 1000
   32 MINIMUM_BANDWIDTH_EGRESS_KBPS = 1000
   33 
   34 
   35 class ConfigFixture(config_fixtures.ConfigFileFixture):
   36     """A fixture that holds an actual Neutron configuration.
   37 
   38     Note that 'self.config' is intended to only be updated once, during
   39     the constructor, so if this fixture is re-used (setUp is called twice),
   40     then the dynamic configuration values won't change. The correct usage
   41     is initializing a new instance of the class.
   42     """
   43     def __init__(self, env_desc, host_desc, temp_dir, base_filename):
   44         super(ConfigFixture, self).__init__(
   45             base_filename, config_fixtures.ConfigDict(), temp_dir)
   46         self.env_desc = env_desc
   47         self.host_desc = host_desc
   48 
   49     def _generate_namespace_suffix(self):
   50         return utils.get_rand_name(prefix='test')
   51 
   52 
   53 class NeutronConfigFixture(ConfigFixture):
   54 
   55     def __init__(self, env_desc, host_desc, temp_dir,
   56                  connection, rabbitmq_environment):
   57         super(NeutronConfigFixture, self).__init__(
   58             env_desc, host_desc, temp_dir, base_filename='neutron.conf')
   59 
   60         self.config.update({
   61             'DEFAULT': {
   62                 'host': self._generate_host(),
   63                 'state_path': self._generate_state_path(self.temp_dir),
   64                 'api_paste_config': self._generate_api_paste(),
   65                 'core_plugin': 'ml2',
   66                 'service_plugins': env_desc.service_plugins,
   67                 'auth_strategy': 'noauth',
   68                 'debug': 'True',
   69                 'global_physnet_mtu': str(env_desc.global_mtu),
   70                 'agent_down_time': str(env_desc.agent_down_time),
   71                 'transport_url':
   72                     'rabbit://%(user)s:%(password)s@%(host)s:5672/%(vhost)s' %
   73                     {'user': rabbitmq_environment.user,
   74                      'password': rabbitmq_environment.password,
   75                      'host': rabbitmq_environment.host,
   76                      'vhost': rabbitmq_environment.vhost},
   77                 'api_workers': '2',
   78             },
   79             'database': {
   80                 'connection': connection,
   81             },
   82             'oslo_concurrency': {
   83                 'lock_path': '$state_path/lock',
   84             },
   85             'agent': {
   86                 'report_interval': str(env_desc.agent_down_time / 2.0),
   87                 'log_agent_heartbeats': 'True',
   88             },
   89         })
   90         policy_file = self._generate_policy_json()
   91         if policy_file:
   92             self.config['oslo_policy'] = {'policy_file': policy_file}
   93 
   94         # Set root_helper/root_helper_daemon only when env var is set
   95         root_helper = os.environ.get('OS_ROOTWRAP_CMD')
   96         if root_helper:
   97             self.config['agent']['root_helper'] = root_helper
   98         root_helper_daemon = os.environ.get('OS_ROOTWRAP_DAEMON_CMD')
   99         if root_helper_daemon:
  100             self.config['agent']['root_helper_daemon'] = root_helper_daemon
  101         if env_desc.router_scheduler:
  102             self.config['DEFAULT']['router_scheduler_driver'] = (
  103                 env_desc.router_scheduler)
  104 
  105     def _setUp(self):
  106         self.config['DEFAULT'].update({
  107             'bind_port': self.useFixture(
  108                 port.ExclusivePort(constants.PROTO_NAME_TCP)).port
  109         })
  110         super(NeutronConfigFixture, self)._setUp()
  111 
  112     def _generate_host(self):
  113         return utils.get_rand_name(prefix='host-')
  114 
  115     def _generate_state_path(self, temp_dir):
  116         # Assume that temp_dir will be removed by the caller
  117         self.state_path = tempfile.mkdtemp(prefix='state_path', dir=temp_dir)
  118         return self.state_path
  119 
  120     def _generate_api_paste(self):
  121         return c_helpers.find_sample_file('api-paste.ini')
  122 
  123     def _generate_policy_json(self):
  124         return c_helpers.find_sample_file('policy.json')
  125 
  126     def get_host(self):
  127         return self.config['DEFAULT']['host']
  128 
  129 
  130 class ML2ConfigFixture(ConfigFixture):
  131 
  132     def __init__(self, env_desc, host_desc, temp_dir, tenant_network_types):
  133         super(ML2ConfigFixture, self).__init__(
  134             env_desc, host_desc, temp_dir, base_filename='ml2_conf.ini')
  135 
  136         mechanism_drivers = self.env_desc.mech_drivers
  137         if self.env_desc.l2_pop:
  138             mechanism_drivers += ',l2population'
  139 
  140         self.config.update({
  141             'ml2': {
  142                 'tenant_network_types': tenant_network_types,
  143                 'mechanism_drivers': mechanism_drivers,
  144             },
  145             'ml2_type_vlan': {
  146                 'network_vlan_ranges': PHYSICAL_NETWORK_NAME + ':1000:2999',
  147             },
  148             'ml2_type_gre': {
  149                 'tunnel_id_ranges': '1:1000',
  150             },
  151             'ml2_type_vxlan': {
  152                 'vni_ranges': '1001:2000',
  153             },
  154         })
  155 
  156         extension_drivers = ['port_security']
  157         if env_desc.qos:
  158             extension_drivers.append(qos_ext.QOS_EXT_DRIVER_ALIAS)
  159         self.config['ml2']['extension_drivers'] = ','.join(extension_drivers)
  160 
  161 
  162 class OVSConfigFixture(ConfigFixture):
  163 
  164     def __init__(self, env_desc, host_desc, temp_dir, local_ip, **kwargs):
  165         super(OVSConfigFixture, self).__init__(
  166             env_desc, host_desc, temp_dir,
  167             base_filename='openvswitch_agent.ini')
  168 
  169         self.tunneling_enabled = self.env_desc.tunneling_enabled
  170         ext_dev = utils.get_rand_device_name(prefix='br-eth')
  171         self.config.update({
  172             'ovs': {
  173                 'local_ip': local_ip,
  174                 'integration_bridge': self._generate_integration_bridge(),
  175                 'of_interface': host_desc.of_interface,
  176                 'bridge_mappings': '%s:%s' % (PHYSICAL_NETWORK_NAME, ext_dev),
  177                 'of_inactivity_probe': '0',
  178             },
  179             'securitygroup': {
  180                 'firewall_driver': host_desc.firewall_driver,
  181             },
  182             'agent': {
  183                 'l2_population': str(self.env_desc.l2_pop),
  184                 'arp_responder': str(self.env_desc.arp_responder),
  185                 'debug_iptables_rules': str(env_desc.debug_iptables),
  186                 'use_helper_for_ns_read': 'False',
  187             }
  188         })
  189 
  190         if self.tunneling_enabled:
  191             self.config['agent'].update({
  192                 'tunnel_types': self.env_desc.network_type})
  193             self.config['ovs'].update({
  194                 'tunnel_bridge': self._generate_tunnel_bridge(),
  195                 'int_peer_patch_port': self._generate_int_peer(),
  196                 'tun_peer_patch_port': self._generate_tun_peer()})
  197         else:
  198             if env_desc.report_bandwidths:
  199                 self.config['ovs'][c_const.RP_BANDWIDTHS] = \
  200                     '%s:%s:%s' % (ext_dev, MINIMUM_BANDWIDTH_EGRESS_KBPS,
  201                                   MINIMUM_BANDWIDTH_INGRESS_KBPS)
  202 
  203         if env_desc.qos:
  204             self.config['agent']['extensions'] = 'qos'
  205         if env_desc.log:
  206             self.config['agent']['extensions'] = 'log'
  207             test_name = kwargs.get("test_name")
  208             test_name = base.sanitize_log_path(test_name)
  209             self.config.update({
  210                 'network_log': {
  211                     'local_output_log_base':
  212                         self._generate_temp_log_file(test_name)}
  213             })
  214 
  215     def _setUp(self):
  216         if self.config['ovs']['of_interface'] == 'native':
  217             self.config['ovs'].update({
  218                 'of_listen_port': self.useFixture(
  219                     port.ExclusivePort(constants.PROTO_NAME_TCP)).port
  220             })
  221         super(OVSConfigFixture, self)._setUp()
  222 
  223     def _generate_integration_bridge(self):
  224         return utils.get_rand_device_name(prefix='br-int')
  225 
  226     def _generate_tunnel_bridge(self):
  227         return utils.get_rand_device_name(prefix='br-tun')
  228 
  229     def _generate_int_peer(self):
  230         return utils.get_rand_device_name(prefix='patch-tun')
  231 
  232     def _generate_tun_peer(self):
  233         return utils.get_rand_device_name(prefix='patch-int')
  234 
  235     def _generate_temp_log_file(self, test_name):
  236         log_dir_path = os.path.join(fullstack_base.DEFAULT_LOG_DIR, test_name)
  237         if not os.path.exists(log_dir_path):
  238             os.mkdir(log_dir_path, 0o755)
  239         return '%s/%s.log' % (log_dir_path,
  240                               utils.get_rand_name(prefix="test-sg-"))
  241 
  242     def get_br_int_name(self):
  243         return self.config.ovs.integration_bridge
  244 
  245     def get_br_phys_name(self):
  246         return self.config.ovs.bridge_mappings.split(':')[1]
  247 
  248     def get_br_tun_name(self):
  249         return self.config.ovs.tunnel_bridge
  250 
  251 
  252 class SRIOVConfigFixture(ConfigFixture):
  253     def __init__(self, env_desc, host_desc, temp_dir, local_ip):
  254         super(SRIOVConfigFixture, self).__init__(
  255             env_desc, host_desc, temp_dir,
  256             base_filename='sriov_agent.ini')
  257 
  258         device1 = utils.get_rand_device_name(prefix='ens5')
  259         device2 = utils.get_rand_device_name(prefix='ens6')
  260         phys_dev_mapping = '%s:%s,%s:%s' % (PHYSICAL_NETWORK_NAME, device1,
  261                                             PHYSICAL_NETWORK_NAME, device2)
  262         rp_bandwidths = '%s:%s:%s,%s:%s:%s' % (device1,
  263                                                MINIMUM_BANDWIDTH_EGRESS_KBPS,
  264                                                MINIMUM_BANDWIDTH_INGRESS_KBPS,
  265                                                device2,
  266                                                MINIMUM_BANDWIDTH_EGRESS_KBPS,
  267                                                MINIMUM_BANDWIDTH_INGRESS_KBPS)
  268         self.config.update({
  269             'sriov_nic': {
  270                 'physical_device_mappings': phys_dev_mapping,
  271                 'resource_provider_bandwidths': rp_bandwidths,
  272             }
  273         })
  274 
  275     def _setUp(self):
  276         super(SRIOVConfigFixture, self)._setUp()
  277 
  278 
  279 class LinuxBridgeConfigFixture(ConfigFixture):
  280 
  281     def __init__(self, env_desc, host_desc, temp_dir, local_ip,
  282                  physical_device_name):
  283         super(LinuxBridgeConfigFixture, self).__init__(
  284             env_desc, host_desc, temp_dir,
  285             base_filename="linuxbridge_agent.ini"
  286         )
  287         self.config.update({
  288             'VXLAN': {
  289                 'enable_vxlan': str(self.env_desc.tunneling_enabled),
  290                 'local_ip': local_ip,
  291                 'l2_population': str(self.env_desc.l2_pop),
  292             },
  293             'securitygroup': {
  294                 'firewall_driver': host_desc.firewall_driver,
  295             },
  296             'AGENT': {
  297                 'debug_iptables_rules': str(env_desc.debug_iptables),
  298                 'use_helper_for_ns_read': 'False',
  299             }
  300         })
  301         if env_desc.qos:
  302             self.config.update({
  303                 'AGENT': {
  304                     'extensions': 'qos'
  305                 }
  306             })
  307         if self.env_desc.tunneling_enabled:
  308             self.config.update({
  309                 'LINUX_BRIDGE': {
  310                     'bridge_mappings': self._generate_bridge_mappings(
  311                         physical_device_name
  312                     )
  313                 }
  314             })
  315         else:
  316             self.config.update({
  317                 'LINUX_BRIDGE': {
  318                     'physical_interface_mappings':
  319                         self._generate_bridge_mappings(
  320                             physical_device_name
  321                         )
  322                 }
  323             })
  324 
  325     def _generate_bridge_mappings(self, device_name):
  326         return '%s:%s' % (PHYSICAL_NETWORK_NAME, device_name)
  327 
  328 
  329 class L3ConfigFixture(ConfigFixture):
  330 
  331     def __init__(self, env_desc, host_desc, temp_dir, integration_bridge=None):
  332         super(L3ConfigFixture, self).__init__(
  333             env_desc, host_desc, temp_dir, base_filename='l3_agent.ini')
  334         if host_desc.l2_agent_type == constants.AGENT_TYPE_OVS:
  335             self._prepare_config_with_ovs_agent(integration_bridge)
  336         elif host_desc.l2_agent_type == constants.AGENT_TYPE_LINUXBRIDGE:
  337             self._prepare_config_with_linuxbridge_agent()
  338         if host_desc.l3_agent_mode:
  339             self.config['DEFAULT'].update({
  340                 'agent_mode': host_desc.l3_agent_mode})
  341         self.config['DEFAULT'].update({
  342             'debug': 'True',
  343             'test_namespace_suffix': self._generate_namespace_suffix(),
  344         })
  345         self.config.update({
  346             'agent': {'use_helper_for_ns_read': 'False'}
  347         })
  348         if host_desc.availability_zone:
  349             self.config['agent'].update({
  350                 'availability_zone': host_desc.availability_zone
  351             })
  352 
  353     def _prepare_config_with_ovs_agent(self, integration_bridge):
  354         self.config.update({
  355             'DEFAULT': {
  356                 'interface_driver': ('neutron.agent.linux.interface.'
  357                                      'OVSInterfaceDriver'),
  358                 'ovs_integration_bridge': integration_bridge,
  359             }
  360         })
  361 
  362     def _prepare_config_with_linuxbridge_agent(self):
  363         self.config.update({
  364             'DEFAULT': {
  365                 'interface_driver': ('neutron.agent.linux.interface.'
  366                                      'BridgeInterfaceDriver'),
  367             }
  368         })
  369 
  370 
  371 class DhcpConfigFixture(ConfigFixture):
  372 
  373     def __init__(self, env_desc, host_desc, temp_dir, integration_bridge=None):
  374         super(DhcpConfigFixture, self).__init__(
  375             env_desc, host_desc, temp_dir, base_filename='dhcp_agent.ini')
  376 
  377         if host_desc.l2_agent_type == constants.AGENT_TYPE_OVS:
  378             self._prepare_config_with_ovs_agent(integration_bridge)
  379         elif host_desc.l2_agent_type == constants.AGENT_TYPE_LINUXBRIDGE:
  380             self._prepare_config_with_linuxbridge_agent()
  381         self.config['DEFAULT'].update({
  382             'debug': 'True',
  383             'dhcp_confs': self._generate_dhcp_path(),
  384             'test_namespace_suffix': self._generate_namespace_suffix()
  385         })
  386         self.config.update({
  387             'AGENT': {'use_helper_for_ns_read': 'False'}
  388         })
  389         if host_desc.availability_zone:
  390             self.config['AGENT'].update({
  391                 'availability_zone': host_desc.availability_zone
  392             })
  393 
  394     def _setUp(self):
  395         super(DhcpConfigFixture, self)._setUp()
  396         self.addCleanup(self._clean_dhcp_path)
  397 
  398     def _prepare_config_with_ovs_agent(self, integration_bridge):
  399         self.config.update({
  400             'DEFAULT': {
  401                 'interface_driver': 'openvswitch',
  402                 'ovs_integration_bridge': integration_bridge,
  403             }
  404         })
  405 
  406     def _prepare_config_with_linuxbridge_agent(self):
  407         self.config.update({
  408             'DEFAULT': {
  409                 'interface_driver': 'linuxbridge',
  410             }
  411         })
  412 
  413     def _generate_dhcp_path(self):
  414         # NOTE(slaweq): dhcp_conf path needs to be directory with read
  415         # permission for everyone, otherwise dnsmasq process will not be able
  416         # to read his configs
  417         self.dhcp_path = tempfile.mkdtemp(prefix="dhcp_configs_", dir="/tmp/")
  418         os.chmod(self.dhcp_path, 0o755)
  419         return self.dhcp_path
  420 
  421     def _clean_dhcp_path(self):
  422         shutil.rmtree(self.dhcp_path, ignore_errors=True)