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