blob: fdf55e5daa93639c2a2fa41cc8eb1344f32c5a26 [file] [log] [blame]
Dan Smith8ad1c472013-02-26 13:03:16 -05001# 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
Rohan Kanade9ce97df2013-12-10 18:59:35 +053016import time
17
Sean Dague1937d092013-05-17 16:36:38 -040018from tempest.api.compute import base
Ryan Tidwell1964a262016-05-04 15:13:23 -070019from tempest.common.utils import net_utils
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000020from tempest import config
Oleg Bondarevee50bb12014-01-16 00:01:34 +040021from tempest import exceptions
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050022from tempest.lib import exceptions as lib_exc
Yuiko Takadae9999d62014-03-06 09:22:54 +000023from tempest import test
Dan Smith8ad1c472013-02-26 13:03:16 -050024
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000025CONF = config.CONF
26
Dan Smith8ad1c472013-02-26 13:03:16 -050027
ivan-zhuf2b00502013-10-18 10:06:52 +080028class AttachInterfacesTestJSON(base.BaseV2ComputeTest):
Dan Smith8ad1c472013-02-26 13:03:16 -050029
30 @classmethod
Emily Hugenbruche7991d92014-12-12 16:53:36 +000031 def skip_checks(cls):
32 super(AttachInterfacesTestJSON, cls).skip_checks()
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000033 if not CONF.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040034 raise cls.skipException("Neutron is required")
Adam Gandelman7186f7a2014-07-23 09:28:56 -040035 if not CONF.compute_feature_enabled.interface_attach:
36 raise cls.skipException("Interface attachment is not available.")
Emily Hugenbruche7991d92014-12-12 16:53:36 +000037
38 @classmethod
39 def setup_credentials(cls):
Salvatore Orlando5a337242014-01-15 22:49:22 +000040 # This test class requires network and subnet
41 cls.set_network_resources(network=True, subnet=True)
Emily Hugenbruche7991d92014-12-12 16:53:36 +000042 super(AttachInterfacesTestJSON, cls).setup_credentials()
43
44 @classmethod
45 def setup_clients(cls):
46 super(AttachInterfacesTestJSON, cls).setup_clients()
David Kranzb9017e72013-05-07 11:16:29 -040047 cls.client = cls.os.interfaces_client
Maho Koshiya7b629582016-02-22 10:59:01 +090048 cls.networks_client = cls.os.networks_client
49 cls.subnets_client = cls.os.subnets_client
Maho Koshiya3fc12462015-12-14 19:03:12 +090050 cls.ports_client = cls.os.ports_client
Dan Smith8ad1c472013-02-26 13:03:16 -050051
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000052 def wait_for_interface_status(self, server, port_id, status):
Maho Koshiya3fc12462015-12-14 19:03:12 +090053 """Waits for an interface to reach a given status."""
ghanshyama2364f12015-08-24 15:45:37 +090054 body = (self.client.show_interface(server, port_id)
55 ['interfaceAttachment'])
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000056 interface_status = body['port_state']
57 start = int(time.time())
58
59 while(interface_status != status):
60 time.sleep(self.build_interval)
ghanshyama2364f12015-08-24 15:45:37 +090061 body = (self.client.show_interface(server, port_id)
62 ['interfaceAttachment'])
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000063 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 Smith8ad1c472013-02-26 13:03:16 -050076 def _check_interface(self, iface, port_id=None, network_id=None,
venkata anil45375302014-12-30 10:41:43 +000077 fixed_ip=None, mac_addr=None):
Dan Smith8ad1c472013-02-26 13:03:16 -050078 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 anil45375302014-12-30 10:41:43 +000085 if mac_addr:
86 self.assertEqual(iface['mac_addr'], mac_addr)
Dan Smith8ad1c472013-02-26 13:03:16 -050087
88 def _create_server_get_interfaces(self):
David Kranz0fb14292015-02-11 15:55:20 -050089 server = self.create_test_server(wait_until='ACTIVE')
ghanshyama2364f12015-08-24 15:45:37 +090090 ifs = (self.client.list_interfaces(server['id'])
91 ['interfaceAttachments'])
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000092 body = self.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +090093 server['id'], ifs[0]['port_id'], 'ACTIVE')
94 ifs[0]['port_state'] = body['port_state']
Dan Smith8ad1c472013-02-26 13:03:16 -050095 return server, ifs
96
97 def _test_create_interface(self, server):
ghanshyama2364f12015-08-24 15:45:37 +090098 iface = (self.client.create_interface(server['id'])
99 ['interfaceAttachment'])
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +0000100 iface = self.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +0900101 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -0500102 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']
ghanshyama2364f12015-08-24 15:45:37 +0900107 iface = self.client.create_interface(
108 server['id'], net_id=network_id)['interfaceAttachment']
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +0000109 iface = self.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +0900110 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -0500111 self._check_interface(iface, network_id=network_id)
112 return iface
113
Maho Koshiya3fc12462015-12-14 19:03:12 +0900114 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 Koshiya7b629582016-02-22 10:59:01 +0900126 def _test_create_interface_by_fixed_ips(self, server, ifs):
127 network_id = ifs[0]['net_id']
Ryan Tidwell1964a262016-05-04 15:13:23 -0700128 subnet_id = ifs[0]['fixed_ips'][0]['subnet_id']
129 ip_list = net_utils.get_unused_ip_addresses(self.ports_client,
130 self.subnets_client,
131 network_id,
132 subnet_id,
133 1)
Maho Koshiya7b629582016-02-22 10:59:01 +0900134
Ryan Tidwell1964a262016-05-04 15:13:23 -0700135 fixed_ips = [{'ip_address': ip_list[0]}]
Maho Koshiya7b629582016-02-22 10:59:01 +0900136 iface = self.client.create_interface(
137 server['id'], net_id=network_id,
138 fixed_ips=fixed_ips)['interfaceAttachment']
139 self.addCleanup(self.ports_client.delete_port, iface['port_id'])
140 iface = self.wait_for_interface_status(
141 server['id'], iface['port_id'], 'ACTIVE')
Ryan Tidwell1964a262016-05-04 15:13:23 -0700142 self._check_interface(iface, fixed_ip=ip_list[0])
Maho Koshiya7b629582016-02-22 10:59:01 +0900143 return iface
144
Dan Smith8ad1c472013-02-26 13:03:16 -0500145 def _test_show_interface(self, server, ifs):
146 iface = ifs[0]
ghanshyama2364f12015-08-24 15:45:37 +0900147 _iface = self.client.show_interface(
148 server['id'], iface['port_id'])['interfaceAttachment']
venkata anil45375302014-12-30 10:41:43 +0000149 self._check_interface(iface, port_id=_iface['port_id'],
150 network_id=_iface['net_id'],
151 fixed_ip=_iface['fixed_ips'][0]['ip_address'],
152 mac_addr=_iface['mac_addr'])
Dan Smith8ad1c472013-02-26 13:03:16 -0500153
154 def _test_delete_interface(self, server, ifs):
155 # NOTE(danms): delete not the first or last, but one in the middle
156 iface = ifs[1]
David Kranzb2b0c182015-02-18 13:28:19 -0500157 self.client.delete_interface(server['id'], iface['port_id'])
ghanshyama2364f12015-08-24 15:45:37 +0900158 _ifs = (self.client.list_interfaces(server['id'])
159 ['interfaceAttachments'])
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400160 start = int(time.time())
Dan Smith8ad1c472013-02-26 13:03:16 -0500161
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400162 while len(ifs) == len(_ifs):
163 time.sleep(self.build_interval)
ghanshyama2364f12015-08-24 15:45:37 +0900164 _ifs = (self.client.list_interfaces(server['id'])
165 ['interfaceAttachments'])
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400166 timed_out = int(time.time()) - start >= self.build_timeout
167 if len(ifs) == len(_ifs) and timed_out:
168 message = ('Failed to delete interface within '
169 'the required time: %s sec.' % self.build_timeout)
170 raise exceptions.TimeoutException(message)
171
172 self.assertNotIn(iface['port_id'], [i['port_id'] for i in _ifs])
Dan Smith8ad1c472013-02-26 13:03:16 -0500173 return _ifs
174
175 def _compare_iface_list(self, list1, list2):
176 # NOTE(danms): port_state will likely have changed, so just
177 # confirm the port_ids are the same at least
178 list1 = [x['port_id'] for x in list1]
179 list2 = [x['port_id'] for x in list2]
180
181 self.assertEqual(sorted(list1), sorted(list2))
182
Chris Hoge7579c1a2015-02-26 14:12:15 -0800183 @test.idempotent_id('73fe8f02-590d-4bf1-b184-e9ca81065051')
Matthew Treinish2df97482014-06-13 15:02:26 -0400184 @test.services('network')
Dan Smith8ad1c472013-02-26 13:03:16 -0500185 def test_create_list_show_delete_interfaces(self):
186 server, ifs = self._create_server_get_interfaces()
187 interface_count = len(ifs)
188 self.assertTrue(interface_count > 0)
189 self._check_interface(ifs[0])
190
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530191 try:
192 iface = self._test_create_interface(server)
193 except lib_exc.BadRequest as e:
194 msg = ('Multiple possible networks found, use a Network ID to be '
195 'more specific.')
196 if not CONF.compute.fixed_network_name and e.message == msg:
197 raise
198 else:
199 ifs.append(iface)
Dan Smith8ad1c472013-02-26 13:03:16 -0500200
201 iface = self._test_create_interface_by_network_id(server, ifs)
202 ifs.append(iface)
203
Maho Koshiya3fc12462015-12-14 19:03:12 +0900204 iface = self._test_create_interface_by_port_id(server, ifs)
205 ifs.append(iface)
206
Maho Koshiya7b629582016-02-22 10:59:01 +0900207 iface = self._test_create_interface_by_fixed_ips(server, ifs)
208 ifs.append(iface)
209
ghanshyama2364f12015-08-24 15:45:37 +0900210 _ifs = (self.client.list_interfaces(server['id'])
211 ['interfaceAttachments'])
Dan Smith8ad1c472013-02-26 13:03:16 -0500212 self._compare_iface_list(ifs, _ifs)
213
214 self._test_show_interface(server, ifs)
215
216 _ifs = self._test_delete_interface(server, ifs)
217 self.assertEqual(len(ifs) - 1, len(_ifs))
218
Masayuki Igawa750aa922014-03-20 09:46:37 +0900219 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800220 @test.idempotent_id('c7e0e60b-ee45-43d0-abeb-8596fd42a2f9')
Matthew Treinish2df97482014-06-13 15:02:26 -0400221 @test.services('network')
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900222 def test_add_remove_fixed_ip(self):
223 # Add and Remove the fixed IP to server.
224 server, ifs = self._create_server_get_interfaces()
225 interface_count = len(ifs)
226 self.assertTrue(interface_count > 0)
227 self._check_interface(ifs[0])
228 network_id = ifs[0]['net_id']
ghanshyam4c7d2a02015-09-14 16:05:13 +0900229 self.servers_client.add_fixed_ip(server['id'], networkId=network_id)
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900230 # Remove the fixed IP from server.
Ken'ichi Ohmichi76800242015-07-03 05:12:31 +0000231 server_detail = self.os.servers_client.show_server(
ghanshyam0f825252015-08-25 16:02:50 +0900232 server['id'])['server']
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900233 # Get the Fixed IP from server.
234 fixed_ip = None
235 for ip_set in server_detail['addresses']:
236 for ip in server_detail['addresses'][ip_set]:
237 if ip['OS-EXT-IPS:type'] == 'fixed':
238 fixed_ip = ip['addr']
239 break
240 if fixed_ip is not None:
241 break
ghanshyam4c7d2a02015-09-14 16:05:13 +0900242 self.servers_client.remove_fixed_ip(server['id'], address=fixed_ip)