Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 1 | # 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 Dague | 1937d09 | 2013-05-17 16:36:38 -0400 | [diff] [blame] | 16 | from tempest.api.compute import base |
Giampaolo Lauria | e9c7702 | 2013-05-22 01:23:58 -0400 | [diff] [blame] | 17 | from tempest.test import attr |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 18 | |
| 19 | import time |
| 20 | |
| 21 | |
| 22 | class AttachInterfacesTestJSON(base.BaseComputeTest): |
| 23 | _interface = 'json' |
| 24 | |
| 25 | @classmethod |
| 26 | def setUpClass(cls): |
Matthew Treinish | faa340d | 2013-07-19 16:26:21 -0400 | [diff] [blame] | 27 | if not cls.config.service_available.neutron: |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 28 | raise cls.skipException("Neutron is required") |
David Kranz | b9017e7 | 2013-05-07 11:16:29 -0400 | [diff] [blame] | 29 | super(AttachInterfacesTestJSON, cls).setUpClass() |
| 30 | cls.client = cls.os.interfaces_client |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 31 | |
| 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 Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 43 | resp, server = self.create_server() |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 44 | self.os.servers_client.wait_for_server_status(server['id'], 'ACTIVE') |
| 45 | resp, ifs = self.client.list_interfaces(server['id']) |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 46 | 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 Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 49 | return server, ifs |
| 50 | |
| 51 | def _test_create_interface(self, server): |
| 52 | resp, iface = self.client.create_interface(server['id']) |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 53 | resp, iface = self.client.wait_for_interface_status( |
| 54 | server['id'], iface['port_id'], 'ACTIVE') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 55 | 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 Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 62 | resp, iface = self.client.wait_for_interface_status( |
| 63 | server['id'], iface['port_id'], 'ACTIVE') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 64 | 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 Lauria | e9c7702 | 2013-05-22 01:23:58 -0400 | [diff] [blame] | 96 | @attr(type='gate') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 97 | 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 | |
| 118 | class AttachInterfacesTestXML(AttachInterfacesTestJSON): |
| 119 | _interface = 'xml' |