blob: b6c2679ad244ef12c20b882fce71e9a4b66de4c5 [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
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021from tempest.common.utils.data_utils import rand_name
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 = []
Jay Pipesf4dad392012-06-05 16:03:58 -040068
69 @classmethod
70 def tearDownClass(cls):
Anju Tiwari860097d2013-10-17 11:10:39 +053071 has_exception = False
72 for vpnservice in cls.vpnservices:
73 try:
74 cls.client.delete_vpn_service(vpnservice['id'])
75 except Exception as exc:
76 LOG.exception(exc)
77 has_exception = True
78
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070079 for router in cls.routers:
Anju Tiwari860097d2013-10-17 11:10:39 +053080 try:
81 resp, body = cls.client.list_router_interfaces(router['id'])
82 interfaces = body['ports']
83 for i in interfaces:
84 cls.client.remove_router_interface_with_subnet_id(
85 router['id'], i['fixed_ips'][0]['subnet_id'])
86 except Exception as exc:
87 LOG.exception(exc)
88 has_exception = True
89 try:
90 cls.client.delete_router(router['id'])
91 except Exception as exc:
92 LOG.exception(exc)
93 has_exception = True
94
95 for health_monitor in cls.health_monitors:
96 try:
97 cls.client.delete_health_monitor(health_monitor['id'])
98 except Exception as exc:
99 LOG.exception(exc)
100 has_exception = True
101 for member in cls.members:
102 try:
103 cls.client.delete_member(member['id'])
104 except Exception as exc:
105 LOG.exception(exc)
106 has_exception = True
107 for vip in cls.vips:
108 try:
109 cls.client.delete_vip(vip['id'])
110 except Exception as exc:
111 LOG.exception(exc)
112 has_exception = True
113 for pool in cls.pools:
114 try:
115 cls.client.delete_pool(pool['id'])
116 except Exception as exc:
117 LOG.exception(exc)
118 has_exception = True
119 for port in cls.ports:
120 try:
121 cls.client.delete_port(port['id'])
122 except Exception as exc:
123 LOG.exception(exc)
124 has_exception = True
Miguel Lavallecc939612013-02-22 17:27:20 -0600125 for subnet in cls.subnets:
Anju Tiwari860097d2013-10-17 11:10:39 +0530126 try:
127 cls.client.delete_subnet(subnet['id'])
128 except Exception as exc:
129 LOG.exception(exc)
130 has_exception = True
Jay Pipesf4dad392012-06-05 16:03:58 -0400131 for network in cls.networks:
Anju Tiwari860097d2013-10-17 11:10:39 +0530132 try:
133 cls.client.delete_network(network['id'])
134 except Exception as exc:
135 LOG.exception(exc)
136 has_exception = True
Attila Fazekasf86fa312013-07-30 19:56:39 +0200137 super(BaseNetworkTest, cls).tearDownClass()
Anju Tiwari860097d2013-10-17 11:10:39 +0530138 if has_exception:
139 raise exceptions.TearDownException()
Jay Pipesf4dad392012-06-05 16:03:58 -0400140
Miguel Lavallecc939612013-02-22 17:27:20 -0600141 @classmethod
142 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500143 """Wrapper utility that returns a test network."""
Miguel Lavallecc939612013-02-22 17:27:20 -0600144 network_name = network_name or rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -0400145
Miguel Lavallecc939612013-02-22 17:27:20 -0600146 resp, body = cls.client.create_network(network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -0400147 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -0600148 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -0400149 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600150
151 @classmethod
152 def create_subnet(cls, network):
153 """Wrapper utility that returns a test subnet."""
154 cidr = netaddr.IPNetwork(cls.network_cfg.tenant_network_cidr)
155 mask_bits = cls.network_cfg.tenant_network_mask_bits
156 # Find a cidr that is not in use yet and create a subnet with it
Matt Riedemann9c9fa412013-06-19 18:33:47 -0700157 body = None
Matt Riedemannd052c572013-05-31 17:10:11 -0700158 failure = None
Miguel Lavallecc939612013-02-22 17:27:20 -0600159 for subnet_cidr in cidr.subnet(mask_bits):
160 try:
161 resp, body = cls.client.create_subnet(network['id'],
162 str(subnet_cidr))
163 break
164 except exceptions.BadRequest as e:
165 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
166 if not is_overlapping_cidr:
167 raise
Matt Riedemannd052c572013-05-31 17:10:11 -0700168 # save the failure in case all of the CIDRs are overlapping
169 failure = e
170
171 if not body and failure:
172 raise failure
173
Miguel Lavallecc939612013-02-22 17:27:20 -0600174 subnet = body['subnet']
175 cls.subnets.append(subnet)
176 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530177
178 @classmethod
179 def create_port(cls, network):
180 """Wrapper utility that returns a test port."""
181 resp, body = cls.client.create_port(network['id'])
182 port = body['port']
183 cls.ports.append(port)
184 return port
raiesmh080fe76852013-09-13 11:52:56 +0530185
186 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700187 def create_router(cls, router_name=None, admin_state_up=False,
188 external_network_id=None, enable_snat=None):
189 ext_gw_info = {}
190 if external_network_id:
191 ext_gw_info['network_id'] = external_network_id
192 if enable_snat:
193 ext_gw_info['enable_snat'] = enable_snat
194 resp, body = cls.client.create_router(
195 router_name, external_gateway_info=ext_gw_info,
196 admin_state_up=admin_state_up)
197 router = body['router']
198 cls.routers.append(router)
199 return router
200
201 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530202 def create_pool(cls, name, lb_method, protocol, subnet):
203 """Wrapper utility that returns a test pool."""
204 resp, body = cls.client.create_pool(name, lb_method, protocol,
205 subnet['id'])
206 pool = body['pool']
207 cls.pools.append(pool)
208 return pool
209
210 @classmethod
211 def create_vip(cls, name, protocol, protocol_port, subnet, pool):
212 """Wrapper utility that returns a test vip."""
213 resp, body = cls.client.create_vip(name, protocol, protocol_port,
214 subnet['id'], pool['id'])
215 vip = body['vip']
216 cls.vips.append(vip)
217 return vip
raiesmh08f8437ed2013-09-17 10:59:38 +0530218
219 @classmethod
220 def create_member(cls, protocol_port, pool):
221 """Wrapper utility that returns a test member."""
222 resp, body = cls.client.create_member("10.0.9.46",
223 protocol_port,
224 pool['id'])
225 member = body['member']
226 cls.members.append(member)
227 return member
raiesmh0832580d02013-09-17 13:11:34 +0530228
229 @classmethod
230 def create_health_monitor(cls, delay, max_retries, Type, timeout):
231 """Wrapper utility that returns a test health monitor."""
232 resp, body = cls.client.create_health_monitor(delay,
233 max_retries,
234 Type, timeout)
235 health_monitor = body['health_monitor']
236 cls.health_monitors.append(health_monitor)
237 return health_monitor
Anju Tiwari860097d2013-10-17 11:10:39 +0530238
239 @classmethod
240 def create_router_interface(cls, router_id, subnet_id):
241 """Wrapper utility that returns a router interface."""
242 resp, interface = cls.client.add_router_interface_with_subnet_id(
243 router_id, subnet_id)
244
245 @classmethod
246 def create_vpnservice(cls, subnet_id, router_id):
247 """Wrapper utility that returns a test vpn service."""
248 resp, body = cls.client.create_vpn_service(
249 subnet_id, router_id, admin_state_up=True,
250 name=rand_name("vpnservice-"))
251 vpnservice = body['vpnservice']
252 cls.vpnservices.append(vpnservice)
253 return vpnservice