blob: 516842358a2cee49ba6cfe9866220b6eb4a57273 [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
Elena Ezhova1ec6e182013-12-24 17:45:59 +040025from tempest import config
Ken'ichi Ohmichif50e4df2017-03-10 10:52:53 -080026from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080027from tempest.lib import decorators
Matthew Treinish4217a702016-10-07 17:27:11 -040028from tempest.lib import exceptions
Elena Ezhova1ec6e182013-12-24 17:45:59 +040029
30CONF = config.CONF
31
32
Neeti Dahiyade291772014-09-22 10:43:12 +053033class PortsTestJSON(sec_base.BaseSecGroupTest):
Ken'ichi Ohmichie03bea92015-11-19 07:45:58 +000034 """Test the following operations for ports:
Elena Ezhova5c957152014-03-26 12:01:10 +040035
36 port create
37 port delete
38 port list
39 port show
40 port update
41 """
42
Elena Ezhova1ec6e182013-12-24 17:45:59 +040043 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +010044 def resource_setup(cls):
45 super(PortsTestJSON, cls).resource_setup()
Elena Ezhova1ec6e182013-12-24 17:45:59 +040046 cls.network = cls.create_network()
47 cls.port = cls.create_port(cls.network)
48
49 def _delete_port(self, port_id):
John Warren49c0fe52015-10-22 12:35:54 -040050 self.ports_client.delete_port(port_id)
51 body = self.ports_client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +040052 ports_list = body['ports']
53 self.assertFalse(port_id in [n['id'] for n in ports_list])
54
Jordan Pittier3b46d272017-04-12 16:17:28 +020055 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080056 @decorators.idempotent_id('c72c1c0c-2193-4aca-aaa4-b1442640f51c')
Elena Ezhova1ec6e182013-12-24 17:45:59 +040057 def test_create_update_delete_port(self):
58 # Verify port creation
John Warren49c0fe52015-10-22 12:35:54 -040059 body = self.ports_client.create_port(network_id=self.network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040060 port = body['port']
Dane LeBlanccbc4bc52014-03-19 16:03:23 -040061 # Schedule port deletion with verification upon test completion
62 self.addCleanup(self._delete_port, port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040063 self.assertTrue(port['admin_state_up'])
64 # Verify port update
65 new_name = "New_Port"
John Warren49c0fe52015-10-22 12:35:54 -040066 body = self.ports_client.update_port(port['id'],
67 name=new_name,
68 admin_state_up=False)
Elena Ezhova1ec6e182013-12-24 17:45:59 +040069 updated_port = body['port']
70 self.assertEqual(updated_port['name'], new_name)
71 self.assertFalse(updated_port['admin_state_up'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040072
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080073 @decorators.idempotent_id('67f1b811-f8db-43e2-86bd-72c074d4a42c')
abhishek6001472607373102014-11-25 04:39:20 -080074 def test_create_bulk_port(self):
75 network1 = self.network
zhufld2c40ca2016-10-18 17:03:07 +080076 network2 = self.create_network()
abhishek6001472607373102014-11-25 04:39:20 -080077 network_list = [network1['id'], network2['id']]
78 port_list = [{'network_id': net_id} for net_id in network_list]
Ken'ichi Ohmichi1f52fd92016-03-03 12:24:12 -080079 body = self.ports_client.create_bulk_ports(ports=port_list)
abhishek6001472607373102014-11-25 04:39:20 -080080 created_ports = body['ports']
81 port1 = created_ports[0]
82 port2 = created_ports[1]
83 self.addCleanup(self._delete_port, port1['id'])
84 self.addCleanup(self._delete_port, port2['id'])
85 self.assertEqual(port1['network_id'], network1['id'])
86 self.assertEqual(port2['network_id'], network2['id'])
87 self.assertTrue(port1['admin_state_up'])
88 self.assertTrue(port2['admin_state_up'])
89
Jordan Pittier3b46d272017-04-12 16:17:28 +020090 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080091 @decorators.idempotent_id('0435f278-40ae-48cb-a404-b8a087bc09b1')
abhishek60014726de45a5e2014-12-16 03:01:35 -080092 def test_create_port_in_allowed_allocation_pools(self):
93 network = self.create_network()
94 net_id = network['id']
zhuflec61bac2017-09-01 15:59:50 +080095 address = self.cidr
96 address.prefixlen = self.mask_bits
Guillaume Chenuetbdf1d8d2015-11-09 16:10:48 +010097 if ((address.version == 4 and address.prefixlen >= 30) or
98 (address.version == 6 and address.prefixlen >= 126)):
99 msg = ("Subnet %s isn't large enough for the test" % address.cidr)
zhuflff30ede2016-12-27 10:29:13 +0800100 raise exceptions.InvalidConfiguration(msg)
Guillaume Chenuetbdf1d8d2015-11-09 16:10:48 +0100101 allocation_pools = {'allocation_pools': [{'start': str(address[2]),
102 'end': str(address[-2])}]}
103 subnet = self.create_subnet(network, cidr=address,
104 mask_bits=address.prefixlen,
105 **allocation_pools)
John Warren3961acd2015-10-02 14:38:53 -0400106 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
John Warren49c0fe52015-10-22 12:35:54 -0400107 body = self.ports_client.create_port(network_id=net_id)
108 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
abhishek60014726de45a5e2014-12-16 03:01:35 -0800109 port = body['port']
110 ip_address = port['fixed_ips'][0]['ip_address']
111 start_ip_address = allocation_pools['allocation_pools'][0]['start']
112 end_ip_address = allocation_pools['allocation_pools'][0]['end']
113 ip_range = netaddr.IPRange(start_ip_address, end_ip_address)
114 self.assertIn(ip_address, ip_range)
115
Jordan Pittier3b46d272017-04-12 16:17:28 +0200116 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800117 @decorators.idempotent_id('c9a685bd-e83f-499c-939f-9f7863ca259f')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400118 def test_show_port(self):
119 # Verify the details of port
John Warren49c0fe52015-10-22 12:35:54 -0400120 body = self.ports_client.show_port(self.port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400121 port = body['port']
122 self.assertIn('id', port)
Rafael Folco72788022016-03-24 13:02:09 +0000123 # NOTE(rfolco): created_at and updated_at may get inconsistent values
124 # due to possible delay between POST request and resource creation.
125 # TODO(rfolco): Neutron Bug #1365341 is fixed, can remove the key
126 # extra_dhcp_opts in the O release (K/L gate jobs still need it).
Santosh Kumarf0cbf9a2014-08-25 07:31:15 -0700127 self.assertThat(self.port,
128 custom_matchers.MatchesDictExceptForKeys
Rafael Folco72788022016-03-24 13:02:09 +0000129 (port, excluded_keys=['extra_dhcp_opts',
130 'created_at',
131 'updated_at']))
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400132
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800133 @decorators.idempotent_id('45fcdaf2-dab0-4c13-ac6c-fcddfb579dbd')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400134 def test_show_port_fields(self):
135 # Verify specific fields of a port
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500136 fields = ['id', 'mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400137 body = self.ports_client.show_port(self.port['id'],
138 fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400139 port = body['port']
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500140 self.assertEqual(sorted(port.keys()), sorted(fields))
141 for field_name in fields:
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400142 self.assertEqual(port[field_name], self.port[field_name])
143
Jordan Pittier3b46d272017-04-12 16:17:28 +0200144 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800145 @decorators.idempotent_id('cf95b358-3e92-4a29-a148-52445e1ac50e')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400146 def test_list_ports(self):
147 # Verify the port exists in the list of all ports
John Warren49c0fe52015-10-22 12:35:54 -0400148 body = self.ports_client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400149 ports = [port['id'] for port in body['ports']
150 if port['id'] == self.port['id']]
151 self.assertNotEmpty(ports, "Created port not found in the list")
152
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800153 @decorators.idempotent_id('e7fe260b-1e79-4dd3-86d9-bec6a7959fc5')
Arx Cruze8dfad92015-05-29 09:57:14 +0200154 def test_port_list_filter_by_ip(self):
155 # Create network and subnet
156 network = self.create_network()
157 subnet = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400158 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300159 # Create two ports
John Warren49c0fe52015-10-22 12:35:54 -0400160 port_1 = self.ports_client.create_port(network_id=network['id'])
161 self.addCleanup(self.ports_client.delete_port, port_1['port']['id'])
162 port_2 = self.ports_client.create_port(network_id=network['id'])
163 self.addCleanup(self.ports_client.delete_port, port_2['port']['id'])
Arx Cruze8dfad92015-05-29 09:57:14 +0200164 # List ports filtered by fixed_ips
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300165 port_1_fixed_ip = port_1['port']['fixed_ips'][0]['ip_address']
166 fixed_ips = 'ip_address=' + port_1_fixed_ip
John Warren49c0fe52015-10-22 12:35:54 -0400167 port_list = self.ports_client.list_ports(fixed_ips=fixed_ips)
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300168 # Check that we got the desired port
Arx Cruze8dfad92015-05-29 09:57:14 +0200169 ports = port_list['ports']
Matthew Treinish1d942542015-08-25 18:16:14 -0400170 tenant_ids = set([port['tenant_id'] for port in ports])
171 self.assertEqual(len(tenant_ids), 1,
172 'Ports from multiple tenants are in the list resp')
173 port_ids = [port['id'] for port in ports]
174 fixed_ips = [port['fixed_ips'] for port in ports]
175 port_ips = []
176 for addr in fixed_ips:
177 port_ips.extend([port['ip_address'] for port in addr])
178
179 port_net_ids = [port['network_id'] for port in ports]
180 self.assertIn(port_1['port']['id'], port_ids)
181 self.assertIn(port_1_fixed_ip, port_ips)
182 self.assertIn(network['id'], port_net_ids)
Arx Cruze8dfad92015-05-29 09:57:14 +0200183
Hongbin Lu0ac946c2017-12-04 20:50:33 +0000184 @decorators.idempotent_id('79895408-85d5-460d-94e7-9531c5fd9123')
185 @testtools.skipUnless(
186 utils.is_extension_enabled('ip-substring-filtering', 'network'),
187 'ip-substring-filtering extension not enabled.')
188 def test_port_list_filter_by_ip_substr(self):
189 # Create network and subnet
190 network = self.create_network()
191 subnet = self.create_subnet(network)
192 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
193
194 # Get two IP addresses
195 ip_address_1 = None
196 ip_address_2 = None
197 ip_network = ipaddress.ip_network(six.text_type(subnet['cidr']))
198 for ip in ip_network:
199 if ip == ip_network.network_address:
200 continue
201 if ip_address_1 is None:
202 ip_address_1 = six.text_type(ip)
203 else:
204 ip_address_2 = ip_address_1
205 ip_address_1 = six.text_type(ip)
206 # Make sure these two IP addresses have different substring
207 if ip_address_1[:-1] != ip_address_2[:-1]:
208 break
209
210 # Create two ports
211 fixed_ips = [{'subnet_id': subnet['id'], 'ip_address': ip_address_1}]
212 port_1 = self.ports_client.create_port(network_id=network['id'],
213 fixed_ips=fixed_ips)
214 self.addCleanup(self.ports_client.delete_port, port_1['port']['id'])
215 fixed_ips = [{'subnet_id': subnet['id'], 'ip_address': ip_address_2}]
216 port_2 = self.ports_client.create_port(network_id=network['id'],
217 fixed_ips=fixed_ips)
218 self.addCleanup(self.ports_client.delete_port, port_2['port']['id'])
219
220 # Scenario 1: List port1 (port2 is filtered out)
221 if ip_address_1[:-1] != ip_address_2[:-1]:
222 ips_filter = 'ip_address_substr=' + ip_address_1[:-1]
223 else:
224 ips_filter = 'ip_address_substr=' + ip_address_1
225 ports = self.ports_client.list_ports(fixed_ips=ips_filter)['ports']
226 # Check that we got the desired port
227 port_ids = [port['id'] for port in ports]
228 fixed_ips = [port['fixed_ips'] for port in ports]
229 port_ips = []
230 for addr in fixed_ips:
231 port_ips.extend([a['ip_address'] for a in addr])
232
233 port_net_ids = [port['network_id'] for port in ports]
234 self.assertIn(network['id'], port_net_ids)
235 self.assertIn(port_1['port']['id'], port_ids)
236 self.assertIn(port_1['port']['fixed_ips'][0]['ip_address'], port_ips)
237 self.assertNotIn(port_2['port']['id'], port_ids)
238 self.assertNotIn(
239 port_2['port']['fixed_ips'][0]['ip_address'], port_ips)
240
241 # Scenario 2: List both port1 and port2
242 substr = ip_address_1
243 while substr not in ip_address_2:
244 substr = substr[:-1]
245 ips_filter = 'ip_address_substr=' + substr
246 ports = self.ports_client.list_ports(fixed_ips=ips_filter)['ports']
247 # Check that we got both port
248 port_ids = [port['id'] for port in ports]
249 fixed_ips = [port['fixed_ips'] for port in ports]
250 port_ips = []
251 for addr in fixed_ips:
252 port_ips.extend([a['ip_address'] for a in addr])
253
254 port_net_ids = [port['network_id'] for port in ports]
255 self.assertIn(network['id'], port_net_ids)
256 self.assertIn(port_1['port']['id'], port_ids)
257 self.assertIn(port_1['port']['fixed_ips'][0]['ip_address'], port_ips)
258 self.assertIn(port_2['port']['id'], port_ids)
259 self.assertIn(port_2['port']['fixed_ips'][0]['ip_address'], port_ips)
260
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800261 @decorators.idempotent_id('5ad01ed0-0e6e-4c5d-8194-232801b15c72')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400262 def test_port_list_filter_by_router_id(self):
263 # Create a router
264 network = self.create_network()
John Warren94d8faf2015-09-15 12:22:24 -0400265 self.addCleanup(self.networks_client.delete_network, network['id'])
Adam Gandelmane8650042015-01-16 11:36:37 -0800266 subnet = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400267 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
zhufl39ac5682016-10-24 17:11:34 +0800268 router = self.create_router()
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000269 self.addCleanup(self.routers_client.delete_router, router['id'])
John Warren49c0fe52015-10-22 12:35:54 -0400270 port = self.ports_client.create_port(network_id=network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400271 # Add router interface to port created above
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000272 self.routers_client.add_router_interface(router['id'],
273 port_id=port['port']['id'])
274 self.addCleanup(self.routers_client.remove_router_interface,
275 router['id'], port_id=port['port']['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400276 # List ports filtered by router_id
John Warren49c0fe52015-10-22 12:35:54 -0400277 port_list = self.ports_client.list_ports(device_id=router['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400278 ports = port_list['ports']
279 self.assertEqual(len(ports), 1)
280 self.assertEqual(ports[0]['id'], port['port']['id'])
281 self.assertEqual(ports[0]['device_id'], router['id'])
282
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800283 @decorators.idempotent_id('ff7f117f-f034-4e0e-abff-ccef05c454b4')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400284 def test_list_ports_fields(self):
285 # Verify specific fields of ports
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500286 fields = ['id', 'mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400287 body = self.ports_client.list_ports(fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400288 ports = body['ports']
289 self.assertNotEmpty(ports, "Port list returned is empty")
290 # Asserting the fields returned are correct
291 for port in ports:
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500292 self.assertEqual(sorted(fields), sorted(port.keys()))
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400293
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800294 @decorators.idempotent_id('63aeadd4-3b49-427f-a3b1-19ca81f06270')
Neeti Dahiyade291772014-09-22 10:43:12 +0530295 def test_create_update_port_with_second_ip(self):
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400296 # Create a network with two subnets
297 network = self.create_network()
John Warren94d8faf2015-09-15 12:22:24 -0400298 self.addCleanup(self.networks_client.delete_network, network['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400299 subnet_1 = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400300 self.addCleanup(self.subnets_client.delete_subnet, subnet_1['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400301 subnet_2 = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400302 self.addCleanup(self.subnets_client.delete_subnet, subnet_2['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400303 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
304 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
305
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400306 fixed_ips = fixed_ip_1 + fixed_ip_2
Neeti Dahiyade291772014-09-22 10:43:12 +0530307
308 # Create a port with multiple IP addresses
309 port = self.create_port(network,
310 fixed_ips=fixed_ips)
John Warren49c0fe52015-10-22 12:35:54 -0400311 self.addCleanup(self.ports_client.delete_port, port['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400312 self.assertEqual(2, len(port['fixed_ips']))
Neeti Dahiyade291772014-09-22 10:43:12 +0530313 check_fixed_ips = [subnet_1['id'], subnet_2['id']]
314 for item in port['fixed_ips']:
315 self.assertIn(item['subnet_id'], check_fixed_ips)
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400316
317 # Update the port to return to a single IP address
318 port = self.update_port(port, fixed_ips=fixed_ip_1)
319 self.assertEqual(1, len(port['fixed_ips']))
320
Neeti Dahiyade291772014-09-22 10:43:12 +0530321 # Update the port with a second IP address from second subnet
322 port = self.update_port(port, fixed_ips=fixed_ips)
323 self.assertEqual(2, len(port['fixed_ips']))
324
Ashish Guptadba59f92014-07-16 02:24:45 -0700325 def _update_port_with_security_groups(self, security_groups_names):
Neeti Dahiyade291772014-09-22 10:43:12 +0530326 subnet_1 = self.create_subnet(self.network)
John Warren3961acd2015-10-02 14:38:53 -0400327 self.addCleanup(self.subnets_client.delete_subnet, subnet_1['id'])
Neeti Dahiyade291772014-09-22 10:43:12 +0530328 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
329
Ashish Guptadba59f92014-07-16 02:24:45 -0700330 security_groups_list = list()
John Warrenf9606e92015-12-10 12:12:42 -0500331 sec_grps_client = self.security_groups_client
Ashish Guptadba59f92014-07-16 02:24:45 -0700332 for name in security_groups_names:
John Warrenf9606e92015-12-10 12:12:42 -0500333 group_create_body = sec_grps_client.create_security_group(
Ashish Guptadba59f92014-07-16 02:24:45 -0700334 name=name)
John Warrenf9606e92015-12-10 12:12:42 -0500335 self.addCleanup(self.security_groups_client.delete_security_group,
Ashish Guptadba59f92014-07-16 02:24:45 -0700336 group_create_body['security_group']['id'])
337 security_groups_list.append(group_create_body['security_group']
338 ['id'])
339 # Create a port
Neeti Dahiyade291772014-09-22 10:43:12 +0530340 sec_grp_name = data_utils.rand_name('secgroup')
John Warrenf9606e92015-12-10 12:12:42 -0500341 security_group = sec_grps_client.create_security_group(
342 name=sec_grp_name)
343 self.addCleanup(self.security_groups_client.delete_security_group,
Neeti Dahiyade291772014-09-22 10:43:12 +0530344 security_group['security_group']['id'])
345 post_body = {
346 "name": data_utils.rand_name('port-'),
347 "security_groups": [security_group['security_group']['id']],
348 "network_id": self.network['id'],
349 "admin_state_up": True,
350 "fixed_ips": fixed_ip_1}
John Warren49c0fe52015-10-22 12:35:54 -0400351 body = self.ports_client.create_port(**post_body)
352 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
Ashish Guptadba59f92014-07-16 02:24:45 -0700353 port = body['port']
Neeti Dahiyade291772014-09-22 10:43:12 +0530354
Ashish Guptadba59f92014-07-16 02:24:45 -0700355 # Update the port with security groups
Neeti Dahiyade291772014-09-22 10:43:12 +0530356 subnet_2 = self.create_subnet(self.network)
357 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
358 update_body = {"name": data_utils.rand_name('port-'),
359 "admin_state_up": False,
360 "fixed_ips": fixed_ip_2,
361 "security_groups": security_groups_list}
John Warren49c0fe52015-10-22 12:35:54 -0400362 body = self.ports_client.update_port(port['id'], **update_body)
Ashish Guptadba59f92014-07-16 02:24:45 -0700363 port_show = body['port']
Neeti Dahiyade291772014-09-22 10:43:12 +0530364 # Verify the security groups and other attributes updated to port
365 exclude_keys = set(port_show).symmetric_difference(update_body)
366 exclude_keys.add('fixed_ips')
367 exclude_keys.add('security_groups')
368 self.assertThat(port_show, custom_matchers.MatchesDictExceptForKeys(
369 update_body, exclude_keys))
370 self.assertEqual(fixed_ip_2[0]['subnet_id'],
371 port_show['fixed_ips'][0]['subnet_id'])
372
Ashish Guptadba59f92014-07-16 02:24:45 -0700373 for security_group in security_groups_list:
374 self.assertIn(security_group, port_show['security_groups'])
375
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800376 @decorators.idempotent_id('58091b66-4ff4-4cc1-a549-05d60c7acd1a')
zhuflfffec232016-09-18 17:09:11 +0800377 @testtools.skipUnless(
Andrea Frittolicd368412017-08-14 21:37:56 +0100378 utils.is_extension_enabled('security-group', 'network'),
zhuflfffec232016-09-18 17:09:11 +0800379 'security-group extension not enabled.')
Neeti Dahiyade291772014-09-22 10:43:12 +0530380 def test_update_port_with_security_group_and_extra_attributes(self):
Ashish Guptadba59f92014-07-16 02:24:45 -0700381 self._update_port_with_security_groups(
382 [data_utils.rand_name('secgroup')])
383
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800384 @decorators.idempotent_id('edf6766d-3d40-4621-bc6e-2521a44c257d')
zhuflfffec232016-09-18 17:09:11 +0800385 @testtools.skipUnless(
Andrea Frittolicd368412017-08-14 21:37:56 +0100386 utils.is_extension_enabled('security-group', 'network'),
zhuflfffec232016-09-18 17:09:11 +0800387 'security-group extension not enabled.')
Neeti Dahiyade291772014-09-22 10:43:12 +0530388 def test_update_port_with_two_security_groups_and_extra_attributes(self):
Ashish Guptadba59f92014-07-16 02:24:45 -0700389 self._update_port_with_security_groups(
390 [data_utils.rand_name('secgroup'),
391 data_utils.rand_name('secgroup')])
392
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800393 @decorators.idempotent_id('13e95171-6cbd-489c-9d7c-3f9c58215c18')
Ashish Guptad42b6e12014-11-20 02:06:03 -0800394 def test_create_show_delete_port_user_defined_mac(self):
395 # Create a port for a legal mac
John Warren49c0fe52015-10-22 12:35:54 -0400396 body = self.ports_client.create_port(network_id=self.network['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800397 old_port = body['port']
398 free_mac_address = old_port['mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400399 self.ports_client.delete_port(old_port['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800400 # Create a new port with user defined mac
John Warren49c0fe52015-10-22 12:35:54 -0400401 body = self.ports_client.create_port(network_id=self.network['id'],
402 mac_address=free_mac_address)
403 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800404 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400405 body = self.ports_client.show_port(port['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800406 show_port = body['port']
407 self.assertEqual(free_mac_address,
408 show_port['mac_address'])
409
Jordan Pittier3b46d272017-04-12 16:17:28 +0200410 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800411 @decorators.idempotent_id('4179dcb9-1382-4ced-84fe-1b91c54f5735')
zhuflfffec232016-09-18 17:09:11 +0800412 @testtools.skipUnless(
Andrea Frittolicd368412017-08-14 21:37:56 +0100413 utils.is_extension_enabled('security-group', 'network'),
zhuflfffec232016-09-18 17:09:11 +0800414 'security-group extension not enabled.')
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700415 def test_create_port_with_no_securitygroups(self):
Salvatore18dd66e2014-12-31 00:13:57 +0100416 network = self.create_network()
John Warren94d8faf2015-09-15 12:22:24 -0400417 self.addCleanup(self.networks_client.delete_network, network['id'])
Adam Gandelmane8650042015-01-16 11:36:37 -0800418 subnet = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400419 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
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