blob: 82013632100a5f0602fed03e74c5e12e5488c30d [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
Maho Koshiya7b629582016-02-22 10:59:01 +090016import netaddr
Rohan Kanade9ce97df2013-12-10 18:59:35 +053017import time
18
Sean Dague1937d092013-05-17 16:36:38 -040019from tempest.api.compute import base
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']
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 Smith8ad1c472013-02-26 13:03:16 -0500142 def _test_show_interface(self, server, ifs):
143 iface = ifs[0]
ghanshyama2364f12015-08-24 15:45:37 +0900144 _iface = self.client.show_interface(
145 server['id'], iface['port_id'])['interfaceAttachment']
venkata anil45375302014-12-30 10:41:43 +0000146 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 Smith8ad1c472013-02-26 13:03:16 -0500150
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 Kranzb2b0c182015-02-18 13:28:19 -0500154 self.client.delete_interface(server['id'], iface['port_id'])
ghanshyama2364f12015-08-24 15:45:37 +0900155 _ifs = (self.client.list_interfaces(server['id'])
156 ['interfaceAttachments'])
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400157 start = int(time.time())
Dan Smith8ad1c472013-02-26 13:03:16 -0500158
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400159 while len(ifs) == len(_ifs):
160 time.sleep(self.build_interval)
ghanshyama2364f12015-08-24 15:45:37 +0900161 _ifs = (self.client.list_interfaces(server['id'])
162 ['interfaceAttachments'])
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400163 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 Smith8ad1c472013-02-26 13:03:16 -0500170 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 Hoge7579c1a2015-02-26 14:12:15 -0800180 @test.idempotent_id('73fe8f02-590d-4bf1-b184-e9ca81065051')
Matthew Treinish2df97482014-06-13 15:02:26 -0400181 @test.services('network')
Dan Smith8ad1c472013-02-26 13:03:16 -0500182 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 Kanade9ce97df2013-12-10 18:59:35 +0530188 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 Smith8ad1c472013-02-26 13:03:16 -0500197
198 iface = self._test_create_interface_by_network_id(server, ifs)
199 ifs.append(iface)
200
Maho Koshiya3fc12462015-12-14 19:03:12 +0900201 iface = self._test_create_interface_by_port_id(server, ifs)
202 ifs.append(iface)
203
Maho Koshiya7b629582016-02-22 10:59:01 +0900204 iface = self._test_create_interface_by_fixed_ips(server, ifs)
205 ifs.append(iface)
206
ghanshyama2364f12015-08-24 15:45:37 +0900207 _ifs = (self.client.list_interfaces(server['id'])
208 ['interfaceAttachments'])
Dan Smith8ad1c472013-02-26 13:03:16 -0500209 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 Igawa750aa922014-03-20 09:46:37 +0900216 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800217 @test.idempotent_id('c7e0e60b-ee45-43d0-abeb-8596fd42a2f9')
Matthew Treinish2df97482014-06-13 15:02:26 -0400218 @test.services('network')
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900219 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']
ghanshyam4c7d2a02015-09-14 16:05:13 +0900226 self.servers_client.add_fixed_ip(server['id'], networkId=network_id)
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900227 # Remove the fixed IP from server.
Ken'ichi Ohmichi76800242015-07-03 05:12:31 +0000228 server_detail = self.os.servers_client.show_server(
ghanshyam0f825252015-08-25 16:02:50 +0900229 server['id'])['server']
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900230 # 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
ghanshyam4c7d2a02015-09-14 16:05:13 +0900239 self.servers_client.remove_fixed_ip(server['id'], address=fixed_ip)