blob: 24d503fa11d95ec384af2d5ed725a82a21a00843 [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
18from tempest_lib import exceptions as lib_exc
19
Sean Dague1937d092013-05-17 16:36:38 -040020from tempest.api.compute import base
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000021from tempest import config
Oleg Bondarevee50bb12014-01-16 00:01:34 +040022from tempest import exceptions
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
Dan Smith8ad1c472013-02-26 13:03:16 -050048
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000049 def wait_for_interface_status(self, server, port_id, status):
50 """Waits for a interface to reach a given status."""
ghanshyama2364f12015-08-24 15:45:37 +090051 body = (self.client.show_interface(server, port_id)
52 ['interfaceAttachment'])
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000053 interface_status = body['port_state']
54 start = int(time.time())
55
56 while(interface_status != status):
57 time.sleep(self.build_interval)
ghanshyama2364f12015-08-24 15:45:37 +090058 body = (self.client.show_interface(server, port_id)
59 ['interfaceAttachment'])
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000060 interface_status = body['port_state']
61
62 timed_out = int(time.time()) - start >= self.build_timeout
63
64 if interface_status != status and timed_out:
65 message = ('Interface %s failed to reach %s status '
66 '(current %s) within the required time (%s s).' %
67 (port_id, status, interface_status,
68 self.build_timeout))
69 raise exceptions.TimeoutException(message)
70
71 return body
72
Dan Smith8ad1c472013-02-26 13:03:16 -050073 def _check_interface(self, iface, port_id=None, network_id=None,
venkata anil45375302014-12-30 10:41:43 +000074 fixed_ip=None, mac_addr=None):
Dan Smith8ad1c472013-02-26 13:03:16 -050075 self.assertIn('port_state', iface)
76 if port_id:
77 self.assertEqual(iface['port_id'], port_id)
78 if network_id:
79 self.assertEqual(iface['net_id'], network_id)
80 if fixed_ip:
81 self.assertEqual(iface['fixed_ips'][0]['ip_address'], fixed_ip)
venkata anil45375302014-12-30 10:41:43 +000082 if mac_addr:
83 self.assertEqual(iface['mac_addr'], mac_addr)
Dan Smith8ad1c472013-02-26 13:03:16 -050084
85 def _create_server_get_interfaces(self):
David Kranz0fb14292015-02-11 15:55:20 -050086 server = self.create_test_server(wait_until='ACTIVE')
ghanshyama2364f12015-08-24 15:45:37 +090087 ifs = (self.client.list_interfaces(server['id'])
88 ['interfaceAttachments'])
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000089 body = self.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +090090 server['id'], ifs[0]['port_id'], 'ACTIVE')
91 ifs[0]['port_state'] = body['port_state']
Dan Smith8ad1c472013-02-26 13:03:16 -050092 return server, ifs
93
94 def _test_create_interface(self, server):
ghanshyama2364f12015-08-24 15:45:37 +090095 iface = (self.client.create_interface(server['id'])
96 ['interfaceAttachment'])
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000097 iface = self.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +090098 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050099 self._check_interface(iface)
100 return iface
101
102 def _test_create_interface_by_network_id(self, server, ifs):
103 network_id = ifs[0]['net_id']
ghanshyama2364f12015-08-24 15:45:37 +0900104 iface = self.client.create_interface(
105 server['id'], net_id=network_id)['interfaceAttachment']
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +0000106 iface = self.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +0900107 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -0500108 self._check_interface(iface, network_id=network_id)
109 return iface
110
111 def _test_show_interface(self, server, ifs):
112 iface = ifs[0]
ghanshyama2364f12015-08-24 15:45:37 +0900113 _iface = self.client.show_interface(
114 server['id'], iface['port_id'])['interfaceAttachment']
venkata anil45375302014-12-30 10:41:43 +0000115 self._check_interface(iface, port_id=_iface['port_id'],
116 network_id=_iface['net_id'],
117 fixed_ip=_iface['fixed_ips'][0]['ip_address'],
118 mac_addr=_iface['mac_addr'])
Dan Smith8ad1c472013-02-26 13:03:16 -0500119
120 def _test_delete_interface(self, server, ifs):
121 # NOTE(danms): delete not the first or last, but one in the middle
122 iface = ifs[1]
David Kranzb2b0c182015-02-18 13:28:19 -0500123 self.client.delete_interface(server['id'], iface['port_id'])
ghanshyama2364f12015-08-24 15:45:37 +0900124 _ifs = (self.client.list_interfaces(server['id'])
125 ['interfaceAttachments'])
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400126 start = int(time.time())
Dan Smith8ad1c472013-02-26 13:03:16 -0500127
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400128 while len(ifs) == len(_ifs):
129 time.sleep(self.build_interval)
ghanshyama2364f12015-08-24 15:45:37 +0900130 _ifs = (self.client.list_interfaces(server['id'])
131 ['interfaceAttachments'])
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400132 timed_out = int(time.time()) - start >= self.build_timeout
133 if len(ifs) == len(_ifs) and timed_out:
134 message = ('Failed to delete interface within '
135 'the required time: %s sec.' % self.build_timeout)
136 raise exceptions.TimeoutException(message)
137
138 self.assertNotIn(iface['port_id'], [i['port_id'] for i in _ifs])
Dan Smith8ad1c472013-02-26 13:03:16 -0500139 return _ifs
140
141 def _compare_iface_list(self, list1, list2):
142 # NOTE(danms): port_state will likely have changed, so just
143 # confirm the port_ids are the same at least
144 list1 = [x['port_id'] for x in list1]
145 list2 = [x['port_id'] for x in list2]
146
147 self.assertEqual(sorted(list1), sorted(list2))
148
Chris Hoge7579c1a2015-02-26 14:12:15 -0800149 @test.idempotent_id('73fe8f02-590d-4bf1-b184-e9ca81065051')
Matthew Treinish2df97482014-06-13 15:02:26 -0400150 @test.services('network')
Dan Smith8ad1c472013-02-26 13:03:16 -0500151 def test_create_list_show_delete_interfaces(self):
152 server, ifs = self._create_server_get_interfaces()
153 interface_count = len(ifs)
154 self.assertTrue(interface_count > 0)
155 self._check_interface(ifs[0])
156
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530157 try:
158 iface = self._test_create_interface(server)
159 except lib_exc.BadRequest as e:
160 msg = ('Multiple possible networks found, use a Network ID to be '
161 'more specific.')
162 if not CONF.compute.fixed_network_name and e.message == msg:
163 raise
164 else:
165 ifs.append(iface)
Dan Smith8ad1c472013-02-26 13:03:16 -0500166
167 iface = self._test_create_interface_by_network_id(server, ifs)
168 ifs.append(iface)
169
ghanshyama2364f12015-08-24 15:45:37 +0900170 _ifs = (self.client.list_interfaces(server['id'])
171 ['interfaceAttachments'])
Dan Smith8ad1c472013-02-26 13:03:16 -0500172 self._compare_iface_list(ifs, _ifs)
173
174 self._test_show_interface(server, ifs)
175
176 _ifs = self._test_delete_interface(server, ifs)
177 self.assertEqual(len(ifs) - 1, len(_ifs))
178
Masayuki Igawa750aa922014-03-20 09:46:37 +0900179 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800180 @test.idempotent_id('c7e0e60b-ee45-43d0-abeb-8596fd42a2f9')
Matthew Treinish2df97482014-06-13 15:02:26 -0400181 @test.services('network')
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900182 def test_add_remove_fixed_ip(self):
183 # Add and Remove the fixed IP to server.
184 server, ifs = self._create_server_get_interfaces()
185 interface_count = len(ifs)
186 self.assertTrue(interface_count > 0)
187 self._check_interface(ifs[0])
188 network_id = ifs[0]['net_id']
ghanshyam4c7d2a02015-09-14 16:05:13 +0900189 self.servers_client.add_fixed_ip(server['id'], networkId=network_id)
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900190 # Remove the fixed IP from server.
Ken'ichi Ohmichi76800242015-07-03 05:12:31 +0000191 server_detail = self.os.servers_client.show_server(
ghanshyam0f825252015-08-25 16:02:50 +0900192 server['id'])['server']
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900193 # Get the Fixed IP from server.
194 fixed_ip = None
195 for ip_set in server_detail['addresses']:
196 for ip in server_detail['addresses'][ip_set]:
197 if ip['OS-EXT-IPS:type'] == 'fixed':
198 fixed_ip = ip['addr']
199 break
200 if fixed_ip is not None:
201 break
ghanshyam4c7d2a02015-09-14 16:05:13 +0900202 self.servers_client.remove_fixed_ip(server['id'], address=fixed_ip)