blob: bf85e90f5eafb201ec3bc11ac4706c006b82941c [file] [log] [blame]
Elena Ezhova1ec6e182013-12-24 17:45:59 +04001# Copyright 2014 OpenStack Foundation
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
abhishek60014726de45a5e2014-12-16 03:01:35 -080016import netaddr
Elena Ezhova1ec6e182013-12-24 17:45:59 +040017import socket
18
19from tempest.api.network import base
Neeti Dahiyade291772014-09-22 10:43:12 +053020from tempest.api.network import base_security_groups as sec_base
Santosh Kumarf0cbf9a2014-08-25 07:31:15 -070021from tempest.common import custom_matchers
Elena Ezhova1ec6e182013-12-24 17:45:59 +040022from tempest.common.utils import data_utils
23from tempest import config
24from tempest import test
25
26CONF = config.CONF
27
28
Neeti Dahiyade291772014-09-22 10:43:12 +053029class PortsTestJSON(sec_base.BaseSecGroupTest):
Elena Ezhova1ec6e182013-12-24 17:45:59 +040030 _interface = 'json'
31
Elena Ezhova5c957152014-03-26 12:01:10 +040032 """
33 Test the following operations for ports:
34
35 port create
36 port delete
37 port list
38 port show
39 port update
40 """
41
Elena Ezhova1ec6e182013-12-24 17:45:59 +040042 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +010043 def resource_setup(cls):
44 super(PortsTestJSON, cls).resource_setup()
Elena Ezhova1ec6e182013-12-24 17:45:59 +040045 cls.network = cls.create_network()
46 cls.port = cls.create_port(cls.network)
47
48 def _delete_port(self, port_id):
Rohan Kanadeeeb21642014-08-14 12:00:26 +020049 self.client.delete_port(port_id)
David Kranz34e88122014-12-11 15:24:05 -050050 body = self.client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +040051 ports_list = body['ports']
52 self.assertFalse(port_id in [n['id'] for n in ports_list])
53
54 @test.attr(type='smoke')
55 def test_create_update_delete_port(self):
56 # Verify port creation
David Kranz34e88122014-12-11 15:24:05 -050057 body = self.client.create_port(network_id=self.network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040058 port = body['port']
Dane LeBlanccbc4bc52014-03-19 16:03:23 -040059 # Schedule port deletion with verification upon test completion
60 self.addCleanup(self._delete_port, port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040061 self.assertTrue(port['admin_state_up'])
62 # Verify port update
63 new_name = "New_Port"
David Kranz34e88122014-12-11 15:24:05 -050064 body = self.client.update_port(port['id'],
65 name=new_name,
66 admin_state_up=False)
Elena Ezhova1ec6e182013-12-24 17:45:59 +040067 updated_port = body['port']
68 self.assertEqual(updated_port['name'], new_name)
69 self.assertFalse(updated_port['admin_state_up'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040070
abhishek6001472607373102014-11-25 04:39:20 -080071 def test_create_bulk_port(self):
72 network1 = self.network
73 name = data_utils.rand_name('network-')
74 network2 = self.create_network(network_name=name)
75 network_list = [network1['id'], network2['id']]
76 port_list = [{'network_id': net_id} for net_id in network_list]
David Kranz34e88122014-12-11 15:24:05 -050077 body = self.client.create_bulk_port(port_list)
abhishek6001472607373102014-11-25 04:39:20 -080078 created_ports = body['ports']
79 port1 = created_ports[0]
80 port2 = created_ports[1]
81 self.addCleanup(self._delete_port, port1['id'])
82 self.addCleanup(self._delete_port, port2['id'])
83 self.assertEqual(port1['network_id'], network1['id'])
84 self.assertEqual(port2['network_id'], network2['id'])
85 self.assertTrue(port1['admin_state_up'])
86 self.assertTrue(port2['admin_state_up'])
87
abhishek60014726de45a5e2014-12-16 03:01:35 -080088 @classmethod
89 def _get_ipaddress_from_tempest_conf(cls):
90 """Return first subnet gateway for configured CIDR """
91 if cls._ip_version == 4:
92 cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr)
93
94 elif cls._ip_version == 6:
95 cidr = netaddr.IPNetwork(CONF.network.tenant_network_v6_cidr)
96
97 return netaddr.IPAddress(cidr)
98
99 @test.attr(type='smoke')
100 def test_create_port_in_allowed_allocation_pools(self):
101 network = self.create_network()
102 net_id = network['id']
103 address = self._get_ipaddress_from_tempest_conf()
104 allocation_pools = {'allocation_pools': [{'start': str(address + 4),
105 'end': str(address + 6)}]}
Adam Gandelmane8650042015-01-16 11:36:37 -0800106 subnet = self.create_subnet(network, **allocation_pools)
107 self.addCleanup(self.client.delete_subnet, subnet['id'])
abhishek60014726de45a5e2014-12-16 03:01:35 -0800108 body = self.client.create_port(network_id=net_id)
109 self.addCleanup(self.client.delete_port, body['port']['id'])
110 port = body['port']
111 ip_address = port['fixed_ips'][0]['ip_address']
112 start_ip_address = allocation_pools['allocation_pools'][0]['start']
113 end_ip_address = allocation_pools['allocation_pools'][0]['end']
114 ip_range = netaddr.IPRange(start_ip_address, end_ip_address)
115 self.assertIn(ip_address, ip_range)
116
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400117 @test.attr(type='smoke')
118 def test_show_port(self):
119 # Verify the details of port
David Kranz34e88122014-12-11 15:24:05 -0500120 body = self.client.show_port(self.port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400121 port = body['port']
122 self.assertIn('id', port)
Santosh Kumarf0cbf9a2014-08-25 07:31:15 -0700123 # TODO(Santosh)- This is a temporary workaround to compare create_port
124 # and show_port dict elements.Remove this once extra_dhcp_opts issue
125 # gets fixed in neutron.( bug - 1365341.)
126 self.assertThat(self.port,
127 custom_matchers.MatchesDictExceptForKeys
128 (port, excluded_keys=['extra_dhcp_opts']))
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400129
130 @test.attr(type='smoke')
131 def test_show_port_fields(self):
132 # Verify specific fields of a port
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500133 fields = ['id', 'mac_address']
David Kranz34e88122014-12-11 15:24:05 -0500134 body = self.client.show_port(self.port['id'],
135 fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400136 port = body['port']
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500137 self.assertEqual(sorted(port.keys()), sorted(fields))
138 for field_name in fields:
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400139 self.assertEqual(port[field_name], self.port[field_name])
140
141 @test.attr(type='smoke')
142 def test_list_ports(self):
143 # Verify the port exists in the list of all ports
David Kranz34e88122014-12-11 15:24:05 -0500144 body = self.client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400145 ports = [port['id'] for port in body['ports']
146 if port['id'] == self.port['id']]
147 self.assertNotEmpty(ports, "Created port not found in the list")
148
149 @test.attr(type='smoke')
150 def test_port_list_filter_by_router_id(self):
151 # Create a router
152 network = self.create_network()
Adam Gandelmane8650042015-01-16 11:36:37 -0800153 self.addCleanup(self.client.delete_network, network['id'])
154 subnet = self.create_subnet(network)
155 self.addCleanup(self.client.delete_subnet, subnet['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400156 router = self.create_router(data_utils.rand_name('router-'))
Adam Gandelmane8650042015-01-16 11:36:37 -0800157 self.addCleanup(self.client.delete_router, router['id'])
David Kranz34e88122014-12-11 15:24:05 -0500158 port = self.client.create_port(network_id=network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400159 # Add router interface to port created above
David Kranz34e88122014-12-11 15:24:05 -0500160 self.client.add_router_interface_with_port_id(
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400161 router['id'], port['port']['id'])
162 self.addCleanup(self.client.remove_router_interface_with_port_id,
163 router['id'], port['port']['id'])
164 # List ports filtered by router_id
David Kranz34e88122014-12-11 15:24:05 -0500165 port_list = self.client.list_ports(device_id=router['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400166 ports = port_list['ports']
167 self.assertEqual(len(ports), 1)
168 self.assertEqual(ports[0]['id'], port['port']['id'])
169 self.assertEqual(ports[0]['device_id'], router['id'])
170
171 @test.attr(type='smoke')
172 def test_list_ports_fields(self):
173 # Verify specific fields of ports
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500174 fields = ['id', 'mac_address']
David Kranz34e88122014-12-11 15:24:05 -0500175 body = self.client.list_ports(fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400176 ports = body['ports']
177 self.assertNotEmpty(ports, "Port list returned is empty")
178 # Asserting the fields returned are correct
179 for port in ports:
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500180 self.assertEqual(sorted(fields), sorted(port.keys()))
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400181
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400182 @test.attr(type='smoke')
Neeti Dahiyade291772014-09-22 10:43:12 +0530183 def test_create_update_port_with_second_ip(self):
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400184 # Create a network with two subnets
185 network = self.create_network()
Adam Gandelmane8650042015-01-16 11:36:37 -0800186 self.addCleanup(self.client.delete_network, network['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400187 subnet_1 = self.create_subnet(network)
Adam Gandelmane8650042015-01-16 11:36:37 -0800188 self.addCleanup(self.client.delete_subnet, subnet_1['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400189 subnet_2 = self.create_subnet(network)
Adam Gandelmane8650042015-01-16 11:36:37 -0800190 self.addCleanup(self.client.delete_subnet, subnet_2['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400191 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
192 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
193
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400194 fixed_ips = fixed_ip_1 + fixed_ip_2
Neeti Dahiyade291772014-09-22 10:43:12 +0530195
196 # Create a port with multiple IP addresses
197 port = self.create_port(network,
198 fixed_ips=fixed_ips)
Adam Gandelmane8650042015-01-16 11:36:37 -0800199 self.addCleanup(self.client.delete_port, port['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400200 self.assertEqual(2, len(port['fixed_ips']))
Neeti Dahiyade291772014-09-22 10:43:12 +0530201 check_fixed_ips = [subnet_1['id'], subnet_2['id']]
202 for item in port['fixed_ips']:
203 self.assertIn(item['subnet_id'], check_fixed_ips)
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400204
205 # Update the port to return to a single IP address
206 port = self.update_port(port, fixed_ips=fixed_ip_1)
207 self.assertEqual(1, len(port['fixed_ips']))
208
Neeti Dahiyade291772014-09-22 10:43:12 +0530209 # Update the port with a second IP address from second subnet
210 port = self.update_port(port, fixed_ips=fixed_ips)
211 self.assertEqual(2, len(port['fixed_ips']))
212
Ashish Guptadba59f92014-07-16 02:24:45 -0700213 def _update_port_with_security_groups(self, security_groups_names):
Neeti Dahiyade291772014-09-22 10:43:12 +0530214 subnet_1 = self.create_subnet(self.network)
Adam Gandelmane8650042015-01-16 11:36:37 -0800215 self.addCleanup(self.client.delete_subnet, subnet_1['id'])
Neeti Dahiyade291772014-09-22 10:43:12 +0530216 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
217
Ashish Guptadba59f92014-07-16 02:24:45 -0700218 security_groups_list = list()
219 for name in security_groups_names:
David Kranz34e88122014-12-11 15:24:05 -0500220 group_create_body = self.client.create_security_group(
Ashish Guptadba59f92014-07-16 02:24:45 -0700221 name=name)
222 self.addCleanup(self.client.delete_security_group,
223 group_create_body['security_group']['id'])
224 security_groups_list.append(group_create_body['security_group']
225 ['id'])
226 # Create a port
Neeti Dahiyade291772014-09-22 10:43:12 +0530227 sec_grp_name = data_utils.rand_name('secgroup')
228 security_group = self.client.create_security_group(name=sec_grp_name)
229 self.addCleanup(self.client.delete_security_group,
230 security_group['security_group']['id'])
231 post_body = {
232 "name": data_utils.rand_name('port-'),
233 "security_groups": [security_group['security_group']['id']],
234 "network_id": self.network['id'],
235 "admin_state_up": True,
236 "fixed_ips": fixed_ip_1}
David Kranz34e88122014-12-11 15:24:05 -0500237 body = self.client.create_port(**post_body)
Ashish Guptadba59f92014-07-16 02:24:45 -0700238 self.addCleanup(self.client.delete_port, body['port']['id'])
239 port = body['port']
Neeti Dahiyade291772014-09-22 10:43:12 +0530240
Ashish Guptadba59f92014-07-16 02:24:45 -0700241 # Update the port with security groups
Neeti Dahiyade291772014-09-22 10:43:12 +0530242 subnet_2 = self.create_subnet(self.network)
243 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
244 update_body = {"name": data_utils.rand_name('port-'),
245 "admin_state_up": False,
246 "fixed_ips": fixed_ip_2,
247 "security_groups": security_groups_list}
David Kranz34e88122014-12-11 15:24:05 -0500248 body = self.client.update_port(port['id'], **update_body)
Ashish Guptadba59f92014-07-16 02:24:45 -0700249 port_show = body['port']
Neeti Dahiyade291772014-09-22 10:43:12 +0530250 # Verify the security groups and other attributes updated to port
251 exclude_keys = set(port_show).symmetric_difference(update_body)
252 exclude_keys.add('fixed_ips')
253 exclude_keys.add('security_groups')
254 self.assertThat(port_show, custom_matchers.MatchesDictExceptForKeys(
255 update_body, exclude_keys))
256 self.assertEqual(fixed_ip_2[0]['subnet_id'],
257 port_show['fixed_ips'][0]['subnet_id'])
258
Ashish Guptadba59f92014-07-16 02:24:45 -0700259 for security_group in security_groups_list:
260 self.assertIn(security_group, port_show['security_groups'])
261
262 @test.attr(type='smoke')
Neeti Dahiyade291772014-09-22 10:43:12 +0530263 def test_update_port_with_security_group_and_extra_attributes(self):
Ashish Guptadba59f92014-07-16 02:24:45 -0700264 self._update_port_with_security_groups(
265 [data_utils.rand_name('secgroup')])
266
267 @test.attr(type='smoke')
Neeti Dahiyade291772014-09-22 10:43:12 +0530268 def test_update_port_with_two_security_groups_and_extra_attributes(self):
Ashish Guptadba59f92014-07-16 02:24:45 -0700269 self._update_port_with_security_groups(
270 [data_utils.rand_name('secgroup'),
271 data_utils.rand_name('secgroup')])
272
Ashish Guptad42b6e12014-11-20 02:06:03 -0800273 @test.attr(type='smoke')
274 def test_create_show_delete_port_user_defined_mac(self):
275 # Create a port for a legal mac
David Kranz34e88122014-12-11 15:24:05 -0500276 body = self.client.create_port(network_id=self.network['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800277 old_port = body['port']
278 free_mac_address = old_port['mac_address']
279 self.client.delete_port(old_port['id'])
280 # Create a new port with user defined mac
David Kranz34e88122014-12-11 15:24:05 -0500281 body = self.client.create_port(network_id=self.network['id'],
282 mac_address=free_mac_address)
Ashish Guptad42b6e12014-11-20 02:06:03 -0800283 self.addCleanup(self.client.delete_port, body['port']['id'])
284 port = body['port']
David Kranz34e88122014-12-11 15:24:05 -0500285 body = self.client.show_port(port['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800286 show_port = body['port']
287 self.assertEqual(free_mac_address,
288 show_port['mac_address'])
289
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700290 @test.attr(type='smoke')
291 def test_create_port_with_no_securitygroups(self):
Salvatore18dd66e2014-12-31 00:13:57 +0100292 network = self.create_network()
Adam Gandelmane8650042015-01-16 11:36:37 -0800293 self.addCleanup(self.client.delete_network, network['id'])
294 subnet = self.create_subnet(network)
295 self.addCleanup(self.client.delete_subnet, subnet['id'])
Salvatore18dd66e2014-12-31 00:13:57 +0100296 port = self.create_port(network, security_groups=[])
Adam Gandelmane8650042015-01-16 11:36:37 -0800297 self.addCleanup(self.client.delete_port, port['id'])
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700298 self.assertIsNotNone(port['security_groups'])
299 self.assertEmpty(port['security_groups'])
300
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400301
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400302class PortsAdminExtendedAttrsTestJSON(base.BaseAdminNetworkTest):
303 _interface = 'json'
304
305 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +0100306 def resource_setup(cls):
307 super(PortsAdminExtendedAttrsTestJSON, cls).resource_setup()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400308 cls.identity_client = cls._get_identity_admin_client()
309 cls.tenant = cls.identity_client.get_tenant_by_name(
310 CONF.identity.tenant_name)
311 cls.network = cls.create_network()
312 cls.host_id = socket.gethostname()
313
314 @test.attr(type='smoke')
315 def test_create_port_binding_ext_attr(self):
316 post_body = {"network_id": self.network['id'],
317 "binding:host_id": self.host_id}
David Kranz34e88122014-12-11 15:24:05 -0500318 body = self.admin_client.create_port(**post_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400319 port = body['port']
320 self.addCleanup(self.admin_client.delete_port, port['id'])
321 host_id = port['binding:host_id']
322 self.assertIsNotNone(host_id)
323 self.assertEqual(self.host_id, host_id)
324
325 @test.attr(type='smoke')
326 def test_update_port_binding_ext_attr(self):
327 post_body = {"network_id": self.network['id']}
David Kranz34e88122014-12-11 15:24:05 -0500328 body = self.admin_client.create_port(**post_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400329 port = body['port']
330 self.addCleanup(self.admin_client.delete_port, port['id'])
331 update_body = {"binding:host_id": self.host_id}
David Kranz34e88122014-12-11 15:24:05 -0500332 body = self.admin_client.update_port(port['id'], **update_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400333 updated_port = body['port']
334 host_id = updated_port['binding:host_id']
335 self.assertIsNotNone(host_id)
336 self.assertEqual(self.host_id, host_id)
337
338 @test.attr(type='smoke')
339 def test_list_ports_binding_ext_attr(self):
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700340 # Create a new port
341 post_body = {"network_id": self.network['id']}
David Kranz34e88122014-12-11 15:24:05 -0500342 body = self.admin_client.create_port(**post_body)
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700343 port = body['port']
344 self.addCleanup(self.admin_client.delete_port, port['id'])
345
346 # Update the port's binding attributes so that is now 'bound'
347 # to a host
348 update_body = {"binding:host_id": self.host_id}
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200349 self.admin_client.update_port(port['id'], **update_body)
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700350
351 # List all ports, ensure new port is part of list and its binding
352 # attributes are set and accurate
David Kranz34e88122014-12-11 15:24:05 -0500353 body = self.admin_client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400354 ports_list = body['ports']
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700355 pids_list = [p['id'] for p in ports_list]
356 self.assertIn(port['id'], pids_list)
357 listed_port = [p for p in ports_list if p['id'] == port['id']]
358 self.assertEqual(1, len(listed_port),
359 'Multiple ports listed with id %s in ports listing: '
360 '%s' % (port['id'], ports_list))
361 self.assertEqual(self.host_id, listed_port[0]['binding:host_id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400362
363 @test.attr(type='smoke')
364 def test_show_port_binding_ext_attr(self):
David Kranz34e88122014-12-11 15:24:05 -0500365 body = self.admin_client.create_port(network_id=self.network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400366 port = body['port']
367 self.addCleanup(self.admin_client.delete_port, port['id'])
David Kranz34e88122014-12-11 15:24:05 -0500368 body = self.admin_client.show_port(port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400369 show_port = body['port']
370 self.assertEqual(port['binding:host_id'],
371 show_port['binding:host_id'])
372 self.assertEqual(port['binding:vif_type'],
373 show_port['binding:vif_type'])
374 self.assertEqual(port['binding:vif_details'],
375 show_port['binding:vif_details'])
376
377
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400378class PortsIpV6TestJSON(PortsTestJSON):
379 _ip_version = 6
380 _tenant_network_cidr = CONF.network.tenant_network_v6_cidr
381 _tenant_network_mask_bits = CONF.network.tenant_network_v6_mask_bits
382
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400383
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400384class PortsAdminExtendedAttrsIpV6TestJSON(PortsAdminExtendedAttrsTestJSON):
385 _ip_version = 6
386 _tenant_network_cidr = CONF.network.tenant_network_v6_cidr
387 _tenant_network_mask_bits = CONF.network.tenant_network_v6_mask_bits