blob: 56221e5383d54b5de21b6d584f5e67165d6c9808 [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
131 def create_router_and_interface(cls, subnet_id):
132 router = cls.create_router(
133 data_utils.rand_name('router'), admin_state_up=True,
134 external_network_id=CONF.network.public_network_id)
Alex Stafeyevc4d9c352016-12-12 04:13:33 -0500135 LOG.debug("Created router %s", router['name'])
Itzik Browne67ebb52016-05-15 05:34:41 +0000136 cls.create_router_interface(router['id'], subnet_id)
137 cls.routers.append(router)
138 return router
139
140 @classmethod
141 def create_and_associate_floatingip(cls, port_id):
142 fip = cls.manager.network_client.create_floatingip(
143 CONF.network.public_network_id,
144 port_id=port_id)['floatingip']
145 cls.floating_ips.append(fip)
146 return fip
147
148 @classmethod
149 def check_connectivity(cls, host, ssh_user, ssh_key=None):
150 ssh_client = ssh.Client(host, ssh_user, pkey=ssh_key)
151 ssh_client.test_connection_auth()
Itzik Brown1ef813a2016-06-06 12:56:21 +0000152
153 @classmethod
154 def setup_network_and_server(cls):
Itzik Brownbac51dc2016-10-31 12:25:04 +0000155 """Creating network resources and a server.
156
157 Creating a network, subnet, router, keypair, security group
158 and a server.
159 """
Itzik Brown1ef813a2016-06-06 12:56:21 +0000160 cls.network = cls.create_network()
Alex Stafeyevc4d9c352016-12-12 04:13:33 -0500161 LOG.debug("Created network %s", cls.network['name'])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000162 cls.subnet = cls.create_subnet(cls.network)
Alex Stafeyevc4d9c352016-12-12 04:13:33 -0500163 LOG.debug("Created subnet %s", cls.subnet['id'])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000164
Itzik Brownbac51dc2016-10-31 12:25:04 +0000165 secgroup = cls.manager.network_client.create_security_group(
166 name=data_utils.rand_name('secgroup-'))
Alex Stafeyevc4d9c352016-12-12 04:13:33 -0500167 LOG.debug("Created security group %s",
168 secgroup['security_group']['name'])
Itzik Brownbac51dc2016-10-31 12:25:04 +0000169 cls.security_groups.append(secgroup['security_group'])
170
Itzik Brown1ef813a2016-06-06 12:56:21 +0000171 cls.create_router_and_interface(cls.subnet['id'])
172 cls.keypair = cls.create_keypair()
Itzik Brownbac51dc2016-10-31 12:25:04 +0000173 cls.create_loginable_secgroup_rule(
174 secgroup_id=secgroup['security_group']['id'])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000175 cls.server = cls.create_server(
176 flavor_ref=CONF.compute.flavor_ref,
177 image_ref=CONF.compute.image_ref,
178 key_name=cls.keypair['name'],
Itzik Brownbac51dc2016-10-31 12:25:04 +0000179 networks=[{'uuid': cls.network['id']}],
180 security_groups=[{'name': secgroup['security_group']['name']}])
Itzik Brown1ef813a2016-06-06 12:56:21 +0000181 waiters.wait_for_server_status(cls.manager.servers_client,
182 cls.server['server']['id'],
183 constants.SERVER_STATUS_ACTIVE)
184 port = cls.client.list_ports(network_id=cls.network['id'],
185 device_id=cls.server[
186 'server']['id'])['ports'][0]
187 cls.fip = cls.create_and_associate_floatingip(port['id'])