Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 1 | # 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 Libosvar | 6d397d3 | 2016-12-30 10:57:52 -0500 | [diff] [blame] | 15 | import netaddr |
Jakub Libosvar | 6d397d3 | 2016-12-30 10:57:52 -0500 | [diff] [blame] | 16 | from tempest.common.utils.linux import remote_client |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 17 | from tempest.common import waiters |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 18 | from tempest.lib.common.utils import data_utils |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 19 | from tempest import test |
| 20 | |
| 21 | from neutron.common import utils |
| 22 | from neutron.tests.tempest import config |
| 23 | from neutron.tests.tempest.scenario import base |
| 24 | from neutron.tests.tempest.scenario import constants |
| 25 | |
| 26 | CONF = config.CONF |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 27 | |
Jakub Libosvar | 6d397d3 | 2016-12-30 10:57:52 -0500 | [diff] [blame] | 28 | CONFIGURE_VLAN_INTERFACE_COMMANDS = ( |
| 29 | 'IFACE=$(ip l | grep "^[0-9]*: e" | cut -d \: -f 2) && ' |
| 30 | 'sudo su -c ' |
| 31 | '"ip l a link $IFACE name $IFACE.%(tag)d type vlan id %(tag)d && ' |
| 32 | 'ip l s up dev $IFACE.%(tag)d && ' |
| 33 | 'dhclient $IFACE.%(tag)d"') |
| 34 | |
| 35 | |
| 36 | def get_next_subnet(cidr): |
| 37 | return netaddr.IPNetwork(cidr).next() |
| 38 | |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 39 | |
| 40 | class TrunkTest(base.BaseTempestTestCase): |
| 41 | credentials = ['primary'] |
| 42 | force_tenant_isolation = False |
| 43 | |
| 44 | @classmethod |
| 45 | @test.requires_ext(extension="trunk", service="network") |
| 46 | def resource_setup(cls): |
| 47 | super(TrunkTest, cls).resource_setup() |
| 48 | # setup basic topology for servers we can log into |
| 49 | cls.network = cls.create_network() |
| 50 | cls.subnet = cls.create_subnet(cls.network) |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 51 | router = cls.create_router_by_client() |
| 52 | cls.create_router_interface(router['id'], cls.subnet['id']) |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 53 | cls.keypair = cls.create_keypair() |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 54 | cls.secgroup = cls.manager.network_client.create_security_group( |
| 55 | name=data_utils.rand_name('secgroup-')) |
| 56 | cls.security_groups.append(cls.secgroup['security_group']) |
| 57 | cls.create_loginable_secgroup_rule( |
| 58 | secgroup_id=cls.secgroup['security_group']['id']) |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 59 | |
| 60 | def _create_server_with_trunk_port(self): |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 61 | port = self.create_port(self.network, security_groups=[ |
| 62 | self.secgroup['security_group']['id']]) |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 63 | trunk = self.client.create_trunk(port['id'], subports=[])['trunk'] |
Jakub Libosvar | 6d397d3 | 2016-12-30 10:57:52 -0500 | [diff] [blame] | 64 | server, fip = self._create_server_with_fip(port['id']) |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 65 | self.addCleanup(self._detach_and_delete_trunk, server, trunk) |
| 66 | return {'port': port, 'trunk': trunk, 'fip': fip, |
| 67 | 'server': server} |
| 68 | |
Jakub Libosvar | 6d397d3 | 2016-12-30 10:57:52 -0500 | [diff] [blame] | 69 | def _create_server_with_fip(self, port_id, **server_kwargs): |
| 70 | fip = self.create_and_associate_floatingip(port_id) |
| 71 | return ( |
| 72 | self.create_server( |
| 73 | flavor_ref=CONF.compute.flavor_ref, |
| 74 | image_ref=CONF.compute.image_ref, |
| 75 | key_name=self.keypair['name'], |
| 76 | networks=[{'port': port_id}], |
| 77 | security_groups=[{'name': self.secgroup[ |
| 78 | 'security_group']['name']}], |
| 79 | **server_kwargs)['server'], |
| 80 | fip) |
| 81 | |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 82 | def _detach_and_delete_trunk(self, server, trunk): |
| 83 | # we have to detach the interface from the server before |
| 84 | # the trunk can be deleted. |
| 85 | self.manager.compute.InterfacesClient().delete_interface( |
| 86 | server['id'], trunk['port_id']) |
| 87 | |
| 88 | def is_port_detached(): |
| 89 | p = self.client.show_port(trunk['port_id'])['port'] |
| 90 | return p['device_id'] == '' |
| 91 | utils.wait_until_true(is_port_detached) |
| 92 | self.client.delete_trunk(trunk['id']) |
| 93 | |
| 94 | def _is_port_down(self, port_id): |
| 95 | p = self.client.show_port(port_id)['port'] |
| 96 | return p['status'] == 'DOWN' |
| 97 | |
| 98 | def _is_port_active(self, port_id): |
| 99 | p = self.client.show_port(port_id)['port'] |
| 100 | return p['status'] == 'ACTIVE' |
| 101 | |
| 102 | def _is_trunk_active(self, trunk_id): |
| 103 | t = self.client.show_trunk(trunk_id)['trunk'] |
| 104 | return t['status'] == 'ACTIVE' |
| 105 | |
Jakub Libosvar | 6d397d3 | 2016-12-30 10:57:52 -0500 | [diff] [blame] | 106 | def _create_server_with_port_and_subport(self, vlan_network, vlan_tag): |
| 107 | parent_port = self.create_port(self.network, security_groups=[ |
| 108 | self.secgroup['security_group']['id']]) |
| 109 | port_for_subport = self.create_port( |
| 110 | vlan_network, |
| 111 | security_groups=[self.secgroup['security_group']['id']], |
| 112 | mac_address=parent_port['mac_address']) |
| 113 | subport = { |
| 114 | 'port_id': port_for_subport['id'], |
| 115 | 'segmentation_type': 'vlan', |
| 116 | 'segmentation_id': vlan_tag} |
| 117 | trunk = self.client.create_trunk( |
| 118 | parent_port['id'], subports=[subport])['trunk'] |
| 119 | |
| 120 | server, fip = self._create_server_with_fip(parent_port['id']) |
| 121 | self.addCleanup(self._detach_and_delete_trunk, server, trunk) |
| 122 | |
| 123 | server_ssh_client = remote_client.RemoteClient( |
| 124 | fip['floating_ip_address'], |
| 125 | CONF.validation.image_ssh_user, |
| 126 | pkey=self.keypair['private_key'], |
| 127 | server=server) |
| 128 | |
| 129 | return { |
| 130 | 'server': server, |
| 131 | 'fip': fip, |
| 132 | 'ssh_client': server_ssh_client, |
| 133 | 'subport': port_for_subport, |
| 134 | } |
| 135 | |
| 136 | def _wait_for_server(self, server): |
| 137 | waiters.wait_for_server_status(self.manager.servers_client, |
| 138 | server['server']['id'], |
| 139 | constants.SERVER_STATUS_ACTIVE) |
| 140 | self.check_connectivity(server['fip']['floating_ip_address'], |
| 141 | CONF.validation.image_ssh_user, |
| 142 | self.keypair['private_key']) |
| 143 | |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 144 | @test.idempotent_id('bb13fe28-f152-4000-8131-37890a40c79e') |
| 145 | def test_trunk_subport_lifecycle(self): |
| 146 | """Test trunk creation and subport transition to ACTIVE status. |
| 147 | |
| 148 | This is a basic test for the trunk extension to ensure that we |
| 149 | can create a trunk, attach it to a server, add/remove subports, |
| 150 | while ensuring the status transitions as appropriate. |
| 151 | |
| 152 | This test does not assert any dataplane behavior for the subports. |
| 153 | It's just a high-level check to ensure the agents claim to have |
| 154 | wired the port correctly and that the trunk port itself maintains |
| 155 | connectivity. |
| 156 | """ |
| 157 | server1 = self._create_server_with_trunk_port() |
| 158 | server2 = self._create_server_with_trunk_port() |
| 159 | for server in (server1, server2): |
Jakub Libosvar | 6d397d3 | 2016-12-30 10:57:52 -0500 | [diff] [blame] | 160 | self._wait_for_server(server) |
Kevin Benton | a305d59 | 2016-09-19 04:26:10 -0700 | [diff] [blame] | 161 | trunk1_id, trunk2_id = server1['trunk']['id'], server2['trunk']['id'] |
| 162 | # trunks should transition to ACTIVE without any subports |
| 163 | utils.wait_until_true( |
| 164 | lambda: self._is_trunk_active(trunk1_id), |
| 165 | exception=RuntimeError("Timed out waiting for trunk %s to " |
| 166 | "transition to ACTIVE." % trunk1_id)) |
| 167 | utils.wait_until_true( |
| 168 | lambda: self._is_trunk_active(trunk2_id), |
| 169 | exception=RuntimeError("Timed out waiting for trunk %s to " |
| 170 | "transition to ACTIVE." % trunk2_id)) |
| 171 | # create a few more networks and ports for subports |
| 172 | subports = [{'port_id': self.create_port(self.create_network())['id'], |
| 173 | 'segmentation_type': 'vlan', 'segmentation_id': seg_id} |
| 174 | for seg_id in range(3, 7)] |
| 175 | # add all subports to server1 |
| 176 | self.client.add_subports(trunk1_id, subports) |
| 177 | # ensure trunk transitions to ACTIVE |
| 178 | utils.wait_until_true( |
| 179 | lambda: self._is_trunk_active(trunk1_id), |
| 180 | exception=RuntimeError("Timed out waiting for trunk %s to " |
| 181 | "transition to ACTIVE." % trunk1_id)) |
| 182 | # ensure all underlying subports transitioned to ACTIVE |
| 183 | for s in subports: |
| 184 | utils.wait_until_true(lambda: self._is_port_active(s['port_id'])) |
| 185 | # ensure main dataplane wasn't interrupted |
| 186 | self.check_connectivity(server1['fip']['floating_ip_address'], |
| 187 | CONF.validation.image_ssh_user, |
| 188 | self.keypair['private_key']) |
| 189 | # move subports over to other server |
| 190 | self.client.remove_subports(trunk1_id, subports) |
| 191 | # ensure all subports go down |
| 192 | for s in subports: |
| 193 | utils.wait_until_true( |
| 194 | lambda: self._is_port_down(s['port_id']), |
| 195 | exception=RuntimeError("Timed out waiting for subport %s to " |
| 196 | "transition to DOWN." % s['port_id'])) |
| 197 | self.client.add_subports(trunk2_id, subports) |
| 198 | # wait for both trunks to go back to ACTIVE |
| 199 | utils.wait_until_true( |
| 200 | lambda: self._is_trunk_active(trunk1_id), |
| 201 | exception=RuntimeError("Timed out waiting for trunk %s to " |
| 202 | "transition to ACTIVE." % trunk1_id)) |
| 203 | utils.wait_until_true( |
| 204 | lambda: self._is_trunk_active(trunk2_id), |
| 205 | exception=RuntimeError("Timed out waiting for trunk %s to " |
| 206 | "transition to ACTIVE." % trunk2_id)) |
| 207 | # ensure subports come up on other trunk |
| 208 | for s in subports: |
| 209 | utils.wait_until_true( |
| 210 | lambda: self._is_port_active(s['port_id']), |
| 211 | exception=RuntimeError("Timed out waiting for subport %s to " |
| 212 | "transition to ACTIVE." % s['port_id'])) |
| 213 | # final connectivity check |
| 214 | self.check_connectivity(server1['fip']['floating_ip_address'], |
| 215 | CONF.validation.image_ssh_user, |
| 216 | self.keypair['private_key']) |
| 217 | self.check_connectivity(server2['fip']['floating_ip_address'], |
| 218 | CONF.validation.image_ssh_user, |
| 219 | self.keypair['private_key']) |
Jakub Libosvar | 6d397d3 | 2016-12-30 10:57:52 -0500 | [diff] [blame] | 220 | |
| 221 | @test.idempotent_id('a8a02c9b-b453-49b5-89a2-cce7da66aafb') |
| 222 | def test_subport_connectivity(self): |
| 223 | vlan_tag = 10 |
| 224 | |
| 225 | vlan_network = self.create_network() |
| 226 | new_subnet_cidr = get_next_subnet( |
| 227 | config.safe_get_config_value('network', 'project_network_cidr')) |
| 228 | self.create_subnet(vlan_network, cidr=new_subnet_cidr) |
| 229 | |
| 230 | servers = [ |
| 231 | self._create_server_with_port_and_subport(vlan_network, vlan_tag) |
| 232 | for i in range(2)] |
| 233 | |
| 234 | for server in servers: |
| 235 | self._wait_for_server(server) |
| 236 | # Configure VLAN interfaces on server |
| 237 | command = CONFIGURE_VLAN_INTERFACE_COMMANDS % {'tag': vlan_tag} |
| 238 | server['ssh_client'].exec_command(command) |
| 239 | |
| 240 | # Ping from server1 to server2 via VLAN interface |
| 241 | servers[0]['ssh_client'].ping_host( |
| 242 | servers[1]['subport']['fixed_ips'][0]['ip_address']) |