blob: 443a23ec5dfc969bda9bf0251c9d57a70514c888 [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 = []
rosselladd68b232013-11-13 10:21:59 +010069 cls.floating_ips = []
Jay Pipesf4dad392012-06-05 16:03:58 -040070
71 @classmethod
72 def tearDownClass(cls):
raiesmh08bd6070d2013-12-06 15:13:38 +053073 # Clean up ike policies
74 for ikepolicy in cls.ikepolicies:
Eugene Nikanorov909ded12013-12-15 17:45:37 +040075 cls.client.delete_ikepolicy(ikepolicy['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +000076 # Clean up vpn services
Anju Tiwari860097d2013-10-17 11:10:39 +053077 for vpnservice in cls.vpnservices:
Eugene Nikanorov909ded12013-12-15 17:45:37 +040078 cls.client.delete_vpnservice(vpnservice['id'])
rosselladd68b232013-11-13 10:21:59 +010079 # Clean up floating IPs
80 for floating_ip in cls.floating_ips:
81 cls.client.delete_floating_ip(floating_ip['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +000082 # Clean up routers
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070083 for router in cls.routers:
Matthew Treinishec3489c2013-10-25 17:26:50 +000084 resp, body = cls.client.list_router_interfaces(router['id'])
85 interfaces = body['ports']
86 for i in interfaces:
87 cls.client.remove_router_interface_with_subnet_id(
88 router['id'], i['fixed_ips'][0]['subnet_id'])
89 cls.client.delete_router(router['id'])
90 # Clean up health monitors
Anju Tiwari860097d2013-10-17 11:10:39 +053091 for health_monitor in cls.health_monitors:
Matthew Treinishec3489c2013-10-25 17:26:50 +000092 cls.client.delete_health_monitor(health_monitor['id'])
93 # Clean up members
Anju Tiwari860097d2013-10-17 11:10:39 +053094 for member in cls.members:
Matthew Treinishec3489c2013-10-25 17:26:50 +000095 cls.client.delete_member(member['id'])
96 # Clean up vips
Anju Tiwari860097d2013-10-17 11:10:39 +053097 for vip in cls.vips:
Matthew Treinishec3489c2013-10-25 17:26:50 +000098 cls.client.delete_vip(vip['id'])
99 # Clean up pools
Anju Tiwari860097d2013-10-17 11:10:39 +0530100 for pool in cls.pools:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000101 cls.client.delete_pool(pool['id'])
102 # Clean up ports
Anju Tiwari860097d2013-10-17 11:10:39 +0530103 for port in cls.ports:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000104 cls.client.delete_port(port['id'])
105 # Clean up subnets
Miguel Lavallecc939612013-02-22 17:27:20 -0600106 for subnet in cls.subnets:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000107 cls.client.delete_subnet(subnet['id'])
108 # Clean up networks
Jay Pipesf4dad392012-06-05 16:03:58 -0400109 for network in cls.networks:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000110 cls.client.delete_network(network['id'])
Attila Fazekasf86fa312013-07-30 19:56:39 +0200111 super(BaseNetworkTest, cls).tearDownClass()
Jay Pipesf4dad392012-06-05 16:03:58 -0400112
Miguel Lavallecc939612013-02-22 17:27:20 -0600113 @classmethod
114 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500115 """Wrapper utility that returns a test network."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900116 network_name = network_name or data_utils.rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -0400117
Miguel Lavallecc939612013-02-22 17:27:20 -0600118 resp, body = cls.client.create_network(network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -0400119 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -0600120 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -0400121 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600122
123 @classmethod
124 def create_subnet(cls, network):
125 """Wrapper utility that returns a test subnet."""
126 cidr = netaddr.IPNetwork(cls.network_cfg.tenant_network_cidr)
127 mask_bits = cls.network_cfg.tenant_network_mask_bits
128 # Find a cidr that is not in use yet and create a subnet with it
Matt Riedemann9c9fa412013-06-19 18:33:47 -0700129 body = None
Matt Riedemannd052c572013-05-31 17:10:11 -0700130 failure = None
Miguel Lavallecc939612013-02-22 17:27:20 -0600131 for subnet_cidr in cidr.subnet(mask_bits):
132 try:
133 resp, body = cls.client.create_subnet(network['id'],
134 str(subnet_cidr))
135 break
136 except exceptions.BadRequest as e:
137 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
138 if not is_overlapping_cidr:
139 raise
Matt Riedemannd052c572013-05-31 17:10:11 -0700140 # save the failure in case all of the CIDRs are overlapping
141 failure = e
142
143 if not body and failure:
144 raise failure
145
Miguel Lavallecc939612013-02-22 17:27:20 -0600146 subnet = body['subnet']
147 cls.subnets.append(subnet)
148 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530149
150 @classmethod
151 def create_port(cls, network):
152 """Wrapper utility that returns a test port."""
153 resp, body = cls.client.create_port(network['id'])
154 port = body['port']
155 cls.ports.append(port)
156 return port
raiesmh080fe76852013-09-13 11:52:56 +0530157
158 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700159 def create_router(cls, router_name=None, admin_state_up=False,
160 external_network_id=None, enable_snat=None):
161 ext_gw_info = {}
162 if external_network_id:
163 ext_gw_info['network_id'] = external_network_id
164 if enable_snat:
165 ext_gw_info['enable_snat'] = enable_snat
166 resp, body = cls.client.create_router(
167 router_name, external_gateway_info=ext_gw_info,
168 admin_state_up=admin_state_up)
169 router = body['router']
170 cls.routers.append(router)
171 return router
172
173 @classmethod
rosselladd68b232013-11-13 10:21:59 +0100174 def create_floating_ip(cls, external_network_id, **kwargs):
175 """Wrapper utility that returns a test floating IP."""
176 resp, body = cls.client.create_floating_ip(
177 external_network_id,
178 **kwargs)
179 fip = body['floatingip']
180 cls.floating_ips.append(fip)
181 return fip
182
183 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530184 def create_pool(cls, name, lb_method, protocol, subnet):
185 """Wrapper utility that returns a test pool."""
186 resp, body = cls.client.create_pool(name, lb_method, protocol,
187 subnet['id'])
188 pool = body['pool']
189 cls.pools.append(pool)
190 return pool
191
192 @classmethod
193 def create_vip(cls, name, protocol, protocol_port, subnet, pool):
194 """Wrapper utility that returns a test vip."""
195 resp, body = cls.client.create_vip(name, protocol, protocol_port,
196 subnet['id'], pool['id'])
197 vip = body['vip']
198 cls.vips.append(vip)
199 return vip
raiesmh08f8437ed2013-09-17 10:59:38 +0530200
201 @classmethod
202 def create_member(cls, protocol_port, pool):
203 """Wrapper utility that returns a test member."""
204 resp, body = cls.client.create_member("10.0.9.46",
205 protocol_port,
206 pool['id'])
207 member = body['member']
208 cls.members.append(member)
209 return member
raiesmh0832580d02013-09-17 13:11:34 +0530210
211 @classmethod
212 def create_health_monitor(cls, delay, max_retries, Type, timeout):
213 """Wrapper utility that returns a test health monitor."""
214 resp, body = cls.client.create_health_monitor(delay,
215 max_retries,
216 Type, timeout)
217 health_monitor = body['health_monitor']
218 cls.health_monitors.append(health_monitor)
219 return health_monitor
Anju Tiwari860097d2013-10-17 11:10:39 +0530220
221 @classmethod
222 def create_router_interface(cls, router_id, subnet_id):
223 """Wrapper utility that returns a router interface."""
224 resp, interface = cls.client.add_router_interface_with_subnet_id(
225 router_id, subnet_id)
226
227 @classmethod
228 def create_vpnservice(cls, subnet_id, router_id):
229 """Wrapper utility that returns a test vpn service."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400230 resp, body = cls.client.create_vpnservice(
Anju Tiwari860097d2013-10-17 11:10:39 +0530231 subnet_id, router_id, admin_state_up=True,
Masayuki Igawa259c1132013-10-31 17:48:44 +0900232 name=data_utils.rand_name("vpnservice-"))
Anju Tiwari860097d2013-10-17 11:10:39 +0530233 vpnservice = body['vpnservice']
234 cls.vpnservices.append(vpnservice)
235 return vpnservice
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700236
raiesmh08bd6070d2013-12-06 15:13:38 +0530237 @classmethod
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400238 def create_ikepolicy(cls, name):
raiesmh08bd6070d2013-12-06 15:13:38 +0530239 """Wrapper utility that returns a test ike policy."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400240 resp, body = cls.client.create_ikepolicy(name)
raiesmh08bd6070d2013-12-06 15:13:38 +0530241 ikepolicy = body['ikepolicy']
242 cls.ikepolicies.append(ikepolicy)
243 return ikepolicy
244
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700245
246class BaseAdminNetworkTest(BaseNetworkTest):
247
248 @classmethod
249 def setUpClass(cls):
250 super(BaseAdminNetworkTest, cls).setUpClass()
251 admin_username = cls.config.compute_admin.username
252 admin_password = cls.config.compute_admin.password
253 admin_tenant = cls.config.compute_admin.tenant_name
254 if not (admin_username and admin_password and admin_tenant):
255 msg = ("Missing Administrative Network API credentials "
256 "in configuration.")
257 raise cls.skipException(msg)
258 cls.admin_manager = clients.AdminManager(interface=cls._interface)
259 cls.admin_client = cls.admin_manager.network_client