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 | |
ivan-zhu | f2b0050 | 2013-10-18 10:06:52 +0800 | [diff] [blame] | 22 | class AttachInterfacesTestJSON(base.BaseV2ComputeTest): |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 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") |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame^] | 29 | # This test class requires network and subnet |
| 30 | cls.set_network_resources(network=True, subnet=True) |
David Kranz | b9017e7 | 2013-05-07 11:16:29 -0400 | [diff] [blame] | 31 | super(AttachInterfacesTestJSON, cls).setUpClass() |
| 32 | cls.client = cls.os.interfaces_client |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 33 | |
| 34 | def _check_interface(self, iface, port_id=None, network_id=None, |
| 35 | fixed_ip=None): |
| 36 | self.assertIn('port_state', iface) |
| 37 | if port_id: |
| 38 | self.assertEqual(iface['port_id'], port_id) |
| 39 | if network_id: |
| 40 | self.assertEqual(iface['net_id'], network_id) |
| 41 | if fixed_ip: |
| 42 | self.assertEqual(iface['fixed_ips'][0]['ip_address'], fixed_ip) |
| 43 | |
| 44 | def _create_server_get_interfaces(self): |
Ken'ichi Ohmichi | cfc052e | 2013-10-23 11:50:04 +0900 | [diff] [blame] | 45 | resp, server = self.create_test_server(wait_until='ACTIVE') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 46 | resp, ifs = self.client.list_interfaces(server['id']) |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 47 | resp, body = self.client.wait_for_interface_status( |
| 48 | server['id'], ifs[0]['port_id'], 'ACTIVE') |
| 49 | ifs[0]['port_state'] = body['port_state'] |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 50 | return server, ifs |
| 51 | |
| 52 | def _test_create_interface(self, server): |
| 53 | resp, iface = self.client.create_interface(server['id']) |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 54 | resp, iface = self.client.wait_for_interface_status( |
| 55 | server['id'], iface['port_id'], 'ACTIVE') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 56 | self._check_interface(iface) |
| 57 | return iface |
| 58 | |
| 59 | def _test_create_interface_by_network_id(self, server, ifs): |
| 60 | network_id = ifs[0]['net_id'] |
| 61 | resp, iface = self.client.create_interface(server['id'], |
| 62 | network_id=network_id) |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 63 | resp, iface = self.client.wait_for_interface_status( |
| 64 | server['id'], iface['port_id'], 'ACTIVE') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 65 | self._check_interface(iface, network_id=network_id) |
| 66 | return iface |
| 67 | |
| 68 | def _test_show_interface(self, server, ifs): |
| 69 | iface = ifs[0] |
| 70 | resp, _iface = self.client.show_interface(server['id'], |
| 71 | iface['port_id']) |
| 72 | self.assertEqual(iface, _iface) |
| 73 | |
| 74 | def _test_delete_interface(self, server, ifs): |
| 75 | # NOTE(danms): delete not the first or last, but one in the middle |
| 76 | iface = ifs[1] |
| 77 | self.client.delete_interface(server['id'], iface['port_id']) |
| 78 | for i in range(0, 5): |
| 79 | _r, _ifs = self.client.list_interfaces(server['id']) |
| 80 | if len(ifs) != len(_ifs): |
| 81 | break |
| 82 | time.sleep(1) |
| 83 | |
| 84 | self.assertEqual(len(_ifs), len(ifs) - 1) |
| 85 | for _iface in _ifs: |
| 86 | self.assertNotEqual(iface['port_id'], _iface['port_id']) |
| 87 | return _ifs |
| 88 | |
| 89 | def _compare_iface_list(self, list1, list2): |
| 90 | # NOTE(danms): port_state will likely have changed, so just |
| 91 | # confirm the port_ids are the same at least |
| 92 | list1 = [x['port_id'] for x in list1] |
| 93 | list2 = [x['port_id'] for x in list2] |
| 94 | |
| 95 | self.assertEqual(sorted(list1), sorted(list2)) |
| 96 | |
Attila Fazekas | f15c4eb | 2013-12-17 11:34:48 +0100 | [diff] [blame] | 97 | @attr(type='smoke') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 98 | def test_create_list_show_delete_interfaces(self): |
| 99 | server, ifs = self._create_server_get_interfaces() |
| 100 | interface_count = len(ifs) |
| 101 | self.assertTrue(interface_count > 0) |
| 102 | self._check_interface(ifs[0]) |
| 103 | |
| 104 | iface = self._test_create_interface(server) |
| 105 | ifs.append(iface) |
| 106 | |
| 107 | iface = self._test_create_interface_by_network_id(server, ifs) |
| 108 | ifs.append(iface) |
| 109 | |
| 110 | resp, _ifs = self.client.list_interfaces(server['id']) |
| 111 | self._compare_iface_list(ifs, _ifs) |
| 112 | |
| 113 | self._test_show_interface(server, ifs) |
| 114 | |
| 115 | _ifs = self._test_delete_interface(server, ifs) |
| 116 | self.assertEqual(len(ifs) - 1, len(_ifs)) |
| 117 | |
| 118 | |
| 119 | class AttachInterfacesTestXML(AttachInterfacesTestJSON): |
| 120 | _interface = 'xml' |