blob: e7153f034576005c028289268b32fb8fe42e48d2 [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
zhuflfffec232016-09-18 17:09:11 +080019import testtools
Matthew Treinish01472ff2015-02-20 17:26:52 -050020
Elena Ezhova1ec6e182013-12-24 17:45:59 +040021from tempest.api.network import base
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
Fei Long Wangd39431f2015-05-14 11:30:48 +120024from tempest.common.utils import data_utils
Elena Ezhova1ec6e182013-12-24 17:45:59 +040025from tempest import config
Matthew Treinish4217a702016-10-07 17:27:11 -040026from tempest.lib import exceptions
Elena Ezhova1ec6e182013-12-24 17:45:59 +040027from tempest import test
28
29CONF = config.CONF
30
31
Neeti Dahiyade291772014-09-22 10:43:12 +053032class PortsTestJSON(sec_base.BaseSecGroupTest):
Ken'ichi Ohmichie03bea92015-11-19 07:45:58 +000033 """Test the following operations for ports:
Elena Ezhova5c957152014-03-26 12:01:10 +040034
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):
John Warren49c0fe52015-10-22 12:35:54 -040049 self.ports_client.delete_port(port_id)
50 body = self.ports_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')
Chris Hoge7579c1a2015-02-26 14:12:15 -080055 @test.idempotent_id('c72c1c0c-2193-4aca-aaa4-b1442640f51c')
Elena Ezhova1ec6e182013-12-24 17:45:59 +040056 def test_create_update_delete_port(self):
57 # Verify port creation
John Warren49c0fe52015-10-22 12:35:54 -040058 body = self.ports_client.create_port(network_id=self.network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040059 port = body['port']
Dane LeBlanccbc4bc52014-03-19 16:03:23 -040060 # Schedule port deletion with verification upon test completion
61 self.addCleanup(self._delete_port, port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040062 self.assertTrue(port['admin_state_up'])
63 # Verify port update
64 new_name = "New_Port"
John Warren49c0fe52015-10-22 12:35:54 -040065 body = self.ports_client.update_port(port['id'],
66 name=new_name,
67 admin_state_up=False)
Elena Ezhova1ec6e182013-12-24 17:45:59 +040068 updated_port = body['port']
69 self.assertEqual(updated_port['name'], new_name)
70 self.assertFalse(updated_port['admin_state_up'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040071
Chris Hoge7579c1a2015-02-26 14:12:15 -080072 @test.idempotent_id('67f1b811-f8db-43e2-86bd-72c074d4a42c')
abhishek6001472607373102014-11-25 04:39:20 -080073 def test_create_bulk_port(self):
74 network1 = self.network
zhufld2c40ca2016-10-18 17:03:07 +080075 network2 = self.create_network()
abhishek6001472607373102014-11-25 04:39:20 -080076 network_list = [network1['id'], network2['id']]
77 port_list = [{'network_id': net_id} for net_id in network_list]
Ken'ichi Ohmichi1f52fd92016-03-03 12:24:12 -080078 body = self.ports_client.create_bulk_ports(ports=port_list)
abhishek6001472607373102014-11-25 04:39:20 -080079 created_ports = body['ports']
80 port1 = created_ports[0]
81 port2 = created_ports[1]
82 self.addCleanup(self._delete_port, port1['id'])
83 self.addCleanup(self._delete_port, port2['id'])
84 self.assertEqual(port1['network_id'], network1['id'])
85 self.assertEqual(port2['network_id'], network2['id'])
86 self.assertTrue(port1['admin_state_up'])
87 self.assertTrue(port2['admin_state_up'])
88
abhishek60014726de45a5e2014-12-16 03:01:35 -080089 @classmethod
90 def _get_ipaddress_from_tempest_conf(cls):
Guillaume Chenuetbdf1d8d2015-11-09 16:10:48 +010091 """Return subnet with mask bits for configured CIDR """
abhishek60014726de45a5e2014-12-16 03:01:35 -080092 if cls._ip_version == 4:
Sean Dagueed6e5862016-04-04 10:49:13 -040093 cidr = netaddr.IPNetwork(CONF.network.project_network_cidr)
94 cidr.prefixlen = CONF.network.project_network_mask_bits
abhishek60014726de45a5e2014-12-16 03:01:35 -080095
96 elif cls._ip_version == 6:
Sean Dagueed6e5862016-04-04 10:49:13 -040097 cidr = netaddr.IPNetwork(CONF.network.project_network_v6_cidr)
98 cidr.prefixlen = CONF.network.project_network_v6_mask_bits
abhishek60014726de45a5e2014-12-16 03:01:35 -080099
Guillaume Chenuetbdf1d8d2015-11-09 16:10:48 +0100100 return cidr
abhishek60014726de45a5e2014-12-16 03:01:35 -0800101
102 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800103 @test.idempotent_id('0435f278-40ae-48cb-a404-b8a087bc09b1')
abhishek60014726de45a5e2014-12-16 03:01:35 -0800104 def test_create_port_in_allowed_allocation_pools(self):
105 network = self.create_network()
106 net_id = network['id']
107 address = self._get_ipaddress_from_tempest_conf()
Guillaume Chenuetbdf1d8d2015-11-09 16:10:48 +0100108 if ((address.version == 4 and address.prefixlen >= 30) or
109 (address.version == 6 and address.prefixlen >= 126)):
110 msg = ("Subnet %s isn't large enough for the test" % address.cidr)
zhuflff30ede2016-12-27 10:29:13 +0800111 raise exceptions.InvalidConfiguration(msg)
Guillaume Chenuetbdf1d8d2015-11-09 16:10:48 +0100112 allocation_pools = {'allocation_pools': [{'start': str(address[2]),
113 'end': str(address[-2])}]}
114 subnet = self.create_subnet(network, cidr=address,
115 mask_bits=address.prefixlen,
116 **allocation_pools)
John Warren3961acd2015-10-02 14:38:53 -0400117 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
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
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400127 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800128 @test.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
Chris Hoge7579c1a2015-02-26 14:12:15 -0800144 @test.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
155 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800156 @test.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
Arx Cruze8dfad92015-05-29 09:57:14 +0200164 @test.idempotent_id('e7fe260b-1e79-4dd3-86d9-bec6a7959fc5')
165 def test_port_list_filter_by_ip(self):
166 # Create network and subnet
167 network = self.create_network()
168 subnet = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400169 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300170 # Create two ports
John Warren49c0fe52015-10-22 12:35:54 -0400171 port_1 = self.ports_client.create_port(network_id=network['id'])
172 self.addCleanup(self.ports_client.delete_port, port_1['port']['id'])
173 port_2 = self.ports_client.create_port(network_id=network['id'])
174 self.addCleanup(self.ports_client.delete_port, port_2['port']['id'])
Arx Cruze8dfad92015-05-29 09:57:14 +0200175 # List ports filtered by fixed_ips
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300176 port_1_fixed_ip = port_1['port']['fixed_ips'][0]['ip_address']
177 fixed_ips = 'ip_address=' + port_1_fixed_ip
John Warren49c0fe52015-10-22 12:35:54 -0400178 port_list = self.ports_client.list_ports(fixed_ips=fixed_ips)
Yaroslav Lobankov5d71ac82015-06-16 16:43:43 +0300179 # Check that we got the desired port
Arx Cruze8dfad92015-05-29 09:57:14 +0200180 ports = port_list['ports']
Matthew Treinish1d942542015-08-25 18:16:14 -0400181 tenant_ids = set([port['tenant_id'] for port in ports])
182 self.assertEqual(len(tenant_ids), 1,
183 'Ports from multiple tenants are in the list resp')
184 port_ids = [port['id'] for port in ports]
185 fixed_ips = [port['fixed_ips'] for port in ports]
186 port_ips = []
187 for addr in fixed_ips:
188 port_ips.extend([port['ip_address'] for port in addr])
189
190 port_net_ids = [port['network_id'] for port in ports]
191 self.assertIn(port_1['port']['id'], port_ids)
192 self.assertIn(port_1_fixed_ip, port_ips)
193 self.assertIn(network['id'], port_net_ids)
Arx Cruze8dfad92015-05-29 09:57:14 +0200194
Chris Hoge7579c1a2015-02-26 14:12:15 -0800195 @test.idempotent_id('5ad01ed0-0e6e-4c5d-8194-232801b15c72')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400196 def test_port_list_filter_by_router_id(self):
197 # Create a router
198 network = self.create_network()
John Warren94d8faf2015-09-15 12:22:24 -0400199 self.addCleanup(self.networks_client.delete_network, network['id'])
Adam Gandelmane8650042015-01-16 11:36:37 -0800200 subnet = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400201 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
zhufl39ac5682016-10-24 17:11:34 +0800202 router = self.create_router()
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000203 self.addCleanup(self.routers_client.delete_router, router['id'])
John Warren49c0fe52015-10-22 12:35:54 -0400204 port = self.ports_client.create_port(network_id=network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400205 # Add router interface to port created above
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000206 self.routers_client.add_router_interface(router['id'],
207 port_id=port['port']['id'])
208 self.addCleanup(self.routers_client.remove_router_interface,
209 router['id'], port_id=port['port']['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400210 # List ports filtered by router_id
John Warren49c0fe52015-10-22 12:35:54 -0400211 port_list = self.ports_client.list_ports(device_id=router['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400212 ports = port_list['ports']
213 self.assertEqual(len(ports), 1)
214 self.assertEqual(ports[0]['id'], port['port']['id'])
215 self.assertEqual(ports[0]['device_id'], router['id'])
216
Chris Hoge7579c1a2015-02-26 14:12:15 -0800217 @test.idempotent_id('ff7f117f-f034-4e0e-abff-ccef05c454b4')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400218 def test_list_ports_fields(self):
219 # Verify specific fields of ports
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500220 fields = ['id', 'mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400221 body = self.ports_client.list_ports(fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400222 ports = body['ports']
223 self.assertNotEmpty(ports, "Port list returned is empty")
224 # Asserting the fields returned are correct
225 for port in ports:
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500226 self.assertEqual(sorted(fields), sorted(port.keys()))
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400227
Chris Hoge7579c1a2015-02-26 14:12:15 -0800228 @test.idempotent_id('63aeadd4-3b49-427f-a3b1-19ca81f06270')
Neeti Dahiyade291772014-09-22 10:43:12 +0530229 def test_create_update_port_with_second_ip(self):
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400230 # Create a network with two subnets
231 network = self.create_network()
John Warren94d8faf2015-09-15 12:22:24 -0400232 self.addCleanup(self.networks_client.delete_network, network['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400233 subnet_1 = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400234 self.addCleanup(self.subnets_client.delete_subnet, subnet_1['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400235 subnet_2 = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400236 self.addCleanup(self.subnets_client.delete_subnet, subnet_2['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400237 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
238 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
239
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400240 fixed_ips = fixed_ip_1 + fixed_ip_2
Neeti Dahiyade291772014-09-22 10:43:12 +0530241
242 # Create a port with multiple IP addresses
243 port = self.create_port(network,
244 fixed_ips=fixed_ips)
John Warren49c0fe52015-10-22 12:35:54 -0400245 self.addCleanup(self.ports_client.delete_port, port['id'])
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400246 self.assertEqual(2, len(port['fixed_ips']))
Neeti Dahiyade291772014-09-22 10:43:12 +0530247 check_fixed_ips = [subnet_1['id'], subnet_2['id']]
248 for item in port['fixed_ips']:
249 self.assertIn(item['subnet_id'], check_fixed_ips)
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400250
251 # Update the port to return to a single IP address
252 port = self.update_port(port, fixed_ips=fixed_ip_1)
253 self.assertEqual(1, len(port['fixed_ips']))
254
Neeti Dahiyade291772014-09-22 10:43:12 +0530255 # Update the port with a second IP address from second subnet
256 port = self.update_port(port, fixed_ips=fixed_ips)
257 self.assertEqual(2, len(port['fixed_ips']))
258
Ashish Guptadba59f92014-07-16 02:24:45 -0700259 def _update_port_with_security_groups(self, security_groups_names):
Neeti Dahiyade291772014-09-22 10:43:12 +0530260 subnet_1 = self.create_subnet(self.network)
John Warren3961acd2015-10-02 14:38:53 -0400261 self.addCleanup(self.subnets_client.delete_subnet, subnet_1['id'])
Neeti Dahiyade291772014-09-22 10:43:12 +0530262 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
263
Ashish Guptadba59f92014-07-16 02:24:45 -0700264 security_groups_list = list()
John Warrenf9606e92015-12-10 12:12:42 -0500265 sec_grps_client = self.security_groups_client
Ashish Guptadba59f92014-07-16 02:24:45 -0700266 for name in security_groups_names:
John Warrenf9606e92015-12-10 12:12:42 -0500267 group_create_body = sec_grps_client.create_security_group(
Ashish Guptadba59f92014-07-16 02:24:45 -0700268 name=name)
John Warrenf9606e92015-12-10 12:12:42 -0500269 self.addCleanup(self.security_groups_client.delete_security_group,
Ashish Guptadba59f92014-07-16 02:24:45 -0700270 group_create_body['security_group']['id'])
271 security_groups_list.append(group_create_body['security_group']
272 ['id'])
273 # Create a port
Neeti Dahiyade291772014-09-22 10:43:12 +0530274 sec_grp_name = data_utils.rand_name('secgroup')
John Warrenf9606e92015-12-10 12:12:42 -0500275 security_group = sec_grps_client.create_security_group(
276 name=sec_grp_name)
277 self.addCleanup(self.security_groups_client.delete_security_group,
Neeti Dahiyade291772014-09-22 10:43:12 +0530278 security_group['security_group']['id'])
279 post_body = {
280 "name": data_utils.rand_name('port-'),
281 "security_groups": [security_group['security_group']['id']],
282 "network_id": self.network['id'],
283 "admin_state_up": True,
284 "fixed_ips": fixed_ip_1}
John Warren49c0fe52015-10-22 12:35:54 -0400285 body = self.ports_client.create_port(**post_body)
286 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
Ashish Guptadba59f92014-07-16 02:24:45 -0700287 port = body['port']
Neeti Dahiyade291772014-09-22 10:43:12 +0530288
Ashish Guptadba59f92014-07-16 02:24:45 -0700289 # Update the port with security groups
Neeti Dahiyade291772014-09-22 10:43:12 +0530290 subnet_2 = self.create_subnet(self.network)
291 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
292 update_body = {"name": data_utils.rand_name('port-'),
293 "admin_state_up": False,
294 "fixed_ips": fixed_ip_2,
295 "security_groups": security_groups_list}
John Warren49c0fe52015-10-22 12:35:54 -0400296 body = self.ports_client.update_port(port['id'], **update_body)
Ashish Guptadba59f92014-07-16 02:24:45 -0700297 port_show = body['port']
Neeti Dahiyade291772014-09-22 10:43:12 +0530298 # Verify the security groups and other attributes updated to port
299 exclude_keys = set(port_show).symmetric_difference(update_body)
300 exclude_keys.add('fixed_ips')
301 exclude_keys.add('security_groups')
302 self.assertThat(port_show, custom_matchers.MatchesDictExceptForKeys(
303 update_body, exclude_keys))
304 self.assertEqual(fixed_ip_2[0]['subnet_id'],
305 port_show['fixed_ips'][0]['subnet_id'])
306
Ashish Guptadba59f92014-07-16 02:24:45 -0700307 for security_group in security_groups_list:
308 self.assertIn(security_group, port_show['security_groups'])
309
Chris Hoge7579c1a2015-02-26 14:12:15 -0800310 @test.idempotent_id('58091b66-4ff4-4cc1-a549-05d60c7acd1a')
zhuflfffec232016-09-18 17:09:11 +0800311 @testtools.skipUnless(
312 test.is_extension_enabled('security-group', 'network'),
313 'security-group extension not enabled.')
Neeti Dahiyade291772014-09-22 10:43:12 +0530314 def test_update_port_with_security_group_and_extra_attributes(self):
Ashish Guptadba59f92014-07-16 02:24:45 -0700315 self._update_port_with_security_groups(
316 [data_utils.rand_name('secgroup')])
317
Chris Hoge7579c1a2015-02-26 14:12:15 -0800318 @test.idempotent_id('edf6766d-3d40-4621-bc6e-2521a44c257d')
zhuflfffec232016-09-18 17:09:11 +0800319 @testtools.skipUnless(
320 test.is_extension_enabled('security-group', 'network'),
321 'security-group extension not enabled.')
Neeti Dahiyade291772014-09-22 10:43:12 +0530322 def test_update_port_with_two_security_groups_and_extra_attributes(self):
Ashish Guptadba59f92014-07-16 02:24:45 -0700323 self._update_port_with_security_groups(
324 [data_utils.rand_name('secgroup'),
325 data_utils.rand_name('secgroup')])
326
Chris Hoge7579c1a2015-02-26 14:12:15 -0800327 @test.idempotent_id('13e95171-6cbd-489c-9d7c-3f9c58215c18')
Ashish Guptad42b6e12014-11-20 02:06:03 -0800328 def test_create_show_delete_port_user_defined_mac(self):
329 # Create a port for a legal mac
John Warren49c0fe52015-10-22 12:35:54 -0400330 body = self.ports_client.create_port(network_id=self.network['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800331 old_port = body['port']
332 free_mac_address = old_port['mac_address']
John Warren49c0fe52015-10-22 12:35:54 -0400333 self.ports_client.delete_port(old_port['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800334 # Create a new port with user defined mac
John Warren49c0fe52015-10-22 12:35:54 -0400335 body = self.ports_client.create_port(network_id=self.network['id'],
336 mac_address=free_mac_address)
337 self.addCleanup(self.ports_client.delete_port, body['port']['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800338 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400339 body = self.ports_client.show_port(port['id'])
Ashish Guptad42b6e12014-11-20 02:06:03 -0800340 show_port = body['port']
341 self.assertEqual(free_mac_address,
342 show_port['mac_address'])
343
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700344 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800345 @test.idempotent_id('4179dcb9-1382-4ced-84fe-1b91c54f5735')
zhuflfffec232016-09-18 17:09:11 +0800346 @testtools.skipUnless(
347 test.is_extension_enabled('security-group', 'network'),
348 'security-group extension not enabled.')
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700349 def test_create_port_with_no_securitygroups(self):
Salvatore18dd66e2014-12-31 00:13:57 +0100350 network = self.create_network()
John Warren94d8faf2015-09-15 12:22:24 -0400351 self.addCleanup(self.networks_client.delete_network, network['id'])
Adam Gandelmane8650042015-01-16 11:36:37 -0800352 subnet = self.create_subnet(network)
John Warren3961acd2015-10-02 14:38:53 -0400353 self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
Salvatore18dd66e2014-12-31 00:13:57 +0100354 port = self.create_port(network, security_groups=[])
John Warren49c0fe52015-10-22 12:35:54 -0400355 self.addCleanup(self.ports_client.delete_port, port['id'])
Rajkumar Thiyagarajan0f562a82014-08-21 01:59:19 -0700356 self.assertIsNotNone(port['security_groups'])
357 self.assertEmpty(port['security_groups'])
358
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400359
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400360class PortsAdminExtendedAttrsTestJSON(base.BaseAdminNetworkTest):
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400361
362 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +0100363 def resource_setup(cls):
364 super(PortsAdminExtendedAttrsTestJSON, cls).resource_setup()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400365 cls.network = cls.create_network()
366 cls.host_id = socket.gethostname()
367
Chris Hoge7579c1a2015-02-26 14:12:15 -0800368 @test.idempotent_id('8e8569c1-9ac7-44db-8bc1-f5fb2814f29b')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400369 def test_create_port_binding_ext_attr(self):
370 post_body = {"network_id": self.network['id'],
371 "binding:host_id": self.host_id}
John Warren49c0fe52015-10-22 12:35:54 -0400372 body = self.admin_ports_client.create_port(**post_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400373 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400374 self.addCleanup(self.admin_ports_client.delete_port, port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400375 host_id = port['binding:host_id']
376 self.assertIsNotNone(host_id)
377 self.assertEqual(self.host_id, host_id)
378
Chris Hoge7579c1a2015-02-26 14:12:15 -0800379 @test.idempotent_id('6f6c412c-711f-444d-8502-0ac30fbf5dd5')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400380 def test_update_port_binding_ext_attr(self):
381 post_body = {"network_id": self.network['id']}
John Warren49c0fe52015-10-22 12:35:54 -0400382 body = self.admin_ports_client.create_port(**post_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400383 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400384 self.addCleanup(self.admin_ports_client.delete_port, port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400385 update_body = {"binding:host_id": self.host_id}
John Warren49c0fe52015-10-22 12:35:54 -0400386 body = self.admin_ports_client.update_port(port['id'], **update_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400387 updated_port = body['port']
388 host_id = updated_port['binding:host_id']
389 self.assertIsNotNone(host_id)
390 self.assertEqual(self.host_id, host_id)
391
Chris Hoge7579c1a2015-02-26 14:12:15 -0800392 @test.idempotent_id('1c82a44a-6c6e-48ff-89e1-abe7eaf8f9f8')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400393 def test_list_ports_binding_ext_attr(self):
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700394 # Create a new port
395 post_body = {"network_id": self.network['id']}
John Warren49c0fe52015-10-22 12:35:54 -0400396 body = self.admin_ports_client.create_port(**post_body)
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700397 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400398 self.addCleanup(self.admin_ports_client.delete_port, port['id'])
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700399
400 # Update the port's binding attributes so that is now 'bound'
401 # to a host
402 update_body = {"binding:host_id": self.host_id}
John Warren49c0fe52015-10-22 12:35:54 -0400403 self.admin_ports_client.update_port(port['id'], **update_body)
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700404
405 # List all ports, ensure new port is part of list and its binding
406 # attributes are set and accurate
John Warren49c0fe52015-10-22 12:35:54 -0400407 body = self.admin_ports_client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400408 ports_list = body['ports']
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700409 pids_list = [p['id'] for p in ports_list]
410 self.assertIn(port['id'], pids_list)
411 listed_port = [p for p in ports_list if p['id'] == port['id']]
412 self.assertEqual(1, len(listed_port),
413 'Multiple ports listed with id %s in ports listing: '
414 '%s' % (port['id'], ports_list))
415 self.assertEqual(self.host_id, listed_port[0]['binding:host_id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400416
Chris Hoge7579c1a2015-02-26 14:12:15 -0800417 @test.idempotent_id('b54ac0ff-35fc-4c79-9ca3-c7dbd4ea4f13')
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400418 def test_show_port_binding_ext_attr(self):
John Warren49c0fe52015-10-22 12:35:54 -0400419 body = self.admin_ports_client.create_port(
420 network_id=self.network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400421 port = body['port']
John Warren49c0fe52015-10-22 12:35:54 -0400422 self.addCleanup(self.admin_ports_client.delete_port, port['id'])
423 body = self.admin_ports_client.show_port(port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400424 show_port = body['port']
425 self.assertEqual(port['binding:host_id'],
426 show_port['binding:host_id'])
427 self.assertEqual(port['binding:vif_type'],
428 show_port['binding:vif_type'])
429 self.assertEqual(port['binding:vif_details'],
430 show_port['binding:vif_details'])
431
432
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400433class PortsIpV6TestJSON(PortsTestJSON):
434 _ip_version = 6
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400435
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400436
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400437class PortsAdminExtendedAttrsIpV6TestJSON(PortsAdminExtendedAttrsTestJSON):
438 _ip_version = 6