blob: 9f66a6c202225ec997ef19832429e20ddb569992 [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
Giampaolo Lauriae9c77022013-05-22 01:23:58 -040017from tempest.test import attr
Dan Smith8ad1c472013-02-26 13:03:16 -050018
19import time
20
21
22class AttachInterfacesTestJSON(base.BaseComputeTest):
23 _interface = 'json'
24
25 @classmethod
26 def setUpClass(cls):
Matthew Treinishfaa340d2013-07-19 16:26:21 -040027 if not cls.config.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040028 raise cls.skipException("Neutron is required")
David Kranzb9017e72013-05-07 11:16:29 -040029 super(AttachInterfacesTestJSON, cls).setUpClass()
30 cls.client = cls.os.interfaces_client
Dan Smith8ad1c472013-02-26 13:03:16 -050031
32 def _check_interface(self, iface, port_id=None, network_id=None,
33 fixed_ip=None):
34 self.assertIn('port_state', iface)
35 if port_id:
36 self.assertEqual(iface['port_id'], port_id)
37 if network_id:
38 self.assertEqual(iface['net_id'], network_id)
39 if fixed_ip:
40 self.assertEqual(iface['fixed_ips'][0]['ip_address'], fixed_ip)
41
42 def _create_server_get_interfaces(self):
Leo Toyodaba9e9092013-04-08 09:02:11 +090043 resp, server = self.create_server()
Dan Smith8ad1c472013-02-26 13:03:16 -050044 self.os.servers_client.wait_for_server_status(server['id'], 'ACTIVE')
45 resp, ifs = self.client.list_interfaces(server['id'])
Leo Toyodaba9e9092013-04-08 09:02:11 +090046 resp, body = self.client.wait_for_interface_status(
47 server['id'], ifs[0]['port_id'], 'ACTIVE')
48 ifs[0]['port_state'] = body['port_state']
Dan Smith8ad1c472013-02-26 13:03:16 -050049 return server, ifs
50
51 def _test_create_interface(self, server):
52 resp, iface = self.client.create_interface(server['id'])
Leo Toyodaba9e9092013-04-08 09:02:11 +090053 resp, iface = self.client.wait_for_interface_status(
54 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050055 self._check_interface(iface)
56 return iface
57
58 def _test_create_interface_by_network_id(self, server, ifs):
59 network_id = ifs[0]['net_id']
60 resp, iface = self.client.create_interface(server['id'],
61 network_id=network_id)
Leo Toyodaba9e9092013-04-08 09:02:11 +090062 resp, iface = self.client.wait_for_interface_status(
63 server['id'], iface['port_id'], 'ACTIVE')
Dan Smith8ad1c472013-02-26 13:03:16 -050064 self._check_interface(iface, network_id=network_id)
65 return iface
66
67 def _test_show_interface(self, server, ifs):
68 iface = ifs[0]
69 resp, _iface = self.client.show_interface(server['id'],
70 iface['port_id'])
71 self.assertEqual(iface, _iface)
72
73 def _test_delete_interface(self, server, ifs):
74 # NOTE(danms): delete not the first or last, but one in the middle
75 iface = ifs[1]
76 self.client.delete_interface(server['id'], iface['port_id'])
77 for i in range(0, 5):
78 _r, _ifs = self.client.list_interfaces(server['id'])
79 if len(ifs) != len(_ifs):
80 break
81 time.sleep(1)
82
83 self.assertEqual(len(_ifs), len(ifs) - 1)
84 for _iface in _ifs:
85 self.assertNotEqual(iface['port_id'], _iface['port_id'])
86 return _ifs
87
88 def _compare_iface_list(self, list1, list2):
89 # NOTE(danms): port_state will likely have changed, so just
90 # confirm the port_ids are the same at least
91 list1 = [x['port_id'] for x in list1]
92 list2 = [x['port_id'] for x in list2]
93
94 self.assertEqual(sorted(list1), sorted(list2))
95
Giampaolo Lauriae9c77022013-05-22 01:23:58 -040096 @attr(type='gate')
Dan Smith8ad1c472013-02-26 13:03:16 -050097 def test_create_list_show_delete_interfaces(self):
98 server, ifs = self._create_server_get_interfaces()
99 interface_count = len(ifs)
100 self.assertTrue(interface_count > 0)
101 self._check_interface(ifs[0])
102
103 iface = self._test_create_interface(server)
104 ifs.append(iface)
105
106 iface = self._test_create_interface_by_network_id(server, ifs)
107 ifs.append(iface)
108
109 resp, _ifs = self.client.list_interfaces(server['id'])
110 self._compare_iface_list(ifs, _ifs)
111
112 self._test_show_interface(server, ifs)
113
114 _ifs = self._test_delete_interface(server, ifs)
115 self.assertEqual(len(ifs) - 1, len(_ifs))
116
117
118class AttachInterfacesTestXML(AttachInterfacesTestJSON):
119 _interface = 'xml'