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 | |
| 16 | from tempest import config |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 17 | |
Andrea Frittoli (andreaf) | 8def7ca | 2015-05-13 14:24:19 +0100 | [diff] [blame] | 18 | from tempest.common.utils import data_utils |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 19 | from tempest.lib import exceptions as lib_exc |
Andrea Frittoli (andreaf) | 8def7ca | 2015-05-13 14:24:19 +0100 | [diff] [blame] | 20 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 21 | CONF = config.CONF |
| 22 | LOG = logging.getLogger(__name__) |
| 23 | |
| 24 | |
Matthew Treinish | 861619c | 2016-06-16 17:11:49 -0400 | [diff] [blame^] | 25 | def _create_neutron_sec_group_rules(os, sec_group): |
| 26 | sec_group_rules_client = os.security_group_rules_client |
| 27 | ethertype = 'IPv4' |
| 28 | if CONF.validation.ip_version_for_ssh == 6: |
| 29 | ethertype = 'IPv6' |
| 30 | |
| 31 | sec_group_rules_client.create_security_group_rule( |
| 32 | security_group_id=sec_group['id'], |
| 33 | protocol='tcp', |
| 34 | ethertype=ethertype, |
| 35 | port_range_min=22, |
| 36 | port_range_max=22, |
| 37 | direction='ingress') |
| 38 | sec_group_rules_client.create_security_group_rule( |
| 39 | security_group_id=sec_group['id'], |
| 40 | protocol='icmp', |
| 41 | ethertype=ethertype, |
| 42 | direction='ingress') |
| 43 | |
| 44 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 45 | def create_ssh_security_group(os, add_rule=False): |
John Warren | f234551 | 2015-12-10 13:39:30 -0500 | [diff] [blame] | 46 | security_groups_client = os.compute_security_groups_client |
John Warren | 5cdbf42 | 2016-01-05 12:42:43 -0500 | [diff] [blame] | 47 | security_group_rules_client = os.compute_security_group_rules_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 48 | sg_name = data_utils.rand_name('securitygroup-') |
| 49 | sg_description = data_utils.rand_name('description-') |
Yaroslav Lobankov | e5cc9fb | 2015-08-07 17:30:51 +0300 | [diff] [blame] | 50 | security_group = security_groups_client.create_security_group( |
ghanshyam | b610b77 | 2015-08-24 17:29:38 +0900 | [diff] [blame] | 51 | name=sg_name, description=sg_description)['security_group'] |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 52 | if add_rule: |
Matthew Treinish | 861619c | 2016-06-16 17:11:49 -0400 | [diff] [blame^] | 53 | if CONF.service_available.neutron: |
| 54 | _create_neutron_sec_group_rules(os, security_group) |
| 55 | else: |
| 56 | security_group_rules_client.create_security_group_rule( |
| 57 | parent_group_id=security_group['id'], ip_protocol='tcp', |
| 58 | from_port=22, to_port=22) |
| 59 | security_group_rules_client.create_security_group_rule( |
| 60 | parent_group_id=security_group['id'], ip_protocol='icmp', |
| 61 | from_port=-1, to_port=-1) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 62 | LOG.debug("SSH Validation resource security group with tcp and icmp " |
| 63 | "rules %s created" |
| 64 | % sg_name) |
| 65 | return security_group |
| 66 | |
| 67 | |
| 68 | def create_validation_resources(os, validation_resources=None): |
| 69 | # Create and Return the validation resources required to validate a VM |
| 70 | validation_data = {} |
| 71 | if validation_resources: |
| 72 | if validation_resources['keypair']: |
| 73 | keypair_name = data_utils.rand_name('keypair') |
ghanshyam | dee01f2 | 2015-08-17 11:41:47 +0900 | [diff] [blame] | 74 | validation_data.update(os.keypairs_client.create_keypair( |
| 75 | name=keypair_name)) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 76 | LOG.debug("Validation resource key %s created" % keypair_name) |
| 77 | add_rule = False |
| 78 | if validation_resources['security_group']: |
| 79 | if validation_resources['security_group_rules']: |
| 80 | add_rule = True |
| 81 | validation_data['security_group'] = \ |
| 82 | create_ssh_security_group(os, add_rule) |
| 83 | if validation_resources['floating_ip']: |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 84 | floating_client = os.compute_floating_ips_client |
Marc Koderer | 3b57d80 | 2016-03-22 15:23:31 +0100 | [diff] [blame] | 85 | validation_data.update( |
| 86 | floating_client.create_floating_ip( |
| 87 | pool=CONF.network.floating_network_name)) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 88 | return validation_data |
| 89 | |
| 90 | |
| 91 | def clear_validation_resources(os, validation_data=None): |
| 92 | # Cleanup the vm validation resources |
| 93 | has_exception = None |
| 94 | if validation_data: |
| 95 | if 'keypair' in validation_data: |
| 96 | keypair_client = os.keypairs_client |
| 97 | keypair_name = validation_data['keypair']['name'] |
| 98 | try: |
| 99 | keypair_client.delete_keypair(keypair_name) |
| 100 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 101 | LOG.warning("Keypair %s is not found when attempting to delete" |
| 102 | % keypair_name) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 103 | except Exception as exc: |
| 104 | LOG.exception('Exception raised while deleting key %s' |
| 105 | % keypair_name) |
| 106 | if not has_exception: |
| 107 | has_exception = exc |
| 108 | if 'security_group' in validation_data: |
John Warren | f234551 | 2015-12-10 13:39:30 -0500 | [diff] [blame] | 109 | security_group_client = os.compute_security_groups_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 110 | sec_id = validation_data['security_group']['id'] |
| 111 | try: |
| 112 | security_group_client.delete_security_group(sec_id) |
| 113 | security_group_client.wait_for_resource_deletion(sec_id) |
| 114 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 115 | LOG.warning("Security group %s is not found when attempting " |
| 116 | "to delete" % sec_id) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 117 | except lib_exc.Conflict as exc: |
| 118 | LOG.exception('Conflict while deleting security ' |
| 119 | 'group %s VM might not be deleted ' % sec_id) |
| 120 | if not has_exception: |
| 121 | has_exception = exc |
| 122 | except Exception as exc: |
| 123 | LOG.exception('Exception raised while deleting security ' |
| 124 | 'group %s ' % sec_id) |
| 125 | if not has_exception: |
| 126 | has_exception = exc |
| 127 | if 'floating_ip' in validation_data: |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 128 | floating_client = os.compute_floating_ips_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 129 | fip_id = validation_data['floating_ip']['id'] |
| 130 | try: |
| 131 | floating_client.delete_floating_ip(fip_id) |
| 132 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 133 | LOG.warning('Floating ip %s not found while attempting to ' |
| 134 | 'delete' % fip_id) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 135 | except Exception as exc: |
| 136 | LOG.exception('Exception raised while deleting ip %s ' |
| 137 | % fip_id) |
| 138 | if not has_exception: |
| 139 | has_exception = exc |
| 140 | if has_exception: |
| 141 | raise has_exception |