blob: b222ae3ec9e95796467451eee66921005a8c63fe [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 = []
Jay Pipesf4dad392012-06-05 16:03:58 -040068
69 @classmethod
70 def tearDownClass(cls):
Matthew Treinishec3489c2013-10-25 17:26:50 +000071 # Clean up vpn services
Anju Tiwari860097d2013-10-17 11:10:39 +053072 for vpnservice in cls.vpnservices:
Matthew Treinishec3489c2013-10-25 17:26:50 +000073 cls.client.delete_vpn_service(vpnservice['id'])
74 # Clean up routers
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070075 for router in cls.routers:
Matthew Treinishec3489c2013-10-25 17:26:50 +000076 resp, body = cls.client.list_router_interfaces(router['id'])
77 interfaces = body['ports']
78 for i in interfaces:
79 cls.client.remove_router_interface_with_subnet_id(
80 router['id'], i['fixed_ips'][0]['subnet_id'])
81 cls.client.delete_router(router['id'])
82 # Clean up health monitors
Anju Tiwari860097d2013-10-17 11:10:39 +053083 for health_monitor in cls.health_monitors:
Matthew Treinishec3489c2013-10-25 17:26:50 +000084 cls.client.delete_health_monitor(health_monitor['id'])
85 # Clean up members
Anju Tiwari860097d2013-10-17 11:10:39 +053086 for member in cls.members:
Matthew Treinishec3489c2013-10-25 17:26:50 +000087 cls.client.delete_member(member['id'])
88 # Clean up vips
Anju Tiwari860097d2013-10-17 11:10:39 +053089 for vip in cls.vips:
Matthew Treinishec3489c2013-10-25 17:26:50 +000090 cls.client.delete_vip(vip['id'])
91 # Clean up pools
Anju Tiwari860097d2013-10-17 11:10:39 +053092 for pool in cls.pools:
Matthew Treinishec3489c2013-10-25 17:26:50 +000093 cls.client.delete_pool(pool['id'])
94 # Clean up ports
Anju Tiwari860097d2013-10-17 11:10:39 +053095 for port in cls.ports:
Matthew Treinishec3489c2013-10-25 17:26:50 +000096 cls.client.delete_port(port['id'])
97 # Clean up subnets
Miguel Lavallecc939612013-02-22 17:27:20 -060098 for subnet in cls.subnets:
Matthew Treinishec3489c2013-10-25 17:26:50 +000099 cls.client.delete_subnet(subnet['id'])
100 # Clean up networks
Jay Pipesf4dad392012-06-05 16:03:58 -0400101 for network in cls.networks:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000102 cls.client.delete_network(network['id'])
Attila Fazekasf86fa312013-07-30 19:56:39 +0200103 super(BaseNetworkTest, cls).tearDownClass()
Jay Pipesf4dad392012-06-05 16:03:58 -0400104
Miguel Lavallecc939612013-02-22 17:27:20 -0600105 @classmethod
106 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500107 """Wrapper utility that returns a test network."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900108 network_name = network_name or data_utils.rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -0400109
Miguel Lavallecc939612013-02-22 17:27:20 -0600110 resp, body = cls.client.create_network(network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -0400111 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -0600112 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -0400113 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600114
115 @classmethod
116 def create_subnet(cls, network):
117 """Wrapper utility that returns a test subnet."""
118 cidr = netaddr.IPNetwork(cls.network_cfg.tenant_network_cidr)
119 mask_bits = cls.network_cfg.tenant_network_mask_bits
120 # Find a cidr that is not in use yet and create a subnet with it
Matt Riedemann9c9fa412013-06-19 18:33:47 -0700121 body = None
Matt Riedemannd052c572013-05-31 17:10:11 -0700122 failure = None
Miguel Lavallecc939612013-02-22 17:27:20 -0600123 for subnet_cidr in cidr.subnet(mask_bits):
124 try:
125 resp, body = cls.client.create_subnet(network['id'],
126 str(subnet_cidr))
127 break
128 except exceptions.BadRequest as e:
129 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
130 if not is_overlapping_cidr:
131 raise
Matt Riedemannd052c572013-05-31 17:10:11 -0700132 # save the failure in case all of the CIDRs are overlapping
133 failure = e
134
135 if not body and failure:
136 raise failure
137
Miguel Lavallecc939612013-02-22 17:27:20 -0600138 subnet = body['subnet']
139 cls.subnets.append(subnet)
140 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530141
142 @classmethod
143 def create_port(cls, network):
144 """Wrapper utility that returns a test port."""
145 resp, body = cls.client.create_port(network['id'])
146 port = body['port']
147 cls.ports.append(port)
148 return port
raiesmh080fe76852013-09-13 11:52:56 +0530149
150 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700151 def create_router(cls, router_name=None, admin_state_up=False,
152 external_network_id=None, enable_snat=None):
153 ext_gw_info = {}
154 if external_network_id:
155 ext_gw_info['network_id'] = external_network_id
156 if enable_snat:
157 ext_gw_info['enable_snat'] = enable_snat
158 resp, body = cls.client.create_router(
159 router_name, external_gateway_info=ext_gw_info,
160 admin_state_up=admin_state_up)
161 router = body['router']
162 cls.routers.append(router)
163 return router
164
165 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530166 def create_pool(cls, name, lb_method, protocol, subnet):
167 """Wrapper utility that returns a test pool."""
168 resp, body = cls.client.create_pool(name, lb_method, protocol,
169 subnet['id'])
170 pool = body['pool']
171 cls.pools.append(pool)
172 return pool
173
174 @classmethod
175 def create_vip(cls, name, protocol, protocol_port, subnet, pool):
176 """Wrapper utility that returns a test vip."""
177 resp, body = cls.client.create_vip(name, protocol, protocol_port,
178 subnet['id'], pool['id'])
179 vip = body['vip']
180 cls.vips.append(vip)
181 return vip
raiesmh08f8437ed2013-09-17 10:59:38 +0530182
183 @classmethod
184 def create_member(cls, protocol_port, pool):
185 """Wrapper utility that returns a test member."""
186 resp, body = cls.client.create_member("10.0.9.46",
187 protocol_port,
188 pool['id'])
189 member = body['member']
190 cls.members.append(member)
191 return member
raiesmh0832580d02013-09-17 13:11:34 +0530192
193 @classmethod
194 def create_health_monitor(cls, delay, max_retries, Type, timeout):
195 """Wrapper utility that returns a test health monitor."""
196 resp, body = cls.client.create_health_monitor(delay,
197 max_retries,
198 Type, timeout)
199 health_monitor = body['health_monitor']
200 cls.health_monitors.append(health_monitor)
201 return health_monitor
Anju Tiwari860097d2013-10-17 11:10:39 +0530202
203 @classmethod
204 def create_router_interface(cls, router_id, subnet_id):
205 """Wrapper utility that returns a router interface."""
206 resp, interface = cls.client.add_router_interface_with_subnet_id(
207 router_id, subnet_id)
208
209 @classmethod
210 def create_vpnservice(cls, subnet_id, router_id):
211 """Wrapper utility that returns a test vpn service."""
212 resp, body = cls.client.create_vpn_service(
213 subnet_id, router_id, admin_state_up=True,
Masayuki Igawa259c1132013-10-31 17:48:44 +0900214 name=data_utils.rand_name("vpnservice-"))
Anju Tiwari860097d2013-10-17 11:10:39 +0530215 vpnservice = body['vpnservice']
216 cls.vpnservices.append(vpnservice)
217 return vpnservice
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700218
219
220class BaseAdminNetworkTest(BaseNetworkTest):
221
222 @classmethod
223 def setUpClass(cls):
224 super(BaseAdminNetworkTest, cls).setUpClass()
225 admin_username = cls.config.compute_admin.username
226 admin_password = cls.config.compute_admin.password
227 admin_tenant = cls.config.compute_admin.tenant_name
228 if not (admin_username and admin_password and admin_tenant):
229 msg = ("Missing Administrative Network API credentials "
230 "in configuration.")
231 raise cls.skipException(msg)
232 cls.admin_manager = clients.AdminManager(interface=cls._interface)
233 cls.admin_client = cls.admin_manager.network_client