blob: 4072a1fadd5407f8476552949bac0d0cf1a55e96 [file] [log] [blame]
Itzik Browne67ebb52016-05-15 05:34:41 +00001# 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 Browne67ebb52016-05-15 05:34:41 +000015
16from tempest.common import waiters
17from tempest.lib.common import ssh
18from tempest.lib.common.utils import data_utils
19
20from neutron.tests.tempest.api import base as base_api
21from neutron.tests.tempest import config
22from neutron.tests.tempest.scenario import constants
23
24CONF = config.CONF
Itzik Browne67ebb52016-05-15 05:34:41 +000025
26
27class 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,
Itzik Brownbac51dc2016-10-31 12:25:04 +000050 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 Browne67ebb52016-05-15 05:34:41 +000070 name = name or data_utils.rand_name('server-test')
Itzik Brownbac51dc2016-10-31 12:25:04 +000071 if not security_groups:
72 security_groups = [{'name': 'default'}]
73
Itzik Browne67ebb52016-05-15 05:34:41 +000074 server = cls.manager.servers_client.create_server(
Itzik Brownbac51dc2016-10-31 12:25:04 +000075 name=name,
76 flavorRef=flavor_ref,
Itzik Browne67ebb52016-05-15 05:34:41 +000077 imageRef=image_ref,
78 key_name=key_name,
Itzik Brownbac51dc2016-10-31 12:25:04 +000079 networks=networks,
80 security_groups=security_groups)
Itzik Browne67ebb52016-05-15 05:34:41 +000081 cls.servers.append(server['server']['id'])
82 return server
83
84 @classmethod
85 def create_keypair(cls, client=None):
86 client = client or cls.manager.keypairs_client
87 name = data_utils.rand_name('keypair-test')
88 body = client.create_keypair(name=name)
89 cls.keypairs.append(body['keypair'])
90 return body['keypair']
91
92 @classmethod
Itzik Brown1ef813a2016-06-06 12:56:21 +000093 def create_secgroup_rules(cls, rule_list, secgroup_id=None):
Itzik Browne67ebb52016-05-15 05:34:41 +000094 client = cls.manager.network_client
95 if not secgroup_id:
96 sgs = client.list_security_groups()['security_groups']
97 for sg in sgs:
98 if sg['name'] == constants.DEFAULT_SECURITY_GROUP:
99 secgroup_id = sg['id']
100 break
101
Itzik Brown1ef813a2016-06-06 12:56:21 +0000102 for rule in rule_list:
103 direction = rule.pop('direction')
104 client.create_security_group_rule(
105 direction=direction,
106 security_group_id=secgroup_id,
107 **rule)
108
109 @classmethod
110 def create_loginable_secgroup_rule(cls, secgroup_id=None):
111 """This rule is intended to permit inbound ssh
112
113 Allowing ssh traffic traffic from all sources, so no group_id is
114 provided.
115 Setting a group_id would only permit traffic from ports
116 belonging to the same security group.
117 """
118
119 rule_list = [{'protocol': 'tcp',
120 'direction': 'ingress',
121 'port_range_min': 22,
122 'port_range_max': 22,
123 'remote_ip_prefix': '0.0.0.0/0'}]
124 cls.create_secgroup_rules(rule_list, secgroup_id=secgroup_id)
Itzik Browne67ebb52016-05-15 05:34:41 +0000125
126 @classmethod
127 def create_router_and_interface(cls, subnet_id):
128 router = cls.create_router(
129 data_utils.rand_name('router'), admin_state_up=True,
130 external_network_id=CONF.network.public_network_id)
131 cls.create_router_interface(router['id'], subnet_id)
132 cls.routers.append(router)
133 return router
134
135 @classmethod
136 def create_and_associate_floatingip(cls, port_id):
137 fip = cls.manager.network_client.create_floatingip(
138 CONF.network.public_network_id,
139 port_id=port_id)['floatingip']
140 cls.floating_ips.append(fip)
141 return fip
142
143 @classmethod
144 def check_connectivity(cls, host, ssh_user, ssh_key=None):
145 ssh_client = ssh.Client(host, ssh_user, pkey=ssh_key)
146 ssh_client.test_connection_auth()
Itzik Brown1ef813a2016-06-06 12:56:21 +0000147
148 @classmethod
149 def setup_network_and_server(cls):
Itzik Brownbac51dc2016-10-31 12:25:04 +0000150 """Creating network resources and a server.
151
152 Creating a network, subnet, router, keypair, security group
153 and a server.
154 """
Itzik Brown1ef813a2016-06-06 12:56:21 +0000155 cls.network = cls.create_network()
156 cls.subnet = cls.create_subnet(cls.network)
157
Itzik Brownbac51dc2016-10-31 12:25:04 +0000158 secgroup = cls.manager.network_client.create_security_group(
159 name=data_utils.rand_name('secgroup-'))
160 cls.security_groups.append(secgroup['security_group'])
161
Itzik Brown1ef813a2016-06-06 12:56:21 +0000162 cls.create_router_and_interface(cls.subnet['id'])
163 cls.keypair = cls.create_keypair()
Itzik Brownbac51dc2016-10-31 12:25:04 +0000164 cls.create_loginable_secgroup_rule(
165 secgroup_id=secgroup['security_group']['id'])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000166 cls.server = cls.create_server(
167 flavor_ref=CONF.compute.flavor_ref,
168 image_ref=CONF.compute.image_ref,
169 key_name=cls.keypair['name'],
Itzik Brownbac51dc2016-10-31 12:25:04 +0000170 networks=[{'uuid': cls.network['id']}],
171 security_groups=[{'name': secgroup['security_group']['name']}])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000172 waiters.wait_for_server_status(cls.manager.servers_client,
173 cls.server['server']['id'],
174 constants.SERVER_STATUS_ACTIVE)
175 port = cls.client.list_ports(network_id=cls.network['id'],
176 device_id=cls.server[
177 'server']['id'])['ports'][0]
178 cls.fip = cls.create_and_associate_floatingip(port['id'])