blob: c0b58ffdf21dd16bad109ed34c3d718c99be6f49 [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
Sean Dague1937d092013-05-17 16:36:38 -040016from tempest.api.compute import base
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000017from tempest import config
Oleg Bondarevee50bb12014-01-16 00:01:34 +040018from tempest import exceptions
Yuiko Takadae9999d62014-03-06 09:22:54 +000019from tempest import test
Dan Smith8ad1c472013-02-26 13:03:16 -050020
21import time
22
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000023CONF = config.CONF
24
Dan Smith8ad1c472013-02-26 13:03:16 -050025
ivan-zhuf2b00502013-10-18 10:06:52 +080026class AttachInterfacesTestJSON(base.BaseV2ComputeTest):
Dan Smith8ad1c472013-02-26 13:03:16 -050027
28 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010029 def resource_setup(cls):
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000030 if not CONF.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040031 raise cls.skipException("Neutron is required")
Adam Gandelman7186f7a2014-07-23 09:28:56 -040032 if not CONF.compute_feature_enabled.interface_attach:
33 raise cls.skipException("Interface attachment is not available.")
Salvatore Orlando5a337242014-01-15 22:49:22 +000034 # This test class requires network and subnet
35 cls.set_network_resources(network=True, subnet=True)
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010036 super(AttachInterfacesTestJSON, cls).resource_setup()
David Kranzb9017e72013-05-07 11:16:29 -040037 cls.client = cls.os.interfaces_client
Dan Smith8ad1c472013-02-26 13:03:16 -050038
39 def _check_interface(self, iface, port_id=None, network_id=None,
venkata anil45375302014-12-30 10:41:43 +000040 fixed_ip=None, mac_addr=None):
Dan Smith8ad1c472013-02-26 13:03:16 -050041 self.assertIn('port_state', iface)
42 if port_id:
43 self.assertEqual(iface['port_id'], port_id)
44 if network_id:
45 self.assertEqual(iface['net_id'], network_id)
46 if fixed_ip:
47 self.assertEqual(iface['fixed_ips'][0]['ip_address'], fixed_ip)
venkata anil45375302014-12-30 10:41:43 +000048 if mac_addr:
49 self.assertEqual(iface['mac_addr'], mac_addr)
Dan Smith8ad1c472013-02-26 13:03:16 -050050
51 def _create_server_get_interfaces(self):
David Kranz0fb14292015-02-11 15:55:20 -050052 server = self.create_test_server(wait_until='ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050053 resp, ifs = self.client.list_interfaces(server['id'])
Ken'ichi Ohmichi5eb89e02014-03-04 23:45:43 +090054 self.assertEqual(200, resp.status)
Leo Toyodaba9e9092013-04-08 09:02:11 +090055 resp, body = self.client.wait_for_interface_status(
56 server['id'], ifs[0]['port_id'], 'ACTIVE')
57 ifs[0]['port_state'] = body['port_state']
Dan Smith8ad1c472013-02-26 13:03:16 -050058 return server, ifs
59
60 def _test_create_interface(self, server):
61 resp, iface = self.client.create_interface(server['id'])
Ken'ichi Ohmichi5eb89e02014-03-04 23:45:43 +090062 self.assertEqual(200, resp.status)
Leo Toyodaba9e9092013-04-08 09:02:11 +090063 resp, iface = self.client.wait_for_interface_status(
64 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050065 self._check_interface(iface)
66 return iface
67
68 def _test_create_interface_by_network_id(self, server, ifs):
69 network_id = ifs[0]['net_id']
70 resp, iface = self.client.create_interface(server['id'],
71 network_id=network_id)
Ken'ichi Ohmichi5eb89e02014-03-04 23:45:43 +090072 self.assertEqual(200, resp.status)
Leo Toyodaba9e9092013-04-08 09:02:11 +090073 resp, iface = self.client.wait_for_interface_status(
74 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050075 self._check_interface(iface, network_id=network_id)
76 return iface
77
78 def _test_show_interface(self, server, ifs):
79 iface = ifs[0]
80 resp, _iface = self.client.show_interface(server['id'],
81 iface['port_id'])
Ken'ichi Ohmichi5eb89e02014-03-04 23:45:43 +090082 self.assertEqual(200, resp.status)
venkata anil45375302014-12-30 10:41:43 +000083 self._check_interface(iface, port_id=_iface['port_id'],
84 network_id=_iface['net_id'],
85 fixed_ip=_iface['fixed_ips'][0]['ip_address'],
86 mac_addr=_iface['mac_addr'])
Dan Smith8ad1c472013-02-26 13:03:16 -050087
88 def _test_delete_interface(self, server, ifs):
89 # NOTE(danms): delete not the first or last, but one in the middle
90 iface = ifs[1]
Ken'ichi Ohmichi5eb89e02014-03-04 23:45:43 +090091 resp, _ = self.client.delete_interface(server['id'], iface['port_id'])
92 self.assertEqual(202, resp.status)
Oleg Bondarevee50bb12014-01-16 00:01:34 +040093 _ifs = self.client.list_interfaces(server['id'])[1]
94 start = int(time.time())
Dan Smith8ad1c472013-02-26 13:03:16 -050095
Oleg Bondarevee50bb12014-01-16 00:01:34 +040096 while len(ifs) == len(_ifs):
97 time.sleep(self.build_interval)
98 _ifs = self.client.list_interfaces(server['id'])[1]
99 timed_out = int(time.time()) - start >= self.build_timeout
100 if len(ifs) == len(_ifs) and timed_out:
101 message = ('Failed to delete interface within '
102 'the required time: %s sec.' % self.build_timeout)
103 raise exceptions.TimeoutException(message)
104
105 self.assertNotIn(iface['port_id'], [i['port_id'] for i in _ifs])
Dan Smith8ad1c472013-02-26 13:03:16 -0500106 return _ifs
107
108 def _compare_iface_list(self, list1, list2):
109 # NOTE(danms): port_state will likely have changed, so just
110 # confirm the port_ids are the same at least
111 list1 = [x['port_id'] for x in list1]
112 list2 = [x['port_id'] for x in list2]
113
114 self.assertEqual(sorted(list1), sorted(list2))
115
Yuiko Takadae9999d62014-03-06 09:22:54 +0000116 @test.attr(type='smoke')
Matthew Treinish2df97482014-06-13 15:02:26 -0400117 @test.services('network')
Dan Smith8ad1c472013-02-26 13:03:16 -0500118 def test_create_list_show_delete_interfaces(self):
119 server, ifs = self._create_server_get_interfaces()
120 interface_count = len(ifs)
121 self.assertTrue(interface_count > 0)
122 self._check_interface(ifs[0])
123
124 iface = self._test_create_interface(server)
125 ifs.append(iface)
126
127 iface = self._test_create_interface_by_network_id(server, ifs)
128 ifs.append(iface)
129
130 resp, _ifs = self.client.list_interfaces(server['id'])
131 self._compare_iface_list(ifs, _ifs)
132
133 self._test_show_interface(server, ifs)
134
135 _ifs = self._test_delete_interface(server, ifs)
136 self.assertEqual(len(ifs) - 1, len(_ifs))
137
Masayuki Igawa750aa922014-03-20 09:46:37 +0900138 @test.attr(type='smoke')
Matthew Treinish2df97482014-06-13 15:02:26 -0400139 @test.services('network')
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900140 def test_add_remove_fixed_ip(self):
141 # Add and Remove the fixed IP to server.
142 server, ifs = self._create_server_get_interfaces()
143 interface_count = len(ifs)
144 self.assertTrue(interface_count > 0)
145 self._check_interface(ifs[0])
146 network_id = ifs[0]['net_id']
147 resp, body = self.client.add_fixed_ip(server['id'],
148 network_id)
149 self.assertEqual(202, resp.status)
150 # Remove the fixed IP from server.
David Kranz0fb14292015-02-11 15:55:20 -0500151 server_detail = self.os.servers_client.get_server(
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900152 server['id'])
153 # Get the Fixed IP from server.
154 fixed_ip = None
155 for ip_set in server_detail['addresses']:
156 for ip in server_detail['addresses'][ip_set]:
157 if ip['OS-EXT-IPS:type'] == 'fixed':
158 fixed_ip = ip['addr']
159 break
160 if fixed_ip is not None:
161 break
162 resp, body = self.client.remove_fixed_ip(server['id'],
163 fixed_ip)
164 self.assertEqual(202, resp.status)