blob: 5d775ef351c205207a7278c08e949fa543fdefc4 [file] [log] [blame]
Kevin Bentona305d592016-09-19 04:26:10 -07001# All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
Jakub Libosvar6d397d32016-12-30 10:57:52 -050015import netaddr
Kevin Benton07c90562017-02-27 01:53:16 -080016from oslo_log import log as logging
Kevin Bentona305d592016-09-19 04:26:10 -070017from tempest.common import waiters
Itzik Brownbac51dc2016-10-31 12:25:04 +000018from tempest.lib.common.utils import data_utils
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +000019from tempest.lib import decorators
Kevin Bentona305d592016-09-19 04:26:10 -070020from tempest import test
21
22from neutron.common import utils
Jakub Libosvar7c58cb22017-05-03 09:00:14 +000023from neutron.tests.tempest.common import ssh
Kevin Bentona305d592016-09-19 04:26:10 -070024from neutron.tests.tempest import config
25from neutron.tests.tempest.scenario import base
26from neutron.tests.tempest.scenario import constants
27
Kevin Benton07c90562017-02-27 01:53:16 -080028LOG = logging.getLogger(__name__)
Kevin Bentona305d592016-09-19 04:26:10 -070029CONF = config.CONF
Kevin Bentona305d592016-09-19 04:26:10 -070030
Jakub Libosvar6d397d32016-12-30 10:57:52 -050031CONFIGURE_VLAN_INTERFACE_COMMANDS = (
AlexSTafeyev200b7672017-03-28 12:07:59 +030032 'IFACE=$(PATH=$PATH:/usr/sbin ip l | grep "^[0-9]*: e" |'
33 'cut -d \: -f 2) && '
Jakub Libosvar6d397d32016-12-30 10:57:52 -050034 'sudo su -c '
AlexSTafeyev200b7672017-03-28 12:07:59 +030035 '"ip l a link $IFACE name $IFACE.%(tag)d type vlan id %(tag)d &&'
Jakub Libosvar6d397d32016-12-30 10:57:52 -050036 'ip l s up dev $IFACE.%(tag)d && '
37 'dhclient $IFACE.%(tag)d"')
38
39
40def get_next_subnet(cidr):
41 return netaddr.IPNetwork(cidr).next()
42
Kevin Bentona305d592016-09-19 04:26:10 -070043
44class TrunkTest(base.BaseTempestTestCase):
45 credentials = ['primary']
46 force_tenant_isolation = False
47
48 @classmethod
49 @test.requires_ext(extension="trunk", service="network")
50 def resource_setup(cls):
51 super(TrunkTest, cls).resource_setup()
52 # setup basic topology for servers we can log into
53 cls.network = cls.create_network()
54 cls.subnet = cls.create_subnet(cls.network)
Genadi Chereshnyac0411e92016-07-11 16:59:42 +030055 router = cls.create_router_by_client()
56 cls.create_router_interface(router['id'], cls.subnet['id'])
Kevin Bentona305d592016-09-19 04:26:10 -070057 cls.keypair = cls.create_keypair()
rajat294495c042017-06-28 15:37:16 +053058 cls.secgroup = cls.os_primary.network_client.create_security_group(
Itzik Brownbac51dc2016-10-31 12:25:04 +000059 name=data_utils.rand_name('secgroup-'))
60 cls.security_groups.append(cls.secgroup['security_group'])
61 cls.create_loginable_secgroup_rule(
62 secgroup_id=cls.secgroup['security_group']['id'])
Kevin Bentona305d592016-09-19 04:26:10 -070063
64 def _create_server_with_trunk_port(self):
Itzik Brownbac51dc2016-10-31 12:25:04 +000065 port = self.create_port(self.network, security_groups=[
66 self.secgroup['security_group']['id']])
Kevin Bentona305d592016-09-19 04:26:10 -070067 trunk = self.client.create_trunk(port['id'], subports=[])['trunk']
Jakub Libosvar6d397d32016-12-30 10:57:52 -050068 server, fip = self._create_server_with_fip(port['id'])
Kevin Bentona305d592016-09-19 04:26:10 -070069 self.addCleanup(self._detach_and_delete_trunk, server, trunk)
70 return {'port': port, 'trunk': trunk, 'fip': fip,
71 'server': server}
72
Jakub Libosvar6d397d32016-12-30 10:57:52 -050073 def _create_server_with_fip(self, port_id, **server_kwargs):
74 fip = self.create_and_associate_floatingip(port_id)
75 return (
76 self.create_server(
77 flavor_ref=CONF.compute.flavor_ref,
78 image_ref=CONF.compute.image_ref,
79 key_name=self.keypair['name'],
80 networks=[{'port': port_id}],
81 security_groups=[{'name': self.secgroup[
82 'security_group']['name']}],
83 **server_kwargs)['server'],
84 fip)
85
Kevin Bentona305d592016-09-19 04:26:10 -070086 def _detach_and_delete_trunk(self, server, trunk):
87 # we have to detach the interface from the server before
88 # the trunk can be deleted.
Brian Haleyf86ac2e2017-06-21 10:43:50 -040089 self.os_primary.compute.InterfacesClient().delete_interface(
Kevin Bentona305d592016-09-19 04:26:10 -070090 server['id'], trunk['port_id'])
91
92 def is_port_detached():
93 p = self.client.show_port(trunk['port_id'])['port']
94 return p['device_id'] == ''
95 utils.wait_until_true(is_port_detached)
96 self.client.delete_trunk(trunk['id'])
97
98 def _is_port_down(self, port_id):
99 p = self.client.show_port(port_id)['port']
100 return p['status'] == 'DOWN'
101
102 def _is_port_active(self, port_id):
103 p = self.client.show_port(port_id)['port']
104 return p['status'] == 'ACTIVE'
105
106 def _is_trunk_active(self, trunk_id):
107 t = self.client.show_trunk(trunk_id)['trunk']
108 return t['status'] == 'ACTIVE'
109
Jakub Libosvar6d397d32016-12-30 10:57:52 -0500110 def _create_server_with_port_and_subport(self, vlan_network, vlan_tag):
111 parent_port = self.create_port(self.network, security_groups=[
112 self.secgroup['security_group']['id']])
113 port_for_subport = self.create_port(
114 vlan_network,
115 security_groups=[self.secgroup['security_group']['id']],
116 mac_address=parent_port['mac_address'])
117 subport = {
118 'port_id': port_for_subport['id'],
119 'segmentation_type': 'vlan',
120 'segmentation_id': vlan_tag}
121 trunk = self.client.create_trunk(
122 parent_port['id'], subports=[subport])['trunk']
123
124 server, fip = self._create_server_with_fip(parent_port['id'])
125 self.addCleanup(self._detach_and_delete_trunk, server, trunk)
126
Kevin Benton6f1f9d52017-03-01 09:14:45 -0800127 server_ssh_client = ssh.Client(
Jakub Libosvar6d397d32016-12-30 10:57:52 -0500128 fip['floating_ip_address'],
129 CONF.validation.image_ssh_user,
Kevin Benton6f1f9d52017-03-01 09:14:45 -0800130 pkey=self.keypair['private_key'])
Jakub Libosvar6d397d32016-12-30 10:57:52 -0500131
132 return {
133 'server': server,
134 'fip': fip,
135 'ssh_client': server_ssh_client,
136 'subport': port_for_subport,
137 }
138
139 def _wait_for_server(self, server):
Brian Haleyf86ac2e2017-06-21 10:43:50 -0400140 waiters.wait_for_server_status(self.os_primary.servers_client,
Jakub Libosvar6d397d32016-12-30 10:57:52 -0500141 server['server']['id'],
142 constants.SERVER_STATUS_ACTIVE)
143 self.check_connectivity(server['fip']['floating_ip_address'],
144 CONF.validation.image_ssh_user,
145 self.keypair['private_key'])
146
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000147 @decorators.idempotent_id('bb13fe28-f152-4000-8131-37890a40c79e')
Kevin Bentona305d592016-09-19 04:26:10 -0700148 def test_trunk_subport_lifecycle(self):
149 """Test trunk creation and subport transition to ACTIVE status.
150
151 This is a basic test for the trunk extension to ensure that we
152 can create a trunk, attach it to a server, add/remove subports,
153 while ensuring the status transitions as appropriate.
154
155 This test does not assert any dataplane behavior for the subports.
156 It's just a high-level check to ensure the agents claim to have
157 wired the port correctly and that the trunk port itself maintains
158 connectivity.
159 """
160 server1 = self._create_server_with_trunk_port()
161 server2 = self._create_server_with_trunk_port()
162 for server in (server1, server2):
Jakub Libosvar6d397d32016-12-30 10:57:52 -0500163 self._wait_for_server(server)
Kevin Bentona305d592016-09-19 04:26:10 -0700164 trunk1_id, trunk2_id = server1['trunk']['id'], server2['trunk']['id']
165 # trunks should transition to ACTIVE without any subports
166 utils.wait_until_true(
167 lambda: self._is_trunk_active(trunk1_id),
168 exception=RuntimeError("Timed out waiting for trunk %s to "
169 "transition to ACTIVE." % trunk1_id))
170 utils.wait_until_true(
171 lambda: self._is_trunk_active(trunk2_id),
172 exception=RuntimeError("Timed out waiting for trunk %s to "
173 "transition to ACTIVE." % trunk2_id))
174 # create a few more networks and ports for subports
175 subports = [{'port_id': self.create_port(self.create_network())['id'],
176 'segmentation_type': 'vlan', 'segmentation_id': seg_id}
177 for seg_id in range(3, 7)]
178 # add all subports to server1
179 self.client.add_subports(trunk1_id, subports)
180 # ensure trunk transitions to ACTIVE
181 utils.wait_until_true(
182 lambda: self._is_trunk_active(trunk1_id),
183 exception=RuntimeError("Timed out waiting for trunk %s to "
184 "transition to ACTIVE." % trunk1_id))
185 # ensure all underlying subports transitioned to ACTIVE
186 for s in subports:
187 utils.wait_until_true(lambda: self._is_port_active(s['port_id']))
188 # ensure main dataplane wasn't interrupted
189 self.check_connectivity(server1['fip']['floating_ip_address'],
190 CONF.validation.image_ssh_user,
191 self.keypair['private_key'])
192 # move subports over to other server
193 self.client.remove_subports(trunk1_id, subports)
194 # ensure all subports go down
195 for s in subports:
196 utils.wait_until_true(
197 lambda: self._is_port_down(s['port_id']),
198 exception=RuntimeError("Timed out waiting for subport %s to "
199 "transition to DOWN." % s['port_id']))
200 self.client.add_subports(trunk2_id, subports)
201 # wait for both trunks to go back to ACTIVE
202 utils.wait_until_true(
203 lambda: self._is_trunk_active(trunk1_id),
204 exception=RuntimeError("Timed out waiting for trunk %s to "
205 "transition to ACTIVE." % trunk1_id))
206 utils.wait_until_true(
207 lambda: self._is_trunk_active(trunk2_id),
208 exception=RuntimeError("Timed out waiting for trunk %s to "
209 "transition to ACTIVE." % trunk2_id))
210 # ensure subports come up on other trunk
211 for s in subports:
212 utils.wait_until_true(
213 lambda: self._is_port_active(s['port_id']),
214 exception=RuntimeError("Timed out waiting for subport %s to "
215 "transition to ACTIVE." % s['port_id']))
216 # final connectivity check
217 self.check_connectivity(server1['fip']['floating_ip_address'],
218 CONF.validation.image_ssh_user,
219 self.keypair['private_key'])
220 self.check_connectivity(server2['fip']['floating_ip_address'],
221 CONF.validation.image_ssh_user,
222 self.keypair['private_key'])
Jakub Libosvar6d397d32016-12-30 10:57:52 -0500223
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000224 @decorators.idempotent_id('a8a02c9b-b453-49b5-89a2-cce7da66aafb')
Jakub Libosvar6d397d32016-12-30 10:57:52 -0500225 def test_subport_connectivity(self):
226 vlan_tag = 10
227
228 vlan_network = self.create_network()
229 new_subnet_cidr = get_next_subnet(
230 config.safe_get_config_value('network', 'project_network_cidr'))
AlexSTafeyev5d704992017-04-23 10:59:12 +0000231 self.create_subnet(vlan_network, gateway=None, cidr=new_subnet_cidr)
Jakub Libosvar6d397d32016-12-30 10:57:52 -0500232
233 servers = [
234 self._create_server_with_port_and_subport(vlan_network, vlan_tag)
235 for i in range(2)]
236
237 for server in servers:
238 self._wait_for_server(server)
239 # Configure VLAN interfaces on server
240 command = CONFIGURE_VLAN_INTERFACE_COMMANDS % {'tag': vlan_tag}
241 server['ssh_client'].exec_command(command)
AlexSTafeyev0aa817c2017-05-08 10:57:37 +0000242 out = server['ssh_client'].exec_command(
243 'PATH=$PATH:/usr/sbin;ip addr list')
Kevin Benton07c90562017-02-27 01:53:16 -0800244 LOG.debug("Interfaces on server %s: %s", server, out)
Jakub Libosvar6d397d32016-12-30 10:57:52 -0500245
Kevin Benton6f1f9d52017-03-01 09:14:45 -0800246 # Ping from server1 to server2 via VLAN interface should fail because
247 # we haven't allowed ICMP
248 self.check_remote_connectivity(
249 servers[0]['ssh_client'],
250 servers[1]['subport']['fixed_ips'][0]['ip_address'],
251 should_succeed=False
252 )
253 # allow intra-securitygroup traffic
254 self.client.create_security_group_rule(
255 security_group_id=self.secgroup['security_group']['id'],
256 direction='ingress', ethertype='IPv4', protocol='icmp',
257 remote_group_id=self.secgroup['security_group']['id'])
258 self.check_remote_connectivity(
259 servers[0]['ssh_client'],
260 servers[1]['subport']['fixed_ips'][0]['ip_address'],
261 should_succeed=True
262 )