blob: f6eed00ecc425f8af014c42e197ff32a71acdc79 [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
29 def setUpClass(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")
Salvatore Orlando5a337242014-01-15 22:49:22 +000032 # This test class requires network and subnet
33 cls.set_network_resources(network=True, subnet=True)
David Kranzb9017e72013-05-07 11:16:29 -040034 super(AttachInterfacesTestJSON, cls).setUpClass()
35 cls.client = cls.os.interfaces_client
Dan Smith8ad1c472013-02-26 13:03:16 -050036
37 def _check_interface(self, iface, port_id=None, network_id=None,
38 fixed_ip=None):
39 self.assertIn('port_state', iface)
40 if port_id:
41 self.assertEqual(iface['port_id'], port_id)
42 if network_id:
43 self.assertEqual(iface['net_id'], network_id)
44 if fixed_ip:
45 self.assertEqual(iface['fixed_ips'][0]['ip_address'], fixed_ip)
46
47 def _create_server_get_interfaces(self):
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +090048 resp, server = self.create_test_server(wait_until='ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050049 resp, ifs = self.client.list_interfaces(server['id'])
Ken'ichi Ohmichi5eb89e02014-03-04 23:45:43 +090050 self.assertEqual(200, resp.status)
Leo Toyodaba9e9092013-04-08 09:02:11 +090051 resp, body = self.client.wait_for_interface_status(
52 server['id'], ifs[0]['port_id'], 'ACTIVE')
53 ifs[0]['port_state'] = body['port_state']
Dan Smith8ad1c472013-02-26 13:03:16 -050054 return server, ifs
55
56 def _test_create_interface(self, server):
57 resp, iface = self.client.create_interface(server['id'])
Ken'ichi Ohmichi5eb89e02014-03-04 23:45:43 +090058 self.assertEqual(200, resp.status)
Leo Toyodaba9e9092013-04-08 09:02:11 +090059 resp, iface = self.client.wait_for_interface_status(
60 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050061 self._check_interface(iface)
62 return iface
63
64 def _test_create_interface_by_network_id(self, server, ifs):
65 network_id = ifs[0]['net_id']
66 resp, iface = self.client.create_interface(server['id'],
67 network_id=network_id)
Ken'ichi Ohmichi5eb89e02014-03-04 23:45:43 +090068 self.assertEqual(200, resp.status)
Leo Toyodaba9e9092013-04-08 09:02:11 +090069 resp, iface = self.client.wait_for_interface_status(
70 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050071 self._check_interface(iface, network_id=network_id)
72 return iface
73
74 def _test_show_interface(self, server, ifs):
75 iface = ifs[0]
76 resp, _iface = self.client.show_interface(server['id'],
77 iface['port_id'])
Ken'ichi Ohmichi5eb89e02014-03-04 23:45:43 +090078 self.assertEqual(200, resp.status)
Dan Smith8ad1c472013-02-26 13:03:16 -050079 self.assertEqual(iface, _iface)
80
81 def _test_delete_interface(self, server, ifs):
82 # NOTE(danms): delete not the first or last, but one in the middle
83 iface = ifs[1]
Ken'ichi Ohmichi5eb89e02014-03-04 23:45:43 +090084 resp, _ = self.client.delete_interface(server['id'], iface['port_id'])
85 self.assertEqual(202, resp.status)
Oleg Bondarevee50bb12014-01-16 00:01:34 +040086 _ifs = self.client.list_interfaces(server['id'])[1]
87 start = int(time.time())
Dan Smith8ad1c472013-02-26 13:03:16 -050088
Oleg Bondarevee50bb12014-01-16 00:01:34 +040089 while len(ifs) == len(_ifs):
90 time.sleep(self.build_interval)
91 _ifs = self.client.list_interfaces(server['id'])[1]
92 timed_out = int(time.time()) - start >= self.build_timeout
93 if len(ifs) == len(_ifs) and timed_out:
94 message = ('Failed to delete interface within '
95 'the required time: %s sec.' % self.build_timeout)
96 raise exceptions.TimeoutException(message)
97
98 self.assertNotIn(iface['port_id'], [i['port_id'] for i in _ifs])
Dan Smith8ad1c472013-02-26 13:03:16 -050099 return _ifs
100
101 def _compare_iface_list(self, list1, list2):
102 # NOTE(danms): port_state will likely have changed, so just
103 # confirm the port_ids are the same at least
104 list1 = [x['port_id'] for x in list1]
105 list2 = [x['port_id'] for x in list2]
106
107 self.assertEqual(sorted(list1), sorted(list2))
108
Yuiko Takadae9999d62014-03-06 09:22:54 +0000109 @test.attr(type='smoke')
Dan Smith8ad1c472013-02-26 13:03:16 -0500110 def test_create_list_show_delete_interfaces(self):
111 server, ifs = self._create_server_get_interfaces()
112 interface_count = len(ifs)
113 self.assertTrue(interface_count > 0)
114 self._check_interface(ifs[0])
115
116 iface = self._test_create_interface(server)
117 ifs.append(iface)
118
119 iface = self._test_create_interface_by_network_id(server, ifs)
120 ifs.append(iface)
121
122 resp, _ifs = self.client.list_interfaces(server['id'])
123 self._compare_iface_list(ifs, _ifs)
124
125 self._test_show_interface(server, ifs)
126
127 _ifs = self._test_delete_interface(server, ifs)
128 self.assertEqual(len(ifs) - 1, len(_ifs))
129
Yuiko Takadae9999d62014-03-06 09:22:54 +0000130 @test.attr(type='gate')
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900131 def test_add_remove_fixed_ip(self):
132 # Add and Remove the fixed IP to server.
133 server, ifs = self._create_server_get_interfaces()
134 interface_count = len(ifs)
135 self.assertTrue(interface_count > 0)
136 self._check_interface(ifs[0])
137 network_id = ifs[0]['net_id']
138 resp, body = self.client.add_fixed_ip(server['id'],
139 network_id)
140 self.assertEqual(202, resp.status)
141 # Remove the fixed IP from server.
142 server_resp, server_detail = self.os.servers_client.get_server(
143 server['id'])
144 # Get the Fixed IP from server.
145 fixed_ip = None
146 for ip_set in server_detail['addresses']:
147 for ip in server_detail['addresses'][ip_set]:
148 if ip['OS-EXT-IPS:type'] == 'fixed':
149 fixed_ip = ip['addr']
150 break
151 if fixed_ip is not None:
152 break
153 resp, body = self.client.remove_fixed_ip(server['id'],
154 fixed_ip)
155 self.assertEqual(202, resp.status)
156
Dan Smith8ad1c472013-02-26 13:03:16 -0500157
158class AttachInterfacesTestXML(AttachInterfacesTestJSON):
159 _interface = 'xml'