blob: 25976cea59eaa5fb0d65e610ea38b6e50210125a [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
Hongbin Lu0ac946c2017-12-04 20:50:33 +000016import ipaddress
17
Matthew Treinish01472ff2015-02-20 17:26:52 -050018import netaddr
Hongbin Lu0ac946c2017-12-04 20:50:33 +000019import six
zhuflfffec232016-09-18 17:09:11 +080020import testtools
Matthew Treinish01472ff2015-02-20 17:26:52 -050021
Neeti Dahiyade291772014-09-22 10:43:12 +053022from tempest.api.network import base_security_groups as sec_base
Santosh Kumarf0cbf9a2014-08-25 07:31:15 -070023from tempest.common import custom_matchers
Andrea Frittolicd368412017-08-14 21:37:56 +010024from tempest.common import utils
Ken'ichi Ohmichif50e4df2017-03-10 10:52:53 -080025from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080026from tempest.lib import decorators
Matthew Treinish4217a702016-10-07 17:27:11 -040027from tempest.lib import exceptions
Elena Ezhova1ec6e182013-12-24 17:45:59 +040028
Elena Ezhova1ec6e182013-12-24 17:45:59 +040029
Neeti Dahiyade291772014-09-22 10:43:12 +053030class PortsTestJSON(sec_base.BaseSecGroupTest):
Ken'ichi Ohmichie03bea92015-11-19 07:45:58 +000031 """Test the following operations for ports:
Elena Ezhova5c957152014-03-26 12:01:10 +040032
33 port create
34 port delete
35 port list
36 port show
37 port update
38 """
39
Elena Ezhova1ec6e182013-12-24 17:45:59 +040040 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +010041 def resource_setup(cls):
42 super(PortsTestJSON, cls).resource_setup()
Elena Ezhova1ec6e182013-12-24 17:45:59 +040043 cls.network = cls.create_network()
44 cls.port = cls.create_port(cls.network)
45
46 def _delete_port(self, port_id):
John Warren49c0fe52015-10-22 12:35:54 -040047 self.ports_client.delete_port(port_id)
48 body = self.ports_client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +040049 ports_list = body['ports']
50 self.assertFalse(port_id in [n['id'] for n in ports_list])
51
deepak_mouryab4fb4382018-06-04 10:11:32 +053052 def _create_subnet(self, network, gateway='',
53 cidr=None, mask_bits=None, **kwargs):
54 subnet = self.create_subnet(network, gateway, cidr, mask_bits)
55 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
56 return subnet
57
58 def _create_network(self, network_name=None, **kwargs):
59 network_name = network_name or data_utils.rand_name(
60 self.__class__.__name__)
61 network = self.networks_client.create_network(
62 name=network_name, **kwargs)['network']
63 self.addCleanup(self.networks_client.delete_network,
64 network['id'])
65 return network
66
Jordan Pittier3b46d272017-04-12 16:17:28 +020067 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080068 @decorators.idempotent_id('c72c1c0c-2193-4aca-aaa4-b1442640f51c')
Elena Ezhova1ec6e182013-12-24 17:45:59 +040069 def test_create_update_delete_port(self):
70 # Verify port creation
John Warren49c0fe52015-10-22 12:35:54 -040071 body = self.ports_client.create_port(network_id=self.network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040072 port = body['port']
Dane LeBlanccbc4bc52014-03-19 16:03:23 -040073 # Schedule port deletion with verification upon test completion
74 self.addCleanup(self._delete_port, port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040075 self.assertTrue(port['admin_state_up'])
76 # Verify port update
77 new_name = "New_Port"
John Warren49c0fe52015-10-22 12:35:54 -040078 body = self.ports_client.update_port(port['id'],
79 name=new_name,
80 admin_state_up=False)
Elena Ezhova1ec6e182013-12-24 17:45:59 +040081 updated_port = body['port']
82 self.assertEqual(updated_port['name'], new_name)
83 self.assertFalse(updated_port['admin_state_up'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040084
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080085 @decorators.idempotent_id('67f1b811-f8db-43e2-86bd-72c074d4a42c')
abhishek6001472607373102014-11-25 04:39:20 -080086 def test_create_bulk_port(self):
87 network1 = self.network
deepak_mouryab4fb4382018-06-04 10:11:32 +053088 network2 = self._create_network()
abhishek6001472607373102014-11-25 04:39:20 -080089 network_list = [network1['id'], network2['id']]
90 port_list = [{'network_id': net_id} for net_id in network_list]
Ken'ichi Ohmichi1f52fd92016-03-03 12:24:12 -080091 body = self.ports_client.create_bulk_ports(ports=port_list)
abhishek6001472607373102014-11-25 04:39:20 -080092 created_ports = body['ports']
93 port1 = created_ports[0]
94 port2 = created_ports[1]
95 self.addCleanup(self._delete_port, port1['id'])
96 self.addCleanup(self._delete_port, port2['id'])
97 self.assertEqual(port1['network_id'], network1['id'])
98 self.assertEqual(port2['network_id'], network2['id'])
99 self.assertTrue(port1['admin_state_up'])
100 self.assertTrue(port2['admin_state_up'])
101
Jordan Pittier3b46d272017-04-12 16:17:28 +0200102 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800103 @decorators.idempotent_id('0435f278-40ae-48cb-a404-b8a087bc09b1')
abhishek60014726de45a5e2014-12-16 03:01:35 -0800104 def test_create_port_in_allowed_allocation_pools(self):
deepak_mouryab4fb4382018-06-04 10:11:32 +0530105 network = self._create_network()
abhishek60014726de45a5e2014-12-16 03:01:35 -0800106 net_id = network['id']
zhuflec61bac2017-09-01 15:59:50 +0800107 address = self.cidr
108 address.prefixlen = self.mask_bits
Guillaume Chenuetbdf1d8d2015-11-09 16:10:48 +0100109 if ((address.version == 4 and address.prefixlen >= 30) or
afazekas40fcb9b2019-03-08 11:25:11 +0100110 (address.version == 6 and address.prefixlen >= 126)):
Guillaume Chenuetbdf1d8d2015-11-09 16:10:48 +0100111 msg = ("Subnet %s isn't large enough for the test" % address.cidr)
zhuflff30ede2016-12-27 10:29:13 +0800112 raise exceptions.InvalidConfiguration(msg)
Guillaume Chenuetbdf1d8d2015-11-09 16:10:48 +0100113 allocation_pools = {'allocation_pools': [{'start': str(address[2]),
114 'end': str(address[-2])}]}
deepak_mouryab4fb4382018-06-04 10:11:32 +0530115 self._create_subnet(network, cidr=address,
116 mask_bits=address.prefixlen,
117 **allocation_pools)
John Warren49c0fe52015-10-22 12:35:54 -0400118 body = self.ports_client.create_port(network_id=net_id)
119 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
abhishek60014726de45a5e2014-12-16 03:01:35 -0800120 port = body['port']
121 ip_address = port['fixed_ips'][0]['ip_address']
122 start_ip_address = allocation_pools['allocation_pools'][0]['start']
123 end_ip_address = allocation_pools['allocation_pools'][0]['end']
124 ip_range = netaddr.IPRange(start_ip_address, end_ip_address)
125 self.assertIn(ip_address, ip_range)
126
Jordan Pittier3b46d272017-04-12 16:17:28 +0200127 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800128 @decorators.idempotent_id('c9a685bd-e83f-499c-939f-9f7863ca259f')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400129 def test_show_port(self):
130 # Verify the details of port
John Warren49c0fe52015-10-22 12:35:54 -0400131 body = self.ports_client.show_port(self.port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400132 port = body['port']
133 self.assertIn('id', port)
Rafael Folco72788022016-03-24 13:02:09 +0000134 # NOTE(rfolco): created_at and updated_at may get inconsistent values
135 # due to possible delay between POST request and resource creation.
136 # TODO(rfolco): Neutron Bug #1365341 is fixed, can remove the key
137 # extra_dhcp_opts in the O release (K/L gate jobs still need it).
Santosh Kumarf0cbf9a2014-08-25 07:31:15 -0700138 self.assertThat(self.port,
139 custom_matchers.MatchesDictExceptForKeys
Rafael Folco72788022016-03-24 13:02:09 +0000140 (port, excluded_keys=['extra_dhcp_opts',
141 'created_at',
142 'updated_at']))
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400143
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800144 @decorators.idempotent_id('45fcdaf2-dab0-4c13-ac6c-fcddfb579dbd')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400145 def test_show_port_fields(self):
146 # Verify specific fields of a port
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500147 fields = ['id', 'mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400148 body = self.ports_client.show_port(self.port['id'],
149 fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400150 port = body['port']
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500151 self.assertEqual(sorted(port.keys()), sorted(fields))
152 for field_name in fields:
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400153 self.assertEqual(port[field_name], self.port[field_name])
154
Jordan Pittier3b46d272017-04-12 16:17:28 +0200155 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800156 @decorators.idempotent_id('cf95b358-3e92-4a29-a148-52445e1ac50e')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400157 def test_list_ports(self):
158 # Verify the port exists in the list of all ports
John Warren49c0fe52015-10-22 12:35:54 -0400159 body = self.ports_client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400160 ports = [port['id'] for port in body['ports']
161 if port['id'] == self.port['id']]
162 self.assertNotEmpty(ports, "Created port not found in the list")
163
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800164 @decorators.idempotent_id('e7fe260b-1e79-4dd3-86d9-bec6a7959fc5')
Arx Cruze8dfad92015-05-29 09:57:14 +0200165 def test_port_list_filter_by_ip(self):
166 # Create network and subnet
deepak_mouryab4fb4382018-06-04 10:11:32 +0530167 network = self._create_network()
168 self._create_subnet(network)
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300169 # Create two ports
John Warren49c0fe52015-10-22 12:35:54 -0400170 port_1 = self.ports_client.create_port(network_id=network['id'])
171 self.addCleanup(self.ports_client.delete_port, port_1['port']['id'])
172 port_2 = self.ports_client.create_port(network_id=network['id'])
173 self.addCleanup(self.ports_client.delete_port, port_2['port']['id'])
Arx Cruze8dfad92015-05-29 09:57:14 +0200174 # List ports filtered by fixed_ips
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300175 port_1_fixed_ip = port_1['port']['fixed_ips'][0]['ip_address']
176 fixed_ips = 'ip_address=' + port_1_fixed_ip
John Warren49c0fe52015-10-22 12:35:54 -0400177 port_list = self.ports_client.list_ports(fixed_ips=fixed_ips)
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300178 # Check that we got the desired port
Arx Cruze8dfad92015-05-29 09:57:14 +0200179 ports = port_list['ports']
Matthew Treinish1d942542015-08-25 18:16:14 -0400180 tenant_ids = set([port['tenant_id'] for port in ports])
181 self.assertEqual(len(tenant_ids), 1,
182 'Ports from multiple tenants are in the list resp')
183 port_ids = [port['id'] for port in ports]
184 fixed_ips = [port['fixed_ips'] for port in ports]
185 port_ips = []
186 for addr in fixed_ips:
187 port_ips.extend([port['ip_address'] for port in addr])
188
189 port_net_ids = [port['network_id'] for port in ports]
190 self.assertIn(port_1['port']['id'], port_ids)
191 self.assertIn(port_1_fixed_ip, port_ips)
192 self.assertIn(network['id'], port_net_ids)
Arx Cruze8dfad92015-05-29 09:57:14 +0200193
Hongbin Lu0ac946c2017-12-04 20:50:33 +0000194 @decorators.idempotent_id('79895408-85d5-460d-94e7-9531c5fd9123')
195 @testtools.skipUnless(
196 utils.is_extension_enabled('ip-substring-filtering', 'network'),
197 'ip-substring-filtering extension not enabled.')
198 def test_port_list_filter_by_ip_substr(self):
199 # Create network and subnet
deepak_mouryab4fb4382018-06-04 10:11:32 +0530200 network = self._create_network()
201 subnet = self._create_subnet(network)
Hongbin Lu0ac946c2017-12-04 20:50:33 +0000202 # Get two IP addresses
203 ip_address_1 = None
204 ip_address_2 = None
205 ip_network = ipaddress.ip_network(six.text_type(subnet['cidr']))
206 for ip in ip_network:
207 if ip == ip_network.network_address:
208 continue
209 if ip_address_1 is None:
210 ip_address_1 = six.text_type(ip)
211 else:
212 ip_address_2 = ip_address_1
213 ip_address_1 = six.text_type(ip)
214 # Make sure these two IP addresses have different substring
215 if ip_address_1[:-1] != ip_address_2[:-1]:
216 break
217
218 # Create two ports
219 fixed_ips = [{'subnet_id': subnet['id'], 'ip_address': ip_address_1}]
220 port_1 = self.ports_client.create_port(network_id=network['id'],
221 fixed_ips=fixed_ips)
222 self.addCleanup(self.ports_client.delete_port, port_1['port']['id'])
223 fixed_ips = [{'subnet_id': subnet['id'], 'ip_address': ip_address_2}]
224 port_2 = self.ports_client.create_port(network_id=network['id'],
225 fixed_ips=fixed_ips)
226 self.addCleanup(self.ports_client.delete_port, port_2['port']['id'])
227
228 # Scenario 1: List port1 (port2 is filtered out)
229 if ip_address_1[:-1] != ip_address_2[:-1]:
230 ips_filter = 'ip_address_substr=' + ip_address_1[:-1]
231 else:
232 ips_filter = 'ip_address_substr=' + ip_address_1
233 ports = self.ports_client.list_ports(fixed_ips=ips_filter)['ports']
234 # Check that we got the desired port
235 port_ids = [port['id'] for port in ports]
236 fixed_ips = [port['fixed_ips'] for port in ports]
237 port_ips = []
238 for addr in fixed_ips:
239 port_ips.extend([a['ip_address'] for a in addr])
240
241 port_net_ids = [port['network_id'] for port in ports]
242 self.assertIn(network['id'], port_net_ids)
243 self.assertIn(port_1['port']['id'], port_ids)
244 self.assertIn(port_1['port']['fixed_ips'][0]['ip_address'], port_ips)
245 self.assertNotIn(port_2['port']['id'], port_ids)
246 self.assertNotIn(
247 port_2['port']['fixed_ips'][0]['ip_address'], port_ips)
248
249 # Scenario 2: List both port1 and port2
250 substr = ip_address_1
251 while substr not in ip_address_2:
252 substr = substr[:-1]
253 ips_filter = 'ip_address_substr=' + substr
254 ports = self.ports_client.list_ports(fixed_ips=ips_filter)['ports']
255 # Check that we got both port
256 port_ids = [port['id'] for port in ports]
257 fixed_ips = [port['fixed_ips'] for port in ports]
258 port_ips = []
259 for addr in fixed_ips:
260 port_ips.extend([a['ip_address'] for a in addr])
261
262 port_net_ids = [port['network_id'] for port in ports]
263 self.assertIn(network['id'], port_net_ids)
264 self.assertIn(port_1['port']['id'], port_ids)
265 self.assertIn(port_1['port']['fixed_ips'][0]['ip_address'], port_ips)
266 self.assertIn(port_2['port']['id'], port_ids)
267 self.assertIn(port_2['port']['fixed_ips'][0]['ip_address'], port_ips)
268
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800269 @decorators.idempotent_id('5ad01ed0-0e6e-4c5d-8194-232801b15c72')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400270 def test_port_list_filter_by_router_id(self):
271 # Create a router
deepak_mouryab4fb4382018-06-04 10:11:32 +0530272 network = self._create_network()
273 self._create_subnet(network)
zhufl39ac5682016-10-24 17:11:34 +0800274 router = self.create_router()
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000275 self.addCleanup(self.routers_client.delete_router, router['id'])
John Warren49c0fe52015-10-22 12:35:54 -0400276 port = self.ports_client.create_port(network_id=network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400277 # Add router interface to port created above
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000278 self.routers_client.add_router_interface(router['id'],
279 port_id=port['port']['id'])
280 self.addCleanup(self.routers_client.remove_router_interface,
281 router['id'], port_id=port['port']['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400282 # List ports filtered by router_id
John Warren49c0fe52015-10-22 12:35:54 -0400283 port_list = self.ports_client.list_ports(device_id=router['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400284 ports = port_list['ports']
285 self.assertEqual(len(ports), 1)
286 self.assertEqual(ports[0]['id'], port['port']['id'])
287 self.assertEqual(ports[0]['device_id'], router['id'])
288
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800289 @decorators.idempotent_id('ff7f117f-f034-4e0e-abff-ccef05c454b4')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400290 def test_list_ports_fields(self):
291 # Verify specific fields of ports
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500292 fields = ['id', 'mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400293 body = self.ports_client.list_ports(fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400294 ports = body['ports']
295 self.assertNotEmpty(ports, "Port list returned is empty")
296 # Asserting the fields returned are correct
297 for port in ports:
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500298 self.assertEqual(sorted(fields), sorted(port.keys()))
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400299
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800300 @decorators.idempotent_id('63aeadd4-3b49-427f-a3b1-19ca81f06270')
Neeti Dahiyade291772014-09-22 10:43:12 +0530301 def test_create_update_port_with_second_ip(self):
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400302 # Create a network with two subnets
deepak_mouryab4fb4382018-06-04 10:11:32 +0530303 network = self._create_network()
304 subnet_1 = self._create_subnet(network)
305 subnet_2 = self._create_subnet(network)
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400306 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
307 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
308
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400309 fixed_ips = fixed_ip_1 + fixed_ip_2
Neeti Dahiyade291772014-09-22 10:43:12 +0530310
311 # Create a port with multiple IP addresses
312 port = self.create_port(network,
313 fixed_ips=fixed_ips)
John Warren49c0fe52015-10-22 12:35:54 -0400314 self.addCleanup(self.ports_client.delete_port, port['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400315 self.assertEqual(2, len(port['fixed_ips']))
Neeti Dahiyade291772014-09-22 10:43:12 +0530316 check_fixed_ips = [subnet_1['id'], subnet_2['id']]
317 for item in port['fixed_ips']:
318 self.assertIn(item['subnet_id'], check_fixed_ips)
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400319
320 # Update the port to return to a single IP address
321 port = self.update_port(port, fixed_ips=fixed_ip_1)
322 self.assertEqual(1, len(port['fixed_ips']))
323
Neeti Dahiyade291772014-09-22 10:43:12 +0530324 # Update the port with a second IP address from second subnet
325 port = self.update_port(port, fixed_ips=fixed_ips)
326 self.assertEqual(2, len(port['fixed_ips']))
327
Ashish Guptadba59f92014-07-16 02:24:45 -0700328 def _update_port_with_security_groups(self, security_groups_names):
deepak_mouryab4fb4382018-06-04 10:11:32 +0530329 subnet_1 = self._create_subnet(self.network)
Neeti Dahiyade291772014-09-22 10:43:12 +0530330 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
331
Ashish Guptadba59f92014-07-16 02:24:45 -0700332 security_groups_list = list()
John Warrenf9606e92015-12-10 12:12:42 -0500333 sec_grps_client = self.security_groups_client
Ashish Guptadba59f92014-07-16 02:24:45 -0700334 for name in security_groups_names:
John Warrenf9606e92015-12-10 12:12:42 -0500335 group_create_body = sec_grps_client.create_security_group(
Ashish Guptadba59f92014-07-16 02:24:45 -0700336 name=name)
John Warrenf9606e92015-12-10 12:12:42 -0500337 self.addCleanup(self.security_groups_client.delete_security_group,
Ashish Guptadba59f92014-07-16 02:24:45 -0700338 group_create_body['security_group']['id'])
339 security_groups_list.append(group_create_body['security_group']
340 ['id'])
341 # Create a port
Neeti Dahiyade291772014-09-22 10:43:12 +0530342 sec_grp_name = data_utils.rand_name('secgroup')
John Warrenf9606e92015-12-10 12:12:42 -0500343 security_group = sec_grps_client.create_security_group(
344 name=sec_grp_name)
345 self.addCleanup(self.security_groups_client.delete_security_group,
Neeti Dahiyade291772014-09-22 10:43:12 +0530346 security_group['security_group']['id'])
347 post_body = {
348 "name": data_utils.rand_name('port-'),
349 "security_groups": [security_group['security_group']['id']],
350 "network_id": self.network['id'],
351 "admin_state_up": True,
352 "fixed_ips": fixed_ip_1}
John Warren49c0fe52015-10-22 12:35:54 -0400353 body = self.ports_client.create_port(**post_body)
354 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
Ashish Guptadba59f92014-07-16 02:24:45 -0700355 port = body['port']
Neeti Dahiyade291772014-09-22 10:43:12 +0530356
Ashish Guptadba59f92014-07-16 02:24:45 -0700357 # Update the port with security groups
Neeti Dahiyade291772014-09-22 10:43:12 +0530358 subnet_2 = self.create_subnet(self.network)
359 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
360 update_body = {"name": data_utils.rand_name('port-'),
361 "admin_state_up": False,
362 "fixed_ips": fixed_ip_2,
363 "security_groups": security_groups_list}
John Warren49c0fe52015-10-22 12:35:54 -0400364 body = self.ports_client.update_port(port['id'], **update_body)
Ashish Guptadba59f92014-07-16 02:24:45 -0700365 port_show = body['port']
Neeti Dahiyade291772014-09-22 10:43:12 +0530366 # Verify the security groups and other attributes updated to port
367 exclude_keys = set(port_show).symmetric_difference(update_body)
368 exclude_keys.add('fixed_ips')
369 exclude_keys.add('security_groups')
370 self.assertThat(port_show, custom_matchers.MatchesDictExceptForKeys(
371 update_body, exclude_keys))
372 self.assertEqual(fixed_ip_2[0]['subnet_id'],
373 port_show['fixed_ips'][0]['subnet_id'])
374
Ashish Guptadba59f92014-07-16 02:24:45 -0700375 for security_group in security_groups_list:
376 self.assertIn(security_group, port_show['security_groups'])
377
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800378 @decorators.idempotent_id('58091b66-4ff4-4cc1-a549-05d60c7acd1a')
zhuflfffec232016-09-18 17:09:11 +0800379 @testtools.skipUnless(
Andrea Frittolicd368412017-08-14 21:37:56 +0100380 utils.is_extension_enabled('security-group', 'network'),
zhuflfffec232016-09-18 17:09:11 +0800381 'security-group extension not enabled.')
Neeti Dahiyade291772014-09-22 10:43:12 +0530382 def test_update_port_with_security_group_and_extra_attributes(self):
Ashish Guptadba59f92014-07-16 02:24:45 -0700383 self._update_port_with_security_groups(
384 [data_utils.rand_name('secgroup')])
385
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800386 @decorators.idempotent_id('edf6766d-3d40-4621-bc6e-2521a44c257d')
zhuflfffec232016-09-18 17:09:11 +0800387 @testtools.skipUnless(
Andrea Frittolicd368412017-08-14 21:37:56 +0100388 utils.is_extension_enabled('security-group', 'network'),
zhuflfffec232016-09-18 17:09:11 +0800389 'security-group extension not enabled.')
Neeti Dahiyade291772014-09-22 10:43:12 +0530390 def test_update_port_with_two_security_groups_and_extra_attributes(self):
Ashish Guptadba59f92014-07-16 02:24:45 -0700391 self._update_port_with_security_groups(
392 [data_utils.rand_name('secgroup'),
393 data_utils.rand_name('secgroup')])
394
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800395 @decorators.idempotent_id('13e95171-6cbd-489c-9d7c-3f9c58215c18')
Ashish Guptad42b6e12014-11-20 02:06:03 -0800396 def test_create_show_delete_port_user_defined_mac(self):
397 # Create a port for a legal mac
John Warren49c0fe52015-10-22 12:35:54 -0400398 body = self.ports_client.create_port(network_id=self.network['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800399 old_port = body['port']
400 free_mac_address = old_port['mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400401 self.ports_client.delete_port(old_port['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800402 # Create a new port with user defined mac
John Warren49c0fe52015-10-22 12:35:54 -0400403 body = self.ports_client.create_port(network_id=self.network['id'],
404 mac_address=free_mac_address)
405 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800406 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400407 body = self.ports_client.show_port(port['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800408 show_port = body['port']
409 self.assertEqual(free_mac_address,
410 show_port['mac_address'])
411
Jordan Pittier3b46d272017-04-12 16:17:28 +0200412 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800413 @decorators.idempotent_id('4179dcb9-1382-4ced-84fe-1b91c54f5735')
zhuflfffec232016-09-18 17:09:11 +0800414 @testtools.skipUnless(
Andrea Frittolicd368412017-08-14 21:37:56 +0100415 utils.is_extension_enabled('security-group', 'network'),
zhuflfffec232016-09-18 17:09:11 +0800416 'security-group extension not enabled.')
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700417 def test_create_port_with_no_securitygroups(self):
deepak_mouryab4fb4382018-06-04 10:11:32 +0530418 network = self._create_network()
419 self._create_subnet(network)
Salvatore18dd66e2014-12-31 00:13:57 +0100420 port = self.create_port(network, security_groups=[])
John Warren49c0fe52015-10-22 12:35:54 -0400421 self.addCleanup(self.ports_client.delete_port, port['id'])
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700422 self.assertIsNotNone(port['security_groups'])
423 self.assertEmpty(port['security_groups'])
424
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400425
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400426class PortsIpV6TestJSON(PortsTestJSON):
427 _ip_version = 6