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 | |
| 16 | from tempest.common import waiters |
| 17 | from tempest.lib.common import ssh |
| 18 | from tempest.lib.common.utils import data_utils |
| 19 | |
| 20 | from neutron.tests.tempest.api import base as base_api |
| 21 | from neutron.tests.tempest import config |
| 22 | from neutron.tests.tempest.scenario import constants |
| 23 | |
| 24 | CONF = config.CONF |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class BaseTempestTestCase(base_api.BaseNetworkTest): |
| 28 | @classmethod |
| 29 | def resource_setup(cls): |
| 30 | super(BaseTempestTestCase, cls).resource_setup() |
| 31 | |
| 32 | cls.servers = [] |
| 33 | cls.keypairs = [] |
| 34 | |
| 35 | @classmethod |
| 36 | def resource_cleanup(cls): |
| 37 | for server in cls.servers: |
| 38 | cls.manager.servers_client.delete_server(server) |
| 39 | waiters.wait_for_server_termination(cls.manager.servers_client, |
| 40 | server) |
| 41 | |
| 42 | for keypair in cls.keypairs: |
| 43 | cls.manager.keypairs_client.delete_keypair( |
| 44 | keypair_name=keypair['name']) |
| 45 | |
| 46 | super(BaseTempestTestCase, cls).resource_cleanup() |
| 47 | |
| 48 | @classmethod |
| 49 | def create_server(cls, flavor_ref, image_ref, key_name, networks, |
| 50 | name=None): |
| 51 | name = name or data_utils.rand_name('server-test') |
| 52 | server = cls.manager.servers_client.create_server( |
| 53 | name=name, flavorRef=flavor_ref, |
| 54 | imageRef=image_ref, |
| 55 | key_name=key_name, |
| 56 | networks=networks) |
| 57 | cls.servers.append(server['server']['id']) |
| 58 | return server |
| 59 | |
| 60 | @classmethod |
| 61 | def create_keypair(cls, client=None): |
| 62 | client = client or cls.manager.keypairs_client |
| 63 | name = data_utils.rand_name('keypair-test') |
| 64 | body = client.create_keypair(name=name) |
| 65 | cls.keypairs.append(body['keypair']) |
| 66 | return body['keypair'] |
| 67 | |
| 68 | @classmethod |
| 69 | def create_loginable_secgroup_rule(cls, secgroup_id=None): |
| 70 | client = cls.manager.network_client |
| 71 | if not secgroup_id: |
| 72 | sgs = client.list_security_groups()['security_groups'] |
| 73 | for sg in sgs: |
| 74 | if sg['name'] == constants.DEFAULT_SECURITY_GROUP: |
| 75 | secgroup_id = sg['id'] |
| 76 | break |
| 77 | |
| 78 | # This rule is intended to permit inbound ssh |
| 79 | # traffic from all sources, so no group_id is provided. |
| 80 | # Setting a group_id would only permit traffic from ports |
| 81 | # belonging to the same security group. |
| 82 | ruleset = {'protocol': 'tcp', |
| 83 | 'port_range_min': 22, |
| 84 | 'port_range_max': 22, |
| 85 | 'remote_ip_prefix': '0.0.0.0/0'} |
| 86 | rules = [client.create_security_group_rule( |
| 87 | direction='ingress', security_group_id=secgroup_id, |
| 88 | **ruleset)['security_group_rule']] |
| 89 | return rules |
| 90 | |
| 91 | @classmethod |
| 92 | def create_router_and_interface(cls, subnet_id): |
| 93 | router = cls.create_router( |
| 94 | data_utils.rand_name('router'), admin_state_up=True, |
| 95 | external_network_id=CONF.network.public_network_id) |
| 96 | cls.create_router_interface(router['id'], subnet_id) |
| 97 | cls.routers.append(router) |
| 98 | return router |
| 99 | |
| 100 | @classmethod |
| 101 | def create_and_associate_floatingip(cls, port_id): |
| 102 | fip = cls.manager.network_client.create_floatingip( |
| 103 | CONF.network.public_network_id, |
| 104 | port_id=port_id)['floatingip'] |
| 105 | cls.floating_ips.append(fip) |
| 106 | return fip |
| 107 | |
| 108 | @classmethod |
| 109 | def check_connectivity(cls, host, ssh_user, ssh_key=None): |
| 110 | ssh_client = ssh.Client(host, ssh_user, pkey=ssh_key) |
| 111 | ssh_client.test_connection_auth() |