blob: a03e5871f2cc011633ddf3c33f716d4113da8c66 [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
18from tempest.api.network import base
Santosh Kumarf0cbf9a2014-08-25 07:31:15 -070019from tempest.common import custom_matchers
Elena Ezhova1ec6e182013-12-24 17:45:59 +040020from tempest.common.utils import data_utils
21from tempest import config
22from tempest import test
23
24CONF = config.CONF
25
26
27class PortsTestJSON(base.BaseNetworkTest):
28 _interface = 'json'
29
Elena Ezhova5c957152014-03-26 12:01:10 +040030 """
31 Test the following operations for ports:
32
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):
Rohan Kanadeeeb21642014-08-14 12:00:26 +020047 self.client.delete_port(port_id)
48 _, body = self.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')
53 def test_create_update_delete_port(self):
54 # Verify port creation
Rohan Kanadeeeb21642014-08-14 12:00:26 +020055 _, body = self.client.create_port(network_id=self.network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040056 port = body['port']
Dane LeBlanccbc4bc52014-03-19 16:03:23 -040057 # Schedule port deletion with verification upon test completion
58 self.addCleanup(self._delete_port, port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040059 self.assertTrue(port['admin_state_up'])
60 # Verify port update
61 new_name = "New_Port"
Rohan Kanadeeeb21642014-08-14 12:00:26 +020062 _, body = self.client.update_port(port['id'],
63 name=new_name,
64 admin_state_up=False)
Elena Ezhova1ec6e182013-12-24 17:45:59 +040065 updated_port = body['port']
66 self.assertEqual(updated_port['name'], new_name)
67 self.assertFalse(updated_port['admin_state_up'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040068
69 @test.attr(type='smoke')
70 def test_show_port(self):
71 # Verify the details of port
Rohan Kanadeeeb21642014-08-14 12:00:26 +020072 _, body = self.client.show_port(self.port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +040073 port = body['port']
74 self.assertIn('id', port)
Santosh Kumarf0cbf9a2014-08-25 07:31:15 -070075 # TODO(Santosh)- This is a temporary workaround to compare create_port
76 # and show_port dict elements.Remove this once extra_dhcp_opts issue
77 # gets fixed in neutron.( bug - 1365341.)
78 self.assertThat(self.port,
79 custom_matchers.MatchesDictExceptForKeys
80 (port, excluded_keys=['extra_dhcp_opts']))
Elena Ezhova1ec6e182013-12-24 17:45:59 +040081
82 @test.attr(type='smoke')
83 def test_show_port_fields(self):
84 # Verify specific fields of a port
Zhi Kun Liu903596c2014-04-11 08:55:53 -050085 fields = ['id', 'mac_address']
Rohan Kanadeeeb21642014-08-14 12:00:26 +020086 _, body = self.client.show_port(self.port['id'],
87 fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +040088 port = body['port']
Zhi Kun Liu903596c2014-04-11 08:55:53 -050089 self.assertEqual(sorted(port.keys()), sorted(fields))
90 for field_name in fields:
Elena Ezhova1ec6e182013-12-24 17:45:59 +040091 self.assertEqual(port[field_name], self.port[field_name])
92
93 @test.attr(type='smoke')
94 def test_list_ports(self):
95 # Verify the port exists in the list of all ports
Rohan Kanadeeeb21642014-08-14 12:00:26 +020096 _, body = self.client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +040097 ports = [port['id'] for port in body['ports']
98 if port['id'] == self.port['id']]
99 self.assertNotEmpty(ports, "Created port not found in the list")
100
101 @test.attr(type='smoke')
102 def test_port_list_filter_by_router_id(self):
103 # Create a router
104 network = self.create_network()
105 self.create_subnet(network)
106 router = self.create_router(data_utils.rand_name('router-'))
107 resp, port = self.client.create_port(network_id=network['id'])
108 # Add router interface to port created above
109 resp, interface = self.client.add_router_interface_with_port_id(
110 router['id'], port['port']['id'])
111 self.addCleanup(self.client.remove_router_interface_with_port_id,
112 router['id'], port['port']['id'])
113 # List ports filtered by router_id
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200114 _, port_list = self.client.list_ports(device_id=router['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400115 ports = port_list['ports']
116 self.assertEqual(len(ports), 1)
117 self.assertEqual(ports[0]['id'], port['port']['id'])
118 self.assertEqual(ports[0]['device_id'], router['id'])
119
120 @test.attr(type='smoke')
121 def test_list_ports_fields(self):
122 # Verify specific fields of ports
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500123 fields = ['id', 'mac_address']
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200124 _, body = self.client.list_ports(fields=fields)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400125 ports = body['ports']
126 self.assertNotEmpty(ports, "Port list returned is empty")
127 # Asserting the fields returned are correct
128 for port in ports:
Zhi Kun Liu903596c2014-04-11 08:55:53 -0500129 self.assertEqual(sorted(fields), sorted(port.keys()))
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400130
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400131 @test.attr(type='smoke')
132 def test_update_port_with_second_ip(self):
133 # Create a network with two subnets
134 network = self.create_network()
135 subnet_1 = self.create_subnet(network)
136 subnet_2 = self.create_subnet(network)
137 fixed_ip_1 = [{'subnet_id': subnet_1['id']}]
138 fixed_ip_2 = [{'subnet_id': subnet_2['id']}]
139
140 # Create a port with a single IP address from first subnet
141 port = self.create_port(network,
142 fixed_ips=fixed_ip_1)
143 self.assertEqual(1, len(port['fixed_ips']))
144
145 # Update the port with a second IP address from second subnet
146 fixed_ips = fixed_ip_1 + fixed_ip_2
147 port = self.update_port(port, fixed_ips=fixed_ips)
148 self.assertEqual(2, len(port['fixed_ips']))
149
150 # Update the port to return to a single IP address
151 port = self.update_port(port, fixed_ips=fixed_ip_1)
152 self.assertEqual(1, len(port['fixed_ips']))
153
Ashish Guptadba59f92014-07-16 02:24:45 -0700154 def _update_port_with_security_groups(self, security_groups_names):
155 post_body = {"network_id": self.network['id']}
156 self.create_subnet(self.network)
157 security_groups_list = list()
158 for name in security_groups_names:
159 _, group_create_body = self.client.create_security_group(
160 name=name)
161 self.addCleanup(self.client.delete_security_group,
162 group_create_body['security_group']['id'])
163 security_groups_list.append(group_create_body['security_group']
164 ['id'])
165 # Create a port
166 _, body = self.client.create_port(**post_body)
167 self.addCleanup(self.client.delete_port, body['port']['id'])
168 port = body['port']
169 # Update the port with security groups
170 update_body = {"security_groups": security_groups_list}
171 _, body = self.client.update_port(
172 port['id'], **update_body)
173 # Verify the security groups updated to port
174 port_show = body['port']
175 for security_group in security_groups_list:
176 self.assertIn(security_group, port_show['security_groups'])
177
178 @test.attr(type='smoke')
179 def test_update_port_with_security_group(self):
180 self._update_port_with_security_groups(
181 [data_utils.rand_name('secgroup')])
182
183 @test.attr(type='smoke')
184 def test_update_port_with_two_security_groups(self):
185 self._update_port_with_security_groups(
186 [data_utils.rand_name('secgroup'),
187 data_utils.rand_name('secgroup')])
188
Ashish Guptad42b6e12014-11-20 02:06:03 -0800189 @test.attr(type='smoke')
190 def test_create_show_delete_port_user_defined_mac(self):
191 # Create a port for a legal mac
192 _, body = self.client.create_port(network_id=self.network['id'])
193 old_port = body['port']
194 free_mac_address = old_port['mac_address']
195 self.client.delete_port(old_port['id'])
196 # Create a new port with user defined mac
197 _, body = self.client.create_port(network_id=self.network['id'],
198 mac_address=free_mac_address)
199 self.addCleanup(self.client.delete_port, body['port']['id'])
200 port = body['port']
201 _, body = self.client.show_port(port['id'])
202 show_port = body['port']
203 self.assertEqual(free_mac_address,
204 show_port['mac_address'])
205
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400206
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400207class PortsAdminExtendedAttrsTestJSON(base.BaseAdminNetworkTest):
208 _interface = 'json'
209
210 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +0100211 def resource_setup(cls):
212 super(PortsAdminExtendedAttrsTestJSON, cls).resource_setup()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400213 cls.identity_client = cls._get_identity_admin_client()
214 cls.tenant = cls.identity_client.get_tenant_by_name(
215 CONF.identity.tenant_name)
216 cls.network = cls.create_network()
217 cls.host_id = socket.gethostname()
218
219 @test.attr(type='smoke')
220 def test_create_port_binding_ext_attr(self):
221 post_body = {"network_id": self.network['id'],
222 "binding:host_id": self.host_id}
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200223 _, body = self.admin_client.create_port(**post_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400224 port = body['port']
225 self.addCleanup(self.admin_client.delete_port, port['id'])
226 host_id = port['binding:host_id']
227 self.assertIsNotNone(host_id)
228 self.assertEqual(self.host_id, host_id)
229
230 @test.attr(type='smoke')
231 def test_update_port_binding_ext_attr(self):
232 post_body = {"network_id": self.network['id']}
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200233 _, body = self.admin_client.create_port(**post_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400234 port = body['port']
235 self.addCleanup(self.admin_client.delete_port, port['id'])
236 update_body = {"binding:host_id": self.host_id}
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200237 _, body = self.admin_client.update_port(port['id'], **update_body)
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400238 updated_port = body['port']
239 host_id = updated_port['binding:host_id']
240 self.assertIsNotNone(host_id)
241 self.assertEqual(self.host_id, host_id)
242
243 @test.attr(type='smoke')
244 def test_list_ports_binding_ext_attr(self):
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700245 # Create a new port
246 post_body = {"network_id": self.network['id']}
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200247 _, body = self.admin_client.create_port(**post_body)
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700248 port = body['port']
249 self.addCleanup(self.admin_client.delete_port, port['id'])
250
251 # Update the port's binding attributes so that is now 'bound'
252 # to a host
253 update_body = {"binding:host_id": self.host_id}
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200254 self.admin_client.update_port(port['id'], **update_body)
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700255
256 # List all ports, ensure new port is part of list and its binding
257 # attributes are set and accurate
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200258 _, body = self.admin_client.list_ports()
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400259 ports_list = body['ports']
Adam Gandelman7e80e4e2014-04-03 00:38:26 -0700260 pids_list = [p['id'] for p in ports_list]
261 self.assertIn(port['id'], pids_list)
262 listed_port = [p for p in ports_list if p['id'] == port['id']]
263 self.assertEqual(1, len(listed_port),
264 'Multiple ports listed with id %s in ports listing: '
265 '%s' % (port['id'], ports_list))
266 self.assertEqual(self.host_id, listed_port[0]['binding:host_id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400267
268 @test.attr(type='smoke')
269 def test_show_port_binding_ext_attr(self):
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200270 _, body = self.admin_client.create_port(network_id=self.network['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400271 port = body['port']
272 self.addCleanup(self.admin_client.delete_port, port['id'])
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200273 _, body = self.admin_client.show_port(port['id'])
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400274 show_port = body['port']
275 self.assertEqual(port['binding:host_id'],
276 show_port['binding:host_id'])
277 self.assertEqual(port['binding:vif_type'],
278 show_port['binding:vif_type'])
279 self.assertEqual(port['binding:vif_details'],
280 show_port['binding:vif_details'])
281
282
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400283class PortsIpV6TestJSON(PortsTestJSON):
284 _ip_version = 6
285 _tenant_network_cidr = CONF.network.tenant_network_v6_cidr
286 _tenant_network_mask_bits = CONF.network.tenant_network_v6_mask_bits
287
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400288
Elena Ezhova1ec6e182013-12-24 17:45:59 +0400289class PortsAdminExtendedAttrsIpV6TestJSON(PortsAdminExtendedAttrsTestJSON):
290 _ip_version = 6
291 _tenant_network_cidr = CONF.network.tenant_network_v6_cidr
292 _tenant_network_mask_bits = CONF.network.tenant_network_v6_mask_bits