blob: 318d891208d4a867d75cfb5156601922852cf0ae [file] [log] [blame]
Jay Pipesf4dad392012-06-05 16:03:58 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
Jay Pipesf4dad392012-06-05 16:03:58 -04004# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Miguel Lavallecc939612013-02-22 17:27:20 -060018import netaddr
Jay Pipesf4dad392012-06-05 16:03:58 -040019
Matthew Treinish481466b2012-12-20 17:16:01 -050020from tempest import clients
Masayuki Igawa259c1132013-10-31 17:48:44 +090021from tempest.common.utils import data_utils
Miguel Lavallecc939612013-02-22 17:27:20 -060022from tempest import exceptions
Anju Tiwari860097d2013-10-17 11:10:39 +053023from tempest.openstack.common import log as logging
Attila Fazekasdc216422013-01-29 15:12:14 +010024import tempest.test
Jay Pipesf4dad392012-06-05 16:03:58 -040025
Anju Tiwari860097d2013-10-17 11:10:39 +053026LOG = logging.getLogger(__name__)
27
Jay Pipesf4dad392012-06-05 16:03:58 -040028
Attila Fazekasdc216422013-01-29 15:12:14 +010029class BaseNetworkTest(tempest.test.BaseTestCase):
Jay Pipesf4dad392012-06-05 16:03:58 -040030
Miguel Lavallecc939612013-02-22 17:27:20 -060031 """
Mark McClainf2982e82013-07-06 17:48:03 -040032 Base class for the Neutron tests that use the Tempest Neutron REST client
Miguel Lavallecc939612013-02-22 17:27:20 -060033
Mark McClainf2982e82013-07-06 17:48:03 -040034 Per the Neutron API Guide, API v1.x was removed from the source code tree
Miguel Lavallecc939612013-02-22 17:27:20 -060035 (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html)
Mark McClainf2982e82013-07-06 17:48:03 -040036 Therefore, v2.x of the Neutron API is assumed. It is also assumed that the
Miguel Lavallecc939612013-02-22 17:27:20 -060037 following options are defined in the [network] section of etc/tempest.conf:
38
39 tenant_network_cidr with a block of cidr's from which smaller blocks
40 can be allocated for tenant networks
41
42 tenant_network_mask_bits with the mask bits to be used to partition the
43 block defined by tenant-network_cidr
Miguel Lavalle2492d782013-06-16 15:04:15 -050044
45 Finally, it is assumed that the following option is defined in the
46 [service_available] section of etc/tempest.conf
47
48 neutron as True
Miguel Lavallecc939612013-02-22 17:27:20 -060049 """
50
Jay Pipesf4dad392012-06-05 16:03:58 -040051 @classmethod
52 def setUpClass(cls):
Attila Fazekasf86fa312013-07-30 19:56:39 +020053 super(BaseNetworkTest, cls).setUpClass()
Sean Dagueffe8afe2013-10-23 15:28:00 -040054 os = clients.Manager(interface=cls._interface)
55 cls.network_cfg = os.config.network
Matthew Treinishfaa340d2013-07-19 16:26:21 -040056 if not cls.config.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040057 raise cls.skipException("Neutron support is required")
Miguel Lavallecc939612013-02-22 17:27:20 -060058 cls.client = os.network_client
59 cls.networks = []
60 cls.subnets = []
raiesmh08e1aad982013-08-05 14:19:36 +053061 cls.ports = []
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070062 cls.routers = []
raiesmh080fe76852013-09-13 11:52:56 +053063 cls.pools = []
64 cls.vips = []
raiesmh08f8437ed2013-09-17 10:59:38 +053065 cls.members = []
raiesmh0832580d02013-09-17 13:11:34 +053066 cls.health_monitors = []
Anju Tiwari860097d2013-10-17 11:10:39 +053067 cls.vpnservices = []
raiesmh08bd6070d2013-12-06 15:13:38 +053068 cls.ikepolicies = []
Jay Pipesf4dad392012-06-05 16:03:58 -040069
70 @classmethod
71 def tearDownClass(cls):
raiesmh08bd6070d2013-12-06 15:13:38 +053072 # Clean up ike policies
73 for ikepolicy in cls.ikepolicies:
Eugene Nikanorov909ded12013-12-15 17:45:37 +040074 cls.client.delete_ikepolicy(ikepolicy['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +000075 # Clean up vpn services
Anju Tiwari860097d2013-10-17 11:10:39 +053076 for vpnservice in cls.vpnservices:
Eugene Nikanorov909ded12013-12-15 17:45:37 +040077 cls.client.delete_vpnservice(vpnservice['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +000078 # Clean up routers
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070079 for router in cls.routers:
Matthew Treinishec3489c2013-10-25 17:26:50 +000080 resp, body = cls.client.list_router_interfaces(router['id'])
81 interfaces = body['ports']
82 for i in interfaces:
83 cls.client.remove_router_interface_with_subnet_id(
84 router['id'], i['fixed_ips'][0]['subnet_id'])
85 cls.client.delete_router(router['id'])
86 # Clean up health monitors
Anju Tiwari860097d2013-10-17 11:10:39 +053087 for health_monitor in cls.health_monitors:
Matthew Treinishec3489c2013-10-25 17:26:50 +000088 cls.client.delete_health_monitor(health_monitor['id'])
89 # Clean up members
Anju Tiwari860097d2013-10-17 11:10:39 +053090 for member in cls.members:
Matthew Treinishec3489c2013-10-25 17:26:50 +000091 cls.client.delete_member(member['id'])
92 # Clean up vips
Anju Tiwari860097d2013-10-17 11:10:39 +053093 for vip in cls.vips:
Matthew Treinishec3489c2013-10-25 17:26:50 +000094 cls.client.delete_vip(vip['id'])
95 # Clean up pools
Anju Tiwari860097d2013-10-17 11:10:39 +053096 for pool in cls.pools:
Matthew Treinishec3489c2013-10-25 17:26:50 +000097 cls.client.delete_pool(pool['id'])
98 # Clean up ports
Anju Tiwari860097d2013-10-17 11:10:39 +053099 for port in cls.ports:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000100 cls.client.delete_port(port['id'])
101 # Clean up subnets
Miguel Lavallecc939612013-02-22 17:27:20 -0600102 for subnet in cls.subnets:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000103 cls.client.delete_subnet(subnet['id'])
104 # Clean up networks
Jay Pipesf4dad392012-06-05 16:03:58 -0400105 for network in cls.networks:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000106 cls.client.delete_network(network['id'])
Attila Fazekasf86fa312013-07-30 19:56:39 +0200107 super(BaseNetworkTest, cls).tearDownClass()
Jay Pipesf4dad392012-06-05 16:03:58 -0400108
Miguel Lavallecc939612013-02-22 17:27:20 -0600109 @classmethod
110 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500111 """Wrapper utility that returns a test network."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900112 network_name = network_name or data_utils.rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -0400113
Miguel Lavallecc939612013-02-22 17:27:20 -0600114 resp, body = cls.client.create_network(network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -0400115 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -0600116 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -0400117 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600118
119 @classmethod
120 def create_subnet(cls, network):
121 """Wrapper utility that returns a test subnet."""
122 cidr = netaddr.IPNetwork(cls.network_cfg.tenant_network_cidr)
123 mask_bits = cls.network_cfg.tenant_network_mask_bits
124 # Find a cidr that is not in use yet and create a subnet with it
Matt Riedemann9c9fa412013-06-19 18:33:47 -0700125 body = None
Matt Riedemannd052c572013-05-31 17:10:11 -0700126 failure = None
Miguel Lavallecc939612013-02-22 17:27:20 -0600127 for subnet_cidr in cidr.subnet(mask_bits):
128 try:
129 resp, body = cls.client.create_subnet(network['id'],
130 str(subnet_cidr))
131 break
132 except exceptions.BadRequest as e:
133 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
134 if not is_overlapping_cidr:
135 raise
Matt Riedemannd052c572013-05-31 17:10:11 -0700136 # save the failure in case all of the CIDRs are overlapping
137 failure = e
138
139 if not body and failure:
140 raise failure
141
Miguel Lavallecc939612013-02-22 17:27:20 -0600142 subnet = body['subnet']
143 cls.subnets.append(subnet)
144 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530145
146 @classmethod
147 def create_port(cls, network):
148 """Wrapper utility that returns a test port."""
149 resp, body = cls.client.create_port(network['id'])
150 port = body['port']
151 cls.ports.append(port)
152 return port
raiesmh080fe76852013-09-13 11:52:56 +0530153
154 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700155 def create_router(cls, router_name=None, admin_state_up=False,
156 external_network_id=None, enable_snat=None):
157 ext_gw_info = {}
158 if external_network_id:
159 ext_gw_info['network_id'] = external_network_id
160 if enable_snat:
161 ext_gw_info['enable_snat'] = enable_snat
162 resp, body = cls.client.create_router(
163 router_name, external_gateway_info=ext_gw_info,
164 admin_state_up=admin_state_up)
165 router = body['router']
166 cls.routers.append(router)
167 return router
168
169 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530170 def create_pool(cls, name, lb_method, protocol, subnet):
171 """Wrapper utility that returns a test pool."""
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400172 resp, body = cls.client.create_pool(
173 name=name,
174 lb_method=lb_method,
175 protocol=protocol,
176 subnet_id=subnet['id'])
raiesmh080fe76852013-09-13 11:52:56 +0530177 pool = body['pool']
178 cls.pools.append(pool)
179 return pool
180
181 @classmethod
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400182 def update_pool(cls, name):
183 """Wrapper utility that returns a test pool."""
184 resp, body = cls.client.update_pool(name=name)
185 pool = body['pool']
186 return pool
187
188 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530189 def create_vip(cls, name, protocol, protocol_port, subnet, pool):
190 """Wrapper utility that returns a test vip."""
191 resp, body = cls.client.create_vip(name, protocol, protocol_port,
192 subnet['id'], pool['id'])
193 vip = body['vip']
194 cls.vips.append(vip)
195 return vip
raiesmh08f8437ed2013-09-17 10:59:38 +0530196
197 @classmethod
198 def create_member(cls, protocol_port, pool):
199 """Wrapper utility that returns a test member."""
200 resp, body = cls.client.create_member("10.0.9.46",
201 protocol_port,
202 pool['id'])
203 member = body['member']
204 cls.members.append(member)
205 return member
raiesmh0832580d02013-09-17 13:11:34 +0530206
207 @classmethod
208 def create_health_monitor(cls, delay, max_retries, Type, timeout):
209 """Wrapper utility that returns a test health monitor."""
210 resp, body = cls.client.create_health_monitor(delay,
211 max_retries,
212 Type, timeout)
213 health_monitor = body['health_monitor']
214 cls.health_monitors.append(health_monitor)
215 return health_monitor
Anju Tiwari860097d2013-10-17 11:10:39 +0530216
217 @classmethod
218 def create_router_interface(cls, router_id, subnet_id):
219 """Wrapper utility that returns a router interface."""
220 resp, interface = cls.client.add_router_interface_with_subnet_id(
221 router_id, subnet_id)
222
223 @classmethod
224 def create_vpnservice(cls, subnet_id, router_id):
225 """Wrapper utility that returns a test vpn service."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400226 resp, body = cls.client.create_vpnservice(
Anju Tiwari860097d2013-10-17 11:10:39 +0530227 subnet_id, router_id, admin_state_up=True,
Masayuki Igawa259c1132013-10-31 17:48:44 +0900228 name=data_utils.rand_name("vpnservice-"))
Anju Tiwari860097d2013-10-17 11:10:39 +0530229 vpnservice = body['vpnservice']
230 cls.vpnservices.append(vpnservice)
231 return vpnservice
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700232
raiesmh08bd6070d2013-12-06 15:13:38 +0530233 @classmethod
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400234 def create_ikepolicy(cls, name):
raiesmh08bd6070d2013-12-06 15:13:38 +0530235 """Wrapper utility that returns a test ike policy."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400236 resp, body = cls.client.create_ikepolicy(name)
raiesmh08bd6070d2013-12-06 15:13:38 +0530237 ikepolicy = body['ikepolicy']
238 cls.ikepolicies.append(ikepolicy)
239 return ikepolicy
240
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700241
242class BaseAdminNetworkTest(BaseNetworkTest):
243
244 @classmethod
245 def setUpClass(cls):
246 super(BaseAdminNetworkTest, cls).setUpClass()
247 admin_username = cls.config.compute_admin.username
248 admin_password = cls.config.compute_admin.password
249 admin_tenant = cls.config.compute_admin.tenant_name
250 if not (admin_username and admin_password and admin_tenant):
251 msg = ("Missing Administrative Network API credentials "
252 "in configuration.")
253 raise cls.skipException(msg)
254 cls.admin_manager = clients.AdminManager(interface=cls._interface)
255 cls.admin_client = cls.admin_manager.network_client