blob: cf068799194b0646a33ce1e4866c7cf3cc719477 [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
33
34from octavia_tempest_plugin import clients
35from octavia_tempest_plugin.common import constants as const
36from octavia_tempest_plugin.tests import validators
37from octavia_tempest_plugin.tests import waiters
38
39CONF = config.CONF
40LOG = logging.getLogger(__name__)
41
42
43class LoadBalancerBaseTest(test.BaseTestCase):
44 """Base class for load balancer tests."""
45
46 # Setup cls.os_roles_lb_member. cls.os_primary, cls.os_roles_lb_member,
47 # and cls.os_roles_lb_admin credentials.
48 credentials = ['admin', 'primary',
49 ['lb_member', CONF.load_balancer.member_role],
50 ['lb_member2', CONF.load_balancer.member_role],
51 ['lb_admin', CONF.load_balancer.admin_role]]
52
53 client_manager = clients.ManagerV2
Adam Harwelle029af22018-05-24 17:13:28 -070054 webserver1_response = 1
55 webserver2_response = 5
Jude Cross986e3f52017-07-24 14:57:20 -070056
57 @classmethod
58 def skip_checks(cls):
59 """Check if we should skip all of the children tests."""
60 super(LoadBalancerBaseTest, cls).skip_checks()
61
62 service_list = {
63 'load_balancer': CONF.service_available.load_balancer,
64 }
65
66 live_service_list = {
67 'compute': CONF.service_available.nova,
68 'image': CONF.service_available.glance,
69 'neutron': CONF.service_available.neutron
70 }
71
72 if not CONF.load_balancer.test_with_noop:
73 service_list.update(live_service_list)
74
75 for service, available in service_list.items():
76 if not available:
77 skip_msg = ("{0} skipped as {1} serivce is not "
78 "available.".format(cls.__name__, service))
79 raise cls.skipException(skip_msg)
80
81 # We must be able to reach our VIP and instances
82 if not (CONF.network.project_networks_reachable
83 or CONF.network.public_network_id):
84 msg = ('Either project_networks_reachable must be "true", or '
85 'public_network_id must be defined.')
86 raise cls.skipException(msg)
87
88 @classmethod
89 def setup_credentials(cls):
90 """Setup test credentials and network resources."""
91 # Do not auto create network resources
92 cls.set_network_resources()
93 super(LoadBalancerBaseTest, cls).setup_credentials()
94
95 @classmethod
96 def setup_clients(cls):
97 """Setup client aliases."""
98 super(LoadBalancerBaseTest, cls).setup_clients()
99 cls.lb_mem_float_ip_client = cls.os_roles_lb_member.floating_ips_client
100 cls.lb_mem_keypairs_client = cls.os_roles_lb_member.keypairs_client
101 cls.lb_mem_net_client = cls.os_roles_lb_member.networks_client
102 cls.lb_mem_ports_client = cls.os_roles_lb_member.ports_client
103 cls.lb_mem_routers_client = cls.os_roles_lb_member.routers_client
104 cls.lb_mem_SG_client = cls.os_roles_lb_member.security_groups_client
105 cls.lb_mem_SGr_client = (
106 cls.os_roles_lb_member.security_group_rules_client)
107 cls.lb_mem_servers_client = cls.os_roles_lb_member.servers_client
108 cls.lb_mem_subnet_client = cls.os_roles_lb_member.subnets_client
109 cls.mem_lb_client = cls.os_roles_lb_member.loadbalancer_client
Jude Crossfbbd2b42017-08-09 15:21:04 -0700110 cls.mem_listener_client = cls.os_roles_lb_member.listener_client
Adam Harwell8ffce3e2018-05-01 21:18:44 -0700111 cls.mem_pool_client = cls.os_roles_lb_member.pool_client
Adam Harwellde3e0542018-05-03 18:21:06 -0700112 cls.mem_member_client = cls.os_roles_lb_member.member_client
Adam Harwell60ed9d92018-05-10 13:23:13 -0700113 cls.mem_healthmonitor_client = (
114 cls.os_roles_lb_member.healthmonitor_client)
Adam Harwell446f8be2018-05-24 16:51:03 -0700115 cls.mem_l7policy_client = cls.os_roles_lb_member.l7policy_client
Adam Harwelle029af22018-05-24 17:13:28 -0700116 cls.mem_l7rule_client = cls.os_roles_lb_member.l7rule_client
Jude Cross986e3f52017-07-24 14:57:20 -0700117
118 @classmethod
119 def resource_setup(cls):
120 """Setup resources needed by the tests."""
121 super(LoadBalancerBaseTest, cls).resource_setup()
122
123 conf_lb = CONF.load_balancer
124
125 if conf_lb.test_subnet_override and not conf_lb.test_network_override:
126 raise exceptions.InvalidConfiguration(
127 "Configuration value test_network_override must be "
128 "specified if test_subnet_override is used.")
129
130 show_subnet = cls.lb_mem_subnet_client.show_subnet
131 if CONF.load_balancer.test_with_noop:
132 cls.lb_member_vip_net = {'id': uuidutils.generate_uuid()}
133 cls.lb_member_vip_subnet = {'id': uuidutils.generate_uuid()}
134 cls.lb_member_1_net = {'id': uuidutils.generate_uuid()}
135 cls.lb_member_1_subnet = {'id': uuidutils.generate_uuid()}
136 cls.lb_member_2_net = {'id': uuidutils.generate_uuid()}
137 cls.lb_member_2_subnet = {'id': uuidutils.generate_uuid()}
138 if CONF.load_balancer.test_with_ipv6:
139 cls.lb_member_vip_ipv6_subnet = {'id':
140 uuidutils.generate_uuid()}
141 cls.lb_member_1_ipv6_subnet = {'id': uuidutils.generate_uuid()}
142 cls.lb_member_2_ipv6_subnet = {'id': uuidutils.generate_uuid()}
143 return
144 elif CONF.load_balancer.test_network_override:
145 if conf_lb.test_subnet_override:
146 override_subnet = show_subnet(conf_lb.test_subnet_override)
147 else:
148 override_subnet = None
149
150 show_net = cls.lb_mem_net_client.show_network
151 override_network = show_net(conf_lb.test_network_override)
152 override_network = override_network.get('network')
153
154 cls.lb_member_vip_net = override_network
155 cls.lb_member_vip_subnet = override_subnet
156 cls.lb_member_1_net = override_network
157 cls.lb_member_1_subnet = override_subnet
158 cls.lb_member_2_net = override_network
159 cls.lb_member_2_subnet = override_subnet
160
161 if (CONF.load_balancer.test_with_ipv6 and
162 conf_lb.test_IPv6_subnet_override):
163 override_ipv6_subnet = show_subnet(
164 conf_lb.test_IPv6_subnet_override)
165 cls.lb_member_vip_ipv6_subnet = override_ipv6_subnet
166 cls.lb_member_1_ipv6_subnet = override_ipv6_subnet
167 cls.lb_member_2_ipv6_subnet = override_ipv6_subnet
168 else:
169 cls.lb_member_vip_ipv6_subnet = None
170 cls.lb_member_1_ipv6_subnet = None
171 cls.lb_member_2_ipv6_subnet = None
172 else:
173 cls._create_networks()
174
175 LOG.debug('Octavia Setup: lb_member_vip_net = {}'.format(
176 cls.lb_member_vip_net[const.ID]))
177 if cls.lb_member_vip_subnet:
178 LOG.debug('Octavia Setup: lb_member_vip_subnet = {}'.format(
179 cls.lb_member_vip_subnet[const.ID]))
180 LOG.debug('Octavia Setup: lb_member_1_net = {}'.format(
181 cls.lb_member_1_net[const.ID]))
182 if cls.lb_member_1_subnet:
183 LOG.debug('Octavia Setup: lb_member_1_subnet = {}'.format(
184 cls.lb_member_1_subnet[const.ID]))
185 LOG.debug('Octavia Setup: lb_member_2_net = {}'.format(
186 cls.lb_member_2_net[const.ID]))
187 if cls.lb_member_2_subnet:
188 LOG.debug('Octavia Setup: lb_member_2_subnet = {}'.format(
189 cls.lb_member_2_subnet[const.ID]))
190 if cls.lb_member_vip_ipv6_subnet:
191 LOG.debug('Octavia Setup: lb_member_vip_ipv6_subnet = {}'.format(
192 cls.lb_member_vip_ipv6_subnet[const.ID]))
193 if cls.lb_member_1_ipv6_subnet:
194 LOG.debug('Octavia Setup: lb_member_1_ipv6_subnet = {}'.format(
195 cls.lb_member_1_ipv6_subnet[const.ID]))
196 if cls.lb_member_2_ipv6_subnet:
197 LOG.debug('Octavia Setup: lb_member_2_ipv6_subnet = {}'.format(
198 cls.lb_member_2_ipv6_subnet[const.ID]))
199
Jude Cross986e3f52017-07-24 14:57:20 -0700200 @classmethod
201 def _create_networks(cls):
202 """Creates networks, subnets, and routers used in tests.
203
204 The following are expected to be defined and available to the tests:
205 cls.lb_member_vip_net
206 cls.lb_member_vip_subnet
207 cls.lb_member_vip_ipv6_subnet (optional)
208 cls.lb_member_1_net
209 cls.lb_member_1_subnet
210 cls.lb_member_1_ipv6_subnet (optional)
211 cls.lb_member_2_net
212 cls.lb_member_2_subnet
213 cls.lb_member_2_ipv6_subnet (optional)
214 """
215
216 # Create tenant VIP network
217 network_kwargs = {
218 'name': data_utils.rand_name("lb_member_vip_network")}
219 if CONF.network_feature_enabled.port_security:
220 # Note: Allowed Address Pairs requires port security
221 network_kwargs['port_security_enabled'] = True
222 result = cls.lb_mem_net_client.create_network(**network_kwargs)
223 cls.lb_member_vip_net = result['network']
224 LOG.info('lb_member_vip_net: {}'.format(cls.lb_member_vip_net))
225 cls.addClassResourceCleanup(
226 waiters.wait_for_not_found,
227 cls.lb_mem_net_client.delete_network,
228 cls.lb_mem_net_client.show_network,
229 cls.lb_member_vip_net['id'])
230
231 # Create tenant VIP subnet
232 subnet_kwargs = {
233 'name': data_utils.rand_name("lb_member_vip_subnet"),
234 'network_id': cls.lb_member_vip_net['id'],
235 'cidr': CONF.load_balancer.vip_subnet_cidr,
236 'ip_version': 4}
237 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
238 cls.lb_member_vip_subnet = result['subnet']
239 LOG.info('lb_member_vip_subnet: {}'.format(cls.lb_member_vip_subnet))
240 cls.addClassResourceCleanup(
241 waiters.wait_for_not_found,
242 cls.lb_mem_subnet_client.delete_subnet,
243 cls.lb_mem_subnet_client.show_subnet,
244 cls.lb_member_vip_subnet['id'])
245
246 # Create tenant VIP IPv6 subnet
247 if CONF.load_balancer.test_with_ipv6:
248 subnet_kwargs = {
249 'name': data_utils.rand_name("lb_member_vip_ipv6_subnet"),
250 'network_id': cls.lb_member_vip_net['id'],
251 'cidr': CONF.load_balancer.vip_ipv6_subnet_cidr,
252 'ip_version': 6}
253 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
254 cls.lb_member_vip_ipv6_subnet = result['subnet']
255 LOG.info('lb_member_vip_ipv6_subnet: {}'.format(
256 cls.lb_member_vip_ipv6_subnet))
257 cls.addClassResourceCleanup(
258 waiters.wait_for_not_found,
259 cls.lb_mem_subnet_client.delete_subnet,
260 cls.lb_mem_subnet_client.show_subnet,
261 cls.lb_member_vip_ipv6_subnet['id'])
262
263 # Create tenant member 1 network
264 network_kwargs = {
265 'name': data_utils.rand_name("lb_member_1_network")}
266 if CONF.network_feature_enabled.port_security:
267 if CONF.load_balancer.enable_security_groups:
268 network_kwargs['port_security_enabled'] = True
269 else:
270 network_kwargs['port_security_enabled'] = False
271 result = cls.lb_mem_net_client.create_network(**network_kwargs)
272 cls.lb_member_1_net = result['network']
273 LOG.info('lb_member_1_net: {}'.format(cls.lb_member_1_net))
274 cls.addClassResourceCleanup(
275 waiters.wait_for_not_found,
276 cls.lb_mem_net_client.delete_network,
277 cls.lb_mem_net_client.show_network,
278 cls.lb_member_1_net['id'])
279
280 # Create tenant member 1 subnet
281 subnet_kwargs = {
282 'name': data_utils.rand_name("lb_member_1_subnet"),
283 'network_id': cls.lb_member_1_net['id'],
284 'cidr': CONF.load_balancer.member_1_ipv4_subnet_cidr,
285 'ip_version': 4}
286 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
287 cls.lb_member_1_subnet = result['subnet']
288 LOG.info('lb_member_1_subnet: {}'.format(cls.lb_member_1_subnet))
289 cls.addClassResourceCleanup(
290 waiters.wait_for_not_found,
291 cls.lb_mem_subnet_client.delete_subnet,
292 cls.lb_mem_subnet_client.show_subnet,
293 cls.lb_member_1_subnet['id'])
294
295 # Create tenant member 1 ipv6 subnet
296 if CONF.load_balancer.test_with_ipv6:
297 subnet_kwargs = {
298 'name': data_utils.rand_name("lb_member_1_ipv6_subnet"),
299 'network_id': cls.lb_member_1_net['id'],
300 'cidr': CONF.load_balancer.member_1_ipv6_subnet_cidr,
301 'ip_version': 6}
302 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
303 cls.lb_member_1_ipv6_subnet = result['subnet']
304 LOG.info('lb_member_1_ipv6_subnet: {}'.format(
305 cls.lb_member_1_ipv6_subnet))
306 cls.addClassResourceCleanup(
307 waiters.wait_for_not_found,
308 cls.lb_mem_subnet_client.delete_subnet,
309 cls.lb_mem_subnet_client.show_subnet,
310 cls.lb_member_1_ipv6_subnet['id'])
311
312 # Create tenant member 2 network
313 network_kwargs = {
314 'name': data_utils.rand_name("lb_member_2_network")}
315 if CONF.network_feature_enabled.port_security:
316 if CONF.load_balancer.enable_security_groups:
317 network_kwargs['port_security_enabled'] = True
318 else:
319 network_kwargs['port_security_enabled'] = False
320 result = cls.lb_mem_net_client.create_network(**network_kwargs)
321 cls.lb_member_2_net = result['network']
322 LOG.info('lb_member_2_net: {}'.format(cls.lb_member_2_net))
323 cls.addClassResourceCleanup(
324 waiters.wait_for_not_found,
325 cls.lb_mem_net_client.delete_network,
326 cls.lb_mem_net_client.show_network,
327 cls.lb_member_2_net['id'])
328
329 # Create tenant member 2 subnet
330 subnet_kwargs = {
331 'name': data_utils.rand_name("lb_member_2_subnet"),
332 'network_id': cls.lb_member_2_net['id'],
333 'cidr': CONF.load_balancer.member_2_ipv4_subnet_cidr,
334 'ip_version': 4}
335 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
336 cls.lb_member_2_subnet = result['subnet']
337 LOG.info('lb_member_2_subnet: {}'.format(cls.lb_member_2_subnet))
338 cls.addClassResourceCleanup(
339 waiters.wait_for_not_found,
340 cls.lb_mem_subnet_client.delete_subnet,
341 cls.lb_mem_subnet_client.show_subnet,
342 cls.lb_member_2_subnet['id'])
343
344 # Create tenant member 2 ipv6 subnet
345 if CONF.load_balancer.test_with_ipv6:
346 subnet_kwargs = {
347 'name': data_utils.rand_name("lb_member_2_ipv6_subnet"),
348 'network_id': cls.lb_member_2_net['id'],
349 'cidr': CONF.load_balancer.member_2_ipv6_subnet_cidr,
350 'ip_version': 6}
351 result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs)
352 cls.lb_member_2_ipv6_subnet = result['subnet']
353 LOG.info('lb_member_2_ipv6_subnet: {}'.format(
354 cls.lb_member_2_ipv6_subnet))
355 cls.addClassResourceCleanup(
356 waiters.wait_for_not_found,
357 cls.lb_mem_subnet_client.delete_subnet,
358 cls.lb_mem_subnet_client.show_subnet,
359 cls.lb_member_2_ipv6_subnet['id'])
360
Adam Harwellcd72b562018-05-07 11:37:22 -0700361 @classmethod
Adam Harwell60ed9d92018-05-10 13:23:13 -0700362 def _setup_lb_network_kwargs(cls, lb_kwargs, ip_version=None):
363 if not ip_version:
364 ip_version = 6 if CONF.load_balancer.test_with_ipv6 else 4
Adam Harwellcd72b562018-05-07 11:37:22 -0700365 if cls.lb_member_vip_subnet:
366 ip_index = data_utils.rand_int_id(start=10, end=100)
367 if ip_version == 4:
368 network = ipaddress.IPv4Network(
369 six.u(CONF.load_balancer.vip_subnet_cidr))
370 lb_vip_address = str(network[ip_index])
371 subnet_id = cls.lb_member_vip_subnet[const.ID]
372 else:
373 network = ipaddress.IPv6Network(
374 six.u(CONF.load_balancer.vip_ipv6_subnet_cidr))
375 lb_vip_address = str(network[ip_index])
376 subnet_id = cls.lb_member_vip_ipv6_subnet[const.ID]
377 lb_kwargs[const.VIP_SUBNET_ID] = subnet_id
378 lb_kwargs[const.VIP_ADDRESS] = lb_vip_address
379 if CONF.load_balancer.test_with_noop:
380 lb_kwargs[const.VIP_NETWORK_ID] = (
381 cls.lb_member_vip_net[const.ID])
382 else:
383 lb_kwargs[const.VIP_NETWORK_ID] = cls.lb_member_vip_net[const.ID]
384 lb_kwargs[const.VIP_SUBNET_ID] = None
385
386
387class LoadBalancerBaseTestWithCompute(LoadBalancerBaseTest):
388 @classmethod
389 def resource_setup(cls):
390 super(LoadBalancerBaseTestWithCompute, cls).resource_setup()
391 # If validation is disabled in this cloud, we won't be able to
392 # start the webservers, so don't even boot them.
393 if not CONF.validation.run_validation:
394 return
395
396 # Create a keypair for the webservers
397 keypair_name = data_utils.rand_name('lb_member_keypair')
398 result = cls.lb_mem_keypairs_client.create_keypair(
399 name=keypair_name)
400 cls.lb_member_keypair = result['keypair']
401 LOG.info('lb_member_keypair: {}'.format(cls.lb_member_keypair))
402 cls.addClassResourceCleanup(
403 waiters.wait_for_not_found,
404 cls.lb_mem_keypairs_client.delete_keypair,
405 cls.lb_mem_keypairs_client.show_keypair,
406 keypair_name)
407
408 if (CONF.load_balancer.enable_security_groups and
409 CONF.network_feature_enabled.port_security):
410 # Set up the security group for the webservers
411 SG_name = data_utils.rand_name('lb_member_SG')
412 cls.lb_member_sec_group = (
413 cls.lb_mem_SG_client.create_security_group(
414 name=SG_name)['security_group'])
415 cls.addClassResourceCleanup(
416 waiters.wait_for_not_found,
417 cls.lb_mem_SG_client.delete_security_group,
418 cls.lb_mem_SG_client.show_security_group,
419 cls.lb_member_sec_group['id'])
420
421 # Create a security group rule to allow 80-81 (test webservers)
422 SGr = cls.lb_mem_SGr_client.create_security_group_rule(
423 direction='ingress',
424 security_group_id=cls.lb_member_sec_group['id'],
425 protocol='tcp',
426 ethertype='IPv4',
427 port_range_min=80,
428 port_range_max=81)['security_group_rule']
429 cls.addClassResourceCleanup(
430 waiters.wait_for_not_found,
431 cls.lb_mem_SGr_client.delete_security_group_rule,
432 cls.lb_mem_SGr_client.show_security_group_rule,
433 SGr['id'])
434 # Create a security group rule to allow 22 (ssh)
435 SGr = cls.lb_mem_SGr_client.create_security_group_rule(
436 direction='ingress',
437 security_group_id=cls.lb_member_sec_group['id'],
438 protocol='tcp',
439 ethertype='IPv4',
440 port_range_min=22,
441 port_range_max=22)['security_group_rule']
442 cls.addClassResourceCleanup(
443 waiters.wait_for_not_found,
444 cls.lb_mem_SGr_client.delete_security_group_rule,
445 cls.lb_mem_SGr_client.show_security_group_rule,
446 SGr['id'])
447 if CONF.load_balancer.test_with_ipv6:
448 # Create a security group rule to allow 80-81 (test webservers)
449 SGr = cls.lb_mem_SGr_client.create_security_group_rule(
450 direction='ingress',
451 security_group_id=cls.lb_member_sec_group['id'],
452 protocol='tcp',
453 ethertype='IPv6',
454 port_range_min=80,
455 port_range_max=81)['security_group_rule']
456 cls.addClassResourceCleanup(
457 waiters.wait_for_not_found,
458 cls.lb_mem_SGr_client.delete_security_group_rule,
459 cls.lb_mem_SGr_client.show_security_group_rule,
460 SGr['id'])
461 # Create a security group rule to allow 22 (ssh)
462 SGr = cls.lb_mem_SGr_client.create_security_group_rule(
463 direction='ingress',
464 security_group_id=cls.lb_member_sec_group['id'],
465 protocol='tcp',
466 ethertype='IPv6',
467 port_range_min=22,
468 port_range_max=22)['security_group_rule']
469 cls.addClassResourceCleanup(
470 waiters.wait_for_not_found,
471 cls.lb_mem_SGr_client.delete_security_group_rule,
472 cls.lb_mem_SGr_client.show_security_group_rule,
473 SGr['id'])
474
475 LOG.info('lb_member_sec_group: {}'.format(cls.lb_member_sec_group))
476
477 # Create webserver 1 instance
478 server_details = cls._create_webserver('lb_member_webserver1',
479 cls.lb_member_1_net)
480
481 cls.lb_member_webserver1 = server_details['server']
482 cls.webserver1_ip = server_details.get('ipv4_address')
483 cls.webserver1_ipv6 = server_details.get('ipv6_address')
484 cls.webserver1_public_ip = server_details['public_ipv4_address']
485
486 LOG.debug('Octavia Setup: lb_member_webserver1 = {}'.format(
487 cls.lb_member_webserver1[const.ID]))
488 LOG.debug('Octavia Setup: webserver1_ip = {}'.format(
489 cls.webserver1_ip))
490 LOG.debug('Octavia Setup: webserver1_ipv6 = {}'.format(
491 cls.webserver1_ipv6))
492 LOG.debug('Octavia Setup: webserver1_public_ip = {}'.format(
493 cls.webserver1_public_ip))
494
495 # Create webserver 2 instance
496 server_details = cls._create_webserver('lb_member_webserver2',
497 cls.lb_member_2_net)
498
499 cls.lb_member_webserver2 = server_details['server']
500 cls.webserver2_ip = server_details.get('ipv4_address')
501 cls.webserver2_ipv6 = server_details.get('ipv6_address')
502 cls.webserver2_public_ip = server_details['public_ipv4_address']
503
504 LOG.debug('Octavia Setup: lb_member_webserver2 = {}'.format(
505 cls.lb_member_webserver2[const.ID]))
506 LOG.debug('Octavia Setup: webserver2_ip = {}'.format(
507 cls.webserver2_ip))
508 LOG.debug('Octavia Setup: webserver2_ipv6 = {}'.format(
509 cls.webserver2_ipv6))
510 LOG.debug('Octavia Setup: webserver2_public_ip = {}'.format(
511 cls.webserver2_public_ip))
512
513 # Set up serving on webserver 1
514 cls._install_start_webserver(cls.webserver1_public_ip,
Adam Harwelle029af22018-05-24 17:13:28 -0700515 cls.lb_member_keypair['private_key'],
516 cls.webserver1_response)
Adam Harwellcd72b562018-05-07 11:37:22 -0700517
518 # Validate webserver 1
Adam Harwelle029af22018-05-24 17:13:28 -0700519 cls._validate_webserver(cls.webserver1_public_ip,
520 cls.webserver1_response)
Adam Harwellcd72b562018-05-07 11:37:22 -0700521
522 # Set up serving on webserver 2
523 cls._install_start_webserver(cls.webserver2_public_ip,
Adam Harwelle029af22018-05-24 17:13:28 -0700524 cls.lb_member_keypair['private_key'],
525 cls.webserver2_response)
Adam Harwellcd72b562018-05-07 11:37:22 -0700526
527 # Validate webserver 2
Adam Harwelle029af22018-05-24 17:13:28 -0700528 cls._validate_webserver(cls.webserver2_public_ip,
529 cls.webserver2_response)
Adam Harwellcd72b562018-05-07 11:37:22 -0700530
531 @classmethod
532 def _create_networks(cls):
533 super(LoadBalancerBaseTestWithCompute, cls)._create_networks()
Jude Cross986e3f52017-07-24 14:57:20 -0700534 # Create a router for the subnets (required for the floating IP)
535 router_name = data_utils.rand_name("lb_member_router")
536 result = cls.lb_mem_routers_client.create_router(
537 name=router_name, admin_state_up=True,
538 external_gateway_info=dict(
539 network_id=CONF.network.public_network_id))
540 cls.lb_member_router = result['router']
541 LOG.info('lb_member_router: {}'.format(cls.lb_member_router))
542 cls.addClassResourceCleanup(
543 waiters.wait_for_not_found,
544 cls.lb_mem_routers_client.delete_router,
545 cls.lb_mem_routers_client.show_router,
546 cls.lb_member_router['id'])
547
548 # Add VIP subnet to router
549 cls.lb_mem_routers_client.add_router_interface(
550 cls.lb_member_router['id'],
551 subnet_id=cls.lb_member_vip_subnet['id'])
552 cls.addClassResourceCleanup(
553 waiters.wait_for_not_found,
554 cls.lb_mem_routers_client.remove_router_interface,
555 cls.lb_mem_routers_client.remove_router_interface,
556 cls.lb_member_router['id'],
557 subnet_id=cls.lb_member_vip_subnet['id'])
558
559 # Add member subnet 1 to router
560 cls.lb_mem_routers_client.add_router_interface(
561 cls.lb_member_router['id'],
562 subnet_id=cls.lb_member_1_subnet['id'])
563 cls.addClassResourceCleanup(
564 waiters.wait_for_not_found,
Jude Cross986e3f52017-07-24 14:57:20 -0700565 cls.lb_mem_routers_client.remove_router_interface,
566 cls.lb_mem_routers_client.remove_router_interface,
567 cls.lb_member_router['id'], subnet_id=cls.lb_member_1_subnet['id'])
568
569 # Add member subnet 2 to router
570 cls.lb_mem_routers_client.add_router_interface(
571 cls.lb_member_router['id'],
572 subnet_id=cls.lb_member_2_subnet['id'])
573 cls.addClassResourceCleanup(
574 waiters.wait_for_not_found,
575 cls.lb_mem_routers_client.remove_router_interface,
576 cls.lb_mem_routers_client.remove_router_interface,
577 cls.lb_member_router['id'], subnet_id=cls.lb_member_2_subnet['id'])
578
579 @classmethod
580 def _create_webserver(cls, name, network):
581 """Creates a webserver with two ports.
582
583 webserver_details dictionary contains:
584 server - The compute server object
585 ipv4_address - The IPv4 address for the server (optional)
586 ipv6_address - The IPv6 address for the server (optional)
587 public_ipv4_address - The publicly accessible IPv4 address for the
588 server, this may be a floating IP (optional)
589
590 :param name: The name of the server to create.
591 :param network: The network to boot the server on.
592 :returns: webserver_details dictionary.
593 """
594 server_kwargs = {
595 'name': data_utils.rand_name(name),
596 'flavorRef': CONF.compute.flavor_ref,
597 'imageRef': CONF.compute.image_ref,
598 'key_name': cls.lb_member_keypair['name']}
599 if (CONF.load_balancer.enable_security_groups and
600 CONF.network_feature_enabled.port_security):
601 server_kwargs['security_groups'] = [
602 {'name': cls.lb_member_sec_group['name']}]
603 if not CONF.load_balancer.disable_boot_network:
604 server_kwargs['networks'] = [{'uuid': network['id']}]
605
606 # Replace the name for clouds that have limitations
607 if CONF.load_balancer.random_server_name_length:
608 r = random.SystemRandom()
609 server_kwargs['name'] = "m{}".format("".join(
610 [r.choice(string.ascii_uppercase + string.digits)
611 for _ in range(
612 CONF.load_balancer.random_server_name_length - 1)]
613 ))
614 if CONF.load_balancer.availability_zone:
615 server_kwargs['availability_zone'] = (
616 CONF.load_balancer.availability_zone)
617
618 server = cls.lb_mem_servers_client.create_server(
619 **server_kwargs)['server']
620 cls.addClassResourceCleanup(
621 waiters.wait_for_not_found,
622 cls.lb_mem_servers_client.delete_server,
623 cls.lb_mem_servers_client.show_server,
624 server['id'])
625 server = waiters.wait_for_status(
626 cls.lb_mem_servers_client.show_server,
627 server['id'], 'status', 'ACTIVE',
628 CONF.load_balancer.build_interval,
629 CONF.load_balancer.build_timeout,
630 root_tag='server')
631 webserver_details = {'server': server}
632 LOG.info('Created server: {}'.format(server))
633
634 addresses = server['addresses']
635 if CONF.load_balancer.disable_boot_network:
636 instance_network = addresses.values()[0]
637 else:
638 instance_network = addresses[network['name']]
639 for addr in instance_network:
640 if addr['version'] == 4:
641 webserver_details['ipv4_address'] = addr['addr']
642 if addr['version'] == 6:
643 webserver_details['ipv6_address'] = addr['addr']
644
645 if CONF.validation.connect_method == 'floating':
646 result = cls.lb_mem_ports_client.list_ports(
647 network_id=network['id'],
648 mac_address=instance_network[0]['OS-EXT-IPS-MAC:mac_addr'])
649 port_id = result['ports'][0]['id']
650 result = cls.lb_mem_float_ip_client.create_floatingip(
651 floating_network_id=CONF.network.public_network_id,
652 port_id=port_id)
653 floating_ip = result['floatingip']
654 LOG.info('webserver1_floating_ip: {}'.format(floating_ip))
655 cls.addClassResourceCleanup(
656 waiters.wait_for_not_found,
657 cls.lb_mem_float_ip_client.delete_floatingip,
658 cls.lb_mem_float_ip_client.show_floatingip,
659 floatingip_id=floating_ip['id'])
660 webserver_details['public_ipv4_address'] = (
661 floating_ip['floating_ip_address'])
662 else:
663 webserver_details['public_ipv4_address'] = (
664 instance_network[0]['addr'])
665
666 return webserver_details
667
668 @classmethod
Adam Harwellcd72b562018-05-07 11:37:22 -0700669 def _install_start_webserver(cls, ip_address, ssh_key, start_id):
670 local_file = pkg_resources.resource_filename(
671 'octavia_tempest_plugin.contrib.httpd', 'httpd.bin')
672 dest_file = '/dev/shm/httpd.bin'
673
674 linux_client = remote_client.RemoteClient(
675 ip_address, CONF.validation.image_ssh_user, pkey=ssh_key)
676 linux_client.validate_authentication()
677
678 with tempfile.NamedTemporaryFile() as key:
679 key.write(ssh_key.encode('utf-8'))
680 key.flush()
681 cmd = ("scp -v -o UserKnownHostsFile=/dev/null "
682 "-o StrictHostKeyChecking=no "
683 "-o ConnectTimeout={0} -o ConnectionAttempts={1} "
684 "-i {2} {3} {4}@{5}:{6}").format(
685 CONF.load_balancer.scp_connection_timeout,
686 CONF.load_balancer.scp_connection_attempts,
687 key.name, local_file, CONF.validation.image_ssh_user,
688 ip_address, dest_file)
689 args = shlex.split(cmd)
690 subprocess_args = {'stdout': subprocess.PIPE,
691 'stderr': subprocess.STDOUT,
692 'cwd': None}
693 proc = subprocess.Popen(args, **subprocess_args)
694 stdout, stderr = proc.communicate()
695 if proc.returncode != 0:
696 raise exceptions.CommandFailed(proc.returncode, cmd,
697 stdout, stderr)
698 linux_client.exec_command('sudo screen -d -m {0} -port 80 '
699 '-id {1}'.format(dest_file, start_id))
700 linux_client.exec_command('sudo screen -d -m {0} -port 81 '
701 '-id {1}'.format(dest_file, start_id + 1))
702
703 @classmethod
Jude Cross986e3f52017-07-24 14:57:20 -0700704 def _validate_webserver(cls, ip_address, start_id):
705 URL = 'http://{0}'.format(ip_address)
706 validators.validate_URL_response(URL, expected_body=str(start_id))
707 URL = 'http://{0}:81'.format(ip_address)
708 validators.validate_URL_response(URL, expected_body=str(start_id + 1))
709
Adam Harwellcd72b562018-05-07 11:37:22 -0700710 def _wait_for_lb_functional(self, vip_address):
711 session = requests.Session()
712 start = time.time()
713
714 while time.time() - start < CONF.load_balancer.build_timeout:
715 try:
716 session.get("http://{0}".format(vip_address), timeout=2)
717 time.sleep(1)
718 return
719 except Exception:
720 LOG.warning('Server is not passing initial traffic. Waiting.')
721 time.sleep(1)
722 LOG.error('Server did not begin passing traffic within the timeout '
723 'period. Failing test.')
724 raise Exception()
725
Adam Harwelle029af22018-05-24 17:13:28 -0700726 def check_members_balanced(self, vip_address, traffic_member_count=2):
Adam Harwellcd72b562018-05-07 11:37:22 -0700727 session = requests.Session()
728 response_counts = {}
729
730 self._wait_for_lb_functional(vip_address)
731
732 # Send a number requests to lb vip
733 for i in range(20):
734 try:
735 r = session.get('http://{0}'.format(vip_address),
736 timeout=2)
737
738 if r.content in response_counts:
739 response_counts[r.content] += 1
740 else:
741 response_counts[r.content] = 1
742
743 except Exception:
744 LOG.exception('Failed to send request to loadbalancer vip')
745 raise Exception('Failed to connect to lb')
746
747 LOG.debug('Loadbalancer response totals: %s', response_counts)
748 # Ensure the correct number of members
Adam Harwell60ed9d92018-05-10 13:23:13 -0700749 self.assertEqual(traffic_member_count, len(response_counts))
Adam Harwellcd72b562018-05-07 11:37:22 -0700750
751 # Ensure both members got the same number of responses
752 self.assertEqual(1, len(set(response_counts.values())))
Adam Harwelle029af22018-05-24 17:13:28 -0700753
754 def assertConsistentResponse(self, response, url, method='GET', repeat=10,
755 redirect=False, timeout=2, **kwargs):
756 """Assert that a request to URL gets the expected response.
757
758 :param response: Expected response in format (status_code, content).
759 :param url: The URL to request.
760 :param method: The HTTP method to use (GET, POST, PUT, etc)
761 :param repeat: How many times to test the response.
762 :param data: Optional data to send in the request.
763 :param headers: Optional headers to send in the request.
764 :param cookies: Optional cookies to send in the request.
765 :param redirect: Is the request a redirect? If true, assume the passed
766 content should be the next URL in the chain.
767 :return: boolean success status
768
769 :raises: testtools.matchers.MismatchError
770 """
771 session = requests.Session()
772 response_code, response_content = response
773
774 for i in range(0, repeat):
775 req = session.request(method, url, allow_redirects=not redirect,
776 timeout=timeout, **kwargs)
777 if response_code:
778 self.assertEqual(response_code, req.status_code)
779 if redirect:
780 self.assertTrue(req.is_redirect)
781 self.assertEqual(response_content, req.next.url)
782 elif response_content:
783 self.assertEqual(six.text_type(response_content), req.text)