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