Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 1 | # 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 | |
| 16 | import socket |
| 17 | |
| 18 | from tempest.api.network import base |
Santosh Kumar | f0cbf9a | 2014-08-25 07:31:15 -0700 | [diff] [blame] | 19 | from tempest.common import custom_matchers |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 20 | from tempest.common.utils import data_utils |
| 21 | from tempest import config |
| 22 | from tempest import test |
| 23 | |
| 24 | CONF = config.CONF |
| 25 | |
| 26 | |
| 27 | class PortsTestJSON(base.BaseNetworkTest): |
| 28 | _interface = 'json' |
| 29 | |
Elena Ezhova | 5c95715 | 2014-03-26 12:01:10 +0400 | [diff] [blame] | 30 | """ |
| 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 Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 40 | @classmethod |
Andrea Frittoli | da4a245 | 2014-09-15 13:12:08 +0100 | [diff] [blame] | 41 | def resource_setup(cls): |
| 42 | super(PortsTestJSON, cls).resource_setup() |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 43 | cls.network = cls.create_network() |
| 44 | cls.port = cls.create_port(cls.network) |
| 45 | |
| 46 | def _delete_port(self, port_id): |
Rohan Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 47 | self.client.delete_port(port_id) |
| 48 | _, body = self.client.list_ports() |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 49 | 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 Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 55 | _, body = self.client.create_port(network_id=self.network['id']) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 56 | port = body['port'] |
Dane LeBlanc | cbc4bc5 | 2014-03-19 16:03:23 -0400 | [diff] [blame] | 57 | # Schedule port deletion with verification upon test completion |
| 58 | self.addCleanup(self._delete_port, port['id']) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 59 | self.assertTrue(port['admin_state_up']) |
| 60 | # Verify port update |
| 61 | new_name = "New_Port" |
Rohan Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 62 | _, body = self.client.update_port(port['id'], |
| 63 | name=new_name, |
| 64 | admin_state_up=False) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 65 | updated_port = body['port'] |
| 66 | self.assertEqual(updated_port['name'], new_name) |
| 67 | self.assertFalse(updated_port['admin_state_up']) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 68 | |
| 69 | @test.attr(type='smoke') |
| 70 | def test_show_port(self): |
| 71 | # Verify the details of port |
Rohan Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 72 | _, body = self.client.show_port(self.port['id']) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 73 | port = body['port'] |
| 74 | self.assertIn('id', port) |
Santosh Kumar | f0cbf9a | 2014-08-25 07:31:15 -0700 | [diff] [blame] | 75 | # 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 Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 81 | |
| 82 | @test.attr(type='smoke') |
| 83 | def test_show_port_fields(self): |
| 84 | # Verify specific fields of a port |
Zhi Kun Liu | 903596c | 2014-04-11 08:55:53 -0500 | [diff] [blame] | 85 | fields = ['id', 'mac_address'] |
Rohan Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 86 | _, body = self.client.show_port(self.port['id'], |
| 87 | fields=fields) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 88 | port = body['port'] |
Zhi Kun Liu | 903596c | 2014-04-11 08:55:53 -0500 | [diff] [blame] | 89 | self.assertEqual(sorted(port.keys()), sorted(fields)) |
| 90 | for field_name in fields: |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 91 | 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 Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 96 | _, body = self.client.list_ports() |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 97 | 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 Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 114 | _, port_list = self.client.list_ports(device_id=router['id']) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 115 | 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 Liu | 903596c | 2014-04-11 08:55:53 -0500 | [diff] [blame] | 123 | fields = ['id', 'mac_address'] |
Rohan Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 124 | _, body = self.client.list_ports(fields=fields) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 125 | 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 Liu | 903596c | 2014-04-11 08:55:53 -0500 | [diff] [blame] | 129 | self.assertEqual(sorted(fields), sorted(port.keys())) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 130 | |
Dane LeBlanc | cbc4bc5 | 2014-03-19 16:03:23 -0400 | [diff] [blame] | 131 | @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 Gupta | dba59f9 | 2014-07-16 02:24:45 -0700 | [diff] [blame] | 154 | 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 Gupta | d42b6e1 | 2014-11-20 02:06:03 -0800 | [diff] [blame^] | 189 | @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 Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 206 | |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 207 | class PortsAdminExtendedAttrsTestJSON(base.BaseAdminNetworkTest): |
| 208 | _interface = 'json' |
| 209 | |
| 210 | @classmethod |
Andrea Frittoli | da4a245 | 2014-09-15 13:12:08 +0100 | [diff] [blame] | 211 | def resource_setup(cls): |
| 212 | super(PortsAdminExtendedAttrsTestJSON, cls).resource_setup() |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 213 | 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 Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 223 | _, body = self.admin_client.create_port(**post_body) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 224 | 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 Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 233 | _, body = self.admin_client.create_port(**post_body) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 234 | port = body['port'] |
| 235 | self.addCleanup(self.admin_client.delete_port, port['id']) |
| 236 | update_body = {"binding:host_id": self.host_id} |
Rohan Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 237 | _, body = self.admin_client.update_port(port['id'], **update_body) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 238 | 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 Gandelman | 7e80e4e | 2014-04-03 00:38:26 -0700 | [diff] [blame] | 245 | # Create a new port |
| 246 | post_body = {"network_id": self.network['id']} |
Rohan Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 247 | _, body = self.admin_client.create_port(**post_body) |
Adam Gandelman | 7e80e4e | 2014-04-03 00:38:26 -0700 | [diff] [blame] | 248 | 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 Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 254 | self.admin_client.update_port(port['id'], **update_body) |
Adam Gandelman | 7e80e4e | 2014-04-03 00:38:26 -0700 | [diff] [blame] | 255 | |
| 256 | # List all ports, ensure new port is part of list and its binding |
| 257 | # attributes are set and accurate |
Rohan Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 258 | _, body = self.admin_client.list_ports() |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 259 | ports_list = body['ports'] |
Adam Gandelman | 7e80e4e | 2014-04-03 00:38:26 -0700 | [diff] [blame] | 260 | 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 Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 267 | |
| 268 | @test.attr(type='smoke') |
| 269 | def test_show_port_binding_ext_attr(self): |
Rohan Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 270 | _, body = self.admin_client.create_port(network_id=self.network['id']) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 271 | port = body['port'] |
| 272 | self.addCleanup(self.admin_client.delete_port, port['id']) |
Rohan Kanade | eeb2164 | 2014-08-14 12:00:26 +0200 | [diff] [blame] | 273 | _, body = self.admin_client.show_port(port['id']) |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 274 | 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 Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 283 | class 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 Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 288 | |
Elena Ezhova | 1ec6e18 | 2013-12-24 17:45:59 +0400 | [diff] [blame] | 289 | class 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 |