nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 1 | # Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 2 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | # you may not use this file except in compliance with the License. |
| 4 | # You may obtain a copy of the License at |
| 5 | # |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # |
| 8 | # Unless required by applicable law or agreed to in writing, software |
| 9 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
| 14 | from oslo_log import log as logging |
| 15 | |
Ken'ichi Ohmichi | ef1c1ce | 2017-03-10 11:07:10 -0800 | [diff] [blame] | 16 | from tempest.lib.common.utils import data_utils |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 17 | from tempest.lib import exceptions as lib_exc |
Andrea Frittoli (andreaf) | 8def7ca | 2015-05-13 14:24:19 +0100 | [diff] [blame] | 18 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 19 | LOG = logging.getLogger(__name__) |
| 20 | |
| 21 | |
Andrea Frittoli | 1fa7a60 | 2017-08-09 16:28:55 +0100 | [diff] [blame] | 22 | def _create_neutron_sec_group_rules(os, sec_group, ethertype='IPv4'): |
Matthew Treinish | 861619c | 2016-06-16 17:11:49 -0400 | [diff] [blame] | 23 | sec_group_rules_client = os.security_group_rules_client |
Matthew Treinish | 861619c | 2016-06-16 17:11:49 -0400 | [diff] [blame] | 24 | |
| 25 | sec_group_rules_client.create_security_group_rule( |
| 26 | security_group_id=sec_group['id'], |
| 27 | protocol='tcp', |
| 28 | ethertype=ethertype, |
| 29 | port_range_min=22, |
| 30 | port_range_max=22, |
| 31 | direction='ingress') |
| 32 | sec_group_rules_client.create_security_group_rule( |
| 33 | security_group_id=sec_group['id'], |
| 34 | protocol='icmp', |
| 35 | ethertype=ethertype, |
| 36 | direction='ingress') |
| 37 | |
| 38 | |
Andrea Frittoli | 1fa7a60 | 2017-08-09 16:28:55 +0100 | [diff] [blame] | 39 | def create_ssh_security_group(os, add_rule=False, ethertype='IPv4', |
| 40 | use_neutron=True): |
John Warren | f234551 | 2015-12-10 13:39:30 -0500 | [diff] [blame] | 41 | security_groups_client = os.compute_security_groups_client |
John Warren | 5cdbf42 | 2016-01-05 12:42:43 -0500 | [diff] [blame] | 42 | security_group_rules_client = os.compute_security_group_rules_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 43 | sg_name = data_utils.rand_name('securitygroup-') |
| 44 | sg_description = data_utils.rand_name('description-') |
Yaroslav Lobankov | e5cc9fb | 2015-08-07 17:30:51 +0300 | [diff] [blame] | 45 | security_group = security_groups_client.create_security_group( |
ghanshyam | b610b77 | 2015-08-24 17:29:38 +0900 | [diff] [blame] | 46 | name=sg_name, description=sg_description)['security_group'] |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 47 | if add_rule: |
Andrea Frittoli | 1fa7a60 | 2017-08-09 16:28:55 +0100 | [diff] [blame] | 48 | if use_neutron: |
| 49 | _create_neutron_sec_group_rules(os, security_group, |
| 50 | ethertype=ethertype) |
Matthew Treinish | 861619c | 2016-06-16 17:11:49 -0400 | [diff] [blame] | 51 | else: |
| 52 | security_group_rules_client.create_security_group_rule( |
| 53 | parent_group_id=security_group['id'], ip_protocol='tcp', |
| 54 | from_port=22, to_port=22) |
| 55 | security_group_rules_client.create_security_group_rule( |
| 56 | parent_group_id=security_group['id'], ip_protocol='icmp', |
| 57 | from_port=-1, to_port=-1) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 58 | LOG.debug("SSH Validation resource security group with tcp and icmp " |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 59 | "rules %s created", sg_name) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 60 | return security_group |
| 61 | |
| 62 | |
Andrea Frittoli | 1fa7a60 | 2017-08-09 16:28:55 +0100 | [diff] [blame] | 63 | def create_validation_resources(os, validation_resources=None, |
| 64 | ethertype='IPv4', use_neutron=True, |
| 65 | floating_network_id=None, |
| 66 | floating_network_name=None): |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 67 | # Create and Return the validation resources required to validate a VM |
| 68 | validation_data = {} |
| 69 | if validation_resources: |
| 70 | if validation_resources['keypair']: |
| 71 | keypair_name = data_utils.rand_name('keypair') |
ghanshyam | dee01f2 | 2015-08-17 11:41:47 +0900 | [diff] [blame] | 72 | validation_data.update(os.keypairs_client.create_keypair( |
| 73 | name=keypair_name)) |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 74 | LOG.debug("Validation resource key %s created", keypair_name) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 75 | add_rule = False |
| 76 | if validation_resources['security_group']: |
| 77 | if validation_resources['security_group_rules']: |
| 78 | add_rule = True |
| 79 | validation_data['security_group'] = \ |
Andrea Frittoli | 1fa7a60 | 2017-08-09 16:28:55 +0100 | [diff] [blame] | 80 | create_ssh_security_group( |
| 81 | os, add_rule, use_neutron=use_neutron, ethertype=ethertype) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 82 | if validation_resources['floating_ip']: |
Andrea Frittoli | 1fa7a60 | 2017-08-09 16:28:55 +0100 | [diff] [blame] | 83 | if use_neutron: |
Matt Riedemann | dfbefae | 2017-06-05 15:50:14 -0400 | [diff] [blame] | 84 | floatingip = os.floating_ips_client.create_floatingip( |
Andrea Frittoli | 1fa7a60 | 2017-08-09 16:28:55 +0100 | [diff] [blame] | 85 | floating_network_id=floating_network_id) |
Matt Riedemann | dfbefae | 2017-06-05 15:50:14 -0400 | [diff] [blame] | 86 | # validation_resources['floating_ip'] has historically looked |
| 87 | # like a compute API POST /os-floating-ips response, so we need |
| 88 | # to mangle it a bit for a Neutron response with different |
| 89 | # fields. |
| 90 | validation_data['floating_ip'] = floatingip['floatingip'] |
| 91 | validation_data['floating_ip']['ip'] = ( |
| 92 | floatingip['floatingip']['floating_ip_address']) |
| 93 | else: |
| 94 | # NOTE(mriedem): The os-floating-ips compute API was deprecated |
| 95 | # in the 2.36 microversion. Any tests for CRUD operations on |
| 96 | # floating IPs using the compute API should be capped at 2.35. |
| 97 | validation_data.update( |
| 98 | os.compute_floating_ips_client.create_floating_ip( |
Andrea Frittoli | 1fa7a60 | 2017-08-09 16:28:55 +0100 | [diff] [blame] | 99 | pool=floating_network_name)) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 100 | return validation_data |
| 101 | |
| 102 | |
| 103 | def clear_validation_resources(os, validation_data=None): |
| 104 | # Cleanup the vm validation resources |
| 105 | has_exception = None |
| 106 | if validation_data: |
| 107 | if 'keypair' in validation_data: |
| 108 | keypair_client = os.keypairs_client |
| 109 | keypair_name = validation_data['keypair']['name'] |
| 110 | try: |
| 111 | keypair_client.delete_keypair(keypair_name) |
| 112 | except lib_exc.NotFound: |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 113 | LOG.warning( |
| 114 | "Keypair %s is not found when attempting to delete", |
| 115 | keypair_name |
| 116 | ) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 117 | except Exception as exc: |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 118 | LOG.exception('Exception raised while deleting key %s', |
| 119 | keypair_name) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 120 | if not has_exception: |
| 121 | has_exception = exc |
| 122 | if 'security_group' in validation_data: |
John Warren | f234551 | 2015-12-10 13:39:30 -0500 | [diff] [blame] | 123 | security_group_client = os.compute_security_groups_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 124 | sec_id = validation_data['security_group']['id'] |
| 125 | try: |
| 126 | security_group_client.delete_security_group(sec_id) |
| 127 | security_group_client.wait_for_resource_deletion(sec_id) |
| 128 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 129 | LOG.warning("Security group %s is not found when attempting " |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 130 | "to delete", sec_id) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 131 | except lib_exc.Conflict as exc: |
| 132 | LOG.exception('Conflict while deleting security ' |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 133 | 'group %s VM might not be deleted', sec_id) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 134 | if not has_exception: |
| 135 | has_exception = exc |
| 136 | except Exception as exc: |
| 137 | LOG.exception('Exception raised while deleting security ' |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 138 | 'group %s', sec_id) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 139 | if not has_exception: |
| 140 | has_exception = exc |
| 141 | if 'floating_ip' in validation_data: |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 142 | floating_client = os.compute_floating_ips_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 143 | fip_id = validation_data['floating_ip']['id'] |
| 144 | try: |
| 145 | floating_client.delete_floating_ip(fip_id) |
| 146 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 147 | LOG.warning('Floating ip %s not found while attempting to ' |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 148 | 'delete', fip_id) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 149 | except Exception as exc: |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 150 | LOG.exception('Exception raised while deleting ip %s', fip_id) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 151 | if not has_exception: |
| 152 | has_exception = exc |
| 153 | if has_exception: |
| 154 | raise has_exception |