blob: b253890a40d337c6c51598adabeed761836f89ee [file] [log] [blame]
YAMAMOTO Takashi25935722017-01-23 15:34:11 +09001# Copyright (c) 2017 Midokura SARL
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.
15
16import netaddr
Chandan Kumarc125fd12017-11-15 19:41:01 +053017from tempest.common import utils
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090018from tempest.common import waiters
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090019from tempest.lib.common.utils import data_utils
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +000020from tempest.lib import decorators
Ihar Hrachyshkace9c4862018-01-18 18:26:14 +000021import testscenarios
YAMAMOTO Takashi60faf4f2017-01-25 08:03:07 +090022from testscenarios.scenarios import multiply_scenarios
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090023
Itzik Browna31510f2018-01-19 11:09:48 +020024from neutron_lib import constants as lib_constants
Chandan Kumar667d3d32017-09-22 12:24:06 +053025from neutron_tempest_plugin.common import ssh
Brian Haleyba800452017-12-14 10:30:48 -050026from neutron_tempest_plugin.common import utils as common_utils
Chandan Kumar667d3d32017-09-22 12:24:06 +053027from neutron_tempest_plugin import config
28from neutron_tempest_plugin.scenario import base
29from neutron_tempest_plugin.scenario import constants
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090030
31
32CONF = config.CONF
33
34
35load_tests = testscenarios.load_tests_apply_scenarios
36
37
38class FloatingIpTestCasesMixin(object):
39 credentials = ['primary', 'admin']
40
41 @classmethod
Chandan Kumarc125fd12017-11-15 19:41:01 +053042 @utils.requires_ext(extension="router", service="network")
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090043 def resource_setup(cls):
44 super(FloatingIpTestCasesMixin, cls).resource_setup()
45 cls.network = cls.create_network()
46 cls.subnet = cls.create_subnet(cls.network)
47 cls.router = cls.create_router_by_client()
48 cls.create_router_interface(cls.router['id'], cls.subnet['id'])
49 cls.keypair = cls.create_keypair()
50
rajat294495c042017-06-28 15:37:16 +053051 cls.secgroup = cls.os_primary.network_client.create_security_group(
Chandan Kumarc125fd12017-11-15 19:41:01 +053052 name=data_utils.rand_name('secgroup'))['security_group']
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090053 cls.security_groups.append(cls.secgroup)
54 cls.create_loginable_secgroup_rule(secgroup_id=cls.secgroup['id'])
55 cls.create_pingable_secgroup_rule(secgroup_id=cls.secgroup['id'])
56
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090057 if cls.same_network:
58 cls._dest_network = cls.network
59 else:
60 cls._dest_network = cls._create_dest_network()
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090061
62 @classmethod
Itzik Browna31510f2018-01-19 11:09:48 +020063 def _get_external_gateway(cls):
64 if CONF.network.public_network_id:
65 subnets = cls.os_admin.network_client.list_subnets(
66 network_id=CONF.network.public_network_id)
67
68 for subnet in subnets['subnets']:
69 if (subnet['gateway_ip']
70 and subnet['ip_version'] == lib_constants.IP_VERSION_4):
71 return subnet['gateway_ip']
72
73 @classmethod
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090074 def _create_dest_network(cls):
75 network = cls.create_network()
76 subnet = cls.create_subnet(network,
77 cidr=netaddr.IPNetwork('10.10.0.0/24'))
78 cls.create_router_interface(cls.router['id'], subnet['id'])
79 return network
80
Genadi Chereshnya918dd0b2017-05-17 13:02:20 +000081 def _create_server(self, create_floating_ip=True, network=None):
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090082 if network is None:
Genadi Chereshnya918dd0b2017-05-17 13:02:20 +000083 network = self.network
84 port = self.create_port(network, security_groups=[self.secgroup['id']])
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090085 if create_floating_ip:
Genadi Chereshnya918dd0b2017-05-17 13:02:20 +000086 fip = self.create_and_associate_floatingip(port['id'])
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090087 else:
88 fip = None
Genadi Chereshnya918dd0b2017-05-17 13:02:20 +000089 server = self.create_server(
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090090 flavor_ref=CONF.compute.flavor_ref,
91 image_ref=CONF.compute.image_ref,
Genadi Chereshnya918dd0b2017-05-17 13:02:20 +000092 key_name=self.keypair['name'],
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090093 networks=[{'port': port['id']}])['server']
rajat294495c042017-06-28 15:37:16 +053094 waiters.wait_for_server_status(self.os_primary.servers_client,
YAMAMOTO Takashi25935722017-01-23 15:34:11 +090095 server['id'],
96 constants.SERVER_STATUS_ACTIVE)
97 return {'port': port, 'fip': fip, 'server': server}
98
99 def _test_east_west(self):
YAMAMOTO Takashi60faf4f2017-01-25 08:03:07 +0900100 # The proxy VM is used to control the source VM when it doesn't
101 # have a floating-ip.
102 if self.src_has_fip:
103 proxy = None
104 proxy_client = None
105 else:
106 proxy = self._create_server()
107 proxy_client = ssh.Client(proxy['fip']['floating_ip_address'],
108 CONF.validation.image_ssh_user,
109 pkey=self.keypair['private_key'])
110
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900111 # Source VM
YAMAMOTO Takashi60faf4f2017-01-25 08:03:07 +0900112 if self.src_has_fip:
113 src_server = self._create_server()
114 src_server_ip = src_server['fip']['floating_ip_address']
115 else:
116 src_server = self._create_server(create_floating_ip=False)
117 src_server_ip = src_server['port']['fixed_ips'][0]['ip_address']
118 ssh_client = ssh.Client(src_server_ip,
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900119 CONF.validation.image_ssh_user,
YAMAMOTO Takashi60faf4f2017-01-25 08:03:07 +0900120 pkey=self.keypair['private_key'],
121 proxy_client=proxy_client)
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900122
123 # Destination VM
124 if self.dest_has_fip:
Genadi Chereshnya918dd0b2017-05-17 13:02:20 +0000125 dest_server = self._create_server(network=self._dest_network)
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900126 else:
Genadi Chereshnya918dd0b2017-05-17 13:02:20 +0000127 dest_server = self._create_server(create_floating_ip=False,
128 network=self._dest_network)
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900129
130 # Check connectivity
131 self.check_remote_connectivity(ssh_client,
132 dest_server['port']['fixed_ips'][0]['ip_address'])
133 if self.dest_has_fip:
134 self.check_remote_connectivity(ssh_client,
135 dest_server['fip']['floating_ip_address'])
136
137
138class FloatingIpSameNetwork(FloatingIpTestCasesMixin,
139 base.BaseTempestTestCase):
YAMAMOTO Takashi60faf4f2017-01-25 08:03:07 +0900140 scenarios = multiply_scenarios([
141 ('SRC with FIP', dict(src_has_fip=True)),
142 ('SRC without FIP', dict(src_has_fip=False)),
143 ], [
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900144 ('DEST with FIP', dict(dest_has_fip=True)),
145 ('DEST without FIP', dict(dest_has_fip=False)),
YAMAMOTO Takashi60faf4f2017-01-25 08:03:07 +0900146 ])
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900147
148 same_network = True
149
Brian Haleyba800452017-12-14 10:30:48 -0500150 @common_utils.unstable_test("bug 1717302")
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000151 @decorators.idempotent_id('05c4e3b3-7319-4052-90ad-e8916436c23b')
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900152 def test_east_west(self):
153 self._test_east_west()
154
155
156class FloatingIpSeparateNetwork(FloatingIpTestCasesMixin,
157 base.BaseTempestTestCase):
YAMAMOTO Takashi60faf4f2017-01-25 08:03:07 +0900158 scenarios = multiply_scenarios([
159 ('SRC with FIP', dict(src_has_fip=True)),
160 ('SRC without FIP', dict(src_has_fip=False)),
161 ], [
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900162 ('DEST with FIP', dict(dest_has_fip=True)),
163 ('DEST without FIP', dict(dest_has_fip=False)),
YAMAMOTO Takashi60faf4f2017-01-25 08:03:07 +0900164 ])
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900165
166 same_network = False
167
Brian Haleyba800452017-12-14 10:30:48 -0500168 @common_utils.unstable_test("bug 1717302")
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000169 @decorators.idempotent_id('f18f0090-3289-4783-b956-a0f8ac511e8b')
YAMAMOTO Takashi25935722017-01-23 15:34:11 +0900170 def test_east_west(self):
171 self._test_east_west()
Itzik Browna31510f2018-01-19 11:09:48 +0200172
173
174class DefaultSnatToExternal(FloatingIpTestCasesMixin,
175 base.BaseTempestTestCase):
176 same_network = True
177
178 @decorators.idempotent_id('3d73ea1a-27c6-45a9-b0f8-04a283d9d764')
179 def test_snat_external_ip(self):
180 """Check connectivity to an external IP"""
181 gateway_external_ip = self._get_external_gateway()
182
183 if not gateway_external_ip:
184 raise self.skipTest("IPv4 gateway is not configured for public "
185 "network or public_network_id is not "
186 "configured")
187 proxy = self._create_server()
188 proxy_client = ssh.Client(proxy['fip']['floating_ip_address'],
189 CONF.validation.image_ssh_user,
190 pkey=self.keypair['private_key'])
191 src_server = self._create_server(create_floating_ip=False)
192 src_server_ip = src_server['port']['fixed_ips'][0]['ip_address']
193 ssh_client = ssh.Client(src_server_ip,
194 CONF.validation.image_ssh_user,
195 pkey=self.keypair['private_key'],
196 proxy_client=proxy_client)
197 self.check_remote_connectivity(ssh_client,
198 gateway_external_ip)