blob: 67f2c83e5901f672788309276fa63488cc2a53ad [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
16import socket
17
Matthew Treinish01472ff2015-02-20 17:26:52 -050018import netaddr
Matthew Treinish01472ff2015-02-20 17:26:52 -050019
Elena Ezhova1ec6e182013-12-24 17:45:59 +040020from tempest.api.network import base
Neeti Dahiyade291772014-09-22 10:43:12 +053021from tempest.api.network import base_security_groups as sec_base
Santosh Kumarf0cbf9a2014-08-25 07:31:15 -070022from tempest.common import custom_matchers
Fei Long Wangd39431f2015-05-14 11:30:48 +120023from tempest.common.utils import data_utils
Elena Ezhova1ec6e182013-12-24 17:45:59 +040024from tempest import config
25from tempest import test
26
27CONF = config.CONF
28
29
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
52 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -080053 @test.idempotent_id('c72c1c0c-2193-4aca-aaa4-b1442640f51c')
Elena Ezhova1ec6e182013-12-24 17:45:59 +040054 def test_create_update_delete_port(self):
55 # Verify port creation
John Warren49c0fe52015-10-22 12:35:54 -040056 body = self.ports_client.create_port(network_id=self.network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040057 port = body['port']
Dane LeBlanccbc4bc52014-03-19 16:03:23 -040058 # Schedule port deletion with verification upon test completion
59 self.addCleanup(self._delete_port, port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040060 self.assertTrue(port['admin_state_up'])
61 # Verify port update
62 new_name = "New_Port"
John Warren49c0fe52015-10-22 12:35:54 -040063 body = self.ports_client.update_port(port['id'],
64 name=new_name,
65 admin_state_up=False)
Elena Ezhova1ec6e182013-12-24 17:45:59 +040066 updated_port = body['port']
67 self.assertEqual(updated_port['name'], new_name)
68 self.assertFalse(updated_port['admin_state_up'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040069
Chris Hoge7579c1a2015-02-26 14:12:15 -080070 @test.idempotent_id('67f1b811-f8db-43e2-86bd-72c074d4a42c')
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]
piyush1107867584aa72015-12-15 18:46:38 +053077 body = self.client.create_bulk_port(ports=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')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800100 @test.idempotent_id('0435f278-40ae-48cb-a404-b8a087bc09b1')
abhishek60014726de45a5e2014-12-16 03:01:35 -0800101 def test_create_port_in_allowed_allocation_pools(self):
102 network = self.create_network()
103 net_id = network['id']
104 address = self._get_ipaddress_from_tempest_conf()
105 allocation_pools = {'allocation_pools': [{'start': str(address + 4),
106 'end': str(address + 6)}]}
Adam Gandelmane8650042015-01-16 11:36:37 -0800107 subnet = self.create_subnet(network, **allocation_pools)
John Warren3961acd2015-10-02 14:38:53 -0400108 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
John Warren49c0fe52015-10-22 12:35:54 -0400109 body = self.ports_client.create_port(network_id=net_id)
110 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
abhishek60014726de45a5e2014-12-16 03:01:35 -0800111 port = body['port']
112 ip_address = port['fixed_ips'][0]['ip_address']
113 start_ip_address = allocation_pools['allocation_pools'][0]['start']
114 end_ip_address = allocation_pools['allocation_pools'][0]['end']
115 ip_range = netaddr.IPRange(start_ip_address, end_ip_address)
116 self.assertIn(ip_address, ip_range)
117
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400118 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800119 @test.idempotent_id('c9a685bd-e83f-499c-939f-9f7863ca259f')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400120 def test_show_port(self):
121 # Verify the details of port
John Warren49c0fe52015-10-22 12:35:54 -0400122 body = self.ports_client.show_port(self.port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400123 port = body['port']
124 self.assertIn('id', port)
Santosh Kumarf0cbf9a2014-08-25 07:31:15 -0700125 # TODO(Santosh)- This is a temporary workaround to compare create_port
126 # and show_port dict elements.Remove this once extra_dhcp_opts issue
127 # gets fixed in neutron.( bug - 1365341.)
128 self.assertThat(self.port,
129 custom_matchers.MatchesDictExceptForKeys
130 (port, excluded_keys=['extra_dhcp_opts']))
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400131
Chris Hoge7579c1a2015-02-26 14:12:15 -0800132 @test.idempotent_id('45fcdaf2-dab0-4c13-ac6c-fcddfb579dbd')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400133 def test_show_port_fields(self):
134 # Verify specific fields of a port
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500135 fields = ['id', 'mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400136 body = self.ports_client.show_port(self.port['id'],
137 fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400138 port = body['port']
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500139 self.assertEqual(sorted(port.keys()), sorted(fields))
140 for field_name in fields:
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400141 self.assertEqual(port[field_name], self.port[field_name])
142
143 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800144 @test.idempotent_id('cf95b358-3e92-4a29-a148-52445e1ac50e')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400145 def test_list_ports(self):
146 # Verify the port exists in the list of all ports
John Warren49c0fe52015-10-22 12:35:54 -0400147 body = self.ports_client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400148 ports = [port['id'] for port in body['ports']
149 if port['id'] == self.port['id']]
150 self.assertNotEmpty(ports, "Created port not found in the list")
151
Arx Cruze8dfad92015-05-29 09:57:14 +0200152 @test.idempotent_id('e7fe260b-1e79-4dd3-86d9-bec6a7959fc5')
153 def test_port_list_filter_by_ip(self):
154 # Create network and subnet
155 network = self.create_network()
156 subnet = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400157 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300158 # Create two ports
John Warren49c0fe52015-10-22 12:35:54 -0400159 port_1 = self.ports_client.create_port(network_id=network['id'])
160 self.addCleanup(self.ports_client.delete_port, port_1['port']['id'])
161 port_2 = self.ports_client.create_port(network_id=network['id'])
162 self.addCleanup(self.ports_client.delete_port, port_2['port']['id'])
Arx Cruze8dfad92015-05-29 09:57:14 +0200163 # List ports filtered by fixed_ips
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300164 port_1_fixed_ip = port_1['port']['fixed_ips'][0]['ip_address']
165 fixed_ips = 'ip_address=' + port_1_fixed_ip
John Warren49c0fe52015-10-22 12:35:54 -0400166 port_list = self.ports_client.list_ports(fixed_ips=fixed_ips)
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300167 # Check that we got the desired port
Arx Cruze8dfad92015-05-29 09:57:14 +0200168 ports = port_list['ports']
Matthew Treinish1d942542015-08-25 18:16:14 -0400169 tenant_ids = set([port['tenant_id'] for port in ports])
170 self.assertEqual(len(tenant_ids), 1,
171 'Ports from multiple tenants are in the list resp')
172 port_ids = [port['id'] for port in ports]
173 fixed_ips = [port['fixed_ips'] for port in ports]
174 port_ips = []
175 for addr in fixed_ips:
176 port_ips.extend([port['ip_address'] for port in addr])
177
178 port_net_ids = [port['network_id'] for port in ports]
179 self.assertIn(port_1['port']['id'], port_ids)
180 self.assertIn(port_1_fixed_ip, port_ips)
181 self.assertIn(network['id'], port_net_ids)
Arx Cruze8dfad92015-05-29 09:57:14 +0200182
Chris Hoge7579c1a2015-02-26 14:12:15 -0800183 @test.idempotent_id('5ad01ed0-0e6e-4c5d-8194-232801b15c72')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400184 def test_port_list_filter_by_router_id(self):
185 # Create a router
186 network = self.create_network()
John Warren94d8faf2015-09-15 12:22:24 -0400187 self.addCleanup(self.networks_client.delete_network, network['id'])
Adam Gandelmane8650042015-01-16 11:36:37 -0800188 subnet = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400189 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400190 router = self.create_router(data_utils.rand_name('router-'))
Adam Gandelmane8650042015-01-16 11:36:37 -0800191 self.addCleanup(self.client.delete_router, router['id'])
John Warren49c0fe52015-10-22 12:35:54 -0400192 port = self.ports_client.create_port(network_id=network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400193 # Add router interface to port created above
David Kranz34e88122014-12-11 15:24:05 -0500194 self.client.add_router_interface_with_port_id(
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400195 router['id'], port['port']['id'])
196 self.addCleanup(self.client.remove_router_interface_with_port_id,
197 router['id'], port['port']['id'])
198 # List ports filtered by router_id
John Warren49c0fe52015-10-22 12:35:54 -0400199 port_list = self.ports_client.list_ports(device_id=router['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400200 ports = port_list['ports']
201 self.assertEqual(len(ports), 1)
202 self.assertEqual(ports[0]['id'], port['port']['id'])
203 self.assertEqual(ports[0]['device_id'], router['id'])
204
Chris Hoge7579c1a2015-02-26 14:12:15 -0800205 @test.idempotent_id('ff7f117f-f034-4e0e-abff-ccef05c454b4')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400206 def test_list_ports_fields(self):
207 # Verify specific fields of ports
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500208 fields = ['id', 'mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400209 body = self.ports_client.list_ports(fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400210 ports = body['ports']
211 self.assertNotEmpty(ports, "Port list returned is empty")
212 # Asserting the fields returned are correct
213 for port in ports:
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500214 self.assertEqual(sorted(fields), sorted(port.keys()))
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400215
Chris Hoge7579c1a2015-02-26 14:12:15 -0800216 @test.idempotent_id('63aeadd4-3b49-427f-a3b1-19ca81f06270')
Neeti Dahiyade291772014-09-22 10:43:12 +0530217 def test_create_update_port_with_second_ip(self):
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400218 # Create a network with two subnets
219 network = self.create_network()
John Warren94d8faf2015-09-15 12:22:24 -0400220 self.addCleanup(self.networks_client.delete_network, network['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400221 subnet_1 = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400222 self.addCleanup(self.subnets_client.delete_subnet, subnet_1['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400223 subnet_2 = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400224 self.addCleanup(self.subnets_client.delete_subnet, subnet_2['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400225 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
226 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
227
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400228 fixed_ips = fixed_ip_1 + fixed_ip_2
Neeti Dahiyade291772014-09-22 10:43:12 +0530229
230 # Create a port with multiple IP addresses
231 port = self.create_port(network,
232 fixed_ips=fixed_ips)
John Warren49c0fe52015-10-22 12:35:54 -0400233 self.addCleanup(self.ports_client.delete_port, port['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400234 self.assertEqual(2, len(port['fixed_ips']))
Neeti Dahiyade291772014-09-22 10:43:12 +0530235 check_fixed_ips = [subnet_1['id'], subnet_2['id']]
236 for item in port['fixed_ips']:
237 self.assertIn(item['subnet_id'], check_fixed_ips)
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400238
239 # Update the port to return to a single IP address
240 port = self.update_port(port, fixed_ips=fixed_ip_1)
241 self.assertEqual(1, len(port['fixed_ips']))
242
Neeti Dahiyade291772014-09-22 10:43:12 +0530243 # Update the port with a second IP address from second subnet
244 port = self.update_port(port, fixed_ips=fixed_ips)
245 self.assertEqual(2, len(port['fixed_ips']))
246
Ashish Guptadba59f92014-07-16 02:24:45 -0700247 def _update_port_with_security_groups(self, security_groups_names):
Neeti Dahiyade291772014-09-22 10:43:12 +0530248 subnet_1 = self.create_subnet(self.network)
John Warren3961acd2015-10-02 14:38:53 -0400249 self.addCleanup(self.subnets_client.delete_subnet, subnet_1['id'])
Neeti Dahiyade291772014-09-22 10:43:12 +0530250 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
251
Ashish Guptadba59f92014-07-16 02:24:45 -0700252 security_groups_list = list()
John Warrenf9606e92015-12-10 12:12:42 -0500253 sec_grps_client = self.security_groups_client
Ashish Guptadba59f92014-07-16 02:24:45 -0700254 for name in security_groups_names:
John Warrenf9606e92015-12-10 12:12:42 -0500255 group_create_body = sec_grps_client.create_security_group(
Ashish Guptadba59f92014-07-16 02:24:45 -0700256 name=name)
John Warrenf9606e92015-12-10 12:12:42 -0500257 self.addCleanup(self.security_groups_client.delete_security_group,
Ashish Guptadba59f92014-07-16 02:24:45 -0700258 group_create_body['security_group']['id'])
259 security_groups_list.append(group_create_body['security_group']
260 ['id'])
261 # Create a port
Neeti Dahiyade291772014-09-22 10:43:12 +0530262 sec_grp_name = data_utils.rand_name('secgroup')
John Warrenf9606e92015-12-10 12:12:42 -0500263 security_group = sec_grps_client.create_security_group(
264 name=sec_grp_name)
265 self.addCleanup(self.security_groups_client.delete_security_group,
Neeti Dahiyade291772014-09-22 10:43:12 +0530266 security_group['security_group']['id'])
267 post_body = {
268 "name": data_utils.rand_name('port-'),
269 "security_groups": [security_group['security_group']['id']],
270 "network_id": self.network['id'],
271 "admin_state_up": True,
272 "fixed_ips": fixed_ip_1}
John Warren49c0fe52015-10-22 12:35:54 -0400273 body = self.ports_client.create_port(**post_body)
274 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
Ashish Guptadba59f92014-07-16 02:24:45 -0700275 port = body['port']
Neeti Dahiyade291772014-09-22 10:43:12 +0530276
Ashish Guptadba59f92014-07-16 02:24:45 -0700277 # Update the port with security groups
Neeti Dahiyade291772014-09-22 10:43:12 +0530278 subnet_2 = self.create_subnet(self.network)
279 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
280 update_body = {"name": data_utils.rand_name('port-'),
281 "admin_state_up": False,
282 "fixed_ips": fixed_ip_2,
283 "security_groups": security_groups_list}
John Warren49c0fe52015-10-22 12:35:54 -0400284 body = self.ports_client.update_port(port['id'], **update_body)
Ashish Guptadba59f92014-07-16 02:24:45 -0700285 port_show = body['port']
Neeti Dahiyade291772014-09-22 10:43:12 +0530286 # Verify the security groups and other attributes updated to port
287 exclude_keys = set(port_show).symmetric_difference(update_body)
288 exclude_keys.add('fixed_ips')
289 exclude_keys.add('security_groups')
290 self.assertThat(port_show, custom_matchers.MatchesDictExceptForKeys(
291 update_body, exclude_keys))
292 self.assertEqual(fixed_ip_2[0]['subnet_id'],
293 port_show['fixed_ips'][0]['subnet_id'])
294
Ashish Guptadba59f92014-07-16 02:24:45 -0700295 for security_group in security_groups_list:
296 self.assertIn(security_group, port_show['security_groups'])
297
Chris Hoge7579c1a2015-02-26 14:12:15 -0800298 @test.idempotent_id('58091b66-4ff4-4cc1-a549-05d60c7acd1a')
Neeti Dahiyade291772014-09-22 10:43:12 +0530299 def test_update_port_with_security_group_and_extra_attributes(self):
Ashish Guptadba59f92014-07-16 02:24:45 -0700300 self._update_port_with_security_groups(
301 [data_utils.rand_name('secgroup')])
302
Chris Hoge7579c1a2015-02-26 14:12:15 -0800303 @test.idempotent_id('edf6766d-3d40-4621-bc6e-2521a44c257d')
Neeti Dahiyade291772014-09-22 10:43:12 +0530304 def test_update_port_with_two_security_groups_and_extra_attributes(self):
Ashish Guptadba59f92014-07-16 02:24:45 -0700305 self._update_port_with_security_groups(
306 [data_utils.rand_name('secgroup'),
307 data_utils.rand_name('secgroup')])
308
Chris Hoge7579c1a2015-02-26 14:12:15 -0800309 @test.idempotent_id('13e95171-6cbd-489c-9d7c-3f9c58215c18')
Ashish Guptad42b6e12014-11-20 02:06:03 -0800310 def test_create_show_delete_port_user_defined_mac(self):
311 # Create a port for a legal mac
John Warren49c0fe52015-10-22 12:35:54 -0400312 body = self.ports_client.create_port(network_id=self.network['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800313 old_port = body['port']
314 free_mac_address = old_port['mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400315 self.ports_client.delete_port(old_port['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800316 # Create a new port with user defined mac
John Warren49c0fe52015-10-22 12:35:54 -0400317 body = self.ports_client.create_port(network_id=self.network['id'],
318 mac_address=free_mac_address)
319 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800320 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400321 body = self.ports_client.show_port(port['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800322 show_port = body['port']
323 self.assertEqual(free_mac_address,
324 show_port['mac_address'])
325
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700326 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800327 @test.idempotent_id('4179dcb9-1382-4ced-84fe-1b91c54f5735')
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700328 def test_create_port_with_no_securitygroups(self):
Salvatore18dd66e2014-12-31 00:13:57 +0100329 network = self.create_network()
John Warren94d8faf2015-09-15 12:22:24 -0400330 self.addCleanup(self.networks_client.delete_network, network['id'])
Adam Gandelmane8650042015-01-16 11:36:37 -0800331 subnet = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400332 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
Salvatore18dd66e2014-12-31 00:13:57 +0100333 port = self.create_port(network, security_groups=[])
John Warren49c0fe52015-10-22 12:35:54 -0400334 self.addCleanup(self.ports_client.delete_port, port['id'])
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700335 self.assertIsNotNone(port['security_groups'])
336 self.assertEmpty(port['security_groups'])
337
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400338
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400339class PortsAdminExtendedAttrsTestJSON(base.BaseAdminNetworkTest):
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400340
341 @classmethod
Rohan Kanadea565e452015-01-27 14:00:13 +0530342 def setup_clients(cls):
343 super(PortsAdminExtendedAttrsTestJSON, cls).setup_clients()
344 cls.identity_client = cls.os_adm.identity_client
345
346 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +0100347 def resource_setup(cls):
348 super(PortsAdminExtendedAttrsTestJSON, cls).resource_setup()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400349 cls.network = cls.create_network()
350 cls.host_id = socket.gethostname()
351
Chris Hoge7579c1a2015-02-26 14:12:15 -0800352 @test.idempotent_id('8e8569c1-9ac7-44db-8bc1-f5fb2814f29b')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400353 def test_create_port_binding_ext_attr(self):
354 post_body = {"network_id": self.network['id'],
355 "binding:host_id": self.host_id}
John Warren49c0fe52015-10-22 12:35:54 -0400356 body = self.admin_ports_client.create_port(**post_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400357 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400358 self.addCleanup(self.admin_ports_client.delete_port, port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400359 host_id = port['binding:host_id']
360 self.assertIsNotNone(host_id)
361 self.assertEqual(self.host_id, host_id)
362
Chris Hoge7579c1a2015-02-26 14:12:15 -0800363 @test.idempotent_id('6f6c412c-711f-444d-8502-0ac30fbf5dd5')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400364 def test_update_port_binding_ext_attr(self):
365 post_body = {"network_id": self.network['id']}
John Warren49c0fe52015-10-22 12:35:54 -0400366 body = self.admin_ports_client.create_port(**post_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400367 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400368 self.addCleanup(self.admin_ports_client.delete_port, port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400369 update_body = {"binding:host_id": self.host_id}
John Warren49c0fe52015-10-22 12:35:54 -0400370 body = self.admin_ports_client.update_port(port['id'], **update_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400371 updated_port = body['port']
372 host_id = updated_port['binding:host_id']
373 self.assertIsNotNone(host_id)
374 self.assertEqual(self.host_id, host_id)
375
Chris Hoge7579c1a2015-02-26 14:12:15 -0800376 @test.idempotent_id('1c82a44a-6c6e-48ff-89e1-abe7eaf8f9f8')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400377 def test_list_ports_binding_ext_attr(self):
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700378 # Create a new port
379 post_body = {"network_id": self.network['id']}
John Warren49c0fe52015-10-22 12:35:54 -0400380 body = self.admin_ports_client.create_port(**post_body)
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700381 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400382 self.addCleanup(self.admin_ports_client.delete_port, port['id'])
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700383
384 # Update the port's binding attributes so that is now 'bound'
385 # to a host
386 update_body = {"binding:host_id": self.host_id}
John Warren49c0fe52015-10-22 12:35:54 -0400387 self.admin_ports_client.update_port(port['id'], **update_body)
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700388
389 # List all ports, ensure new port is part of list and its binding
390 # attributes are set and accurate
John Warren49c0fe52015-10-22 12:35:54 -0400391 body = self.admin_ports_client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400392 ports_list = body['ports']
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700393 pids_list = [p['id'] for p in ports_list]
394 self.assertIn(port['id'], pids_list)
395 listed_port = [p for p in ports_list if p['id'] == port['id']]
396 self.assertEqual(1, len(listed_port),
397 'Multiple ports listed with id %s in ports listing: '
398 '%s' % (port['id'], ports_list))
399 self.assertEqual(self.host_id, listed_port[0]['binding:host_id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400400
Chris Hoge7579c1a2015-02-26 14:12:15 -0800401 @test.idempotent_id('b54ac0ff-35fc-4c79-9ca3-c7dbd4ea4f13')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400402 def test_show_port_binding_ext_attr(self):
John Warren49c0fe52015-10-22 12:35:54 -0400403 body = self.admin_ports_client.create_port(
404 network_id=self.network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400405 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400406 self.addCleanup(self.admin_ports_client.delete_port, port['id'])
407 body = self.admin_ports_client.show_port(port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400408 show_port = body['port']
409 self.assertEqual(port['binding:host_id'],
410 show_port['binding:host_id'])
411 self.assertEqual(port['binding:vif_type'],
412 show_port['binding:vif_type'])
413 self.assertEqual(port['binding:vif_details'],
414 show_port['binding:vif_details'])
415
416
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400417class PortsIpV6TestJSON(PortsTestJSON):
418 _ip_version = 6
419 _tenant_network_cidr = CONF.network.tenant_network_v6_cidr
420 _tenant_network_mask_bits = CONF.network.tenant_network_v6_mask_bits
421
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400422
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400423class PortsAdminExtendedAttrsIpV6TestJSON(PortsAdminExtendedAttrsTestJSON):
424 _ip_version = 6
425 _tenant_network_cidr = CONF.network.tenant_network_v6_cidr
426 _tenant_network_mask_bits = CONF.network.tenant_network_v6_mask_bits