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 |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame^] | 69 | def create_secgroup_rules(cls, rule_list, secgroup_id=None): |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 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 | |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame^] | 78 | for rule in rule_list: |
| 79 | direction = rule.pop('direction') |
| 80 | client.create_security_group_rule( |
| 81 | direction=direction, |
| 82 | security_group_id=secgroup_id, |
| 83 | **rule) |
| 84 | |
| 85 | @classmethod |
| 86 | def create_loginable_secgroup_rule(cls, secgroup_id=None): |
| 87 | """This rule is intended to permit inbound ssh |
| 88 | |
| 89 | Allowing ssh traffic traffic from all sources, so no group_id is |
| 90 | provided. |
| 91 | Setting a group_id would only permit traffic from ports |
| 92 | belonging to the same security group. |
| 93 | """ |
| 94 | |
| 95 | rule_list = [{'protocol': 'tcp', |
| 96 | 'direction': 'ingress', |
| 97 | 'port_range_min': 22, |
| 98 | 'port_range_max': 22, |
| 99 | 'remote_ip_prefix': '0.0.0.0/0'}] |
| 100 | cls.create_secgroup_rules(rule_list, secgroup_id=secgroup_id) |
Itzik Brown | e67ebb5 | 2016-05-15 05:34:41 +0000 | [diff] [blame] | 101 | |
| 102 | @classmethod |
| 103 | def create_router_and_interface(cls, subnet_id): |
| 104 | router = cls.create_router( |
| 105 | data_utils.rand_name('router'), admin_state_up=True, |
| 106 | external_network_id=CONF.network.public_network_id) |
| 107 | cls.create_router_interface(router['id'], subnet_id) |
| 108 | cls.routers.append(router) |
| 109 | return router |
| 110 | |
| 111 | @classmethod |
| 112 | def create_and_associate_floatingip(cls, port_id): |
| 113 | fip = cls.manager.network_client.create_floatingip( |
| 114 | CONF.network.public_network_id, |
| 115 | port_id=port_id)['floatingip'] |
| 116 | cls.floating_ips.append(fip) |
| 117 | return fip |
| 118 | |
| 119 | @classmethod |
| 120 | def check_connectivity(cls, host, ssh_user, ssh_key=None): |
| 121 | ssh_client = ssh.Client(host, ssh_user, pkey=ssh_key) |
| 122 | ssh_client.test_connection_auth() |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame^] | 123 | |
| 124 | @classmethod |
| 125 | def setup_network_and_server(cls): |
| 126 | cls.network = cls.create_network() |
| 127 | cls.subnet = cls.create_subnet(cls.network) |
| 128 | |
| 129 | cls.create_router_and_interface(cls.subnet['id']) |
| 130 | cls.keypair = cls.create_keypair() |
| 131 | cls.create_loginable_secgroup_rule() |
| 132 | cls.server = cls.create_server( |
| 133 | flavor_ref=CONF.compute.flavor_ref, |
| 134 | image_ref=CONF.compute.image_ref, |
| 135 | key_name=cls.keypair['name'], |
| 136 | networks=[{'uuid': cls.network['id']}]) |
| 137 | waiters.wait_for_server_status(cls.manager.servers_client, |
| 138 | cls.server['server']['id'], |
| 139 | constants.SERVER_STATUS_ACTIVE) |
| 140 | port = cls.client.list_ports(network_id=cls.network['id'], |
| 141 | device_id=cls.server[ |
| 142 | 'server']['id'])['ports'][0] |
| 143 | cls.fip = cls.create_and_associate_floatingip(port['id']) |