Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
| 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 | |
Maho Koshiya | 7b62958 | 2016-02-22 10:59:01 +0900 | [diff] [blame^] | 16 | import netaddr |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 17 | import time |
| 18 | |
Sean Dague | 1937d09 | 2013-05-17 16:36:38 -0400 | [diff] [blame] | 19 | from tempest.api.compute import base |
Matthew Treinish | b0a78fc | 2014-01-29 16:49:12 +0000 | [diff] [blame] | 20 | from tempest import config |
Oleg Bondarev | ee50bb1 | 2014-01-16 00:01:34 +0400 | [diff] [blame] | 21 | from tempest import exceptions |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 22 | from tempest.lib import exceptions as lib_exc |
Yuiko Takada | e9999d6 | 2014-03-06 09:22:54 +0000 | [diff] [blame] | 23 | from tempest import test |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 24 | |
Matthew Treinish | b0a78fc | 2014-01-29 16:49:12 +0000 | [diff] [blame] | 25 | CONF = config.CONF |
| 26 | |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 27 | |
ivan-zhu | f2b0050 | 2013-10-18 10:06:52 +0800 | [diff] [blame] | 28 | class AttachInterfacesTestJSON(base.BaseV2ComputeTest): |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 29 | |
| 30 | @classmethod |
Emily Hugenbruch | e7991d9 | 2014-12-12 16:53:36 +0000 | [diff] [blame] | 31 | def skip_checks(cls): |
| 32 | super(AttachInterfacesTestJSON, cls).skip_checks() |
Matthew Treinish | b0a78fc | 2014-01-29 16:49:12 +0000 | [diff] [blame] | 33 | if not CONF.service_available.neutron: |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 34 | raise cls.skipException("Neutron is required") |
Adam Gandelman | 7186f7a | 2014-07-23 09:28:56 -0400 | [diff] [blame] | 35 | if not CONF.compute_feature_enabled.interface_attach: |
| 36 | raise cls.skipException("Interface attachment is not available.") |
Emily Hugenbruch | e7991d9 | 2014-12-12 16:53:36 +0000 | [diff] [blame] | 37 | |
| 38 | @classmethod |
| 39 | def setup_credentials(cls): |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 40 | # This test class requires network and subnet |
| 41 | cls.set_network_resources(network=True, subnet=True) |
Emily Hugenbruch | e7991d9 | 2014-12-12 16:53:36 +0000 | [diff] [blame] | 42 | super(AttachInterfacesTestJSON, cls).setup_credentials() |
| 43 | |
| 44 | @classmethod |
| 45 | def setup_clients(cls): |
| 46 | super(AttachInterfacesTestJSON, cls).setup_clients() |
David Kranz | b9017e7 | 2013-05-07 11:16:29 -0400 | [diff] [blame] | 47 | cls.client = cls.os.interfaces_client |
Maho Koshiya | 7b62958 | 2016-02-22 10:59:01 +0900 | [diff] [blame^] | 48 | cls.networks_client = cls.os.networks_client |
| 49 | cls.subnets_client = cls.os.subnets_client |
Maho Koshiya | 3fc1246 | 2015-12-14 19:03:12 +0900 | [diff] [blame] | 50 | cls.ports_client = cls.os.ports_client |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 51 | |
Ken'ichi Ohmichi | 84e9968 | 2015-07-08 05:28:57 +0000 | [diff] [blame] | 52 | def wait_for_interface_status(self, server, port_id, status): |
Maho Koshiya | 3fc1246 | 2015-12-14 19:03:12 +0900 | [diff] [blame] | 53 | """Waits for an interface to reach a given status.""" |
ghanshyam | a2364f1 | 2015-08-24 15:45:37 +0900 | [diff] [blame] | 54 | body = (self.client.show_interface(server, port_id) |
| 55 | ['interfaceAttachment']) |
Ken'ichi Ohmichi | 84e9968 | 2015-07-08 05:28:57 +0000 | [diff] [blame] | 56 | interface_status = body['port_state'] |
| 57 | start = int(time.time()) |
| 58 | |
| 59 | while(interface_status != status): |
| 60 | time.sleep(self.build_interval) |
ghanshyam | a2364f1 | 2015-08-24 15:45:37 +0900 | [diff] [blame] | 61 | body = (self.client.show_interface(server, port_id) |
| 62 | ['interfaceAttachment']) |
Ken'ichi Ohmichi | 84e9968 | 2015-07-08 05:28:57 +0000 | [diff] [blame] | 63 | interface_status = body['port_state'] |
| 64 | |
| 65 | timed_out = int(time.time()) - start >= self.build_timeout |
| 66 | |
| 67 | if interface_status != status and timed_out: |
| 68 | message = ('Interface %s failed to reach %s status ' |
| 69 | '(current %s) within the required time (%s s).' % |
| 70 | (port_id, status, interface_status, |
| 71 | self.build_timeout)) |
| 72 | raise exceptions.TimeoutException(message) |
| 73 | |
| 74 | return body |
| 75 | |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 76 | def _check_interface(self, iface, port_id=None, network_id=None, |
venkata anil | 4537530 | 2014-12-30 10:41:43 +0000 | [diff] [blame] | 77 | fixed_ip=None, mac_addr=None): |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 78 | self.assertIn('port_state', iface) |
| 79 | if port_id: |
| 80 | self.assertEqual(iface['port_id'], port_id) |
| 81 | if network_id: |
| 82 | self.assertEqual(iface['net_id'], network_id) |
| 83 | if fixed_ip: |
| 84 | self.assertEqual(iface['fixed_ips'][0]['ip_address'], fixed_ip) |
venkata anil | 4537530 | 2014-12-30 10:41:43 +0000 | [diff] [blame] | 85 | if mac_addr: |
| 86 | self.assertEqual(iface['mac_addr'], mac_addr) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 87 | |
| 88 | def _create_server_get_interfaces(self): |
David Kranz | 0fb1429 | 2015-02-11 15:55:20 -0500 | [diff] [blame] | 89 | server = self.create_test_server(wait_until='ACTIVE') |
ghanshyam | a2364f1 | 2015-08-24 15:45:37 +0900 | [diff] [blame] | 90 | ifs = (self.client.list_interfaces(server['id']) |
| 91 | ['interfaceAttachments']) |
Ken'ichi Ohmichi | 84e9968 | 2015-07-08 05:28:57 +0000 | [diff] [blame] | 92 | body = self.wait_for_interface_status( |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 93 | server['id'], ifs[0]['port_id'], 'ACTIVE') |
| 94 | ifs[0]['port_state'] = body['port_state'] |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 95 | return server, ifs |
| 96 | |
| 97 | def _test_create_interface(self, server): |
ghanshyam | a2364f1 | 2015-08-24 15:45:37 +0900 | [diff] [blame] | 98 | iface = (self.client.create_interface(server['id']) |
| 99 | ['interfaceAttachment']) |
Ken'ichi Ohmichi | 84e9968 | 2015-07-08 05:28:57 +0000 | [diff] [blame] | 100 | iface = self.wait_for_interface_status( |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 101 | server['id'], iface['port_id'], 'ACTIVE') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 102 | self._check_interface(iface) |
| 103 | return iface |
| 104 | |
| 105 | def _test_create_interface_by_network_id(self, server, ifs): |
| 106 | network_id = ifs[0]['net_id'] |
ghanshyam | a2364f1 | 2015-08-24 15:45:37 +0900 | [diff] [blame] | 107 | iface = self.client.create_interface( |
| 108 | server['id'], net_id=network_id)['interfaceAttachment'] |
Ken'ichi Ohmichi | 84e9968 | 2015-07-08 05:28:57 +0000 | [diff] [blame] | 109 | iface = self.wait_for_interface_status( |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 110 | server['id'], iface['port_id'], 'ACTIVE') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 111 | self._check_interface(iface, network_id=network_id) |
| 112 | return iface |
| 113 | |
Maho Koshiya | 3fc1246 | 2015-12-14 19:03:12 +0900 | [diff] [blame] | 114 | def _test_create_interface_by_port_id(self, server, ifs): |
| 115 | network_id = ifs[0]['net_id'] |
| 116 | port = self.ports_client.create_port(network_id=network_id) |
| 117 | port_id = port['port']['id'] |
| 118 | self.addCleanup(self.ports_client.delete_port, port_id) |
| 119 | iface = self.client.create_interface( |
| 120 | server['id'], port_id=port_id)['interfaceAttachment'] |
| 121 | iface = self.wait_for_interface_status( |
| 122 | server['id'], iface['port_id'], 'ACTIVE') |
| 123 | self._check_interface(iface, port_id=port_id) |
| 124 | return iface |
| 125 | |
Maho Koshiya | 7b62958 | 2016-02-22 10:59:01 +0900 | [diff] [blame^] | 126 | def _test_create_interface_by_fixed_ips(self, server, ifs): |
| 127 | network_id = ifs[0]['net_id'] |
| 128 | ip_list = [ |
| 129 | ifs[n]['fixed_ips'][0]['ip_address'] for n in range(0, len(ifs))] |
| 130 | ip = str(netaddr.IPAddress(sorted(ip_list)[-1]) + 1) |
| 131 | |
| 132 | fixed_ips = [{'ip_address': ip}] |
| 133 | iface = self.client.create_interface( |
| 134 | server['id'], net_id=network_id, |
| 135 | fixed_ips=fixed_ips)['interfaceAttachment'] |
| 136 | self.addCleanup(self.ports_client.delete_port, iface['port_id']) |
| 137 | iface = self.wait_for_interface_status( |
| 138 | server['id'], iface['port_id'], 'ACTIVE') |
| 139 | self._check_interface(iface, fixed_ip=ip) |
| 140 | return iface |
| 141 | |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 142 | def _test_show_interface(self, server, ifs): |
| 143 | iface = ifs[0] |
ghanshyam | a2364f1 | 2015-08-24 15:45:37 +0900 | [diff] [blame] | 144 | _iface = self.client.show_interface( |
| 145 | server['id'], iface['port_id'])['interfaceAttachment'] |
venkata anil | 4537530 | 2014-12-30 10:41:43 +0000 | [diff] [blame] | 146 | self._check_interface(iface, port_id=_iface['port_id'], |
| 147 | network_id=_iface['net_id'], |
| 148 | fixed_ip=_iface['fixed_ips'][0]['ip_address'], |
| 149 | mac_addr=_iface['mac_addr']) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 150 | |
| 151 | def _test_delete_interface(self, server, ifs): |
| 152 | # NOTE(danms): delete not the first or last, but one in the middle |
| 153 | iface = ifs[1] |
David Kranz | b2b0c18 | 2015-02-18 13:28:19 -0500 | [diff] [blame] | 154 | self.client.delete_interface(server['id'], iface['port_id']) |
ghanshyam | a2364f1 | 2015-08-24 15:45:37 +0900 | [diff] [blame] | 155 | _ifs = (self.client.list_interfaces(server['id']) |
| 156 | ['interfaceAttachments']) |
Oleg Bondarev | ee50bb1 | 2014-01-16 00:01:34 +0400 | [diff] [blame] | 157 | start = int(time.time()) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 158 | |
Oleg Bondarev | ee50bb1 | 2014-01-16 00:01:34 +0400 | [diff] [blame] | 159 | while len(ifs) == len(_ifs): |
| 160 | time.sleep(self.build_interval) |
ghanshyam | a2364f1 | 2015-08-24 15:45:37 +0900 | [diff] [blame] | 161 | _ifs = (self.client.list_interfaces(server['id']) |
| 162 | ['interfaceAttachments']) |
Oleg Bondarev | ee50bb1 | 2014-01-16 00:01:34 +0400 | [diff] [blame] | 163 | timed_out = int(time.time()) - start >= self.build_timeout |
| 164 | if len(ifs) == len(_ifs) and timed_out: |
| 165 | message = ('Failed to delete interface within ' |
| 166 | 'the required time: %s sec.' % self.build_timeout) |
| 167 | raise exceptions.TimeoutException(message) |
| 168 | |
| 169 | self.assertNotIn(iface['port_id'], [i['port_id'] for i in _ifs]) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 170 | return _ifs |
| 171 | |
| 172 | def _compare_iface_list(self, list1, list2): |
| 173 | # NOTE(danms): port_state will likely have changed, so just |
| 174 | # confirm the port_ids are the same at least |
| 175 | list1 = [x['port_id'] for x in list1] |
| 176 | list2 = [x['port_id'] for x in list2] |
| 177 | |
| 178 | self.assertEqual(sorted(list1), sorted(list2)) |
| 179 | |
Chris Hoge | 7579c1a | 2015-02-26 14:12:15 -0800 | [diff] [blame] | 180 | @test.idempotent_id('73fe8f02-590d-4bf1-b184-e9ca81065051') |
Matthew Treinish | 2df9748 | 2014-06-13 15:02:26 -0400 | [diff] [blame] | 181 | @test.services('network') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 182 | def test_create_list_show_delete_interfaces(self): |
| 183 | server, ifs = self._create_server_get_interfaces() |
| 184 | interface_count = len(ifs) |
| 185 | self.assertTrue(interface_count > 0) |
| 186 | self._check_interface(ifs[0]) |
| 187 | |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 188 | try: |
| 189 | iface = self._test_create_interface(server) |
| 190 | except lib_exc.BadRequest as e: |
| 191 | msg = ('Multiple possible networks found, use a Network ID to be ' |
| 192 | 'more specific.') |
| 193 | if not CONF.compute.fixed_network_name and e.message == msg: |
| 194 | raise |
| 195 | else: |
| 196 | ifs.append(iface) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 197 | |
| 198 | iface = self._test_create_interface_by_network_id(server, ifs) |
| 199 | ifs.append(iface) |
| 200 | |
Maho Koshiya | 3fc1246 | 2015-12-14 19:03:12 +0900 | [diff] [blame] | 201 | iface = self._test_create_interface_by_port_id(server, ifs) |
| 202 | ifs.append(iface) |
| 203 | |
Maho Koshiya | 7b62958 | 2016-02-22 10:59:01 +0900 | [diff] [blame^] | 204 | iface = self._test_create_interface_by_fixed_ips(server, ifs) |
| 205 | ifs.append(iface) |
| 206 | |
ghanshyam | a2364f1 | 2015-08-24 15:45:37 +0900 | [diff] [blame] | 207 | _ifs = (self.client.list_interfaces(server['id']) |
| 208 | ['interfaceAttachments']) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 209 | self._compare_iface_list(ifs, _ifs) |
| 210 | |
| 211 | self._test_show_interface(server, ifs) |
| 212 | |
| 213 | _ifs = self._test_delete_interface(server, ifs) |
| 214 | self.assertEqual(len(ifs) - 1, len(_ifs)) |
| 215 | |
Masayuki Igawa | 750aa92 | 2014-03-20 09:46:37 +0900 | [diff] [blame] | 216 | @test.attr(type='smoke') |
Chris Hoge | 7579c1a | 2015-02-26 14:12:15 -0800 | [diff] [blame] | 217 | @test.idempotent_id('c7e0e60b-ee45-43d0-abeb-8596fd42a2f9') |
Matthew Treinish | 2df9748 | 2014-06-13 15:02:26 -0400 | [diff] [blame] | 218 | @test.services('network') |
Ghanshyam Mann | 6e855d1 | 2014-02-26 13:31:56 +0900 | [diff] [blame] | 219 | def test_add_remove_fixed_ip(self): |
| 220 | # Add and Remove the fixed IP to server. |
| 221 | server, ifs = self._create_server_get_interfaces() |
| 222 | interface_count = len(ifs) |
| 223 | self.assertTrue(interface_count > 0) |
| 224 | self._check_interface(ifs[0]) |
| 225 | network_id = ifs[0]['net_id'] |
ghanshyam | 4c7d2a0 | 2015-09-14 16:05:13 +0900 | [diff] [blame] | 226 | self.servers_client.add_fixed_ip(server['id'], networkId=network_id) |
Ghanshyam Mann | 6e855d1 | 2014-02-26 13:31:56 +0900 | [diff] [blame] | 227 | # Remove the fixed IP from server. |
Ken'ichi Ohmichi | 7680024 | 2015-07-03 05:12:31 +0000 | [diff] [blame] | 228 | server_detail = self.os.servers_client.show_server( |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 229 | server['id'])['server'] |
Ghanshyam Mann | 6e855d1 | 2014-02-26 13:31:56 +0900 | [diff] [blame] | 230 | # Get the Fixed IP from server. |
| 231 | fixed_ip = None |
| 232 | for ip_set in server_detail['addresses']: |
| 233 | for ip in server_detail['addresses'][ip_set]: |
| 234 | if ip['OS-EXT-IPS:type'] == 'fixed': |
| 235 | fixed_ip = ip['addr'] |
| 236 | break |
| 237 | if fixed_ip is not None: |
| 238 | break |
ghanshyam | 4c7d2a0 | 2015-09-14 16:05:13 +0900 | [diff] [blame] | 239 | self.servers_client.remove_fixed_ip(server['id'], address=fixed_ip) |