blob: 71edf74e4e4b2d2a103a4cc16ae55b1edd9e9e92 [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
Fei Long Wangd39431f2015-05-14 11:30:48 +120018from tempest.common.utils import data_utils
Matthew Treinish03b48df2014-01-29 16:59:49 +000019from tempest import config
Miguel Lavallecc939612013-02-22 17:27:20 -060020from tempest import exceptions
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050021from tempest.lib import exceptions as lib_exc
Attila Fazekasdc216422013-01-29 15:12:14 +010022import tempest.test
Jay Pipesf4dad392012-06-05 16:03:58 -040023
Matthew Treinish03b48df2014-01-29 16:59:49 +000024CONF = config.CONF
25
Jay Pipesf4dad392012-06-05 16:03:58 -040026
Attila Fazekasdc216422013-01-29 15:12:14 +010027class BaseNetworkTest(tempest.test.BaseTestCase):
Ken'ichi Ohmichie03bea92015-11-19 07:45:58 +000028 """Base class for the Neutron tests
Miguel Lavallecc939612013-02-22 17:27:20 -060029
Mark McClainf2982e82013-07-06 17:48:03 -040030 Per the Neutron API Guide, API v1.x was removed from the source code tree
Miguel Lavallecc939612013-02-22 17:27:20 -060031 (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html)
Mark McClainf2982e82013-07-06 17:48:03 -040032 Therefore, v2.x of the Neutron API is assumed. It is also assumed that the
Miguel Lavallecc939612013-02-22 17:27:20 -060033 following options are defined in the [network] section of etc/tempest.conf:
34
35 tenant_network_cidr with a block of cidr's from which smaller blocks
36 can be allocated for tenant networks
37
38 tenant_network_mask_bits with the mask bits to be used to partition the
39 block defined by tenant-network_cidr
Miguel Lavalle2492d782013-06-16 15:04:15 -050040
41 Finally, it is assumed that the following option is defined in the
42 [service_available] section of etc/tempest.conf
43
44 neutron as True
Miguel Lavallecc939612013-02-22 17:27:20 -060045 """
46
Matthew Treinish2f6628c2013-10-21 21:06:27 +000047 force_tenant_isolation = False
Andrea Frittolib21de6c2015-02-06 20:12:38 +000048 credentials = ['primary']
Matthew Treinish2f6628c2013-10-21 21:06:27 +000049
Henry Gessauffda37a2014-01-16 11:17:55 -050050 # Default to ipv4.
51 _ip_version = 4
Henry Gessauffda37a2014-01-16 11:17:55 -050052
Jay Pipesf4dad392012-06-05 16:03:58 -040053 @classmethod
Rohan Kanadea565e452015-01-27 14:00:13 +053054 def skip_checks(cls):
55 super(BaseNetworkTest, cls).skip_checks()
Matthew Treinish03b48df2014-01-29 16:59:49 +000056 if not CONF.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040057 raise cls.skipException("Neutron support is required")
Sergey Shnaidman97e6a0f2014-11-12 20:00:53 +030058 if cls._ip_version == 6 and not CONF.network_feature_enabled.ipv6:
59 raise cls.skipException("IPv6 Tests are disabled.")
Matthew Treinish2f6628c2013-10-21 21:06:27 +000060
Rohan Kanadea565e452015-01-27 14:00:13 +053061 @classmethod
62 def setup_credentials(cls):
63 # Create no network resources for these test.
64 cls.set_network_resources()
65 super(BaseNetworkTest, cls).setup_credentials()
Rohan Kanadea565e452015-01-27 14:00:13 +053066
67 @classmethod
68 def setup_clients(cls):
69 super(BaseNetworkTest, cls).setup_clients()
70 cls.client = cls.os.network_client
Ken'ichi Ohmichi71860062015-12-15 08:20:13 +000071 cls.agents_client = cls.os.network_agents_client
Ken'ichi Ohmichi52bb8122016-01-26 01:43:06 +000072 cls.network_extensions_client = cls.os.network_extensions_client
John Warren94d8faf2015-09-15 12:22:24 -040073 cls.networks_client = cls.os.networks_client
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +000074 cls.routers_client = cls.os.routers_client
Ken'ichi Ohmichif3f8aba2015-12-15 08:11:00 +000075 cls.subnetpools_client = cls.os.subnetpools_client
John Warren3961acd2015-10-02 14:38:53 -040076 cls.subnets_client = cls.os.subnets_client
John Warren49c0fe52015-10-22 12:35:54 -040077 cls.ports_client = cls.os.ports_client
Ken'ichi Ohmichi756d8ff2015-12-15 09:47:54 +000078 cls.quotas_client = cls.os.network_quotas_client
John Warrenfbf2a892015-11-17 12:36:14 -050079 cls.floating_ips_client = cls.os.floating_ips_client
John Warrenf9606e92015-12-10 12:12:42 -050080 cls.security_groups_client = cls.os.security_groups_client
John Warren456d9ae2016-01-12 15:36:33 -050081 cls.security_group_rules_client = (
82 cls.os.security_group_rules_client)
Rohan Kanadea565e452015-01-27 14:00:13 +053083
84 @classmethod
85 def resource_setup(cls):
86 super(BaseNetworkTest, cls).resource_setup()
Miguel Lavallecc939612013-02-22 17:27:20 -060087 cls.networks = []
88 cls.subnets = []
raiesmh08e1aad982013-08-05 14:19:36 +053089 cls.ports = []
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070090 cls.routers = []
rosselladd68b232013-11-13 10:21:59 +010091 cls.floating_ips = []
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -050092 cls.metering_labels = []
93 cls.metering_label_rules = []
sridhargaddam510f8962014-09-08 23:37:16 +053094 cls.ethertype = "IPv" + str(cls._ip_version)
Jay Pipesf4dad392012-06-05 16:03:58 -040095
96 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +010097 def resource_cleanup(cls):
Daniel P. Berrange690c26a2014-07-01 12:51:36 +010098 if CONF.service_available.neutron:
Daniel P. Berrange690c26a2014-07-01 12:51:36 +010099 # Clean up floating IPs
100 for floating_ip in cls.floating_ips:
John Warrenfbf2a892015-11-17 12:36:14 -0500101 cls._try_delete_resource(
102 cls.floating_ips_client.delete_floatingip,
103 floating_ip['id'])
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700104
Daniel P. Berrange690c26a2014-07-01 12:51:36 +0100105 # Clean up metering label rules
John Warrendd20b3e2015-12-03 13:11:28 -0500106 # Not all classes in the hierarchy have the client class variable
107 if len(cls.metering_label_rules) > 0:
108 label_rules_client = cls.admin_metering_label_rules_client
Daniel P. Berrange690c26a2014-07-01 12:51:36 +0100109 for metering_label_rule in cls.metering_label_rules:
Yair Fried4f923282014-11-19 10:55:57 +0200110 cls._try_delete_resource(
John Warrendd20b3e2015-12-03 13:11:28 -0500111 label_rules_client.delete_metering_label_rule,
Daniel P. Berrange690c26a2014-07-01 12:51:36 +0100112 metering_label_rule['id'])
113 # Clean up metering labels
114 for metering_label in cls.metering_labels:
Yair Fried4f923282014-11-19 10:55:57 +0200115 cls._try_delete_resource(
John Warren6d0083a2015-11-30 18:12:30 -0500116 cls.admin_metering_labels_client.delete_metering_label,
Yair Fried4f923282014-11-19 10:55:57 +0200117 metering_label['id'])
Daniel P. Berrange690c26a2014-07-01 12:51:36 +0100118 # Clean up ports
119 for port in cls.ports:
John Warren49c0fe52015-10-22 12:35:54 -0400120 cls._try_delete_resource(cls.ports_client.delete_port,
Yair Fried4f923282014-11-19 10:55:57 +0200121 port['id'])
PrinikaSNe46fbd12015-05-29 16:41:30 -0700122 # Clean up routers
123 for router in cls.routers:
124 cls._try_delete_resource(cls.delete_router,
125 router)
Daniel P. Berrange690c26a2014-07-01 12:51:36 +0100126 # Clean up subnets
127 for subnet in cls.subnets:
John Warren3961acd2015-10-02 14:38:53 -0400128 cls._try_delete_resource(cls.subnets_client.delete_subnet,
Yair Fried4f923282014-11-19 10:55:57 +0200129 subnet['id'])
Daniel P. Berrange690c26a2014-07-01 12:51:36 +0100130 # Clean up networks
131 for network in cls.networks:
John Warren94d8faf2015-09-15 12:22:24 -0400132 cls._try_delete_resource(cls.networks_client.delete_network,
Yair Fried4f923282014-11-19 10:55:57 +0200133 network['id'])
Andrea Frittolida4a2452014-09-15 13:12:08 +0100134 super(BaseNetworkTest, cls).resource_cleanup()
Jay Pipesf4dad392012-06-05 16:03:58 -0400135
Miguel Lavallecc939612013-02-22 17:27:20 -0600136 @classmethod
Yair Fried4f923282014-11-19 10:55:57 +0200137 def _try_delete_resource(self, delete_callable, *args, **kwargs):
138 """Cleanup resources in case of test-failure
139
140 Some resources are explicitly deleted by the test.
141 If the test failed to delete a resource, this method will execute
142 the appropriate delete methods. Otherwise, the method ignores NotFound
143 exceptions thrown for resources that were correctly deleted by the
144 test.
145
146 :param delete_callable: delete method
147 :param args: arguments for delete method
148 :param kwargs: keyword arguments for delete method
149 """
150 try:
151 delete_callable(*args, **kwargs)
152 # if resource is not found, this means it was deleted in the test
Masayuki Igawabfa07602015-01-20 18:47:17 +0900153 except lib_exc.NotFound:
Yair Fried4f923282014-11-19 10:55:57 +0200154 pass
155
156 @classmethod
Miguel Lavallecc939612013-02-22 17:27:20 -0600157 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500158 """Wrapper utility that returns a test network."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900159 network_name = network_name or data_utils.rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -0400160
John Warren94d8faf2015-09-15 12:22:24 -0400161 body = cls.networks_client.create_network(name=network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -0400162 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -0600163 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -0400164 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600165
166 @classmethod
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400167 def create_subnet(cls, network, gateway='', cidr=None, mask_bits=None,
Yair Fried52ee1362014-09-29 14:47:03 +0300168 ip_version=None, client=None, **kwargs):
Miguel Lavallecc939612013-02-22 17:27:20 -0600169 """Wrapper utility that returns a test subnet."""
Yair Fried52ee1362014-09-29 14:47:03 +0300170
171 # allow tests to use admin client
172 if not client:
John Warren3961acd2015-10-02 14:38:53 -0400173 client = cls.subnets_client
Yair Fried52ee1362014-09-29 14:47:03 +0300174
Henry Gessauffda37a2014-01-16 11:17:55 -0500175 # The cidr and mask_bits depend on the ip version.
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400176 ip_version = ip_version if ip_version is not None else cls._ip_version
177 gateway_not_set = gateway == ''
178 if ip_version == 4:
armando-migliaccioee90a4d2014-05-06 11:54:07 -0700179 cidr = cidr or netaddr.IPNetwork(CONF.network.tenant_network_cidr)
180 mask_bits = mask_bits or CONF.network.tenant_network_mask_bits
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400181 elif ip_version == 6:
armando-migliaccioee90a4d2014-05-06 11:54:07 -0700182 cidr = (
183 cidr or netaddr.IPNetwork(CONF.network.tenant_network_v6_cidr))
184 mask_bits = mask_bits or CONF.network.tenant_network_v6_mask_bits
Miguel Lavallecc939612013-02-22 17:27:20 -0600185 # Find a cidr that is not in use yet and create a subnet with it
186 for subnet_cidr in cidr.subnet(mask_bits):
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400187 if gateway_not_set:
188 gateway_ip = str(netaddr.IPAddress(subnet_cidr) + 1)
189 else:
190 gateway_ip = gateway
Miguel Lavallecc939612013-02-22 17:27:20 -0600191 try:
David Kranz34e88122014-12-11 15:24:05 -0500192 body = client.create_subnet(
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400193 network_id=network['id'],
194 cidr=str(subnet_cidr),
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400195 ip_version=ip_version,
196 gateway_ip=gateway_ip,
Sean M. Collinsdd27a4d2014-05-13 10:33:15 -0400197 **kwargs)
Miguel Lavallecc939612013-02-22 17:27:20 -0600198 break
Masayuki Igawa4b29e472015-02-16 10:41:54 +0900199 except lib_exc.BadRequest as e:
Miguel Lavallecc939612013-02-22 17:27:20 -0600200 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
201 if not is_overlapping_cidr:
202 raise
Matthew Treinish6b8cd2a2014-03-03 20:45:56 +0000203 else:
204 message = 'Available CIDR for subnet creation could not be found'
205 raise exceptions.BuildErrorException(message)
Miguel Lavallecc939612013-02-22 17:27:20 -0600206 subnet = body['subnet']
207 cls.subnets.append(subnet)
208 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530209
210 @classmethod
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400211 def create_port(cls, network, **kwargs):
raiesmh08e1aad982013-08-05 14:19:36 +0530212 """Wrapper utility that returns a test port."""
John Warren49c0fe52015-10-22 12:35:54 -0400213 body = cls.ports_client.create_port(network_id=network['id'],
214 **kwargs)
raiesmh08e1aad982013-08-05 14:19:36 +0530215 port = body['port']
216 cls.ports.append(port)
217 return port
raiesmh080fe76852013-09-13 11:52:56 +0530218
219 @classmethod
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400220 def update_port(cls, port, **kwargs):
221 """Wrapper utility that updates a test port."""
John Warren49c0fe52015-10-22 12:35:54 -0400222 body = cls.ports_client.update_port(port['id'],
223 **kwargs)
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400224 return body['port']
225
226 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700227 def create_router(cls, router_name=None, admin_state_up=False,
armando-migliaccio88f7b702014-06-05 12:59:09 -0700228 external_network_id=None, enable_snat=None,
229 **kwargs):
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700230 ext_gw_info = {}
231 if external_network_id:
232 ext_gw_info['network_id'] = external_network_id
Shuquan Huang813362a2015-09-18 08:24:13 +0000233 if enable_snat is not None:
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700234 ext_gw_info['enable_snat'] = enable_snat
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000235 body = cls.routers_client.create_router(
Ken'ichi Ohmichi6665c972016-02-24 13:09:09 -0800236 name=router_name, external_gateway_info=ext_gw_info,
armando-migliaccio88f7b702014-06-05 12:59:09 -0700237 admin_state_up=admin_state_up, **kwargs)
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700238 router = body['router']
239 cls.routers.append(router)
240 return router
241
242 @classmethod
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400243 def create_floatingip(cls, external_network_id):
rosselladd68b232013-11-13 10:21:59 +0100244 """Wrapper utility that returns a test floating IP."""
John Warrenfbf2a892015-11-17 12:36:14 -0500245 body = cls.floating_ips_client.create_floatingip(
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400246 floating_network_id=external_network_id)
rosselladd68b232013-11-13 10:21:59 +0100247 fip = body['floatingip']
248 cls.floating_ips.append(fip)
249 return fip
250
251 @classmethod
Anju Tiwari860097d2013-10-17 11:10:39 +0530252 def create_router_interface(cls, router_id, subnet_id):
253 """Wrapper utility that returns a router interface."""
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000254 interface = cls.routers_client.add_router_interface(
255 router_id, subnet_id=subnet_id)
wanglianmin5e4b47a2014-03-12 18:16:18 +0800256 return interface
Anju Tiwari860097d2013-10-17 11:10:39 +0530257
258 @classmethod
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700259 def delete_router(cls, router):
Ken'ichi Ohmichif3110f02016-03-21 12:29:03 -0700260 body = cls.ports_client.list_ports(device_id=router['id'])
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700261 interfaces = body['ports']
262 for i in interfaces:
Rohan Kanadef37eaef2014-12-26 10:02:04 +0100263 try:
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000264 cls.routers_client.remove_router_interface(
piyush11078694aca952015-12-17 12:54:44 +0530265 router['id'],
266 subnet_id=i['fixed_ips'][0]['subnet_id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900267 except lib_exc.NotFound:
Rohan Kanadef37eaef2014-12-26 10:02:04 +0100268 pass
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000269 cls.routers_client.delete_router(router['id'])
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700270
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700271
272class BaseAdminNetworkTest(BaseNetworkTest):
273
Andrea Frittolib21de6c2015-02-06 20:12:38 +0000274 credentials = ['primary', 'admin']
Rohan Kanadea565e452015-01-27 14:00:13 +0530275
276 @classmethod
277 def setup_clients(cls):
278 super(BaseAdminNetworkTest, cls).setup_clients()
Matthew Treinish2f6628c2013-10-21 21:06:27 +0000279 cls.admin_client = cls.os_adm.network_client
Ken'ichi Ohmichi71860062015-12-15 08:20:13 +0000280 cls.admin_agents_client = cls.os_adm.network_agents_client
John Warren94d8faf2015-09-15 12:22:24 -0400281 cls.admin_networks_client = cls.os_adm.networks_client
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000282 cls.admin_routers_client = cls.os_adm.routers_client
John Warren3961acd2015-10-02 14:38:53 -0400283 cls.admin_subnets_client = cls.os_adm.subnets_client
John Warren49c0fe52015-10-22 12:35:54 -0400284 cls.admin_ports_client = cls.os_adm.ports_client
Ken'ichi Ohmichi756d8ff2015-12-15 09:47:54 +0000285 cls.admin_quotas_client = cls.os_adm.network_quotas_client
John Warrenfbf2a892015-11-17 12:36:14 -0500286 cls.admin_floating_ips_client = cls.os_adm.floating_ips_client
John Warren6d0083a2015-11-30 18:12:30 -0500287 cls.admin_metering_labels_client = cls.os_adm.metering_labels_client
John Warrendd20b3e2015-12-03 13:11:28 -0500288 cls.admin_metering_label_rules_client = (
289 cls.os_adm.metering_label_rules_client)
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -0500290
291 @classmethod
292 def create_metering_label(cls, name, description):
293 """Wrapper utility that returns a test metering label."""
John Warren6d0083a2015-11-30 18:12:30 -0500294 body = cls.admin_metering_labels_client.create_metering_label(
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -0500295 description=description,
296 name=data_utils.rand_name("metering-label"))
297 metering_label = body['metering_label']
298 cls.metering_labels.append(metering_label)
299 return metering_label
300
301 @classmethod
302 def create_metering_label_rule(cls, remote_ip_prefix, direction,
303 metering_label_id):
304 """Wrapper utility that returns a test metering label rule."""
John Warrendd20b3e2015-12-03 13:11:28 -0500305 client = cls.admin_metering_label_rules_client
306 body = client.create_metering_label_rule(
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -0500307 remote_ip_prefix=remote_ip_prefix, direction=direction,
308 metering_label_id=metering_label_id)
309 metering_label_rule = body['metering_label_rule']
310 cls.metering_label_rules.append(metering_label_rule)
311 return metering_label_rule