blob: 88697c4b2a3b79971f44005a7daea48cfc23e862 [file] [log] [blame]
nithya-ganesan222efd72015-01-22 12:20:27 +00001# 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
14from oslo_log import log as logging
15
16from tempest import config
nithya-ganesan222efd72015-01-22 12:20:27 +000017
Andrea Frittoli (andreaf)8def7ca2015-05-13 14:24:19 +010018from tempest.common.utils import data_utils
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050019from tempest.lib import exceptions as lib_exc
Andrea Frittoli (andreaf)8def7ca2015-05-13 14:24:19 +010020
nithya-ganesan222efd72015-01-22 12:20:27 +000021CONF = config.CONF
22LOG = logging.getLogger(__name__)
23
24
Matthew Treinish861619c2016-06-16 17:11:49 -040025def _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-ganesan222efd72015-01-22 12:20:27 +000045def create_ssh_security_group(os, add_rule=False):
John Warrenf2345512015-12-10 13:39:30 -050046 security_groups_client = os.compute_security_groups_client
John Warren5cdbf422016-01-05 12:42:43 -050047 security_group_rules_client = os.compute_security_group_rules_client
nithya-ganesan222efd72015-01-22 12:20:27 +000048 sg_name = data_utils.rand_name('securitygroup-')
49 sg_description = data_utils.rand_name('description-')
Yaroslav Lobankove5cc9fb2015-08-07 17:30:51 +030050 security_group = security_groups_client.create_security_group(
ghanshyamb610b772015-08-24 17:29:38 +090051 name=sg_name, description=sg_description)['security_group']
nithya-ganesan222efd72015-01-22 12:20:27 +000052 if add_rule:
Matthew Treinish861619c2016-06-16 17:11:49 -040053 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-ganesan222efd72015-01-22 12:20:27 +000062 LOG.debug("SSH Validation resource security group with tcp and icmp "
Jordan Pittier525ec712016-12-07 17:51:26 +010063 "rules %s created", sg_name)
nithya-ganesan222efd72015-01-22 12:20:27 +000064 return security_group
65
66
67def create_validation_resources(os, validation_resources=None):
68 # Create and Return the validation resources required to validate a VM
69 validation_data = {}
70 if validation_resources:
71 if validation_resources['keypair']:
72 keypair_name = data_utils.rand_name('keypair')
ghanshyamdee01f22015-08-17 11:41:47 +090073 validation_data.update(os.keypairs_client.create_keypair(
74 name=keypair_name))
Jordan Pittier525ec712016-12-07 17:51:26 +010075 LOG.debug("Validation resource key %s created", keypair_name)
nithya-ganesan222efd72015-01-22 12:20:27 +000076 add_rule = False
77 if validation_resources['security_group']:
78 if validation_resources['security_group_rules']:
79 add_rule = True
80 validation_data['security_group'] = \
81 create_ssh_security_group(os, add_rule)
82 if validation_resources['floating_ip']:
John Warrene74890a2015-11-11 15:18:01 -050083 floating_client = os.compute_floating_ips_client
Marc Koderer3b57d802016-03-22 15:23:31 +010084 validation_data.update(
85 floating_client.create_floating_ip(
86 pool=CONF.network.floating_network_name))
nithya-ganesan222efd72015-01-22 12:20:27 +000087 return validation_data
88
89
90def clear_validation_resources(os, validation_data=None):
91 # Cleanup the vm validation resources
92 has_exception = None
93 if validation_data:
94 if 'keypair' in validation_data:
95 keypair_client = os.keypairs_client
96 keypair_name = validation_data['keypair']['name']
97 try:
98 keypair_client.delete_keypair(keypair_name)
99 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100100 LOG.warning(
101 "Keypair %s is not found when attempting to delete",
102 keypair_name
103 )
nithya-ganesan222efd72015-01-22 12:20:27 +0000104 except Exception as exc:
Jordan Pittier525ec712016-12-07 17:51:26 +0100105 LOG.exception('Exception raised while deleting key %s',
106 keypair_name)
nithya-ganesan222efd72015-01-22 12:20:27 +0000107 if not has_exception:
108 has_exception = exc
109 if 'security_group' in validation_data:
John Warrenf2345512015-12-10 13:39:30 -0500110 security_group_client = os.compute_security_groups_client
nithya-ganesan222efd72015-01-22 12:20:27 +0000111 sec_id = validation_data['security_group']['id']
112 try:
113 security_group_client.delete_security_group(sec_id)
114 security_group_client.wait_for_resource_deletion(sec_id)
115 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000116 LOG.warning("Security group %s is not found when attempting "
Jordan Pittier525ec712016-12-07 17:51:26 +0100117 "to delete", sec_id)
nithya-ganesan222efd72015-01-22 12:20:27 +0000118 except lib_exc.Conflict as exc:
119 LOG.exception('Conflict while deleting security '
Jordan Pittier525ec712016-12-07 17:51:26 +0100120 'group %s VM might not be deleted', sec_id)
nithya-ganesan222efd72015-01-22 12:20:27 +0000121 if not has_exception:
122 has_exception = exc
123 except Exception as exc:
124 LOG.exception('Exception raised while deleting security '
Jordan Pittier525ec712016-12-07 17:51:26 +0100125 'group %s', sec_id)
nithya-ganesan222efd72015-01-22 12:20:27 +0000126 if not has_exception:
127 has_exception = exc
128 if 'floating_ip' in validation_data:
John Warrene74890a2015-11-11 15:18:01 -0500129 floating_client = os.compute_floating_ips_client
nithya-ganesan222efd72015-01-22 12:20:27 +0000130 fip_id = validation_data['floating_ip']['id']
131 try:
132 floating_client.delete_floating_ip(fip_id)
133 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000134 LOG.warning('Floating ip %s not found while attempting to '
Jordan Pittier525ec712016-12-07 17:51:26 +0100135 'delete', fip_id)
nithya-ganesan222efd72015-01-22 12:20:27 +0000136 except Exception as exc:
Jordan Pittier525ec712016-12-07 17:51:26 +0100137 LOG.exception('Exception raised while deleting ip %s', fip_id)
nithya-ganesan222efd72015-01-22 12:20:27 +0000138 if not has_exception:
139 has_exception = exc
140 if has_exception:
141 raise has_exception