blob: 696d68dd656746dd6a4d01fc45e64cd101d435d3 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipesf4dad392012-06-05 16:03:58 -04002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Miguel Lavallecc939612013-02-22 17:27:20 -060016import netaddr
Jay Pipesf4dad392012-06-05 16:03:58 -040017
Matthew Treinish03b48df2014-01-29 16:59:49 +000018from tempest import config
Miguel Lavallecc939612013-02-22 17:27:20 -060019from tempest import exceptions
Ken'ichi Ohmichif50e4df2017-03-10 10:52:53 -080020from tempest.lib.common.utils import data_utils
Jordan Pittier9e227c52016-02-09 14:35:18 +010021from tempest.lib.common.utils import test_utils
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050022from tempest.lib import exceptions as lib_exc
Attila Fazekasdc216422013-01-29 15:12:14 +010023import tempest.test
Jay Pipesf4dad392012-06-05 16:03:58 -040024
Matthew Treinish03b48df2014-01-29 16:59:49 +000025CONF = config.CONF
26
Jay Pipesf4dad392012-06-05 16:03:58 -040027
Attila Fazekasdc216422013-01-29 15:12:14 +010028class BaseNetworkTest(tempest.test.BaseTestCase):
zhufld2c40ca2016-10-18 17:03:07 +080029 """Base class for the Neutron tests.
Miguel Lavallecc939612013-02-22 17:27:20 -060030
Mark McClainf2982e82013-07-06 17:48:03 -040031 Per the Neutron API Guide, API v1.x was removed from the source code tree
Miguel Lavallecc939612013-02-22 17:27:20 -060032 (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html)
Mark McClainf2982e82013-07-06 17:48:03 -040033 Therefore, v2.x of the Neutron API is assumed. It is also assumed that the
Miguel Lavallecc939612013-02-22 17:27:20 -060034 following options are defined in the [network] section of etc/tempest.conf:
35
Sean Dagueed6e5862016-04-04 10:49:13 -040036 project_network_cidr with a block of cidr's from which smaller blocks
37 can be allocated for project networks
Miguel Lavallecc939612013-02-22 17:27:20 -060038
Sean Dagueed6e5862016-04-04 10:49:13 -040039 project_network_mask_bits with the mask bits to be used to partition
40 the block defined by project-network_cidr
Miguel Lavalle2492d782013-06-16 15:04:15 -050041
42 Finally, it is assumed that the following option is defined in the
43 [service_available] section of etc/tempest.conf
44
45 neutron as True
Miguel Lavallecc939612013-02-22 17:27:20 -060046 """
47
Matthew Treinish2f6628c2013-10-21 21:06:27 +000048 force_tenant_isolation = False
Andrea Frittolib21de6c2015-02-06 20:12:38 +000049 credentials = ['primary']
Matthew Treinish2f6628c2013-10-21 21:06:27 +000050
Henry Gessauffda37a2014-01-16 11:17:55 -050051 # Default to ipv4.
52 _ip_version = 4
Henry Gessauffda37a2014-01-16 11:17:55 -050053
Jay Pipesf4dad392012-06-05 16:03:58 -040054 @classmethod
Rohan Kanadea565e452015-01-27 14:00:13 +053055 def skip_checks(cls):
56 super(BaseNetworkTest, cls).skip_checks()
Matthew Treinish03b48df2014-01-29 16:59:49 +000057 if not CONF.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040058 raise cls.skipException("Neutron support is required")
Sergey Shnaidman97e6a0f2014-11-12 20:00:53 +030059 if cls._ip_version == 6 and not CONF.network_feature_enabled.ipv6:
60 raise cls.skipException("IPv6 Tests are disabled.")
Matthew Treinish2f6628c2013-10-21 21:06:27 +000061
Rohan Kanadea565e452015-01-27 14:00:13 +053062 @classmethod
63 def setup_credentials(cls):
64 # Create no network resources for these test.
65 cls.set_network_resources()
66 super(BaseNetworkTest, cls).setup_credentials()
Rohan Kanadea565e452015-01-27 14:00:13 +053067
68 @classmethod
69 def setup_clients(cls):
70 super(BaseNetworkTest, cls).setup_clients()
Jordan Pittier8160d312017-04-18 11:52:23 +020071 cls.agents_client = cls.os_primary.network_agents_client
72 cls.network_extensions_client =\
73 cls.os_primary.network_extensions_client
74 cls.networks_client = cls.os_primary.networks_client
75 cls.routers_client = cls.os_primary.routers_client
76 cls.subnetpools_client = cls.os_primary.subnetpools_client
77 cls.subnets_client = cls.os_primary.subnets_client
78 cls.ports_client = cls.os_primary.ports_client
Soniya Vyasc08dfd22021-09-14 16:57:42 +053079 cls.floating_ips_port_forwarding_client =\
80 cls.os_primary.floating_ips_port_forwarding_client
Jordan Pittier8160d312017-04-18 11:52:23 +020081 cls.quotas_client = cls.os_primary.network_quotas_client
82 cls.floating_ips_client = cls.os_primary.floating_ips_client
83 cls.security_groups_client = cls.os_primary.security_groups_client
John Warren456d9ae2016-01-12 15:36:33 -050084 cls.security_group_rules_client = (
Jordan Pittier8160d312017-04-18 11:52:23 +020085 cls.os_primary.security_group_rules_client)
86 cls.network_versions_client = cls.os_primary.network_versions_client
87 cls.service_providers_client = cls.os_primary.service_providers_client
Felipe Monteiro11779422017-05-18 06:10:26 +010088 cls.tags_client = cls.os_primary.tags_client
Soniya Vyas065715d2021-06-17 14:36:29 +053089 cls.log_resource_client = cls.os_primary.log_resource_client
Soniya Vyas5a3de3e2021-07-09 12:30:36 +053090 cls.loggable_resource_client = cls.os_primary.loggable_resource_client
Rohan Kanadea565e452015-01-27 14:00:13 +053091
92 @classmethod
93 def resource_setup(cls):
94 super(BaseNetworkTest, cls).resource_setup()
sridhargaddam510f8962014-09-08 23:37:16 +053095 cls.ethertype = "IPv" + str(cls._ip_version)
zhuflec61bac2017-09-01 15:59:50 +080096 if cls._ip_version == 4:
97 cls.cidr = netaddr.IPNetwork(CONF.network.project_network_cidr)
98 cls.mask_bits = CONF.network.project_network_mask_bits
99 elif cls._ip_version == 6:
100 cls.cidr = netaddr.IPNetwork(CONF.network.project_network_v6_cidr)
101 cls.mask_bits = CONF.network.project_network_v6_mask_bits
Jay Pipesf4dad392012-06-05 16:03:58 -0400102
103 @classmethod
Bruce Tand3e9b4a2016-09-22 09:17:06 +0000104 def create_network(cls, network_name=None, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500105 """Wrapper utility that returns a test network."""
zhufld2c40ca2016-10-18 17:03:07 +0800106 network_name = network_name or data_utils.rand_name(
Bruce Tand3e9b4a2016-09-22 09:17:06 +0000107 cls.__name__ + '-test-network')
Jay Pipesf4dad392012-06-05 16:03:58 -0400108
Bruce Tand3e9b4a2016-09-22 09:17:06 +0000109 body = cls.networks_client.create_network(name=network_name, **kwargs)
Jay Pipesf4dad392012-06-05 16:03:58 -0400110 network = body['network']
zhufl587efc02017-11-28 09:52:50 +0800111 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
112 cls.networks_client.delete_network,
113 network['id'])
Jay Pipesf4dad392012-06-05 16:03:58 -0400114 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600115
116 @classmethod
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400117 def create_subnet(cls, network, gateway='', cidr=None, mask_bits=None,
Yair Fried52ee1362014-09-29 14:47:03 +0300118 ip_version=None, client=None, **kwargs):
Miguel Lavallecc939612013-02-22 17:27:20 -0600119 """Wrapper utility that returns a test subnet."""
Yair Fried52ee1362014-09-29 14:47:03 +0300120 # allow tests to use admin client
121 if not client:
John Warren3961acd2015-10-02 14:38:53 -0400122 client = cls.subnets_client
Yair Fried52ee1362014-09-29 14:47:03 +0300123
Henry Gessauffda37a2014-01-16 11:17:55 -0500124 # The cidr and mask_bits depend on the ip version.
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400125 ip_version = ip_version if ip_version is not None else cls._ip_version
126 gateway_not_set = gateway == ''
127 if ip_version == 4:
Sean Dagueed6e5862016-04-04 10:49:13 -0400128 cidr = cidr or netaddr.IPNetwork(CONF.network.project_network_cidr)
129 mask_bits = mask_bits or CONF.network.project_network_mask_bits
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400130 elif ip_version == 6:
Sean Dagueed6e5862016-04-04 10:49:13 -0400131 cidr = (cidr or
132 netaddr.IPNetwork(CONF.network.project_network_v6_cidr))
133 mask_bits = mask_bits or CONF.network.project_network_v6_mask_bits
Miguel Lavallecc939612013-02-22 17:27:20 -0600134 # Find a cidr that is not in use yet and create a subnet with it
135 for subnet_cidr in cidr.subnet(mask_bits):
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400136 if gateway_not_set:
137 gateway_ip = str(netaddr.IPAddress(subnet_cidr) + 1)
138 else:
139 gateway_ip = gateway
Miguel Lavallecc939612013-02-22 17:27:20 -0600140 try:
David Kranz34e88122014-12-11 15:24:05 -0500141 body = client.create_subnet(
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400142 network_id=network['id'],
143 cidr=str(subnet_cidr),
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400144 ip_version=ip_version,
145 gateway_ip=gateway_ip,
Sean M. Collinsdd27a4d2014-05-13 10:33:15 -0400146 **kwargs)
Miguel Lavallecc939612013-02-22 17:27:20 -0600147 break
Masayuki Igawa4b29e472015-02-16 10:41:54 +0900148 except lib_exc.BadRequest as e:
Miguel Lavallecc939612013-02-22 17:27:20 -0600149 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
150 if not is_overlapping_cidr:
151 raise
Matthew Treinish6b8cd2a2014-03-03 20:45:56 +0000152 else:
153 message = 'Available CIDR for subnet creation could not be found'
154 raise exceptions.BuildErrorException(message)
Miguel Lavallecc939612013-02-22 17:27:20 -0600155 subnet = body['subnet']
zhufl587efc02017-11-28 09:52:50 +0800156 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
157 cls.subnets_client.delete_subnet,
158 subnet['id'])
Miguel Lavallecc939612013-02-22 17:27:20 -0600159 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530160
161 @classmethod
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400162 def create_port(cls, network, **kwargs):
Doug Schveninger24675aa2019-08-16 22:28:39 -0500163 if 'name' not in kwargs:
164 kwargs['name'] = data_utils.rand_name(cls.__name__)
raiesmh08e1aad982013-08-05 14:19:36 +0530165 """Wrapper utility that returns a test port."""
John Warren49c0fe52015-10-22 12:35:54 -0400166 body = cls.ports_client.create_port(network_id=network['id'],
167 **kwargs)
raiesmh08e1aad982013-08-05 14:19:36 +0530168 port = body['port']
zhufl587efc02017-11-28 09:52:50 +0800169 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
170 cls.ports_client.delete_port, port['id'])
raiesmh08e1aad982013-08-05 14:19:36 +0530171 return port
raiesmh080fe76852013-09-13 11:52:56 +0530172
173 @classmethod
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400174 def update_port(cls, port, **kwargs):
175 """Wrapper utility that updates a test port."""
John Warren49c0fe52015-10-22 12:35:54 -0400176 body = cls.ports_client.update_port(port['id'],
177 **kwargs)
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400178 return body['port']
179
180 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700181 def create_router(cls, router_name=None, admin_state_up=False,
armando-migliaccio88f7b702014-06-05 12:59:09 -0700182 external_network_id=None, enable_snat=None,
183 **kwargs):
zhufld2c40ca2016-10-18 17:03:07 +0800184 router_name = router_name or data_utils.rand_name(
185 cls.__name__ + "-router")
186
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700187 ext_gw_info = {}
188 if external_network_id:
189 ext_gw_info['network_id'] = external_network_id
Shuquan Huang813362a2015-09-18 08:24:13 +0000190 if enable_snat is not None:
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700191 ext_gw_info['enable_snat'] = enable_snat
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000192 body = cls.routers_client.create_router(
Ken'ichi Ohmichi6665c972016-02-24 13:09:09 -0800193 name=router_name, external_gateway_info=ext_gw_info,
armando-migliaccio88f7b702014-06-05 12:59:09 -0700194 admin_state_up=admin_state_up, **kwargs)
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700195 router = body['router']
zhufl587efc02017-11-28 09:52:50 +0800196 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
197 cls.delete_router, router)
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700198 return router
199
200 @classmethod
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400201 def create_floatingip(cls, external_network_id):
rosselladd68b232013-11-13 10:21:59 +0100202 """Wrapper utility that returns a test floating IP."""
John Warrenfbf2a892015-11-17 12:36:14 -0500203 body = cls.floating_ips_client.create_floatingip(
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400204 floating_network_id=external_network_id)
rosselladd68b232013-11-13 10:21:59 +0100205 fip = body['floatingip']
zhufl437e31e2017-11-22 13:34:40 +0800206 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
207 cls.floating_ips_client.delete_floatingip,
208 fip['id'])
rosselladd68b232013-11-13 10:21:59 +0100209 return fip
210
211 @classmethod
Anju Tiwari860097d2013-10-17 11:10:39 +0530212 def create_router_interface(cls, router_id, subnet_id):
213 """Wrapper utility that returns a router interface."""
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000214 interface = cls.routers_client.add_router_interface(
215 router_id, subnet_id=subnet_id)
wanglianmin5e4b47a2014-03-12 18:16:18 +0800216 return interface
Anju Tiwari860097d2013-10-17 11:10:39 +0530217
218 @classmethod
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700219 def delete_router(cls, router):
Ken'ichi Ohmichif3110f02016-03-21 12:29:03 -0700220 body = cls.ports_client.list_ports(device_id=router['id'])
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700221 interfaces = body['ports']
222 for i in interfaces:
Jordan Pittier9e227c52016-02-09 14:35:18 +0100223 test_utils.call_and_ignore_notfound_exc(
224 cls.routers_client.remove_router_interface, router['id'],
225 subnet_id=i['fixed_ips'][0]['subnet_id'])
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000226 cls.routers_client.delete_router(router['id'])
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700227
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700228
229class BaseAdminNetworkTest(BaseNetworkTest):
230
Andrea Frittolib21de6c2015-02-06 20:12:38 +0000231 credentials = ['primary', 'admin']
Rohan Kanadea565e452015-01-27 14:00:13 +0530232
233 @classmethod
234 def setup_clients(cls):
235 super(BaseAdminNetworkTest, cls).setup_clients()
Jordan Pittier8160d312017-04-18 11:52:23 +0200236 cls.admin_agents_client = cls.os_admin.network_agents_client
237 cls.admin_networks_client = cls.os_admin.networks_client
238 cls.admin_routers_client = cls.os_admin.routers_client
239 cls.admin_subnets_client = cls.os_admin.subnets_client
240 cls.admin_ports_client = cls.os_admin.ports_client
241 cls.admin_quotas_client = cls.os_admin.network_quotas_client
242 cls.admin_floating_ips_client = cls.os_admin.floating_ips_client
243 cls.admin_metering_labels_client = cls.os_admin.metering_labels_client
John Warrendd20b3e2015-12-03 13:11:28 -0500244 cls.admin_metering_label_rules_client = (
Jordan Pittier8160d312017-04-18 11:52:23 +0200245 cls.os_admin.metering_label_rules_client)