Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 1 | # Copyright 2016 Red Hat, Inc. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 15 | |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 16 | import netaddr |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 17 | from oslo_log import log |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame^] | 18 | from tempest.common.utils import net_utils |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 19 | from tempest.common import waiters |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 20 | from tempest.lib.common.utils import data_utils |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 21 | from tempest.lib.common.utils import test_utils |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 22 | from tempest.lib import exceptions as lib_exc |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 23 | |
| 24 | from neutron.tests.tempest.api import base as base_api |
Jakub Libosvar | 7c58cb2 | 2017-05-03 09:00:14 +0000 | [diff] [blame] | 25 | from neutron.tests.tempest.common import ssh |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 26 | from neutron.tests.tempest import config |
| 27 | from neutron.tests.tempest.scenario import constants |
| 28 | |
| 29 | CONF = config.CONF |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 30 | |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 31 | LOG = log.getLogger(__name__) |
| 32 | |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 33 | |
| 34 | class BaseTempestTestCase(base_api.BaseNetworkTest): |
| 35 | @classmethod |
| 36 | def resource_setup(cls): |
| 37 | super(BaseTempestTestCase, cls).resource_setup() |
| 38 | |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 39 | cls.keypairs = [] |
| 40 | |
| 41 | @classmethod |
| 42 | def resource_cleanup(cls): |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 43 | for keypair in cls.keypairs: |
rajat29 | 4495c04 | 2017-06-28 15:37:16 +0530 | [diff] [blame] | 44 | cls.os_primary.keypairs_client.delete_keypair( |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 45 | keypair_name=keypair['name']) |
| 46 | |
| 47 | super(BaseTempestTestCase, cls).resource_cleanup() |
| 48 | |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 49 | def create_server(self, flavor_ref, image_ref, key_name, networks, |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 50 | name=None, security_groups=None): |
| 51 | """Create a server using tempest lib |
| 52 | All the parameters are the ones used in Compute API |
| 53 | |
| 54 | Args: |
| 55 | flavor_ref(str): The flavor of the server to be provisioned. |
| 56 | image_ref(str): The image of the server to be provisioned. |
| 57 | key_name(str): SSH key to to be used to connect to the |
| 58 | provisioned server. |
| 59 | networks(list): List of dictionaries where each represent |
| 60 | an interface to be attached to the server. For network |
| 61 | it should be {'uuid': network_uuid} and for port it should |
| 62 | be {'port': port_uuid} |
| 63 | name(str): Name of the server to be provisioned. |
| 64 | security_groups(list): List of dictionaries where |
| 65 | the keys is 'name' and the value is the name of |
| 66 | the security group. If it's not passed the default |
| 67 | security group will be used. |
| 68 | """ |
| 69 | |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 70 | name = name or data_utils.rand_name('server-test') |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 71 | if not security_groups: |
| 72 | security_groups = [{'name': 'default'}] |
| 73 | |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 74 | server = self.os_primary.servers_client.create_server( |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 75 | name=name, |
| 76 | flavorRef=flavor_ref, |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 77 | imageRef=image_ref, |
| 78 | key_name=key_name, |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 79 | networks=networks, |
| 80 | security_groups=security_groups) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 81 | |
| 82 | self.addCleanup(test_utils.call_and_ignore_notfound_exc, |
| 83 | waiters.wait_for_server_termination, |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 84 | self.os_primary.servers_client, server['server']['id']) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 85 | self.addCleanup(test_utils.call_and_ignore_notfound_exc, |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 86 | self.os_primary.servers_client.delete_server, |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 87 | server['server']['id']) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 88 | return server |
| 89 | |
| 90 | @classmethod |
| 91 | def create_keypair(cls, client=None): |
rajat29 | 4495c04 | 2017-06-28 15:37:16 +0530 | [diff] [blame] | 92 | client = client or cls.os_primary.keypairs_client |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 93 | name = data_utils.rand_name('keypair-test') |
| 94 | body = client.create_keypair(name=name) |
| 95 | cls.keypairs.append(body['keypair']) |
| 96 | return body['keypair'] |
| 97 | |
| 98 | @classmethod |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 99 | def create_secgroup_rules(cls, rule_list, secgroup_id=None): |
rajat29 | 4495c04 | 2017-06-28 15:37:16 +0530 | [diff] [blame] | 100 | client = cls.os_primary.network_client |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 101 | if not secgroup_id: |
| 102 | sgs = client.list_security_groups()['security_groups'] |
| 103 | for sg in sgs: |
| 104 | if sg['name'] == constants.DEFAULT_SECURITY_GROUP: |
| 105 | secgroup_id = sg['id'] |
| 106 | break |
| 107 | |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 108 | for rule in rule_list: |
| 109 | direction = rule.pop('direction') |
| 110 | client.create_security_group_rule( |
| 111 | direction=direction, |
| 112 | security_group_id=secgroup_id, |
| 113 | **rule) |
| 114 | |
| 115 | @classmethod |
| 116 | def create_loginable_secgroup_rule(cls, secgroup_id=None): |
| 117 | """This rule is intended to permit inbound ssh |
| 118 | |
| 119 | Allowing ssh traffic traffic from all sources, so no group_id is |
| 120 | provided. |
| 121 | Setting a group_id would only permit traffic from ports |
| 122 | belonging to the same security group. |
| 123 | """ |
| 124 | |
| 125 | rule_list = [{'protocol': 'tcp', |
| 126 | 'direction': 'ingress', |
| 127 | 'port_range_min': 22, |
| 128 | 'port_range_max': 22, |
| 129 | 'remote_ip_prefix': '0.0.0.0/0'}] |
| 130 | cls.create_secgroup_rules(rule_list, secgroup_id=secgroup_id) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 131 | |
| 132 | @classmethod |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 133 | def create_pingable_secgroup_rule(cls, secgroup_id=None): |
| 134 | """This rule is intended to permit inbound ping |
| 135 | """ |
| 136 | |
| 137 | rule_list = [{'protocol': 'icmp', |
| 138 | 'direction': 'ingress', |
| 139 | 'port_range_min': 8, # type |
| 140 | 'port_range_max': 0, # code |
| 141 | 'remote_ip_prefix': '0.0.0.0/0'}] |
| 142 | cls.create_secgroup_rules(rule_list, secgroup_id=secgroup_id) |
| 143 | |
| 144 | @classmethod |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 145 | def create_router_by_client(cls, is_admin=False, **kwargs): |
| 146 | kwargs.update({'router_name': data_utils.rand_name('router'), |
| 147 | 'admin_state_up': True, |
| 148 | 'external_network_id': CONF.network.public_network_id}) |
| 149 | if not is_admin: |
| 150 | router = cls.create_router(**kwargs) |
| 151 | else: |
| 152 | router = cls.create_admin_router(**kwargs) |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 153 | LOG.debug("Created router %s", router['name']) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 154 | cls.routers.append(router) |
| 155 | return router |
| 156 | |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 157 | def create_and_associate_floatingip(self, port_id): |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 158 | fip = self.os_primary.network_client.create_floatingip( |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 159 | CONF.network.public_network_id, |
| 160 | port_id=port_id)['floatingip'] |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 161 | self.floating_ips.append(fip) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 162 | return fip |
| 163 | |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 164 | def setup_network_and_server(self, router=None, **kwargs): |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 165 | """Create network resources and a server. |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 166 | |
| 167 | Creating a network, subnet, router, keypair, security group |
| 168 | and a server. |
| 169 | """ |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 170 | self.network = self.create_network() |
| 171 | LOG.debug("Created network %s", self.network['name']) |
| 172 | self.subnet = self.create_subnet(self.network) |
| 173 | LOG.debug("Created subnet %s", self.subnet['id']) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 174 | |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 175 | secgroup = self.os_primary.network_client.create_security_group( |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 176 | name=data_utils.rand_name('secgroup-')) |
Alex Stafeyev | c4d9c35 | 2016-12-12 04:13:33 -0500 | [diff] [blame] | 177 | LOG.debug("Created security group %s", |
| 178 | secgroup['security_group']['name']) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 179 | self.security_groups.append(secgroup['security_group']) |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 180 | if not router: |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 181 | router = self.create_router_by_client(**kwargs) |
| 182 | self.create_router_interface(router['id'], self.subnet['id']) |
| 183 | self.keypair = self.create_keypair() |
| 184 | self.create_loginable_secgroup_rule( |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 185 | secgroup_id=secgroup['security_group']['id']) |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 186 | self.server = self.create_server( |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 187 | flavor_ref=CONF.compute.flavor_ref, |
| 188 | image_ref=CONF.compute.image_ref, |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 189 | key_name=self.keypair['name'], |
| 190 | networks=[{'uuid': self.network['id']}], |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 191 | security_groups=[{'name': secgroup['security_group']['name']}]) |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 192 | waiters.wait_for_server_status(self.os_primary.servers_client, |
Genadi Chereshnya | 918dd0b | 2017-05-17 13:02:20 +0000 | [diff] [blame] | 193 | self.server['server']['id'], |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 194 | constants.SERVER_STATUS_ACTIVE) |
Jakub Libosvar | 1345d9d | 2017-06-09 13:59:05 +0000 | [diff] [blame] | 195 | self.port = self.client.list_ports(network_id=self.network['id'], |
| 196 | device_id=self.server[ |
| 197 | 'server']['id'])['ports'][0] |
| 198 | self.fip = self.create_and_associate_floatingip(self.port['id']) |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 199 | |
| 200 | def check_connectivity(self, host, ssh_user, ssh_key, servers=None): |
| 201 | ssh_client = ssh.Client(host, ssh_user, pkey=ssh_key) |
| 202 | try: |
| 203 | ssh_client.test_connection_auth() |
| 204 | except lib_exc.SSHTimeout as ssh_e: |
| 205 | LOG.debug(ssh_e) |
| 206 | self._log_console_output(servers) |
| 207 | raise |
| 208 | |
| 209 | def _log_console_output(self, servers=None): |
| 210 | if not CONF.compute_feature_enabled.console_output: |
| 211 | LOG.debug('Console output not supported, cannot log') |
| 212 | return |
| 213 | if not servers: |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 214 | servers = self.os_primary.servers_client.list_servers() |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 215 | servers = servers['servers'] |
| 216 | for server in servers: |
| 217 | try: |
| 218 | console_output = ( |
Brian Haley | f86ac2e | 2017-06-21 10:43:50 -0400 | [diff] [blame] | 219 | self.os_primary.servers_client.get_console_output( |
Jakub Libosvar | c0c2f1d | 2017-01-31 12:12:21 -0500 | [diff] [blame] | 220 | server['id'])['output']) |
| 221 | LOG.debug('Console output for %s\nbody=\n%s', |
| 222 | server['id'], console_output) |
| 223 | except lib_exc.NotFound: |
| 224 | LOG.debug("Server %s disappeared(deleted) while looking " |
| 225 | "for the console log", server['id']) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 226 | |
| 227 | def _check_remote_connectivity(self, source, dest, should_succeed=True, |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame^] | 228 | nic=None, mtu=None, fragmentation=True): |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 229 | """check ping server via source ssh connection |
| 230 | |
| 231 | :param source: RemoteClient: an ssh connection from which to ping |
| 232 | :param dest: and IP to ping against |
| 233 | :param should_succeed: boolean should ping succeed or not |
| 234 | :param nic: specific network interface to ping from |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame^] | 235 | :param mtu: mtu size for the packet to be sent |
| 236 | :param fragmentation: Flag for packet fragmentation |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 237 | :returns: boolean -- should_succeed == ping |
| 238 | :returns: ping is false if ping failed |
| 239 | """ |
| 240 | def ping_host(source, host, count=CONF.validation.ping_count, |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame^] | 241 | size=CONF.validation.ping_size, nic=None, mtu=None, |
| 242 | fragmentation=True): |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 243 | addr = netaddr.IPAddress(host) |
| 244 | cmd = 'ping6' if addr.version == 6 else 'ping' |
| 245 | if nic: |
| 246 | cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic) |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame^] | 247 | if mtu: |
| 248 | if not fragmentation: |
| 249 | cmd += ' -M do' |
| 250 | size = str(net_utils.get_ping_payload_size( |
| 251 | mtu=mtu, ip_version=addr.version)) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 252 | cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host) |
| 253 | return source.exec_command(cmd) |
| 254 | |
| 255 | def ping_remote(): |
| 256 | try: |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame^] | 257 | result = ping_host(source, dest, nic=nic, mtu=mtu, |
| 258 | fragmentation=fragmentation) |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 259 | |
| 260 | except lib_exc.SSHExecCommandFailed: |
| 261 | LOG.warning('Failed to ping IP: %s via a ssh connection ' |
| 262 | 'from: %s.', dest, source.host) |
| 263 | return not should_succeed |
| 264 | LOG.debug('ping result: %s', result) |
| 265 | # Assert that the return traffic was from the correct |
| 266 | # source address. |
| 267 | from_source = 'from %s' % dest |
| 268 | self.assertIn(from_source, result) |
| 269 | return should_succeed |
| 270 | |
| 271 | return test_utils.call_until_true(ping_remote, |
| 272 | CONF.validation.ping_timeout, |
| 273 | 1) |
| 274 | |
| 275 | def check_remote_connectivity(self, source, dest, should_succeed=True, |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame^] | 276 | nic=None, mtu=None, fragmentation=True): |
YAMAMOTO Takashi | 2593572 | 2017-01-23 15:34:11 +0900 | [diff] [blame] | 277 | self.assertTrue(self._check_remote_connectivity( |
Genadi Chereshnya | 6d10c6e | 2017-07-05 11:34:20 +0300 | [diff] [blame^] | 278 | source, dest, should_succeed, nic, mtu, fragmentation)) |