blob: 351e587c5affab0e0a7f42cf53b2196951cb128d [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
Alex Stafeyevc4d9c352016-12-12 04:13:33 -050016from oslo_log import log
17
Itzik Browne67ebb52016-05-15 05:34:41 +000018from tempest.common import waiters
19from tempest.lib.common import ssh
20from tempest.lib.common.utils import data_utils
21
22from neutron.tests.tempest.api import base as base_api
23from neutron.tests.tempest import config
24from neutron.tests.tempest.scenario import constants
25
26CONF = config.CONF
Itzik Browne67ebb52016-05-15 05:34:41 +000027
Alex Stafeyevc4d9c352016-12-12 04:13:33 -050028LOG = log.getLogger(__name__)
29
Itzik Browne67ebb52016-05-15 05:34:41 +000030
31class BaseTempestTestCase(base_api.BaseNetworkTest):
32 @classmethod
33 def resource_setup(cls):
34 super(BaseTempestTestCase, cls).resource_setup()
35
36 cls.servers = []
37 cls.keypairs = []
38
39 @classmethod
40 def resource_cleanup(cls):
41 for server in cls.servers:
42 cls.manager.servers_client.delete_server(server)
43 waiters.wait_for_server_termination(cls.manager.servers_client,
44 server)
45
46 for keypair in cls.keypairs:
47 cls.manager.keypairs_client.delete_keypair(
48 keypair_name=keypair['name'])
49
50 super(BaseTempestTestCase, cls).resource_cleanup()
51
52 @classmethod
53 def create_server(cls, flavor_ref, image_ref, key_name, networks,
Itzik Brownbac51dc2016-10-31 12:25:04 +000054 name=None, security_groups=None):
55 """Create a server using tempest lib
56 All the parameters are the ones used in Compute API
57
58 Args:
59 flavor_ref(str): The flavor of the server to be provisioned.
60 image_ref(str): The image of the server to be provisioned.
61 key_name(str): SSH key to to be used to connect to the
62 provisioned server.
63 networks(list): List of dictionaries where each represent
64 an interface to be attached to the server. For network
65 it should be {'uuid': network_uuid} and for port it should
66 be {'port': port_uuid}
67 name(str): Name of the server to be provisioned.
68 security_groups(list): List of dictionaries where
69 the keys is 'name' and the value is the name of
70 the security group. If it's not passed the default
71 security group will be used.
72 """
73
Itzik Browne67ebb52016-05-15 05:34:41 +000074 name = name or data_utils.rand_name('server-test')
Itzik Brownbac51dc2016-10-31 12:25:04 +000075 if not security_groups:
76 security_groups = [{'name': 'default'}]
77
Itzik Browne67ebb52016-05-15 05:34:41 +000078 server = cls.manager.servers_client.create_server(
Itzik Brownbac51dc2016-10-31 12:25:04 +000079 name=name,
80 flavorRef=flavor_ref,
Itzik Browne67ebb52016-05-15 05:34:41 +000081 imageRef=image_ref,
82 key_name=key_name,
Itzik Brownbac51dc2016-10-31 12:25:04 +000083 networks=networks,
84 security_groups=security_groups)
Itzik Browne67ebb52016-05-15 05:34:41 +000085 cls.servers.append(server['server']['id'])
86 return server
87
88 @classmethod
89 def create_keypair(cls, client=None):
90 client = client or cls.manager.keypairs_client
91 name = data_utils.rand_name('keypair-test')
92 body = client.create_keypair(name=name)
93 cls.keypairs.append(body['keypair'])
94 return body['keypair']
95
96 @classmethod
Itzik Brown1ef813a2016-06-06 12:56:21 +000097 def create_secgroup_rules(cls, rule_list, secgroup_id=None):
Itzik Browne67ebb52016-05-15 05:34:41 +000098 client = cls.manager.network_client
99 if not secgroup_id:
100 sgs = client.list_security_groups()['security_groups']
101 for sg in sgs:
102 if sg['name'] == constants.DEFAULT_SECURITY_GROUP:
103 secgroup_id = sg['id']
104 break
105
Itzik Brown1ef813a2016-06-06 12:56:21 +0000106 for rule in rule_list:
107 direction = rule.pop('direction')
108 client.create_security_group_rule(
109 direction=direction,
110 security_group_id=secgroup_id,
111 **rule)
112
113 @classmethod
114 def create_loginable_secgroup_rule(cls, secgroup_id=None):
115 """This rule is intended to permit inbound ssh
116
117 Allowing ssh traffic traffic from all sources, so no group_id is
118 provided.
119 Setting a group_id would only permit traffic from ports
120 belonging to the same security group.
121 """
122
123 rule_list = [{'protocol': 'tcp',
124 'direction': 'ingress',
125 'port_range_min': 22,
126 'port_range_max': 22,
127 'remote_ip_prefix': '0.0.0.0/0'}]
128 cls.create_secgroup_rules(rule_list, secgroup_id=secgroup_id)
Itzik Browne67ebb52016-05-15 05:34:41 +0000129
130 @classmethod
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300131 def create_router_by_client(cls, is_admin=False, **kwargs):
132 kwargs.update({'router_name': data_utils.rand_name('router'),
133 'admin_state_up': True,
134 'external_network_id': CONF.network.public_network_id})
135 if not is_admin:
136 router = cls.create_router(**kwargs)
137 else:
138 router = cls.create_admin_router(**kwargs)
Alex Stafeyevc4d9c352016-12-12 04:13:33 -0500139 LOG.debug("Created router %s", router['name'])
Itzik Browne67ebb52016-05-15 05:34:41 +0000140 cls.routers.append(router)
141 return router
142
143 @classmethod
144 def create_and_associate_floatingip(cls, port_id):
145 fip = cls.manager.network_client.create_floatingip(
146 CONF.network.public_network_id,
147 port_id=port_id)['floatingip']
148 cls.floating_ips.append(fip)
149 return fip
150
151 @classmethod
152 def check_connectivity(cls, host, ssh_user, ssh_key=None):
153 ssh_client = ssh.Client(host, ssh_user, pkey=ssh_key)
154 ssh_client.test_connection_auth()
Itzik Brown1ef813a2016-06-06 12:56:21 +0000155
156 @classmethod
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300157 def setup_network_and_server(cls, router=None, **kwargs):
158 """Create network resources and a server.
Itzik Brownbac51dc2016-10-31 12:25:04 +0000159
160 Creating a network, subnet, router, keypair, security group
161 and a server.
162 """
Itzik Brown1ef813a2016-06-06 12:56:21 +0000163 cls.network = cls.create_network()
Alex Stafeyevc4d9c352016-12-12 04:13:33 -0500164 LOG.debug("Created network %s", cls.network['name'])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000165 cls.subnet = cls.create_subnet(cls.network)
Alex Stafeyevc4d9c352016-12-12 04:13:33 -0500166 LOG.debug("Created subnet %s", cls.subnet['id'])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000167
Itzik Brownbac51dc2016-10-31 12:25:04 +0000168 secgroup = cls.manager.network_client.create_security_group(
169 name=data_utils.rand_name('secgroup-'))
Alex Stafeyevc4d9c352016-12-12 04:13:33 -0500170 LOG.debug("Created security group %s",
171 secgroup['security_group']['name'])
Itzik Brownbac51dc2016-10-31 12:25:04 +0000172 cls.security_groups.append(secgroup['security_group'])
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300173 if not router:
174 router = cls.create_router_by_client(**kwargs)
175 cls.create_router_interface(router['id'], cls.subnet['id'])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000176 cls.keypair = cls.create_keypair()
Itzik Brownbac51dc2016-10-31 12:25:04 +0000177 cls.create_loginable_secgroup_rule(
178 secgroup_id=secgroup['security_group']['id'])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000179 cls.server = cls.create_server(
180 flavor_ref=CONF.compute.flavor_ref,
181 image_ref=CONF.compute.image_ref,
182 key_name=cls.keypair['name'],
Itzik Brownbac51dc2016-10-31 12:25:04 +0000183 networks=[{'uuid': cls.network['id']}],
184 security_groups=[{'name': secgroup['security_group']['name']}])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000185 waiters.wait_for_server_status(cls.manager.servers_client,
186 cls.server['server']['id'],
187 constants.SERVER_STATUS_ACTIVE)
188 port = cls.client.list_ports(network_id=cls.network['id'],
189 device_id=cls.server[
190 'server']['id'])['ports'][0]
191 cls.fip = cls.create_and_associate_floatingip(port['id'])