blob: 4ef4a9174deba80b27d0078df388bb6c059904eb [file] [log] [blame]
Jude Cross986e3f52017-07-24 14:57:20 -07001# Copyright 2018 Rackspace US Inc. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import ipaddress
16import pkg_resources
17import random
Adam Harwellcd72b562018-05-07 11:37:22 -070018import requests
Jude Cross986e3f52017-07-24 14:57:20 -070019import shlex
20import six
21import string
22import subprocess
23import tempfile
Adam Harwellcd72b562018-05-07 11:37:22 -070024import time
Jude Cross986e3f52017-07-24 14:57:20 -070025
26from oslo_log import log as logging
27from oslo_utils import uuidutils
28from tempest import config
29from tempest.lib.common.utils import data_utils
30from tempest.lib.common.utils.linux import remote_client
Jude Cross986e3f52017-07-24 14:57:20 -070031from tempest.lib import exceptions
32from tempest import test
Michael Johnson04dc5cb2019-01-20 11:03:50 -080033import tenacity
Jude Cross986e3f52017-07-24 14:57:20 -070034
35from octavia_tempest_plugin import clients
36from octavia_tempest_plugin.common import constants as const
37from octavia_tempest_plugin.tests import validators
38from octavia_tempest_plugin.tests import waiters
39
40CONF = config.CONF
41LOG = logging.getLogger(__name__)
42
Michael Johnson04dc5cb2019-01-20 11:03:50 -080043RETRY_ATTEMPTS = 15
44RETRY_INITIAL_DELAY = 1
45RETRY_BACKOFF = 1
46RETRY_MAX = 5
47
Jude Cross986e3f52017-07-24 14:57:20 -070048
49class LoadBalancerBaseTest(test.BaseTestCase):
50 """Base class for load balancer tests."""
51
52 # Setup cls.os_roles_lb_member. cls.os_primary, cls.os_roles_lb_member,
53 # and cls.os_roles_lb_admin credentials.
54 credentials = ['admin', 'primary',
55 ['lb_member', CONF.load_balancer.member_role],
56 ['lb_member2', CONF.load_balancer.member_role],
57 ['lb_admin', CONF.load_balancer.admin_role]]
58
59 client_manager = clients.ManagerV2
Adam Harwelle029af22018-05-24 17:13:28 -070060 webserver1_response = 1
61 webserver2_response = 5
Michael Johnsondfd818a2018-08-21 20:54:54 -070062 used_ips = []
Jude Cross986e3f52017-07-24 14:57:20 -070063
64 @classmethod
65 def skip_checks(cls):
66 """Check if we should skip all of the children tests."""
67 super(LoadBalancerBaseTest, cls).skip_checks()
68
69 service_list = {
70 'load_balancer': CONF.service_available.load_balancer,
71 }
72
73 live_service_list = {
74 'compute': CONF.service_available.nova,
75 'image': CONF.service_available.glance,
76 'neutron': CONF.service_available.neutron
77 }
78
79 if not CONF.load_balancer.test_with_noop:
80 service_list.update(live_service_list)
81
82 for service, available in service_list.items():
83 if not available:
zhangzs2a6cf672018-11-10 16:13:11 +080084 skip_msg = ("{0} skipped as {1} service is not "
Jude Cross986e3f52017-07-24 14:57:20 -070085 "available.".format(cls.__name__, service))
86 raise cls.skipException(skip_msg)
87
88 # We must be able to reach our VIP and instances
89 if not (CONF.network.project_networks_reachable
90 or CONF.network.public_network_id):
91 msg = ('Either project_networks_reachable must be "true", or '
92 'public_network_id must be defined.')
93 raise cls.skipException(msg)
94
95 @classmethod
96 def setup_credentials(cls):
97 """Setup test credentials and network resources."""
98 # Do not auto create network resources
99 cls.set_network_resources()
100 super(LoadBalancerBaseTest, cls).setup_credentials()
101
102 @classmethod
103 def setup_clients(cls):
104 """Setup client aliases."""
105 super(LoadBalancerBaseTest, cls).setup_clients()
106 cls.lb_mem_float_ip_client = cls.os_roles_lb_member.floating_ips_client
107 cls.lb_mem_keypairs_client = cls.os_roles_lb_member.keypairs_client
108 cls.lb_mem_net_client = cls.os_roles_lb_member.networks_client
109 cls.lb_mem_ports_client = cls.os_roles_lb_member.ports_client
110 cls.lb_mem_routers_client = cls.os_roles_lb_member.routers_client
111 cls.lb_mem_SG_client = cls.os_roles_lb_member.security_groups_client
112 cls.lb_mem_SGr_client = (
113 cls.os_roles_lb_member.security_group_rules_client)
114 cls.lb_mem_servers_client = cls.os_roles_lb_member.servers_client
115 cls.lb_mem_subnet_client = cls.os_roles_lb_member.subnets_client
116 cls.mem_lb_client = cls.os_roles_lb_member.loadbalancer_client
Jude Crossfbbd2b42017-08-09 15:21:04 -0700117 cls.mem_listener_client = cls.os_roles_lb_member.listener_client
Adam Harwell8ffce3e2018-05-01 21:18:44 -0700118 cls.mem_pool_client = cls.os_roles_lb_member.pool_client
Adam Harwellde3e0542018-05-03 18:21:06 -0700119 cls.mem_member_client = cls.os_roles_lb_member.member_client
Adam Harwell60ed9d92018-05-10 13:23:13 -0700120 cls.mem_healthmonitor_client = (
121 cls.os_roles_lb_member.healthmonitor_client)
Adam Harwell446f8be2018-05-24 16:51:03 -0700122 cls.mem_l7policy_client = cls.os_roles_lb_member.l7policy_client
Adam Harwelle029af22018-05-24 17:13:28 -0700123 cls.mem_l7rule_client = cls.os_roles_lb_member.l7rule_client
Michael Johnson2b10e0a2019-01-25 15:42:13 -0800124 cls.lb_admin_amphora_client = cls.os_roles_lb_admin.amphora_client
Michael Johnsonaff2e862019-01-11 16:38:00 -0800125 cls.lb_admin_flavor_profile_client = (
126 cls.os_roles_lb_admin.flavor_profile_client)
Michael Johnsona2d03072019-01-14 17:18:21 -0800127 cls.lb_admin_flavor_client = cls.os_roles_lb_admin.flavor_client
128 cls.mem_flavor_client = cls.os_roles_lb_member.flavor_client
Michael Johnsonfc223fe2019-01-15 16:40:05 -0800129 cls.mem_provider_client = cls.os_roles_lb_member.provider_client
Carlos Goncalvesc2e12162019-02-14 23:57:44 +0100130 cls.os_admin_servers_client = cls.os_admin.servers_client
Adam Harwellc2aa20c2019-11-20 11:15:07 -0800131 cls.lb_admin_flavor_capabilities_client = (
Michael Johnson77df0322019-01-15 18:27:58 -0800132 cls.os_roles_lb_admin.flavor_capabilities_client)
Adam Harwellc2aa20c2019-11-20 11:15:07 -0800133 cls.lb_admin_availability_zone_capabilities_client = (
134 cls.os_roles_lb_admin.availability_zone_capabilities_client)
135 cls.lb_admin_availability_zone_profile_client = (
136 cls.os_roles_lb_admin.availability_zone_profile_client)
137 cls.lb_admin_availability_zone_client = (
138 cls.os_roles_lb_admin.availability_zone_client)
139 cls.mem_availability_zone_client = (
140 cls.os_roles_lb_member.availability_zone_client)
Jude Cross986e3f52017-07-24 14:57:20 -0700141
142 @classmethod
143 def resource_setup(cls):
144 """Setup resources needed by the tests."""
145 super(LoadBalancerBaseTest, cls).resource_setup()
146
147 conf_lb = CONF.load_balancer
148
Michael Johnsondfd818a2018-08-21 20:54:54 -0700149 cls.api_version = cls.mem_lb_client.get_max_api_version()
150
Jude Cross986e3f52017-07-24 14:57:20 -0700151 if conf_lb.test_subnet_override and not conf_lb.test_network_override:
152 raise exceptions.InvalidConfiguration(
153 "Configuration value test_network_override must be "
154 "specified if test_subnet_override is used.")
155
156 show_subnet = cls.lb_mem_subnet_client.show_subnet
157 if CONF.load_balancer.test_with_noop:
158 cls.lb_member_vip_net = {'id': uuidutils.generate_uuid()}
159 cls.lb_member_vip_subnet = {'id': uuidutils.generate_uuid()}
160 cls.lb_member_1_net = {'id': uuidutils.generate_uuid()}
161 cls.lb_member_1_subnet = {'id': uuidutils.generate_uuid()}
162 cls.lb_member_2_net = {'id': uuidutils.generate_uuid()}
163 cls.lb_member_2_subnet = {'id': uuidutils.generate_uuid()}
164 if CONF.load_balancer.test_with_ipv6:
Michael Johnson5a16ad32018-10-18 14:49:11 -0700165 cls.lb_member_vip_ipv6_net = {'id': uuidutils.generate_uuid()}
Jude Cross986e3f52017-07-24 14:57:20 -0700166 cls.lb_member_vip_ipv6_subnet = {'id':
167 uuidutils.generate_uuid()}
168 cls.lb_member_1_ipv6_subnet = {'id': uuidutils.generate_uuid()}
169 cls.lb_member_2_ipv6_subnet = {'id': uuidutils.generate_uuid()}
Michael Johnson590fbe12019-07-03 14:30:01 -0700170 cls.lb_member_vip_ipv6_subnet_stateful = True
Jude Cross986e3f52017-07-24 14:57:20 -0700171 return
172 elif CONF.load_balancer.test_network_override:
173 if conf_lb.test_subnet_override:
174 override_subnet = show_subnet(conf_lb.test_subnet_override)
175 else:
176 override_subnet = None
177
178 show_net = cls.lb_mem_net_client.show_network
179 override_network = show_net(conf_lb.test_network_override)
180 override_network = override_network.get('network')
181
182 cls.lb_member_vip_net = override_network
183 cls.lb_member_vip_subnet = override_subnet
184 cls.lb_member_1_net = override_network
185 cls.lb_member_1_subnet = override_subnet
186 cls.lb_member_2_net = override_network
187 cls.lb_member_2_subnet = override_subnet
188
189 if (CONF.load_balancer.test_with_ipv6 and
190 conf_lb.test_IPv6_subnet_override):
191 override_ipv6_subnet = show_subnet(
192 conf_lb.test_IPv6_subnet_override)
193 cls.lb_member_vip_ipv6_subnet = override_ipv6_subnet
194 cls.lb_member_1_ipv6_subnet = override_ipv6_subnet
195 cls.lb_member_2_ipv6_subnet = override_ipv6_subnet
Michael Johnson590fbe12019-07-03 14:30:01 -0700196 cls.lb_member_vip_ipv6_subnet_stateful = False
197 if (override_ipv6_subnet[0]['ipv6_address_mode'] ==
198 'dhcpv6-stateful'):
199 cls.lb_member_vip_ipv6_subnet_stateful = True
Jude Cross986e3f52017-07-24 14:57:20 -0700200 else:
201 cls.lb_member_vip_ipv6_subnet = None
202 cls.lb_member_1_ipv6_subnet = None
203 cls.lb_member_2_ipv6_subnet = None
204 else:
205 cls._create_networks()
206
207 LOG.debug('Octavia Setup: lb_member_vip_net = {}'.format(
208 cls.lb_member_vip_net[const.ID]))
209 if cls.lb_member_vip_subnet:
210 LOG.debug('Octavia Setup: lb_member_vip_subnet = {}'.format(
211 cls.lb_member_vip_subnet[const.ID]))
212 LOG.debug('Octavia Setup: lb_member_1_net = {}'.format(
213 cls.lb_member_1_net[const.ID]))
214 if cls.lb_member_1_subnet:
215 LOG.debug('Octavia Setup: lb_member_1_subnet = {}'.format(
216 cls.lb_member_1_subnet[const.ID]))
217 LOG.debug('Octavia Setup: lb_member_2_net = {}'.format(
218 cls.lb_member_2_net[const.ID]))
219 if cls.lb_member_2_subnet:
220 LOG.debug('Octavia Setup: lb_member_2_subnet = {}'.format(
221 cls.lb_member_2_subnet[const.ID]))
Michael Johnson124ba8b2018-08-30 16:06:05 -0700222 if CONF.load_balancer.test_with_ipv6:
223 if cls.lb_member_vip_ipv6_subnet:
224 LOG.debug('Octavia Setup: lb_member_vip_ipv6_subnet = '
225 '{}'.format(cls.lb_member_vip_ipv6_subnet[const.ID]))
226 if cls.lb_member_1_ipv6_subnet:
227 LOG.debug('Octavia Setup: lb_member_1_ipv6_subnet = {}'.format(
228 cls.lb_member_1_ipv6_subnet[const.ID]))
229 if cls.lb_member_2_ipv6_subnet:
230 LOG.debug('Octavia Setup: lb_member_2_ipv6_subnet = {}'.format(
231 cls.lb_member_2_ipv6_subnet[const.ID]))
Jude Cross986e3f52017-07-24 14:57:20 -0700232
Jude Cross986e3f52017-07-24 14:57:20 -0700233 @classmethod
Michael Johnson04dc5cb2019-01-20 11:03:50 -0800234 # Neutron can be slow to clean up ports from the subnets/networks.
235 # Retry this delete a few times if we get a "Conflict" error to give
236 # neutron time to fully cleanup the ports.
237 @tenacity.retry(
238 retry=tenacity.retry_if_exception_type(exceptions.Conflict),
239 wait=tenacity.wait_incrementing(
240 RETRY_INITIAL_DELAY, RETRY_BACKOFF, RETRY_MAX),
241 stop=tenacity.stop_after_attempt(RETRY_ATTEMPTS))
242 def _logging_delete_network(cls, net_id):
243 try:
244 cls.lb_mem_net_client.delete_network(net_id)
245 except Exception:
246 LOG.error('Unable to delete network {}. Active ports:'.format(
247 net_id))
248 LOG.error(cls.lb_mem_ports_client.list_ports())
249 raise
250
251 @classmethod
252 # Neutron can be slow to clean up ports from the subnets/networks.
253 # Retry this delete a few times if we get a "Conflict" error to give
254 # neutron time to fully cleanup the ports.
255 @tenacity.retry(
256 retry=tenacity.retry_if_exception_type(exceptions.Conflict),
257 wait=tenacity.wait_incrementing(
258 RETRY_INITIAL_DELAY, RETRY_BACKOFF, RETRY_MAX),
259 stop=tenacity.stop_after_attempt(RETRY_ATTEMPTS))
260 def _logging_delete_subnet(cls, subnet_id):
261 try:
262 cls.lb_mem_subnet_client.delete_subnet(subnet_id)
263 except Exception:
264 LOG.error('Unable to delete subnet {}. Active ports:'.format(
265 subnet_id))
266 LOG.error(cls.lb_mem_ports_client.list_ports())
267 raise
268
269 @classmethod
Jude Cross986e3f52017-07-24 14:57:20 -0700270 def _create_networks(cls):
271 """Creates networks, subnets, and routers used in tests.
272
273 The following are expected to be defined and available to the tests:
274 cls.lb_member_vip_net
275 cls.lb_member_vip_subnet
276 cls.lb_member_vip_ipv6_subnet (optional)
277 cls.lb_member_1_net
278 cls.lb_member_1_subnet
279 cls.lb_member_1_ipv6_subnet (optional)
280 cls.lb_member_2_net
281 cls.lb_member_2_subnet
282 cls.lb_member_2_ipv6_subnet (optional)
283 """
284
285 # Create tenant VIP network
286 network_kwargs = {
287 'name': data_utils.rand_name("lb_member_vip_network")}
288 if CONF.network_feature_enabled.port_security:
289 # Note: Allowed Address Pairs requires port security
290 network_kwargs['port_security_enabled'] = True
291 result = cls.lb_mem_net_client.create_network(**network_kwargs)
292 cls.lb_member_vip_net = result['network']
293 LOG.info('lb_member_vip_net: {}'.format(cls.lb_member_vip_net))
294 cls.addClassResourceCleanup(
295 waiters.wait_for_not_found,
Michael Johnson04dc5cb2019-01-20 11:03:50 -0800296 cls._logging_delete_network,
Jude Cross986e3f52017-07-24 14:57:20 -0700297 cls.lb_mem_net_client.show_network,
298 cls.lb_member_vip_net['id'])
299
300 # Create tenant VIP subnet
301 subnet_kwargs = {
302 'name': data_utils.rand_name("lb_member_vip_subnet"),
303 'network_id': cls.lb_member_vip_net['id'],
304 'cidr': CONF.load_balancer.vip_subnet_cidr,
305 'ip_version': 4}
306 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
307 cls.lb_member_vip_subnet = result['subnet']
308 LOG.info('lb_member_vip_subnet: {}'.format(cls.lb_member_vip_subnet))
309 cls.addClassResourceCleanup(
310 waiters.wait_for_not_found,
Michael Johnson04dc5cb2019-01-20 11:03:50 -0800311 cls._logging_delete_subnet,
Jude Cross986e3f52017-07-24 14:57:20 -0700312 cls.lb_mem_subnet_client.show_subnet,
313 cls.lb_member_vip_subnet['id'])
314
315 # Create tenant VIP IPv6 subnet
316 if CONF.load_balancer.test_with_ipv6:
Adam Harwell2b9432f2019-05-02 13:56:09 -0600317 # See if ipv6-private-subnet exists and use it if so.
318 priv_ipv6_subnet = cls.os_admin.subnets_client.list_subnets(
319 name='ipv6-private-subnet')['subnets']
Michael Johnson5a16ad32018-10-18 14:49:11 -0700320
Michael Johnson590fbe12019-07-03 14:30:01 -0700321 cls.lb_member_vip_ipv6_subnet_stateful = False
Adam Harwell2b9432f2019-05-02 13:56:09 -0600322 if len(priv_ipv6_subnet) == 1:
Carlos Goncalves84af48c2019-07-25 15:51:30 +0200323 if (priv_ipv6_subnet[0]['ipv6_address_mode'] ==
324 'dhcpv6-stateful'):
325 cls.lb_member_vip_ipv6_subnet_stateful = True
Adam Harwell2b9432f2019-05-02 13:56:09 -0600326 cls.lb_member_vip_ipv6_subnet = priv_ipv6_subnet[0]
Michael Johnson5a16ad32018-10-18 14:49:11 -0700327 cls.lb_member_vip_ipv6_net = {
Adam Harwell2b9432f2019-05-02 13:56:09 -0600328 'id': priv_ipv6_subnet[0]['network_id']}
Michael Johnson5a16ad32018-10-18 14:49:11 -0700329 else:
330 subnet_kwargs = {
331 'name': data_utils.rand_name("lb_member_vip_ipv6_subnet"),
332 'network_id': cls.lb_member_vip_net['id'],
333 'cidr': CONF.load_balancer.vip_ipv6_subnet_cidr,
334 'ip_version': 6}
335 result = cls.lb_mem_subnet_client.create_subnet(
336 **subnet_kwargs)
Michael Johnson7c5b9012019-05-28 11:02:29 -0700337 cls.lb_member_vip_ipv6_net = cls.lb_member_vip_net
Michael Johnson5a16ad32018-10-18 14:49:11 -0700338 cls.lb_member_vip_ipv6_subnet = result['subnet']
339 cls.addClassResourceCleanup(
340 waiters.wait_for_not_found,
Michael Johnson04dc5cb2019-01-20 11:03:50 -0800341 cls._logging_delete_subnet,
Michael Johnson5a16ad32018-10-18 14:49:11 -0700342 cls.lb_mem_subnet_client.show_subnet,
343 cls.lb_member_vip_ipv6_subnet['id'])
Carlos Goncalves84af48c2019-07-25 15:51:30 +0200344
Jude Cross986e3f52017-07-24 14:57:20 -0700345 LOG.info('lb_member_vip_ipv6_subnet: {}'.format(
346 cls.lb_member_vip_ipv6_subnet))
Jude Cross986e3f52017-07-24 14:57:20 -0700347
348 # Create tenant member 1 network
349 network_kwargs = {
350 'name': data_utils.rand_name("lb_member_1_network")}
351 if CONF.network_feature_enabled.port_security:
352 if CONF.load_balancer.enable_security_groups:
353 network_kwargs['port_security_enabled'] = True
354 else:
355 network_kwargs['port_security_enabled'] = False
356 result = cls.lb_mem_net_client.create_network(**network_kwargs)
357 cls.lb_member_1_net = result['network']
358 LOG.info('lb_member_1_net: {}'.format(cls.lb_member_1_net))
359 cls.addClassResourceCleanup(
360 waiters.wait_for_not_found,
Michael Johnson04dc5cb2019-01-20 11:03:50 -0800361 cls._logging_delete_network,
Jude Cross986e3f52017-07-24 14:57:20 -0700362 cls.lb_mem_net_client.show_network,
363 cls.lb_member_1_net['id'])
364
365 # Create tenant member 1 subnet
366 subnet_kwargs = {
367 'name': data_utils.rand_name("lb_member_1_subnet"),
368 'network_id': cls.lb_member_1_net['id'],
369 'cidr': CONF.load_balancer.member_1_ipv4_subnet_cidr,
370 'ip_version': 4}
371 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
372 cls.lb_member_1_subnet = result['subnet']
373 LOG.info('lb_member_1_subnet: {}'.format(cls.lb_member_1_subnet))
374 cls.addClassResourceCleanup(
375 waiters.wait_for_not_found,
Michael Johnson04dc5cb2019-01-20 11:03:50 -0800376 cls._logging_delete_subnet,
Jude Cross986e3f52017-07-24 14:57:20 -0700377 cls.lb_mem_subnet_client.show_subnet,
378 cls.lb_member_1_subnet['id'])
379
380 # Create tenant member 1 ipv6 subnet
381 if CONF.load_balancer.test_with_ipv6:
382 subnet_kwargs = {
383 'name': data_utils.rand_name("lb_member_1_ipv6_subnet"),
384 'network_id': cls.lb_member_1_net['id'],
385 'cidr': CONF.load_balancer.member_1_ipv6_subnet_cidr,
386 'ip_version': 6}
387 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
Michael Johnsonbf916df2018-10-17 10:59:28 -0700388 cls.lb_member_1_subnet_prefix = (
389 CONF.load_balancer.member_1_ipv6_subnet_cidr.rpartition('/')[2]
390 )
391 assert(cls.lb_member_1_subnet_prefix.isdigit())
Jude Cross986e3f52017-07-24 14:57:20 -0700392 cls.lb_member_1_ipv6_subnet = result['subnet']
393 LOG.info('lb_member_1_ipv6_subnet: {}'.format(
394 cls.lb_member_1_ipv6_subnet))
395 cls.addClassResourceCleanup(
396 waiters.wait_for_not_found,
Michael Johnson04dc5cb2019-01-20 11:03:50 -0800397 cls._logging_delete_subnet,
Jude Cross986e3f52017-07-24 14:57:20 -0700398 cls.lb_mem_subnet_client.show_subnet,
399 cls.lb_member_1_ipv6_subnet['id'])
400
401 # Create tenant member 2 network
402 network_kwargs = {
403 'name': data_utils.rand_name("lb_member_2_network")}
404 if CONF.network_feature_enabled.port_security:
405 if CONF.load_balancer.enable_security_groups:
406 network_kwargs['port_security_enabled'] = True
407 else:
408 network_kwargs['port_security_enabled'] = False
409 result = cls.lb_mem_net_client.create_network(**network_kwargs)
410 cls.lb_member_2_net = result['network']
411 LOG.info('lb_member_2_net: {}'.format(cls.lb_member_2_net))
412 cls.addClassResourceCleanup(
413 waiters.wait_for_not_found,
Michael Johnson04dc5cb2019-01-20 11:03:50 -0800414 cls._logging_delete_network,
Jude Cross986e3f52017-07-24 14:57:20 -0700415 cls.lb_mem_net_client.show_network,
416 cls.lb_member_2_net['id'])
417
418 # Create tenant member 2 subnet
419 subnet_kwargs = {
420 'name': data_utils.rand_name("lb_member_2_subnet"),
421 'network_id': cls.lb_member_2_net['id'],
422 'cidr': CONF.load_balancer.member_2_ipv4_subnet_cidr,
423 'ip_version': 4}
424 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
425 cls.lb_member_2_subnet = result['subnet']
426 LOG.info('lb_member_2_subnet: {}'.format(cls.lb_member_2_subnet))
427 cls.addClassResourceCleanup(
428 waiters.wait_for_not_found,
Michael Johnson04dc5cb2019-01-20 11:03:50 -0800429 cls._logging_delete_subnet,
Jude Cross986e3f52017-07-24 14:57:20 -0700430 cls.lb_mem_subnet_client.show_subnet,
431 cls.lb_member_2_subnet['id'])
432
433 # Create tenant member 2 ipv6 subnet
434 if CONF.load_balancer.test_with_ipv6:
435 subnet_kwargs = {
436 'name': data_utils.rand_name("lb_member_2_ipv6_subnet"),
437 'network_id': cls.lb_member_2_net['id'],
438 'cidr': CONF.load_balancer.member_2_ipv6_subnet_cidr,
439 'ip_version': 6}
440 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
Michael Johnsonbf916df2018-10-17 10:59:28 -0700441 cls.lb_member_2_subnet_prefix = (
442 CONF.load_balancer.member_2_ipv6_subnet_cidr.rpartition('/')[2]
443 )
444 assert(cls.lb_member_2_subnet_prefix.isdigit())
Jude Cross986e3f52017-07-24 14:57:20 -0700445 cls.lb_member_2_ipv6_subnet = result['subnet']
446 LOG.info('lb_member_2_ipv6_subnet: {}'.format(
447 cls.lb_member_2_ipv6_subnet))
448 cls.addClassResourceCleanup(
449 waiters.wait_for_not_found,
Michael Johnson04dc5cb2019-01-20 11:03:50 -0800450 cls._logging_delete_subnet,
Jude Cross986e3f52017-07-24 14:57:20 -0700451 cls.lb_mem_subnet_client.show_subnet,
452 cls.lb_member_2_ipv6_subnet['id'])
453
Adam Harwellcd72b562018-05-07 11:37:22 -0700454 @classmethod
Michael Johnson07c9a632018-06-07 13:27:42 -0700455 def _setup_lb_network_kwargs(cls, lb_kwargs, ip_version=None,
456 use_fixed_ip=False):
Adam Harwell60ed9d92018-05-10 13:23:13 -0700457 if not ip_version:
458 ip_version = 6 if CONF.load_balancer.test_with_ipv6 else 4
Michael Johnson5a16ad32018-10-18 14:49:11 -0700459 if cls.lb_member_vip_subnet or cls.lb_member_vip_ipv6_subnet:
Adam Harwellcd72b562018-05-07 11:37:22 -0700460 ip_index = data_utils.rand_int_id(start=10, end=100)
Michael Johnsondfd818a2018-08-21 20:54:54 -0700461 while ip_index in cls.used_ips:
462 ip_index = data_utils.rand_int_id(start=10, end=100)
463 cls.used_ips.append(ip_index)
Adam Harwellcd72b562018-05-07 11:37:22 -0700464 if ip_version == 4:
Adam Harwellcd72b562018-05-07 11:37:22 -0700465 subnet_id = cls.lb_member_vip_subnet[const.ID]
Michael Johnson5a16ad32018-10-18 14:49:11 -0700466 if CONF.load_balancer.test_with_noop:
467 lb_vip_address = '198.18.33.33'
468 else:
469 subnet = cls.os_admin.subnets_client.show_subnet(subnet_id)
470 network = ipaddress.IPv4Network(subnet['subnet']['cidr'])
471 lb_vip_address = str(network[ip_index])
Adam Harwellcd72b562018-05-07 11:37:22 -0700472 else:
Adam Harwellcd72b562018-05-07 11:37:22 -0700473 subnet_id = cls.lb_member_vip_ipv6_subnet[const.ID]
Michael Johnson5a16ad32018-10-18 14:49:11 -0700474 if CONF.load_balancer.test_with_noop:
475 lb_vip_address = '2001:db8:33:33:33:33:33:33'
476 else:
477 subnet = cls.os_admin.subnets_client.show_subnet(subnet_id)
478 network = ipaddress.IPv6Network(subnet['subnet']['cidr'])
479 lb_vip_address = str(network[ip_index])
Michael Johnson590fbe12019-07-03 14:30:01 -0700480 # If the subnet is IPv6 slaac or dhcpv6-stateless
481 # neutron does not allow a fixed IP
482 if not cls.lb_member_vip_ipv6_subnet_stateful:
483 use_fixed_ip = False
Adam Harwellcd72b562018-05-07 11:37:22 -0700484 lb_kwargs[const.VIP_SUBNET_ID] = subnet_id
Michael Johnson07c9a632018-06-07 13:27:42 -0700485 if use_fixed_ip:
486 lb_kwargs[const.VIP_ADDRESS] = lb_vip_address
Adam Harwellcd72b562018-05-07 11:37:22 -0700487 if CONF.load_balancer.test_with_noop:
488 lb_kwargs[const.VIP_NETWORK_ID] = (
489 cls.lb_member_vip_net[const.ID])
490 else:
491 lb_kwargs[const.VIP_NETWORK_ID] = cls.lb_member_vip_net[const.ID]
492 lb_kwargs[const.VIP_SUBNET_ID] = None
493
494
495class LoadBalancerBaseTestWithCompute(LoadBalancerBaseTest):
496 @classmethod
497 def resource_setup(cls):
498 super(LoadBalancerBaseTestWithCompute, cls).resource_setup()
499 # If validation is disabled in this cloud, we won't be able to
500 # start the webservers, so don't even boot them.
501 if not CONF.validation.run_validation:
502 return
503
504 # Create a keypair for the webservers
505 keypair_name = data_utils.rand_name('lb_member_keypair')
506 result = cls.lb_mem_keypairs_client.create_keypair(
507 name=keypair_name)
508 cls.lb_member_keypair = result['keypair']
509 LOG.info('lb_member_keypair: {}'.format(cls.lb_member_keypair))
510 cls.addClassResourceCleanup(
511 waiters.wait_for_not_found,
512 cls.lb_mem_keypairs_client.delete_keypair,
513 cls.lb_mem_keypairs_client.show_keypair,
514 keypair_name)
515
516 if (CONF.load_balancer.enable_security_groups and
517 CONF.network_feature_enabled.port_security):
518 # Set up the security group for the webservers
519 SG_name = data_utils.rand_name('lb_member_SG')
520 cls.lb_member_sec_group = (
521 cls.lb_mem_SG_client.create_security_group(
522 name=SG_name)['security_group'])
523 cls.addClassResourceCleanup(
524 waiters.wait_for_not_found,
525 cls.lb_mem_SG_client.delete_security_group,
526 cls.lb_mem_SG_client.show_security_group,
527 cls.lb_member_sec_group['id'])
528
529 # Create a security group rule to allow 80-81 (test webservers)
530 SGr = cls.lb_mem_SGr_client.create_security_group_rule(
531 direction='ingress',
532 security_group_id=cls.lb_member_sec_group['id'],
533 protocol='tcp',
534 ethertype='IPv4',
535 port_range_min=80,
536 port_range_max=81)['security_group_rule']
537 cls.addClassResourceCleanup(
538 waiters.wait_for_not_found,
539 cls.lb_mem_SGr_client.delete_security_group_rule,
540 cls.lb_mem_SGr_client.show_security_group_rule,
541 SGr['id'])
542 # Create a security group rule to allow 22 (ssh)
543 SGr = cls.lb_mem_SGr_client.create_security_group_rule(
544 direction='ingress',
545 security_group_id=cls.lb_member_sec_group['id'],
546 protocol='tcp',
547 ethertype='IPv4',
548 port_range_min=22,
549 port_range_max=22)['security_group_rule']
550 cls.addClassResourceCleanup(
551 waiters.wait_for_not_found,
552 cls.lb_mem_SGr_client.delete_security_group_rule,
553 cls.lb_mem_SGr_client.show_security_group_rule,
554 SGr['id'])
555 if CONF.load_balancer.test_with_ipv6:
556 # Create a security group rule to allow 80-81 (test webservers)
557 SGr = cls.lb_mem_SGr_client.create_security_group_rule(
558 direction='ingress',
559 security_group_id=cls.lb_member_sec_group['id'],
560 protocol='tcp',
561 ethertype='IPv6',
562 port_range_min=80,
563 port_range_max=81)['security_group_rule']
564 cls.addClassResourceCleanup(
565 waiters.wait_for_not_found,
566 cls.lb_mem_SGr_client.delete_security_group_rule,
567 cls.lb_mem_SGr_client.show_security_group_rule,
568 SGr['id'])
569 # Create a security group rule to allow 22 (ssh)
570 SGr = cls.lb_mem_SGr_client.create_security_group_rule(
571 direction='ingress',
572 security_group_id=cls.lb_member_sec_group['id'],
573 protocol='tcp',
574 ethertype='IPv6',
575 port_range_min=22,
576 port_range_max=22)['security_group_rule']
577 cls.addClassResourceCleanup(
578 waiters.wait_for_not_found,
579 cls.lb_mem_SGr_client.delete_security_group_rule,
580 cls.lb_mem_SGr_client.show_security_group_rule,
581 SGr['id'])
582
583 LOG.info('lb_member_sec_group: {}'.format(cls.lb_member_sec_group))
584
585 # Create webserver 1 instance
586 server_details = cls._create_webserver('lb_member_webserver1',
587 cls.lb_member_1_net)
588
589 cls.lb_member_webserver1 = server_details['server']
590 cls.webserver1_ip = server_details.get('ipv4_address')
591 cls.webserver1_ipv6 = server_details.get('ipv6_address')
592 cls.webserver1_public_ip = server_details['public_ipv4_address']
593
594 LOG.debug('Octavia Setup: lb_member_webserver1 = {}'.format(
595 cls.lb_member_webserver1[const.ID]))
596 LOG.debug('Octavia Setup: webserver1_ip = {}'.format(
597 cls.webserver1_ip))
598 LOG.debug('Octavia Setup: webserver1_ipv6 = {}'.format(
599 cls.webserver1_ipv6))
600 LOG.debug('Octavia Setup: webserver1_public_ip = {}'.format(
601 cls.webserver1_public_ip))
602
603 # Create webserver 2 instance
604 server_details = cls._create_webserver('lb_member_webserver2',
605 cls.lb_member_2_net)
606
607 cls.lb_member_webserver2 = server_details['server']
608 cls.webserver2_ip = server_details.get('ipv4_address')
609 cls.webserver2_ipv6 = server_details.get('ipv6_address')
610 cls.webserver2_public_ip = server_details['public_ipv4_address']
611
612 LOG.debug('Octavia Setup: lb_member_webserver2 = {}'.format(
613 cls.lb_member_webserver2[const.ID]))
614 LOG.debug('Octavia Setup: webserver2_ip = {}'.format(
615 cls.webserver2_ip))
616 LOG.debug('Octavia Setup: webserver2_ipv6 = {}'.format(
617 cls.webserver2_ipv6))
618 LOG.debug('Octavia Setup: webserver2_public_ip = {}'.format(
619 cls.webserver2_public_ip))
620
Michael Johnsonbf916df2018-10-17 10:59:28 -0700621 if CONF.load_balancer.test_with_ipv6:
622 # Enable the IPv6 nic in webserver 1
623 cls._enable_ipv6_nic_webserver(
624 cls.webserver1_public_ip, cls.lb_member_keypair['private_key'],
625 cls.webserver1_ipv6, cls.lb_member_1_subnet_prefix)
626
627 # Enable the IPv6 nic in webserver 2
628 cls._enable_ipv6_nic_webserver(
629 cls.webserver2_public_ip, cls.lb_member_keypair['private_key'],
630 cls.webserver2_ipv6, cls.lb_member_2_subnet_prefix)
631
Adam Harwellcd72b562018-05-07 11:37:22 -0700632 # Set up serving on webserver 1
633 cls._install_start_webserver(cls.webserver1_public_ip,
Adam Harwelle029af22018-05-24 17:13:28 -0700634 cls.lb_member_keypair['private_key'],
635 cls.webserver1_response)
Adam Harwellcd72b562018-05-07 11:37:22 -0700636
637 # Validate webserver 1
Adam Harwelle029af22018-05-24 17:13:28 -0700638 cls._validate_webserver(cls.webserver1_public_ip,
639 cls.webserver1_response)
Adam Harwellcd72b562018-05-07 11:37:22 -0700640
641 # Set up serving on webserver 2
642 cls._install_start_webserver(cls.webserver2_public_ip,
Adam Harwelle029af22018-05-24 17:13:28 -0700643 cls.lb_member_keypair['private_key'],
644 cls.webserver2_response)
Adam Harwellcd72b562018-05-07 11:37:22 -0700645
646 # Validate webserver 2
Adam Harwelle029af22018-05-24 17:13:28 -0700647 cls._validate_webserver(cls.webserver2_public_ip,
648 cls.webserver2_response)
Adam Harwellcd72b562018-05-07 11:37:22 -0700649
650 @classmethod
651 def _create_networks(cls):
652 super(LoadBalancerBaseTestWithCompute, cls)._create_networks()
Jude Cross986e3f52017-07-24 14:57:20 -0700653 # Create a router for the subnets (required for the floating IP)
654 router_name = data_utils.rand_name("lb_member_router")
655 result = cls.lb_mem_routers_client.create_router(
656 name=router_name, admin_state_up=True,
657 external_gateway_info=dict(
658 network_id=CONF.network.public_network_id))
659 cls.lb_member_router = result['router']
660 LOG.info('lb_member_router: {}'.format(cls.lb_member_router))
661 cls.addClassResourceCleanup(
662 waiters.wait_for_not_found,
663 cls.lb_mem_routers_client.delete_router,
664 cls.lb_mem_routers_client.show_router,
665 cls.lb_member_router['id'])
666
667 # Add VIP subnet to router
668 cls.lb_mem_routers_client.add_router_interface(
669 cls.lb_member_router['id'],
670 subnet_id=cls.lb_member_vip_subnet['id'])
671 cls.addClassResourceCleanup(
672 waiters.wait_for_not_found,
673 cls.lb_mem_routers_client.remove_router_interface,
674 cls.lb_mem_routers_client.remove_router_interface,
675 cls.lb_member_router['id'],
676 subnet_id=cls.lb_member_vip_subnet['id'])
677
678 # Add member subnet 1 to router
679 cls.lb_mem_routers_client.add_router_interface(
680 cls.lb_member_router['id'],
681 subnet_id=cls.lb_member_1_subnet['id'])
682 cls.addClassResourceCleanup(
683 waiters.wait_for_not_found,
Jude Cross986e3f52017-07-24 14:57:20 -0700684 cls.lb_mem_routers_client.remove_router_interface,
685 cls.lb_mem_routers_client.remove_router_interface,
686 cls.lb_member_router['id'], subnet_id=cls.lb_member_1_subnet['id'])
687
688 # Add member subnet 2 to router
689 cls.lb_mem_routers_client.add_router_interface(
690 cls.lb_member_router['id'],
691 subnet_id=cls.lb_member_2_subnet['id'])
692 cls.addClassResourceCleanup(
693 waiters.wait_for_not_found,
694 cls.lb_mem_routers_client.remove_router_interface,
695 cls.lb_mem_routers_client.remove_router_interface,
696 cls.lb_member_router['id'], subnet_id=cls.lb_member_2_subnet['id'])
697
698 @classmethod
699 def _create_webserver(cls, name, network):
700 """Creates a webserver with two ports.
701
702 webserver_details dictionary contains:
703 server - The compute server object
704 ipv4_address - The IPv4 address for the server (optional)
705 ipv6_address - The IPv6 address for the server (optional)
706 public_ipv4_address - The publicly accessible IPv4 address for the
707 server, this may be a floating IP (optional)
708
709 :param name: The name of the server to create.
710 :param network: The network to boot the server on.
711 :returns: webserver_details dictionary.
712 """
713 server_kwargs = {
714 'name': data_utils.rand_name(name),
715 'flavorRef': CONF.compute.flavor_ref,
716 'imageRef': CONF.compute.image_ref,
717 'key_name': cls.lb_member_keypair['name']}
718 if (CONF.load_balancer.enable_security_groups and
719 CONF.network_feature_enabled.port_security):
720 server_kwargs['security_groups'] = [
721 {'name': cls.lb_member_sec_group['name']}]
722 if not CONF.load_balancer.disable_boot_network:
723 server_kwargs['networks'] = [{'uuid': network['id']}]
724
725 # Replace the name for clouds that have limitations
726 if CONF.load_balancer.random_server_name_length:
727 r = random.SystemRandom()
728 server_kwargs['name'] = "m{}".format("".join(
729 [r.choice(string.ascii_uppercase + string.digits)
730 for _ in range(
731 CONF.load_balancer.random_server_name_length - 1)]
732 ))
733 if CONF.load_balancer.availability_zone:
734 server_kwargs['availability_zone'] = (
735 CONF.load_balancer.availability_zone)
736
737 server = cls.lb_mem_servers_client.create_server(
738 **server_kwargs)['server']
739 cls.addClassResourceCleanup(
740 waiters.wait_for_not_found,
741 cls.lb_mem_servers_client.delete_server,
742 cls.lb_mem_servers_client.show_server,
743 server['id'])
744 server = waiters.wait_for_status(
745 cls.lb_mem_servers_client.show_server,
746 server['id'], 'status', 'ACTIVE',
747 CONF.load_balancer.build_interval,
748 CONF.load_balancer.build_timeout,
749 root_tag='server')
750 webserver_details = {'server': server}
751 LOG.info('Created server: {}'.format(server))
752
753 addresses = server['addresses']
754 if CONF.load_balancer.disable_boot_network:
755 instance_network = addresses.values()[0]
756 else:
757 instance_network = addresses[network['name']]
758 for addr in instance_network:
759 if addr['version'] == 4:
760 webserver_details['ipv4_address'] = addr['addr']
761 if addr['version'] == 6:
762 webserver_details['ipv6_address'] = addr['addr']
763
764 if CONF.validation.connect_method == 'floating':
765 result = cls.lb_mem_ports_client.list_ports(
766 network_id=network['id'],
767 mac_address=instance_network[0]['OS-EXT-IPS-MAC:mac_addr'])
768 port_id = result['ports'][0]['id']
769 result = cls.lb_mem_float_ip_client.create_floatingip(
770 floating_network_id=CONF.network.public_network_id,
771 port_id=port_id)
772 floating_ip = result['floatingip']
773 LOG.info('webserver1_floating_ip: {}'.format(floating_ip))
774 cls.addClassResourceCleanup(
775 waiters.wait_for_not_found,
776 cls.lb_mem_float_ip_client.delete_floatingip,
777 cls.lb_mem_float_ip_client.show_floatingip,
778 floatingip_id=floating_ip['id'])
779 webserver_details['public_ipv4_address'] = (
780 floating_ip['floating_ip_address'])
781 else:
782 webserver_details['public_ipv4_address'] = (
783 instance_network[0]['addr'])
784
785 return webserver_details
786
787 @classmethod
Adam Harwellcd72b562018-05-07 11:37:22 -0700788 def _install_start_webserver(cls, ip_address, ssh_key, start_id):
789 local_file = pkg_resources.resource_filename(
790 'octavia_tempest_plugin.contrib.httpd', 'httpd.bin')
791 dest_file = '/dev/shm/httpd.bin'
792
793 linux_client = remote_client.RemoteClient(
794 ip_address, CONF.validation.image_ssh_user, pkey=ssh_key)
795 linux_client.validate_authentication()
796
797 with tempfile.NamedTemporaryFile() as key:
798 key.write(ssh_key.encode('utf-8'))
799 key.flush()
800 cmd = ("scp -v -o UserKnownHostsFile=/dev/null "
801 "-o StrictHostKeyChecking=no "
802 "-o ConnectTimeout={0} -o ConnectionAttempts={1} "
803 "-i {2} {3} {4}@{5}:{6}").format(
804 CONF.load_balancer.scp_connection_timeout,
805 CONF.load_balancer.scp_connection_attempts,
806 key.name, local_file, CONF.validation.image_ssh_user,
807 ip_address, dest_file)
808 args = shlex.split(cmd)
809 subprocess_args = {'stdout': subprocess.PIPE,
810 'stderr': subprocess.STDOUT,
811 'cwd': None}
812 proc = subprocess.Popen(args, **subprocess_args)
813 stdout, stderr = proc.communicate()
814 if proc.returncode != 0:
815 raise exceptions.CommandFailed(proc.returncode, cmd,
816 stdout, stderr)
Gregory Thiemongef72a8862019-08-06 17:25:42 +0200817
818 # Enabling memory overcommit allows to run golang static binaries
819 # compiled with a recent golang toolchain (>=1.11). Those binaries
820 # allocate a large amount of virtual memory at init time, and this
821 # allocation fails in tempest's nano flavor (64MB of RAM)
822 # (golang issue reported in https://github.com/golang/go/issues/28114,
823 # follow-up: https://github.com/golang/go/issues/28081)
824 # TODO(gthiemonge): Remove this call when golang issue is resolved.
825 linux_client.exec_command('sudo sh -c "echo 1 > '
826 '/proc/sys/vm/overcommit_memory"')
827
Adam Harwellcd72b562018-05-07 11:37:22 -0700828 linux_client.exec_command('sudo screen -d -m {0} -port 80 '
829 '-id {1}'.format(dest_file, start_id))
830 linux_client.exec_command('sudo screen -d -m {0} -port 81 '
831 '-id {1}'.format(dest_file, start_id + 1))
832
Michael Johnsonbf916df2018-10-17 10:59:28 -0700833 # Cirros does not configure the assigned IPv6 address by default
834 # so enable it manually like tempest does here:
835 # tempest/scenario/test_netowrk_v6.py turn_nic6_on()
836 @classmethod
837 def _enable_ipv6_nic_webserver(cls, ip_address, ssh_key,
838 ipv6_address, ipv6_prefix):
839 linux_client = remote_client.RemoteClient(
840 ip_address, CONF.validation.image_ssh_user, pkey=ssh_key)
841 linux_client.validate_authentication()
842
843 linux_client.exec_command('sudo ip address add {0}/{1} dev '
844 'eth0'.format(ipv6_address, ipv6_prefix))
845
Adam Harwellcd72b562018-05-07 11:37:22 -0700846 @classmethod
Jude Cross986e3f52017-07-24 14:57:20 -0700847 def _validate_webserver(cls, ip_address, start_id):
848 URL = 'http://{0}'.format(ip_address)
849 validators.validate_URL_response(URL, expected_body=str(start_id))
850 URL = 'http://{0}:81'.format(ip_address)
851 validators.validate_URL_response(URL, expected_body=str(start_id + 1))
852
Michael Johnson0a0f9b32019-01-02 16:58:21 -0800853 def _wait_for_lb_functional(self, vip_address,
854 protocol='http', verify=True):
Adam Harwellcd72b562018-05-07 11:37:22 -0700855 session = requests.Session()
856 start = time.time()
857
858 while time.time() - start < CONF.load_balancer.build_timeout:
859 try:
Michael Johnson0a0f9b32019-01-02 16:58:21 -0800860 session.get("{0}://{1}".format(protocol, vip_address),
861 timeout=2, verify=verify)
Adam Harwellcd72b562018-05-07 11:37:22 -0700862 time.sleep(1)
863 return
864 except Exception:
865 LOG.warning('Server is not passing initial traffic. Waiting.')
866 time.sleep(1)
867 LOG.error('Server did not begin passing traffic within the timeout '
868 'period. Failing test.')
869 raise Exception()
870
Michael Johnson0a0f9b32019-01-02 16:58:21 -0800871 def check_members_balanced(self, vip_address, traffic_member_count=2,
Michael Johnson402de7d2019-10-23 18:46:42 -0700872 protocol='http', verify=True, protocol_port=80):
Maciej Józefczyk6a508ce2019-07-26 13:10:50 +0000873 handler = requests
874 if CONF.load_balancer.test_reuse_connection:
875 handler = requests.Session()
Adam Harwellcd72b562018-05-07 11:37:22 -0700876 response_counts = {}
877
Michael Johnson5a16ad32018-10-18 14:49:11 -0700878 if ipaddress.ip_address(vip_address).version == 6:
879 vip_address = '[{}]'.format(vip_address)
880
Michael Johnson0a0f9b32019-01-02 16:58:21 -0800881 self._wait_for_lb_functional(vip_address, protocol, verify)
Adam Harwellcd72b562018-05-07 11:37:22 -0700882
883 # Send a number requests to lb vip
884 for i in range(20):
885 try:
Michael Johnson402de7d2019-10-23 18:46:42 -0700886 r = handler.get('{0}://{1}:{2}'.format(protocol, vip_address,
887 protocol_port),
Michael Johnson0a0f9b32019-01-02 16:58:21 -0800888 timeout=2, verify=verify)
Adam Harwellcd72b562018-05-07 11:37:22 -0700889
890 if r.content in response_counts:
891 response_counts[r.content] += 1
892 else:
893 response_counts[r.content] = 1
894
895 except Exception:
896 LOG.exception('Failed to send request to loadbalancer vip')
897 raise Exception('Failed to connect to lb')
898
899 LOG.debug('Loadbalancer response totals: %s', response_counts)
900 # Ensure the correct number of members
Adam Harwell60ed9d92018-05-10 13:23:13 -0700901 self.assertEqual(traffic_member_count, len(response_counts))
Adam Harwellcd72b562018-05-07 11:37:22 -0700902
903 # Ensure both members got the same number of responses
904 self.assertEqual(1, len(set(response_counts.values())))
Adam Harwelle029af22018-05-24 17:13:28 -0700905
906 def assertConsistentResponse(self, response, url, method='GET', repeat=10,
907 redirect=False, timeout=2, **kwargs):
908 """Assert that a request to URL gets the expected response.
909
910 :param response: Expected response in format (status_code, content).
911 :param url: The URL to request.
912 :param method: The HTTP method to use (GET, POST, PUT, etc)
913 :param repeat: How many times to test the response.
914 :param data: Optional data to send in the request.
915 :param headers: Optional headers to send in the request.
916 :param cookies: Optional cookies to send in the request.
917 :param redirect: Is the request a redirect? If true, assume the passed
918 content should be the next URL in the chain.
Carlos Goncalvesc451ff92018-12-19 19:11:53 +0100919 :param timeout: Optional seconds to wait for the server to send data.
920
Adam Harwelle029af22018-05-24 17:13:28 -0700921 :return: boolean success status
922
923 :raises: testtools.matchers.MismatchError
924 """
925 session = requests.Session()
926 response_code, response_content = response
927
928 for i in range(0, repeat):
929 req = session.request(method, url, allow_redirects=not redirect,
930 timeout=timeout, **kwargs)
931 if response_code:
932 self.assertEqual(response_code, req.status_code)
933 if redirect:
934 self.assertTrue(req.is_redirect)
Carlos Goncalvesc451ff92018-12-19 19:11:53 +0100935 self.assertEqual(response_content,
936 session.get_redirect_target(req))
Adam Harwelle029af22018-05-24 17:13:28 -0700937 elif response_content:
938 self.assertEqual(six.text_type(response_content), req.text)