blob: 7ce62695d7ea632ebcd2b79c7387848fb95cdbee [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
49 def _check_interface(self, iface, port_id=None, network_id=None,
venkata anil45375302014-12-30 10:41:43 +000050 fixed_ip=None, mac_addr=None):
Dan Smith8ad1c472013-02-26 13:03:16 -050051 self.assertIn('port_state', iface)
52 if port_id:
53 self.assertEqual(iface['port_id'], port_id)
54 if network_id:
55 self.assertEqual(iface['net_id'], network_id)
56 if fixed_ip:
57 self.assertEqual(iface['fixed_ips'][0]['ip_address'], fixed_ip)
venkata anil45375302014-12-30 10:41:43 +000058 if mac_addr:
59 self.assertEqual(iface['mac_addr'], mac_addr)
Dan Smith8ad1c472013-02-26 13:03:16 -050060
61 def _create_server_get_interfaces(self):
David Kranz0fb14292015-02-11 15:55:20 -050062 server = self.create_test_server(wait_until='ACTIVE')
David Kranzb2b0c182015-02-18 13:28:19 -050063 ifs = self.client.list_interfaces(server['id'])
64 body = self.client.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +090065 server['id'], ifs[0]['port_id'], 'ACTIVE')
66 ifs[0]['port_state'] = body['port_state']
Dan Smith8ad1c472013-02-26 13:03:16 -050067 return server, ifs
68
69 def _test_create_interface(self, server):
David Kranzb2b0c182015-02-18 13:28:19 -050070 iface = self.client.create_interface(server['id'])
71 iface = self.client.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +090072 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050073 self._check_interface(iface)
74 return iface
75
76 def _test_create_interface_by_network_id(self, server, ifs):
77 network_id = ifs[0]['net_id']
David Kranzb2b0c182015-02-18 13:28:19 -050078 iface = self.client.create_interface(server['id'],
79 network_id=network_id)
80 iface = self.client.wait_for_interface_status(
Leo Toyodaba9e9092013-04-08 09:02:11 +090081 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050082 self._check_interface(iface, network_id=network_id)
83 return iface
84
85 def _test_show_interface(self, server, ifs):
86 iface = ifs[0]
David Kranzb2b0c182015-02-18 13:28:19 -050087 _iface = self.client.show_interface(server['id'],
88 iface['port_id'])
venkata anil45375302014-12-30 10:41:43 +000089 self._check_interface(iface, port_id=_iface['port_id'],
90 network_id=_iface['net_id'],
91 fixed_ip=_iface['fixed_ips'][0]['ip_address'],
92 mac_addr=_iface['mac_addr'])
Dan Smith8ad1c472013-02-26 13:03:16 -050093
94 def _test_delete_interface(self, server, ifs):
95 # NOTE(danms): delete not the first or last, but one in the middle
96 iface = ifs[1]
David Kranzb2b0c182015-02-18 13:28:19 -050097 self.client.delete_interface(server['id'], iface['port_id'])
98 _ifs = self.client.list_interfaces(server['id'])
Oleg Bondarevee50bb12014-01-16 00:01:34 +040099 start = int(time.time())
Dan Smith8ad1c472013-02-26 13:03:16 -0500100
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400101 while len(ifs) == len(_ifs):
102 time.sleep(self.build_interval)
David Kranzb2b0c182015-02-18 13:28:19 -0500103 _ifs = self.client.list_interfaces(server['id'])
Oleg Bondarevee50bb12014-01-16 00:01:34 +0400104 timed_out = int(time.time()) - start >= self.build_timeout
105 if len(ifs) == len(_ifs) and timed_out:
106 message = ('Failed to delete interface within '
107 'the required time: %s sec.' % self.build_timeout)
108 raise exceptions.TimeoutException(message)
109
110 self.assertNotIn(iface['port_id'], [i['port_id'] for i in _ifs])
Dan Smith8ad1c472013-02-26 13:03:16 -0500111 return _ifs
112
113 def _compare_iface_list(self, list1, list2):
114 # NOTE(danms): port_state will likely have changed, so just
115 # confirm the port_ids are the same at least
116 list1 = [x['port_id'] for x in list1]
117 list2 = [x['port_id'] for x in list2]
118
119 self.assertEqual(sorted(list1), sorted(list2))
120
Chris Hoge7579c1a2015-02-26 14:12:15 -0800121 @test.idempotent_id('73fe8f02-590d-4bf1-b184-e9ca81065051')
Matthew Treinish2df97482014-06-13 15:02:26 -0400122 @test.services('network')
Dan Smith8ad1c472013-02-26 13:03:16 -0500123 def test_create_list_show_delete_interfaces(self):
124 server, ifs = self._create_server_get_interfaces()
125 interface_count = len(ifs)
126 self.assertTrue(interface_count > 0)
127 self._check_interface(ifs[0])
128
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530129 try:
130 iface = self._test_create_interface(server)
131 except lib_exc.BadRequest as e:
132 msg = ('Multiple possible networks found, use a Network ID to be '
133 'more specific.')
134 if not CONF.compute.fixed_network_name and e.message == msg:
135 raise
136 else:
137 ifs.append(iface)
Dan Smith8ad1c472013-02-26 13:03:16 -0500138
139 iface = self._test_create_interface_by_network_id(server, ifs)
140 ifs.append(iface)
141
David Kranzb2b0c182015-02-18 13:28:19 -0500142 _ifs = self.client.list_interfaces(server['id'])
Dan Smith8ad1c472013-02-26 13:03:16 -0500143 self._compare_iface_list(ifs, _ifs)
144
145 self._test_show_interface(server, ifs)
146
147 _ifs = self._test_delete_interface(server, ifs)
148 self.assertEqual(len(ifs) - 1, len(_ifs))
149
Masayuki Igawa750aa922014-03-20 09:46:37 +0900150 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800151 @test.idempotent_id('c7e0e60b-ee45-43d0-abeb-8596fd42a2f9')
Matthew Treinish2df97482014-06-13 15:02:26 -0400152 @test.services('network')
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900153 def test_add_remove_fixed_ip(self):
154 # Add and Remove the fixed IP to server.
155 server, ifs = self._create_server_get_interfaces()
156 interface_count = len(ifs)
157 self.assertTrue(interface_count > 0)
158 self._check_interface(ifs[0])
159 network_id = ifs[0]['net_id']
David Kranzb2b0c182015-02-18 13:28:19 -0500160 self.client.add_fixed_ip(server['id'], network_id)
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900161 # Remove the fixed IP from server.
David Kranz0fb14292015-02-11 15:55:20 -0500162 server_detail = self.os.servers_client.get_server(
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900163 server['id'])
164 # Get the Fixed IP from server.
165 fixed_ip = None
166 for ip_set in server_detail['addresses']:
167 for ip in server_detail['addresses'][ip_set]:
168 if ip['OS-EXT-IPS:type'] == 'fixed':
169 fixed_ip = ip['addr']
170 break
171 if fixed_ip is not None:
172 break
David Kranzb2b0c182015-02-18 13:28:19 -0500173 self.client.remove_fixed_ip(server['id'], fixed_ip)