Jude Cross | 986e3f5 | 2017-07-24 14:57:20 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import ipaddress |
| 16 | import pkg_resources |
| 17 | import random |
| 18 | import shlex |
| 19 | import six |
| 20 | import string |
| 21 | import subprocess |
| 22 | import tempfile |
| 23 | |
| 24 | from oslo_log import log as logging |
| 25 | from oslo_utils import uuidutils |
| 26 | from tempest import config |
| 27 | from tempest.lib.common.utils import data_utils |
| 28 | from tempest.lib.common.utils.linux import remote_client |
Jude Cross | 986e3f5 | 2017-07-24 14:57:20 -0700 | [diff] [blame] | 29 | from tempest.lib import exceptions |
| 30 | from tempest import test |
| 31 | |
| 32 | from octavia_tempest_plugin import clients |
| 33 | from octavia_tempest_plugin.common import constants as const |
| 34 | from octavia_tempest_plugin.tests import validators |
| 35 | from octavia_tempest_plugin.tests import waiters |
| 36 | |
| 37 | CONF = config.CONF |
| 38 | LOG = logging.getLogger(__name__) |
| 39 | |
| 40 | |
| 41 | class LoadBalancerBaseTest(test.BaseTestCase): |
| 42 | """Base class for load balancer tests.""" |
| 43 | |
| 44 | # Setup cls.os_roles_lb_member. cls.os_primary, cls.os_roles_lb_member, |
| 45 | # and cls.os_roles_lb_admin credentials. |
| 46 | credentials = ['admin', 'primary', |
| 47 | ['lb_member', CONF.load_balancer.member_role], |
| 48 | ['lb_member2', CONF.load_balancer.member_role], |
| 49 | ['lb_admin', CONF.load_balancer.admin_role]] |
| 50 | |
| 51 | client_manager = clients.ManagerV2 |
| 52 | |
| 53 | @classmethod |
| 54 | def skip_checks(cls): |
| 55 | """Check if we should skip all of the children tests.""" |
| 56 | super(LoadBalancerBaseTest, cls).skip_checks() |
| 57 | |
| 58 | service_list = { |
| 59 | 'load_balancer': CONF.service_available.load_balancer, |
| 60 | } |
| 61 | |
| 62 | live_service_list = { |
| 63 | 'compute': CONF.service_available.nova, |
| 64 | 'image': CONF.service_available.glance, |
| 65 | 'neutron': CONF.service_available.neutron |
| 66 | } |
| 67 | |
| 68 | if not CONF.load_balancer.test_with_noop: |
| 69 | service_list.update(live_service_list) |
| 70 | |
| 71 | for service, available in service_list.items(): |
| 72 | if not available: |
| 73 | skip_msg = ("{0} skipped as {1} serivce is not " |
| 74 | "available.".format(cls.__name__, service)) |
| 75 | raise cls.skipException(skip_msg) |
| 76 | |
| 77 | # We must be able to reach our VIP and instances |
| 78 | if not (CONF.network.project_networks_reachable |
| 79 | or CONF.network.public_network_id): |
| 80 | msg = ('Either project_networks_reachable must be "true", or ' |
| 81 | 'public_network_id must be defined.') |
| 82 | raise cls.skipException(msg) |
| 83 | |
| 84 | @classmethod |
| 85 | def setup_credentials(cls): |
| 86 | """Setup test credentials and network resources.""" |
| 87 | # Do not auto create network resources |
| 88 | cls.set_network_resources() |
| 89 | super(LoadBalancerBaseTest, cls).setup_credentials() |
| 90 | |
| 91 | @classmethod |
| 92 | def setup_clients(cls): |
| 93 | """Setup client aliases.""" |
| 94 | super(LoadBalancerBaseTest, cls).setup_clients() |
| 95 | cls.lb_mem_float_ip_client = cls.os_roles_lb_member.floating_ips_client |
| 96 | cls.lb_mem_keypairs_client = cls.os_roles_lb_member.keypairs_client |
| 97 | cls.lb_mem_net_client = cls.os_roles_lb_member.networks_client |
| 98 | cls.lb_mem_ports_client = cls.os_roles_lb_member.ports_client |
| 99 | cls.lb_mem_routers_client = cls.os_roles_lb_member.routers_client |
| 100 | cls.lb_mem_SG_client = cls.os_roles_lb_member.security_groups_client |
| 101 | cls.lb_mem_SGr_client = ( |
| 102 | cls.os_roles_lb_member.security_group_rules_client) |
| 103 | cls.lb_mem_servers_client = cls.os_roles_lb_member.servers_client |
| 104 | cls.lb_mem_subnet_client = cls.os_roles_lb_member.subnets_client |
| 105 | cls.mem_lb_client = cls.os_roles_lb_member.loadbalancer_client |
Jude Cross | fbbd2b4 | 2017-08-09 15:21:04 -0700 | [diff] [blame^] | 106 | cls.mem_listener_client = cls.os_roles_lb_member.listener_client |
Jude Cross | 986e3f5 | 2017-07-24 14:57:20 -0700 | [diff] [blame] | 107 | |
| 108 | @classmethod |
| 109 | def resource_setup(cls): |
| 110 | """Setup resources needed by the tests.""" |
| 111 | super(LoadBalancerBaseTest, cls).resource_setup() |
| 112 | |
| 113 | conf_lb = CONF.load_balancer |
| 114 | |
| 115 | if conf_lb.test_subnet_override and not conf_lb.test_network_override: |
| 116 | raise exceptions.InvalidConfiguration( |
| 117 | "Configuration value test_network_override must be " |
| 118 | "specified if test_subnet_override is used.") |
| 119 | |
| 120 | show_subnet = cls.lb_mem_subnet_client.show_subnet |
| 121 | if CONF.load_balancer.test_with_noop: |
| 122 | cls.lb_member_vip_net = {'id': uuidutils.generate_uuid()} |
| 123 | cls.lb_member_vip_subnet = {'id': uuidutils.generate_uuid()} |
| 124 | cls.lb_member_1_net = {'id': uuidutils.generate_uuid()} |
| 125 | cls.lb_member_1_subnet = {'id': uuidutils.generate_uuid()} |
| 126 | cls.lb_member_2_net = {'id': uuidutils.generate_uuid()} |
| 127 | cls.lb_member_2_subnet = {'id': uuidutils.generate_uuid()} |
| 128 | if CONF.load_balancer.test_with_ipv6: |
| 129 | cls.lb_member_vip_ipv6_subnet = {'id': |
| 130 | uuidutils.generate_uuid()} |
| 131 | cls.lb_member_1_ipv6_subnet = {'id': uuidutils.generate_uuid()} |
| 132 | cls.lb_member_2_ipv6_subnet = {'id': uuidutils.generate_uuid()} |
| 133 | return |
| 134 | elif CONF.load_balancer.test_network_override: |
| 135 | if conf_lb.test_subnet_override: |
| 136 | override_subnet = show_subnet(conf_lb.test_subnet_override) |
| 137 | else: |
| 138 | override_subnet = None |
| 139 | |
| 140 | show_net = cls.lb_mem_net_client.show_network |
| 141 | override_network = show_net(conf_lb.test_network_override) |
| 142 | override_network = override_network.get('network') |
| 143 | |
| 144 | cls.lb_member_vip_net = override_network |
| 145 | cls.lb_member_vip_subnet = override_subnet |
| 146 | cls.lb_member_1_net = override_network |
| 147 | cls.lb_member_1_subnet = override_subnet |
| 148 | cls.lb_member_2_net = override_network |
| 149 | cls.lb_member_2_subnet = override_subnet |
| 150 | |
| 151 | if (CONF.load_balancer.test_with_ipv6 and |
| 152 | conf_lb.test_IPv6_subnet_override): |
| 153 | override_ipv6_subnet = show_subnet( |
| 154 | conf_lb.test_IPv6_subnet_override) |
| 155 | cls.lb_member_vip_ipv6_subnet = override_ipv6_subnet |
| 156 | cls.lb_member_1_ipv6_subnet = override_ipv6_subnet |
| 157 | cls.lb_member_2_ipv6_subnet = override_ipv6_subnet |
| 158 | else: |
| 159 | cls.lb_member_vip_ipv6_subnet = None |
| 160 | cls.lb_member_1_ipv6_subnet = None |
| 161 | cls.lb_member_2_ipv6_subnet = None |
| 162 | else: |
| 163 | cls._create_networks() |
| 164 | |
| 165 | LOG.debug('Octavia Setup: lb_member_vip_net = {}'.format( |
| 166 | cls.lb_member_vip_net[const.ID])) |
| 167 | if cls.lb_member_vip_subnet: |
| 168 | LOG.debug('Octavia Setup: lb_member_vip_subnet = {}'.format( |
| 169 | cls.lb_member_vip_subnet[const.ID])) |
| 170 | LOG.debug('Octavia Setup: lb_member_1_net = {}'.format( |
| 171 | cls.lb_member_1_net[const.ID])) |
| 172 | if cls.lb_member_1_subnet: |
| 173 | LOG.debug('Octavia Setup: lb_member_1_subnet = {}'.format( |
| 174 | cls.lb_member_1_subnet[const.ID])) |
| 175 | LOG.debug('Octavia Setup: lb_member_2_net = {}'.format( |
| 176 | cls.lb_member_2_net[const.ID])) |
| 177 | if cls.lb_member_2_subnet: |
| 178 | LOG.debug('Octavia Setup: lb_member_2_subnet = {}'.format( |
| 179 | cls.lb_member_2_subnet[const.ID])) |
| 180 | if cls.lb_member_vip_ipv6_subnet: |
| 181 | LOG.debug('Octavia Setup: lb_member_vip_ipv6_subnet = {}'.format( |
| 182 | cls.lb_member_vip_ipv6_subnet[const.ID])) |
| 183 | if cls.lb_member_1_ipv6_subnet: |
| 184 | LOG.debug('Octavia Setup: lb_member_1_ipv6_subnet = {}'.format( |
| 185 | cls.lb_member_1_ipv6_subnet[const.ID])) |
| 186 | if cls.lb_member_2_ipv6_subnet: |
| 187 | LOG.debug('Octavia Setup: lb_member_2_ipv6_subnet = {}'.format( |
| 188 | cls.lb_member_2_ipv6_subnet[const.ID])) |
| 189 | |
| 190 | # If validation is disabled in this cloud, we won't be able to |
| 191 | # start the webservers, so don't even boot them. |
| 192 | if not CONF.validation.run_validation: |
| 193 | return |
| 194 | |
| 195 | # Create a keypair for the webservers |
| 196 | keypair_name = data_utils.rand_name('lb_member_keypair') |
| 197 | result = cls.lb_mem_keypairs_client.create_keypair( |
| 198 | name=keypair_name) |
| 199 | cls.lb_member_keypair = result['keypair'] |
| 200 | LOG.info('lb_member_keypair: {}'.format(cls.lb_member_keypair)) |
| 201 | cls.addClassResourceCleanup( |
| 202 | waiters.wait_for_not_found, |
| 203 | cls.lb_mem_keypairs_client.delete_keypair, |
| 204 | cls.lb_mem_keypairs_client.show_keypair, |
| 205 | keypair_name) |
| 206 | |
| 207 | if (CONF.load_balancer.enable_security_groups and |
| 208 | CONF.network_feature_enabled.port_security): |
| 209 | # Set up the security group for the webservers |
| 210 | SG_name = data_utils.rand_name('lb_member_SG') |
| 211 | cls.lb_member_sec_group = ( |
| 212 | cls.lb_mem_SG_client.create_security_group( |
| 213 | name=SG_name)['security_group']) |
| 214 | cls.addClassResourceCleanup( |
| 215 | waiters.wait_for_not_found, |
| 216 | cls.lb_mem_SG_client.delete_security_group, |
| 217 | cls.lb_mem_SG_client.show_security_group, |
| 218 | cls.lb_member_sec_group['id']) |
| 219 | |
| 220 | # Create a security group rule to allow 80-81 (test webservers) |
| 221 | SGr = cls.lb_mem_SGr_client.create_security_group_rule( |
| 222 | direction='ingress', |
| 223 | security_group_id=cls.lb_member_sec_group['id'], |
| 224 | protocol='tcp', |
| 225 | ethertype='IPv4', |
| 226 | port_range_min=80, |
| 227 | port_range_max=81)['security_group_rule'] |
| 228 | cls.addClassResourceCleanup( |
| 229 | waiters.wait_for_not_found, |
| 230 | cls.lb_mem_SGr_client.delete_security_group_rule, |
| 231 | cls.lb_mem_SGr_client.show_security_group_rule, |
| 232 | SGr['id']) |
| 233 | # Create a security group rule to allow 22 (ssh) |
| 234 | SGr = cls.lb_mem_SGr_client.create_security_group_rule( |
| 235 | direction='ingress', |
| 236 | security_group_id=cls.lb_member_sec_group['id'], |
| 237 | protocol='tcp', |
| 238 | ethertype='IPv4', |
| 239 | port_range_min=22, |
| 240 | port_range_max=22)['security_group_rule'] |
| 241 | cls.addClassResourceCleanup( |
| 242 | waiters.wait_for_not_found, |
| 243 | cls.lb_mem_SGr_client.delete_security_group_rule, |
| 244 | cls.lb_mem_SGr_client.show_security_group_rule, |
| 245 | SGr['id']) |
| 246 | if CONF.load_balancer.test_with_ipv6: |
| 247 | # Create a security group rule to allow 80-81 (test webservers) |
| 248 | SGr = cls.lb_mem_SGr_client.create_security_group_rule( |
| 249 | direction='ingress', |
| 250 | security_group_id=cls.lb_member_sec_group['id'], |
| 251 | protocol='tcp', |
| 252 | ethertype='IPv6', |
| 253 | port_range_min=80, |
| 254 | port_range_max=81)['security_group_rule'] |
| 255 | cls.addClassResourceCleanup( |
| 256 | waiters.wait_for_not_found, |
| 257 | cls.lb_mem_SGr_client.delete_security_group_rule, |
| 258 | cls.lb_mem_SGr_client.show_security_group_rule, |
| 259 | SGr['id']) |
| 260 | # Create a security group rule to allow 22 (ssh) |
| 261 | SGr = cls.lb_mem_SGr_client.create_security_group_rule( |
| 262 | direction='ingress', |
| 263 | security_group_id=cls.lb_member_sec_group['id'], |
| 264 | protocol='tcp', |
| 265 | ethertype='IPv6', |
| 266 | port_range_min=22, |
| 267 | port_range_max=22)['security_group_rule'] |
| 268 | cls.addClassResourceCleanup( |
| 269 | waiters.wait_for_not_found, |
| 270 | cls.lb_mem_SGr_client.delete_security_group_rule, |
| 271 | cls.lb_mem_SGr_client.show_security_group_rule, |
| 272 | SGr['id']) |
| 273 | |
| 274 | LOG.info('lb_member_sec_group: {}'.format(cls.lb_member_sec_group)) |
| 275 | |
| 276 | # Create webserver 1 instance |
| 277 | server_details = cls._create_webserver('lb_member_webserver1', |
| 278 | cls.lb_member_1_net) |
| 279 | |
| 280 | cls.lb_member_webserver1 = server_details['server'] |
| 281 | cls.webserver1_ip = server_details.get('ipv4_address') |
| 282 | cls.webserver1_ipv6 = server_details.get('ipv6_address') |
| 283 | cls.webserver1_public_ip = server_details['public_ipv4_address'] |
| 284 | |
| 285 | LOG.debug('Octavia Setup: lb_member_webserver1 = {}'.format( |
| 286 | cls.lb_member_webserver1[const.ID])) |
| 287 | LOG.debug('Octavia Setup: webserver1_ip = {}'.format( |
| 288 | cls.webserver1_ip)) |
| 289 | LOG.debug('Octavia Setup: webserver1_ipv6 = {}'.format( |
| 290 | cls.webserver1_ipv6)) |
| 291 | LOG.debug('Octavia Setup: webserver1_public_ip = {}'.format( |
| 292 | cls.webserver1_public_ip)) |
| 293 | |
| 294 | cls._install_start_webserver(cls.webserver1_public_ip, |
| 295 | cls.lb_member_keypair['private_key'], 1) |
| 296 | |
| 297 | # Validate webserver 1 |
| 298 | cls._validate_webserver(cls.webserver1_public_ip, 1) |
| 299 | |
| 300 | # Create webserver 2 instance |
| 301 | server_details = cls._create_webserver('lb_member_webserver2', |
| 302 | cls.lb_member_2_net) |
| 303 | |
| 304 | cls.lb_member_webserver2 = server_details['server'] |
| 305 | cls.webserver2_ip = server_details.get('ipv4_address') |
| 306 | cls.webserver2_ipv6 = server_details.get('ipv6_address') |
| 307 | cls.webserver2_public_ip = server_details['public_ipv4_address'] |
| 308 | |
| 309 | LOG.debug('Octavia Setup: lb_member_webserver2 = {}'.format( |
| 310 | cls.lb_member_webserver2[const.ID])) |
| 311 | LOG.debug('Octavia Setup: webserver2_ip = {}'.format( |
| 312 | cls.webserver2_ip)) |
| 313 | LOG.debug('Octavia Setup: webserver2_ipv6 = {}'.format( |
| 314 | cls.webserver2_ipv6)) |
| 315 | LOG.debug('Octavia Setup: webserver2_public_ip = {}'.format( |
| 316 | cls.webserver2_public_ip)) |
| 317 | |
| 318 | cls._install_start_webserver(cls.webserver2_public_ip, |
| 319 | cls.lb_member_keypair['private_key'], 5) |
| 320 | |
| 321 | # Validate webserver 2 |
| 322 | cls._validate_webserver(cls.webserver2_public_ip, 5) |
| 323 | |
| 324 | @classmethod |
| 325 | def _install_start_webserver(cls, ip_address, ssh_key, start_id): |
| 326 | local_file = pkg_resources.resource_filename( |
| 327 | 'octavia_tempest_plugin.contrib.httpd', 'httpd.bin') |
| 328 | dest_file = '/dev/shm/httpd.bin' |
| 329 | |
| 330 | linux_client = remote_client.RemoteClient( |
| 331 | ip_address, CONF.validation.image_ssh_user, pkey=ssh_key) |
| 332 | linux_client.validate_authentication() |
| 333 | |
| 334 | with tempfile.NamedTemporaryFile() as key: |
| 335 | key.write(ssh_key.encode('utf-8')) |
| 336 | key.flush() |
| 337 | cmd = ("scp -v -o UserKnownHostsFile=/dev/null " |
| 338 | "-o StrictHostKeyChecking=no " |
| 339 | "-o ConnectTimeout={0} -o ConnectionAttempts={1} " |
| 340 | "-i {2} {3} {4}@{5}:{6}").format( |
| 341 | CONF.load_balancer.scp_connection_timeout, |
| 342 | CONF.load_balancer.scp_connection_attempts, |
| 343 | key.name, local_file, CONF.validation.image_ssh_user, |
| 344 | ip_address, dest_file) |
| 345 | args = shlex.split(cmd) |
| 346 | subprocess_args = {'stdout': subprocess.PIPE, |
| 347 | 'stderr': subprocess.STDOUT, |
| 348 | 'cwd': None} |
| 349 | proc = subprocess.Popen(args, **subprocess_args) |
| 350 | stdout, stderr = proc.communicate() |
| 351 | if proc.returncode != 0: |
| 352 | raise exceptions.CommandFailed(proc.returncode, cmd, |
| 353 | stdout, stderr) |
| 354 | linux_client.exec_command('sudo screen -d -m {0} -port 80 ' |
| 355 | '-id {1}'.format(dest_file, start_id)) |
| 356 | linux_client.exec_command('sudo screen -d -m {0} -port 81 ' |
| 357 | '-id {1}'.format(dest_file, start_id + 1)) |
| 358 | |
| 359 | @classmethod |
| 360 | def _create_networks(cls): |
| 361 | """Creates networks, subnets, and routers used in tests. |
| 362 | |
| 363 | The following are expected to be defined and available to the tests: |
| 364 | cls.lb_member_vip_net |
| 365 | cls.lb_member_vip_subnet |
| 366 | cls.lb_member_vip_ipv6_subnet (optional) |
| 367 | cls.lb_member_1_net |
| 368 | cls.lb_member_1_subnet |
| 369 | cls.lb_member_1_ipv6_subnet (optional) |
| 370 | cls.lb_member_2_net |
| 371 | cls.lb_member_2_subnet |
| 372 | cls.lb_member_2_ipv6_subnet (optional) |
| 373 | """ |
| 374 | |
| 375 | # Create tenant VIP network |
| 376 | network_kwargs = { |
| 377 | 'name': data_utils.rand_name("lb_member_vip_network")} |
| 378 | if CONF.network_feature_enabled.port_security: |
| 379 | # Note: Allowed Address Pairs requires port security |
| 380 | network_kwargs['port_security_enabled'] = True |
| 381 | result = cls.lb_mem_net_client.create_network(**network_kwargs) |
| 382 | cls.lb_member_vip_net = result['network'] |
| 383 | LOG.info('lb_member_vip_net: {}'.format(cls.lb_member_vip_net)) |
| 384 | cls.addClassResourceCleanup( |
| 385 | waiters.wait_for_not_found, |
| 386 | cls.lb_mem_net_client.delete_network, |
| 387 | cls.lb_mem_net_client.show_network, |
| 388 | cls.lb_member_vip_net['id']) |
| 389 | |
| 390 | # Create tenant VIP subnet |
| 391 | subnet_kwargs = { |
| 392 | 'name': data_utils.rand_name("lb_member_vip_subnet"), |
| 393 | 'network_id': cls.lb_member_vip_net['id'], |
| 394 | 'cidr': CONF.load_balancer.vip_subnet_cidr, |
| 395 | 'ip_version': 4} |
| 396 | result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs) |
| 397 | cls.lb_member_vip_subnet = result['subnet'] |
| 398 | LOG.info('lb_member_vip_subnet: {}'.format(cls.lb_member_vip_subnet)) |
| 399 | cls.addClassResourceCleanup( |
| 400 | waiters.wait_for_not_found, |
| 401 | cls.lb_mem_subnet_client.delete_subnet, |
| 402 | cls.lb_mem_subnet_client.show_subnet, |
| 403 | cls.lb_member_vip_subnet['id']) |
| 404 | |
| 405 | # Create tenant VIP IPv6 subnet |
| 406 | if CONF.load_balancer.test_with_ipv6: |
| 407 | subnet_kwargs = { |
| 408 | 'name': data_utils.rand_name("lb_member_vip_ipv6_subnet"), |
| 409 | 'network_id': cls.lb_member_vip_net['id'], |
| 410 | 'cidr': CONF.load_balancer.vip_ipv6_subnet_cidr, |
| 411 | 'ip_version': 6} |
| 412 | result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs) |
| 413 | cls.lb_member_vip_ipv6_subnet = result['subnet'] |
| 414 | LOG.info('lb_member_vip_ipv6_subnet: {}'.format( |
| 415 | cls.lb_member_vip_ipv6_subnet)) |
| 416 | cls.addClassResourceCleanup( |
| 417 | waiters.wait_for_not_found, |
| 418 | cls.lb_mem_subnet_client.delete_subnet, |
| 419 | cls.lb_mem_subnet_client.show_subnet, |
| 420 | cls.lb_member_vip_ipv6_subnet['id']) |
| 421 | |
| 422 | # Create tenant member 1 network |
| 423 | network_kwargs = { |
| 424 | 'name': data_utils.rand_name("lb_member_1_network")} |
| 425 | if CONF.network_feature_enabled.port_security: |
| 426 | if CONF.load_balancer.enable_security_groups: |
| 427 | network_kwargs['port_security_enabled'] = True |
| 428 | else: |
| 429 | network_kwargs['port_security_enabled'] = False |
| 430 | result = cls.lb_mem_net_client.create_network(**network_kwargs) |
| 431 | cls.lb_member_1_net = result['network'] |
| 432 | LOG.info('lb_member_1_net: {}'.format(cls.lb_member_1_net)) |
| 433 | cls.addClassResourceCleanup( |
| 434 | waiters.wait_for_not_found, |
| 435 | cls.lb_mem_net_client.delete_network, |
| 436 | cls.lb_mem_net_client.show_network, |
| 437 | cls.lb_member_1_net['id']) |
| 438 | |
| 439 | # Create tenant member 1 subnet |
| 440 | subnet_kwargs = { |
| 441 | 'name': data_utils.rand_name("lb_member_1_subnet"), |
| 442 | 'network_id': cls.lb_member_1_net['id'], |
| 443 | 'cidr': CONF.load_balancer.member_1_ipv4_subnet_cidr, |
| 444 | 'ip_version': 4} |
| 445 | result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs) |
| 446 | cls.lb_member_1_subnet = result['subnet'] |
| 447 | LOG.info('lb_member_1_subnet: {}'.format(cls.lb_member_1_subnet)) |
| 448 | cls.addClassResourceCleanup( |
| 449 | waiters.wait_for_not_found, |
| 450 | cls.lb_mem_subnet_client.delete_subnet, |
| 451 | cls.lb_mem_subnet_client.show_subnet, |
| 452 | cls.lb_member_1_subnet['id']) |
| 453 | |
| 454 | # Create tenant member 1 ipv6 subnet |
| 455 | if CONF.load_balancer.test_with_ipv6: |
| 456 | subnet_kwargs = { |
| 457 | 'name': data_utils.rand_name("lb_member_1_ipv6_subnet"), |
| 458 | 'network_id': cls.lb_member_1_net['id'], |
| 459 | 'cidr': CONF.load_balancer.member_1_ipv6_subnet_cidr, |
| 460 | 'ip_version': 6} |
| 461 | result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs) |
| 462 | cls.lb_member_1_ipv6_subnet = result['subnet'] |
| 463 | LOG.info('lb_member_1_ipv6_subnet: {}'.format( |
| 464 | cls.lb_member_1_ipv6_subnet)) |
| 465 | cls.addClassResourceCleanup( |
| 466 | waiters.wait_for_not_found, |
| 467 | cls.lb_mem_subnet_client.delete_subnet, |
| 468 | cls.lb_mem_subnet_client.show_subnet, |
| 469 | cls.lb_member_1_ipv6_subnet['id']) |
| 470 | |
| 471 | # Create tenant member 2 network |
| 472 | network_kwargs = { |
| 473 | 'name': data_utils.rand_name("lb_member_2_network")} |
| 474 | if CONF.network_feature_enabled.port_security: |
| 475 | if CONF.load_balancer.enable_security_groups: |
| 476 | network_kwargs['port_security_enabled'] = True |
| 477 | else: |
| 478 | network_kwargs['port_security_enabled'] = False |
| 479 | result = cls.lb_mem_net_client.create_network(**network_kwargs) |
| 480 | cls.lb_member_2_net = result['network'] |
| 481 | LOG.info('lb_member_2_net: {}'.format(cls.lb_member_2_net)) |
| 482 | cls.addClassResourceCleanup( |
| 483 | waiters.wait_for_not_found, |
| 484 | cls.lb_mem_net_client.delete_network, |
| 485 | cls.lb_mem_net_client.show_network, |
| 486 | cls.lb_member_2_net['id']) |
| 487 | |
| 488 | # Create tenant member 2 subnet |
| 489 | subnet_kwargs = { |
| 490 | 'name': data_utils.rand_name("lb_member_2_subnet"), |
| 491 | 'network_id': cls.lb_member_2_net['id'], |
| 492 | 'cidr': CONF.load_balancer.member_2_ipv4_subnet_cidr, |
| 493 | 'ip_version': 4} |
| 494 | result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs) |
| 495 | cls.lb_member_2_subnet = result['subnet'] |
| 496 | LOG.info('lb_member_2_subnet: {}'.format(cls.lb_member_2_subnet)) |
| 497 | cls.addClassResourceCleanup( |
| 498 | waiters.wait_for_not_found, |
| 499 | cls.lb_mem_subnet_client.delete_subnet, |
| 500 | cls.lb_mem_subnet_client.show_subnet, |
| 501 | cls.lb_member_2_subnet['id']) |
| 502 | |
| 503 | # Create tenant member 2 ipv6 subnet |
| 504 | if CONF.load_balancer.test_with_ipv6: |
| 505 | subnet_kwargs = { |
| 506 | 'name': data_utils.rand_name("lb_member_2_ipv6_subnet"), |
| 507 | 'network_id': cls.lb_member_2_net['id'], |
| 508 | 'cidr': CONF.load_balancer.member_2_ipv6_subnet_cidr, |
| 509 | 'ip_version': 6} |
| 510 | result = cls.lb_mem_subnet_client.create_subnet(**subnet_kwargs) |
| 511 | cls.lb_member_2_ipv6_subnet = result['subnet'] |
| 512 | LOG.info('lb_member_2_ipv6_subnet: {}'.format( |
| 513 | cls.lb_member_2_ipv6_subnet)) |
| 514 | cls.addClassResourceCleanup( |
| 515 | waiters.wait_for_not_found, |
| 516 | cls.lb_mem_subnet_client.delete_subnet, |
| 517 | cls.lb_mem_subnet_client.show_subnet, |
| 518 | cls.lb_member_2_ipv6_subnet['id']) |
| 519 | |
| 520 | # Create a router for the subnets (required for the floating IP) |
| 521 | router_name = data_utils.rand_name("lb_member_router") |
| 522 | result = cls.lb_mem_routers_client.create_router( |
| 523 | name=router_name, admin_state_up=True, |
| 524 | external_gateway_info=dict( |
| 525 | network_id=CONF.network.public_network_id)) |
| 526 | cls.lb_member_router = result['router'] |
| 527 | LOG.info('lb_member_router: {}'.format(cls.lb_member_router)) |
| 528 | cls.addClassResourceCleanup( |
| 529 | waiters.wait_for_not_found, |
| 530 | cls.lb_mem_routers_client.delete_router, |
| 531 | cls.lb_mem_routers_client.show_router, |
| 532 | cls.lb_member_router['id']) |
| 533 | |
| 534 | # Add VIP subnet to router |
| 535 | cls.lb_mem_routers_client.add_router_interface( |
| 536 | cls.lb_member_router['id'], |
| 537 | subnet_id=cls.lb_member_vip_subnet['id']) |
| 538 | cls.addClassResourceCleanup( |
| 539 | waiters.wait_for_not_found, |
| 540 | cls.lb_mem_routers_client.remove_router_interface, |
| 541 | cls.lb_mem_routers_client.remove_router_interface, |
| 542 | cls.lb_member_router['id'], |
| 543 | subnet_id=cls.lb_member_vip_subnet['id']) |
| 544 | |
| 545 | # Add member subnet 1 to router |
| 546 | cls.lb_mem_routers_client.add_router_interface( |
| 547 | cls.lb_member_router['id'], |
| 548 | subnet_id=cls.lb_member_1_subnet['id']) |
| 549 | cls.addClassResourceCleanup( |
| 550 | waiters.wait_for_not_found, |
Jude Cross | 986e3f5 | 2017-07-24 14:57:20 -0700 | [diff] [blame] | 551 | cls.lb_mem_routers_client.remove_router_interface, |
| 552 | cls.lb_mem_routers_client.remove_router_interface, |
| 553 | cls.lb_member_router['id'], subnet_id=cls.lb_member_1_subnet['id']) |
| 554 | |
| 555 | # Add member subnet 2 to router |
| 556 | cls.lb_mem_routers_client.add_router_interface( |
| 557 | cls.lb_member_router['id'], |
| 558 | subnet_id=cls.lb_member_2_subnet['id']) |
| 559 | cls.addClassResourceCleanup( |
| 560 | waiters.wait_for_not_found, |
| 561 | cls.lb_mem_routers_client.remove_router_interface, |
| 562 | cls.lb_mem_routers_client.remove_router_interface, |
| 563 | cls.lb_member_router['id'], subnet_id=cls.lb_member_2_subnet['id']) |
| 564 | |
| 565 | @classmethod |
| 566 | def _create_webserver(cls, name, network): |
| 567 | """Creates a webserver with two ports. |
| 568 | |
| 569 | webserver_details dictionary contains: |
| 570 | server - The compute server object |
| 571 | ipv4_address - The IPv4 address for the server (optional) |
| 572 | ipv6_address - The IPv6 address for the server (optional) |
| 573 | public_ipv4_address - The publicly accessible IPv4 address for the |
| 574 | server, this may be a floating IP (optional) |
| 575 | |
| 576 | :param name: The name of the server to create. |
| 577 | :param network: The network to boot the server on. |
| 578 | :returns: webserver_details dictionary. |
| 579 | """ |
| 580 | server_kwargs = { |
| 581 | 'name': data_utils.rand_name(name), |
| 582 | 'flavorRef': CONF.compute.flavor_ref, |
| 583 | 'imageRef': CONF.compute.image_ref, |
| 584 | 'key_name': cls.lb_member_keypair['name']} |
| 585 | if (CONF.load_balancer.enable_security_groups and |
| 586 | CONF.network_feature_enabled.port_security): |
| 587 | server_kwargs['security_groups'] = [ |
| 588 | {'name': cls.lb_member_sec_group['name']}] |
| 589 | if not CONF.load_balancer.disable_boot_network: |
| 590 | server_kwargs['networks'] = [{'uuid': network['id']}] |
| 591 | |
| 592 | # Replace the name for clouds that have limitations |
| 593 | if CONF.load_balancer.random_server_name_length: |
| 594 | r = random.SystemRandom() |
| 595 | server_kwargs['name'] = "m{}".format("".join( |
| 596 | [r.choice(string.ascii_uppercase + string.digits) |
| 597 | for _ in range( |
| 598 | CONF.load_balancer.random_server_name_length - 1)] |
| 599 | )) |
| 600 | if CONF.load_balancer.availability_zone: |
| 601 | server_kwargs['availability_zone'] = ( |
| 602 | CONF.load_balancer.availability_zone) |
| 603 | |
| 604 | server = cls.lb_mem_servers_client.create_server( |
| 605 | **server_kwargs)['server'] |
| 606 | cls.addClassResourceCleanup( |
| 607 | waiters.wait_for_not_found, |
| 608 | cls.lb_mem_servers_client.delete_server, |
| 609 | cls.lb_mem_servers_client.show_server, |
| 610 | server['id']) |
| 611 | server = waiters.wait_for_status( |
| 612 | cls.lb_mem_servers_client.show_server, |
| 613 | server['id'], 'status', 'ACTIVE', |
| 614 | CONF.load_balancer.build_interval, |
| 615 | CONF.load_balancer.build_timeout, |
| 616 | root_tag='server') |
| 617 | webserver_details = {'server': server} |
| 618 | LOG.info('Created server: {}'.format(server)) |
| 619 | |
| 620 | addresses = server['addresses'] |
| 621 | if CONF.load_balancer.disable_boot_network: |
| 622 | instance_network = addresses.values()[0] |
| 623 | else: |
| 624 | instance_network = addresses[network['name']] |
| 625 | for addr in instance_network: |
| 626 | if addr['version'] == 4: |
| 627 | webserver_details['ipv4_address'] = addr['addr'] |
| 628 | if addr['version'] == 6: |
| 629 | webserver_details['ipv6_address'] = addr['addr'] |
| 630 | |
| 631 | if CONF.validation.connect_method == 'floating': |
| 632 | result = cls.lb_mem_ports_client.list_ports( |
| 633 | network_id=network['id'], |
| 634 | mac_address=instance_network[0]['OS-EXT-IPS-MAC:mac_addr']) |
| 635 | port_id = result['ports'][0]['id'] |
| 636 | result = cls.lb_mem_float_ip_client.create_floatingip( |
| 637 | floating_network_id=CONF.network.public_network_id, |
| 638 | port_id=port_id) |
| 639 | floating_ip = result['floatingip'] |
| 640 | LOG.info('webserver1_floating_ip: {}'.format(floating_ip)) |
| 641 | cls.addClassResourceCleanup( |
| 642 | waiters.wait_for_not_found, |
| 643 | cls.lb_mem_float_ip_client.delete_floatingip, |
| 644 | cls.lb_mem_float_ip_client.show_floatingip, |
| 645 | floatingip_id=floating_ip['id']) |
| 646 | webserver_details['public_ipv4_address'] = ( |
| 647 | floating_ip['floating_ip_address']) |
| 648 | else: |
| 649 | webserver_details['public_ipv4_address'] = ( |
| 650 | instance_network[0]['addr']) |
| 651 | |
| 652 | return webserver_details |
| 653 | |
| 654 | @classmethod |
| 655 | def _validate_webserver(cls, ip_address, start_id): |
| 656 | URL = 'http://{0}'.format(ip_address) |
| 657 | validators.validate_URL_response(URL, expected_body=str(start_id)) |
| 658 | URL = 'http://{0}:81'.format(ip_address) |
| 659 | validators.validate_URL_response(URL, expected_body=str(start_id + 1)) |
| 660 | |
| 661 | @classmethod |
| 662 | def _setup_lb_network_kwargs(cls, lb_kwargs, ip_version): |
| 663 | if cls.lb_member_vip_subnet: |
| 664 | ip_index = data_utils.rand_int_id(start=10, end=100) |
| 665 | if ip_version == 4: |
| 666 | network = ipaddress.IPv4Network( |
| 667 | six.u(CONF.load_balancer.vip_subnet_cidr)) |
| 668 | lb_vip_address = str(network[ip_index]) |
| 669 | subnet_id = cls.lb_member_vip_subnet[const.ID] |
| 670 | else: |
| 671 | network = ipaddress.IPv6Network( |
| 672 | six.u(CONF.load_balancer.vip_ipv6_subnet_cidr)) |
| 673 | lb_vip_address = str(network[ip_index]) |
| 674 | subnet_id = cls.lb_member_vip_ipv6_subnet[const.ID] |
| 675 | lb_kwargs[const.VIP_SUBNET_ID] = subnet_id |
| 676 | lb_kwargs[const.VIP_ADDRESS] = lb_vip_address |
| 677 | if CONF.load_balancer.test_with_noop: |
| 678 | lb_kwargs[const.VIP_NETWORK_ID] = ( |
| 679 | cls.lb_member_vip_net[const.ID]) |
| 680 | else: |
| 681 | lb_kwargs[const.VIP_NETWORK_ID] = cls.lb_member_vip_net[const.ID] |
| 682 | lb_kwargs[const.VIP_SUBNET_ID] = None |