blob: 9e27f338ee4f3470f6256b051b34e805f1912ca5 [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."""
51 body = self.client.show_interface(server, port_id)
52 interface_status = body['port_state']
53 start = int(time.time())
54
55 while(interface_status != status):
56 time.sleep(self.build_interval)
57 body = self.client.show_interface(server, port_id)
58 interface_status = body['port_state']
59
60 timed_out = int(time.time()) - start >= self.build_timeout
61
62 if interface_status != status and timed_out:
63 message = ('Interface %s failed to reach %s status '
64 '(current %s) within the required time (%s s).' %
65 (port_id, status, interface_status,
66 self.build_timeout))
67 raise exceptions.TimeoutException(message)
68
69 return body
70
Dan Smith8ad1c472013-02-26 13:03:16 -050071 def _check_interface(self, iface, port_id=None, network_id=None,
venkata anil45375302014-12-30 10:41:43 +000072 fixed_ip=None, mac_addr=None):
Dan Smith8ad1c472013-02-26 13:03:16 -050073 self.assertIn('port_state', iface)
74 if port_id:
75 self.assertEqual(iface['port_id'], port_id)
76 if network_id:
77 self.assertEqual(iface['net_id'], network_id)
78 if fixed_ip:
79 self.assertEqual(iface['fixed_ips'][0]['ip_address'], fixed_ip)
venkata anil45375302014-12-30 10:41:43 +000080 if mac_addr:
81 self.assertEqual(iface['mac_addr'], mac_addr)
Dan Smith8ad1c472013-02-26 13:03:16 -050082
83 def _create_server_get_interfaces(self):
David Kranz0fb14292015-02-11 15:55:20 -050084 server = self.create_test_server(wait_until='ACTIVE')
David Kranzb2b0c182015-02-18 13:28:19 -050085 ifs = self.client.list_interfaces(server['id'])
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000086 body = self.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +090087 server['id'], ifs[0]['port_id'], 'ACTIVE')
88 ifs[0]['port_state'] = body['port_state']
Dan Smith8ad1c472013-02-26 13:03:16 -050089 return server, ifs
90
91 def _test_create_interface(self, server):
David Kranzb2b0c182015-02-18 13:28:19 -050092 iface = self.client.create_interface(server['id'])
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +000093 iface = self.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +090094 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050095 self._check_interface(iface)
96 return iface
97
98 def _test_create_interface_by_network_id(self, server, ifs):
99 network_id = ifs[0]['net_id']
David Kranzb2b0c182015-02-18 13:28:19 -0500100 iface = self.client.create_interface(server['id'],
Ken'ichi Ohmichi12e76a12015-07-17 10:11:23 +0000101 net_id=network_id)
Ken'ichi Ohmichi84e99682015-07-08 05:28:57 +0000102 iface = self.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +0900103 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -0500104 self._check_interface(iface, network_id=network_id)
105 return iface
106
107 def _test_show_interface(self, server, ifs):
108 iface = ifs[0]
David Kranzb2b0c182015-02-18 13:28:19 -0500109 _iface = self.client.show_interface(server['id'],
110 iface['port_id'])
venkata anil45375302014-12-30 10:41:43 +0000111 self._check_interface(iface, port_id=_iface['port_id'],
112 network_id=_iface['net_id'],
113 fixed_ip=_iface['fixed_ips'][0]['ip_address'],
114 mac_addr=_iface['mac_addr'])
Dan Smith8ad1c472013-02-26 13:03:16 -0500115
116 def _test_delete_interface(self, server, ifs):
117 # NOTE(danms): delete not the first or last, but one in the middle
118 iface = ifs[1]
David Kranzb2b0c182015-02-18 13:28:19 -0500119 self.client.delete_interface(server['id'], iface['port_id'])
120 _ifs = self.client.list_interfaces(server['id'])
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400121 start = int(time.time())
Dan Smith8ad1c472013-02-26 13:03:16 -0500122
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400123 while len(ifs) == len(_ifs):
124 time.sleep(self.build_interval)
David Kranzb2b0c182015-02-18 13:28:19 -0500125 _ifs = self.client.list_interfaces(server['id'])
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400126 timed_out = int(time.time()) - start >= self.build_timeout
127 if len(ifs) == len(_ifs) and timed_out:
128 message = ('Failed to delete interface within '
129 'the required time: %s sec.' % self.build_timeout)
130 raise exceptions.TimeoutException(message)
131
132 self.assertNotIn(iface['port_id'], [i['port_id'] for i in _ifs])
Dan Smith8ad1c472013-02-26 13:03:16 -0500133 return _ifs
134
135 def _compare_iface_list(self, list1, list2):
136 # NOTE(danms): port_state will likely have changed, so just
137 # confirm the port_ids are the same at least
138 list1 = [x['port_id'] for x in list1]
139 list2 = [x['port_id'] for x in list2]
140
141 self.assertEqual(sorted(list1), sorted(list2))
142
Chris Hoge7579c1a2015-02-26 14:12:15 -0800143 @test.idempotent_id('73fe8f02-590d-4bf1-b184-e9ca81065051')
Matthew Treinish2df97482014-06-13 15:02:26 -0400144 @test.services('network')
Dan Smith8ad1c472013-02-26 13:03:16 -0500145 def test_create_list_show_delete_interfaces(self):
146 server, ifs = self._create_server_get_interfaces()
147 interface_count = len(ifs)
148 self.assertTrue(interface_count > 0)
149 self._check_interface(ifs[0])
150
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530151 try:
152 iface = self._test_create_interface(server)
153 except lib_exc.BadRequest as e:
154 msg = ('Multiple possible networks found, use a Network ID to be '
155 'more specific.')
156 if not CONF.compute.fixed_network_name and e.message == msg:
157 raise
158 else:
159 ifs.append(iface)
Dan Smith8ad1c472013-02-26 13:03:16 -0500160
161 iface = self._test_create_interface_by_network_id(server, ifs)
162 ifs.append(iface)
163
David Kranzb2b0c182015-02-18 13:28:19 -0500164 _ifs = self.client.list_interfaces(server['id'])
Dan Smith8ad1c472013-02-26 13:03:16 -0500165 self._compare_iface_list(ifs, _ifs)
166
167 self._test_show_interface(server, ifs)
168
169 _ifs = self._test_delete_interface(server, ifs)
170 self.assertEqual(len(ifs) - 1, len(_ifs))
171
Masayuki Igawa750aa922014-03-20 09:46:37 +0900172 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800173 @test.idempotent_id('c7e0e60b-ee45-43d0-abeb-8596fd42a2f9')
Matthew Treinish2df97482014-06-13 15:02:26 -0400174 @test.services('network')
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900175 def test_add_remove_fixed_ip(self):
176 # Add and Remove the fixed IP to server.
177 server, ifs = self._create_server_get_interfaces()
178 interface_count = len(ifs)
179 self.assertTrue(interface_count > 0)
180 self._check_interface(ifs[0])
181 network_id = ifs[0]['net_id']
Ken'ichi Ohmichi12e76a12015-07-17 10:11:23 +0000182 self.client.add_fixed_ip(server['id'], networkId=network_id)
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900183 # Remove the fixed IP from server.
Ken'ichi Ohmichi76800242015-07-03 05:12:31 +0000184 server_detail = self.os.servers_client.show_server(
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900185 server['id'])
186 # Get the Fixed IP from server.
187 fixed_ip = None
188 for ip_set in server_detail['addresses']:
189 for ip in server_detail['addresses'][ip_set]:
190 if ip['OS-EXT-IPS:type'] == 'fixed':
191 fixed_ip = ip['addr']
192 break
193 if fixed_ip is not None:
194 break
Ken'ichi Ohmichi12e76a12015-07-17 10:11:23 +0000195 self.client.remove_fixed_ip(server['id'], address=fixed_ip)