blob: e188a45dafbfead168b30289ad44aa2b6375c8c2 [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 Treinish481466b2012-12-20 17:16:01 -050018from tempest import clients
Masayuki Igawa259c1132013-10-31 17:48:44 +090019from tempest.common.utils import data_utils
Matthew Treinish03b48df2014-01-29 16:59:49 +000020from tempest import config
Miguel Lavallecc939612013-02-22 17:27:20 -060021from tempest import exceptions
Anju Tiwari860097d2013-10-17 11:10:39 +053022from tempest.openstack.common import log as logging
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
Anju Tiwari860097d2013-10-17 11:10:39 +053027LOG = logging.getLogger(__name__)
28
Jay Pipesf4dad392012-06-05 16:03:58 -040029
Attila Fazekasdc216422013-01-29 15:12:14 +010030class BaseNetworkTest(tempest.test.BaseTestCase):
Jay Pipesf4dad392012-06-05 16:03:58 -040031
Miguel Lavallecc939612013-02-22 17:27:20 -060032 """
Mark McClainf2982e82013-07-06 17:48:03 -040033 Base class for the Neutron tests that use the Tempest Neutron REST client
Miguel Lavallecc939612013-02-22 17:27:20 -060034
Mark McClainf2982e82013-07-06 17:48:03 -040035 Per the Neutron API Guide, API v1.x was removed from the source code tree
Miguel Lavallecc939612013-02-22 17:27:20 -060036 (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html)
Mark McClainf2982e82013-07-06 17:48:03 -040037 Therefore, v2.x of the Neutron API is assumed. It is also assumed that the
Miguel Lavallecc939612013-02-22 17:27:20 -060038 following options are defined in the [network] section of etc/tempest.conf:
39
40 tenant_network_cidr with a block of cidr's from which smaller blocks
41 can be allocated for tenant networks
42
43 tenant_network_mask_bits with the mask bits to be used to partition the
44 block defined by tenant-network_cidr
Miguel Lavalle2492d782013-06-16 15:04:15 -050045
46 Finally, it is assumed that the following option is defined in the
47 [service_available] section of etc/tempest.conf
48
49 neutron as True
Miguel Lavallecc939612013-02-22 17:27:20 -060050 """
51
Matthew Treinish2f6628c2013-10-21 21:06:27 +000052 force_tenant_isolation = False
53
Henry Gessauffda37a2014-01-16 11:17:55 -050054 # Default to ipv4.
55 _ip_version = 4
56 _tenant_network_cidr = CONF.network.tenant_network_cidr
57 _tenant_network_mask_bits = CONF.network.tenant_network_mask_bits
58
Jay Pipesf4dad392012-06-05 16:03:58 -040059 @classmethod
60 def setUpClass(cls):
Salvatore Orlando5a337242014-01-15 22:49:22 +000061 # Create no network resources for these test.
62 cls.set_network_resources()
Attila Fazekasf86fa312013-07-30 19:56:39 +020063 super(BaseNetworkTest, cls).setUpClass()
Sean Dagueffe8afe2013-10-23 15:28:00 -040064 os = clients.Manager(interface=cls._interface)
Matthew Treinish03b48df2014-01-29 16:59:49 +000065 if not CONF.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040066 raise cls.skipException("Neutron support is required")
Matthew Treinish2f6628c2013-10-21 21:06:27 +000067
68 os = cls.get_client_manager()
69
70 cls.network_cfg = CONF.network
Miguel Lavallecc939612013-02-22 17:27:20 -060071 cls.client = os.network_client
72 cls.networks = []
73 cls.subnets = []
raiesmh08e1aad982013-08-05 14:19:36 +053074 cls.ports = []
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070075 cls.routers = []
raiesmh080fe76852013-09-13 11:52:56 +053076 cls.pools = []
77 cls.vips = []
raiesmh08f8437ed2013-09-17 10:59:38 +053078 cls.members = []
raiesmh0832580d02013-09-17 13:11:34 +053079 cls.health_monitors = []
Anju Tiwari860097d2013-10-17 11:10:39 +053080 cls.vpnservices = []
raiesmh08bd6070d2013-12-06 15:13:38 +053081 cls.ikepolicies = []
rosselladd68b232013-11-13 10:21:59 +010082 cls.floating_ips = []
Jay Pipesf4dad392012-06-05 16:03:58 -040083
84 @classmethod
85 def tearDownClass(cls):
raiesmh08bd6070d2013-12-06 15:13:38 +053086 # Clean up ike policies
87 for ikepolicy in cls.ikepolicies:
Eugene Nikanorov909ded12013-12-15 17:45:37 +040088 cls.client.delete_ikepolicy(ikepolicy['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +000089 # Clean up vpn services
Anju Tiwari860097d2013-10-17 11:10:39 +053090 for vpnservice in cls.vpnservices:
Eugene Nikanorov909ded12013-12-15 17:45:37 +040091 cls.client.delete_vpnservice(vpnservice['id'])
rosselladd68b232013-11-13 10:21:59 +010092 # Clean up floating IPs
93 for floating_ip in cls.floating_ips:
94 cls.client.delete_floating_ip(floating_ip['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +000095 # Clean up routers
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070096 for router in cls.routers:
Matthew Treinishec3489c2013-10-25 17:26:50 +000097 resp, body = cls.client.list_router_interfaces(router['id'])
98 interfaces = body['ports']
99 for i in interfaces:
100 cls.client.remove_router_interface_with_subnet_id(
101 router['id'], i['fixed_ips'][0]['subnet_id'])
102 cls.client.delete_router(router['id'])
103 # Clean up health monitors
Anju Tiwari860097d2013-10-17 11:10:39 +0530104 for health_monitor in cls.health_monitors:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000105 cls.client.delete_health_monitor(health_monitor['id'])
106 # Clean up members
Anju Tiwari860097d2013-10-17 11:10:39 +0530107 for member in cls.members:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000108 cls.client.delete_member(member['id'])
109 # Clean up vips
Anju Tiwari860097d2013-10-17 11:10:39 +0530110 for vip in cls.vips:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000111 cls.client.delete_vip(vip['id'])
112 # Clean up pools
Anju Tiwari860097d2013-10-17 11:10:39 +0530113 for pool in cls.pools:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000114 cls.client.delete_pool(pool['id'])
115 # Clean up ports
Anju Tiwari860097d2013-10-17 11:10:39 +0530116 for port in cls.ports:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000117 cls.client.delete_port(port['id'])
118 # Clean up subnets
Miguel Lavallecc939612013-02-22 17:27:20 -0600119 for subnet in cls.subnets:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000120 cls.client.delete_subnet(subnet['id'])
121 # Clean up networks
Jay Pipesf4dad392012-06-05 16:03:58 -0400122 for network in cls.networks:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000123 cls.client.delete_network(network['id'])
Matthew Treinish2f6628c2013-10-21 21:06:27 +0000124 cls.clear_isolated_creds()
Attila Fazekasf86fa312013-07-30 19:56:39 +0200125 super(BaseNetworkTest, cls).tearDownClass()
Jay Pipesf4dad392012-06-05 16:03:58 -0400126
Miguel Lavallecc939612013-02-22 17:27:20 -0600127 @classmethod
128 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500129 """Wrapper utility that returns a test network."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900130 network_name = network_name or data_utils.rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -0400131
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400132 resp, body = cls.client.create_network(name=network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -0400133 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -0600134 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -0400135 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600136
137 @classmethod
Henry Gessauffda37a2014-01-16 11:17:55 -0500138 def create_subnet(cls, network):
Miguel Lavallecc939612013-02-22 17:27:20 -0600139 """Wrapper utility that returns a test subnet."""
Henry Gessauffda37a2014-01-16 11:17:55 -0500140 # The cidr and mask_bits depend on the ip version.
141 cidr = netaddr.IPNetwork(cls._tenant_network_cidr)
142 mask_bits = cls._tenant_network_mask_bits
Miguel Lavallecc939612013-02-22 17:27:20 -0600143 # Find a cidr that is not in use yet and create a subnet with it
Matt Riedemann9c9fa412013-06-19 18:33:47 -0700144 body = None
Matt Riedemannd052c572013-05-31 17:10:11 -0700145 failure = None
Miguel Lavallecc939612013-02-22 17:27:20 -0600146 for subnet_cidr in cidr.subnet(mask_bits):
147 try:
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400148 resp, body = cls.client.create_subnet(
149 network_id=network['id'],
150 cidr=str(subnet_cidr),
Henry Gessauffda37a2014-01-16 11:17:55 -0500151 ip_version=cls._ip_version)
Miguel Lavallecc939612013-02-22 17:27:20 -0600152 break
153 except exceptions.BadRequest as e:
154 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
155 if not is_overlapping_cidr:
156 raise
Matt Riedemannd052c572013-05-31 17:10:11 -0700157 # save the failure in case all of the CIDRs are overlapping
158 failure = e
159
160 if not body and failure:
161 raise failure
162
Miguel Lavallecc939612013-02-22 17:27:20 -0600163 subnet = body['subnet']
164 cls.subnets.append(subnet)
165 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530166
167 @classmethod
168 def create_port(cls, network):
169 """Wrapper utility that returns a test port."""
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400170 resp, body = cls.client.create_port(network_id=network['id'])
raiesmh08e1aad982013-08-05 14:19:36 +0530171 port = body['port']
172 cls.ports.append(port)
173 return port
raiesmh080fe76852013-09-13 11:52:56 +0530174
175 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700176 def create_router(cls, router_name=None, admin_state_up=False,
177 external_network_id=None, enable_snat=None):
178 ext_gw_info = {}
179 if external_network_id:
180 ext_gw_info['network_id'] = external_network_id
181 if enable_snat:
182 ext_gw_info['enable_snat'] = enable_snat
183 resp, body = cls.client.create_router(
184 router_name, external_gateway_info=ext_gw_info,
185 admin_state_up=admin_state_up)
186 router = body['router']
187 cls.routers.append(router)
188 return router
189
190 @classmethod
rosselladd68b232013-11-13 10:21:59 +0100191 def create_floating_ip(cls, external_network_id, **kwargs):
192 """Wrapper utility that returns a test floating IP."""
193 resp, body = cls.client.create_floating_ip(
194 external_network_id,
195 **kwargs)
196 fip = body['floatingip']
197 cls.floating_ips.append(fip)
198 return fip
199
200 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530201 def create_pool(cls, name, lb_method, protocol, subnet):
202 """Wrapper utility that returns a test pool."""
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400203 resp, body = cls.client.create_pool(
204 name=name,
205 lb_method=lb_method,
206 protocol=protocol,
207 subnet_id=subnet['id'])
raiesmh080fe76852013-09-13 11:52:56 +0530208 pool = body['pool']
209 cls.pools.append(pool)
210 return pool
211
212 @classmethod
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400213 def update_pool(cls, name):
214 """Wrapper utility that returns a test pool."""
215 resp, body = cls.client.update_pool(name=name)
216 pool = body['pool']
217 return pool
218
219 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530220 def create_vip(cls, name, protocol, protocol_port, subnet, pool):
221 """Wrapper utility that returns a test vip."""
222 resp, body = cls.client.create_vip(name, protocol, protocol_port,
223 subnet['id'], pool['id'])
224 vip = body['vip']
225 cls.vips.append(vip)
226 return vip
raiesmh08f8437ed2013-09-17 10:59:38 +0530227
228 @classmethod
229 def create_member(cls, protocol_port, pool):
230 """Wrapper utility that returns a test member."""
231 resp, body = cls.client.create_member("10.0.9.46",
232 protocol_port,
233 pool['id'])
234 member = body['member']
235 cls.members.append(member)
236 return member
raiesmh0832580d02013-09-17 13:11:34 +0530237
238 @classmethod
239 def create_health_monitor(cls, delay, max_retries, Type, timeout):
240 """Wrapper utility that returns a test health monitor."""
241 resp, body = cls.client.create_health_monitor(delay,
242 max_retries,
243 Type, timeout)
244 health_monitor = body['health_monitor']
245 cls.health_monitors.append(health_monitor)
246 return health_monitor
Anju Tiwari860097d2013-10-17 11:10:39 +0530247
248 @classmethod
249 def create_router_interface(cls, router_id, subnet_id):
250 """Wrapper utility that returns a router interface."""
251 resp, interface = cls.client.add_router_interface_with_subnet_id(
252 router_id, subnet_id)
253
254 @classmethod
255 def create_vpnservice(cls, subnet_id, router_id):
256 """Wrapper utility that returns a test vpn service."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400257 resp, body = cls.client.create_vpnservice(
Anju Tiwari860097d2013-10-17 11:10:39 +0530258 subnet_id, router_id, admin_state_up=True,
Masayuki Igawa259c1132013-10-31 17:48:44 +0900259 name=data_utils.rand_name("vpnservice-"))
Anju Tiwari860097d2013-10-17 11:10:39 +0530260 vpnservice = body['vpnservice']
261 cls.vpnservices.append(vpnservice)
262 return vpnservice
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700263
raiesmh08bd6070d2013-12-06 15:13:38 +0530264 @classmethod
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400265 def create_ikepolicy(cls, name):
raiesmh08bd6070d2013-12-06 15:13:38 +0530266 """Wrapper utility that returns a test ike policy."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400267 resp, body = cls.client.create_ikepolicy(name)
raiesmh08bd6070d2013-12-06 15:13:38 +0530268 ikepolicy = body['ikepolicy']
269 cls.ikepolicies.append(ikepolicy)
270 return ikepolicy
271
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700272
273class BaseAdminNetworkTest(BaseNetworkTest):
274
275 @classmethod
276 def setUpClass(cls):
277 super(BaseAdminNetworkTest, cls).setUpClass()
Matthew Treinish03b48df2014-01-29 16:59:49 +0000278 admin_username = CONF.compute_admin.username
279 admin_password = CONF.compute_admin.password
280 admin_tenant = CONF.compute_admin.tenant_name
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700281 if not (admin_username and admin_password and admin_tenant):
282 msg = ("Missing Administrative Network API credentials "
283 "in configuration.")
284 raise cls.skipException(msg)
Matthew Treinish2f6628c2013-10-21 21:06:27 +0000285 if (CONF.compute.allow_tenant_isolation or
286 cls.force_tenant_isolation is True):
287 creds = cls.isolated_creds.get_admin_creds()
288 admin_username, admin_tenant_name, admin_password = creds
289 cls.os_adm = clients.Manager(username=admin_username,
290 password=admin_password,
291 tenant_name=admin_tenant_name,
292 interface=cls._interface)
293 else:
294 cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)
295 cls.admin_client = cls.os_adm.network_client