Ghanshyam | 4541bd1 | 2016-02-29 13:46:39 +0900 | [diff] [blame] | 1 | # Copyright 2016 NEC Corporation. |
| 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 | |
| 16 | from tempest.api.compute.keypairs import base |
Ken'ichi Ohmichi | 757833a | 2017-03-10 10:30:30 -0800 | [diff] [blame] | 17 | from tempest.lib.common.utils import data_utils |
Ken'ichi Ohmichi | ebbfd1c | 2017-01-27 16:37:00 -0800 | [diff] [blame] | 18 | from tempest.lib import decorators |
Ghanshyam | 4541bd1 | 2016-02-29 13:46:39 +0900 | [diff] [blame] | 19 | |
| 20 | |
| 21 | class KeyPairsV210TestJSON(base.BaseKeypairTest): |
| 22 | credentials = ['primary', 'admin'] |
| 23 | min_microversion = '2.10' |
| 24 | |
| 25 | @classmethod |
| 26 | def setup_clients(cls): |
| 27 | super(KeyPairsV210TestJSON, cls).setup_clients() |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 28 | cls.client = cls.os_admin.keypairs_client |
| 29 | cls.non_admin_client = cls.os_primary.keypairs_client |
Ghanshyam | 4541bd1 | 2016-02-29 13:46:39 +0900 | [diff] [blame] | 30 | |
| 31 | def _create_and_check_keypairs(self, user_id): |
| 32 | key_list = list() |
zhufl | 4311dc4 | 2017-01-26 16:26:18 +0800 | [diff] [blame] | 33 | for _ in range(2): |
Ghanshyam | 4541bd1 | 2016-02-29 13:46:39 +0900 | [diff] [blame] | 34 | k_name = data_utils.rand_name('keypair') |
zhufl | 5bcd7ee | 2017-01-13 17:47:14 +0800 | [diff] [blame] | 35 | keypair = self.create_keypair(k_name, |
| 36 | keypair_type='ssh', |
| 37 | user_id=user_id) |
Ghanshyam | 4541bd1 | 2016-02-29 13:46:39 +0900 | [diff] [blame] | 38 | self.assertEqual(k_name, keypair['name'], |
| 39 | "The created keypair name is not equal " |
| 40 | "to the requested name!") |
| 41 | self.assertEqual(user_id, keypair['user_id'], |
| 42 | "The created keypair is not for requested user!") |
| 43 | keypair.pop('private_key', None) |
| 44 | keypair.pop('user_id') |
| 45 | key_list.append(keypair) |
| 46 | return key_list |
| 47 | |
Ken'ichi Ohmichi | ebbfd1c | 2017-01-27 16:37:00 -0800 | [diff] [blame] | 48 | @decorators.idempotent_id('3c8484af-cfb3-48f6-b8ba-d5d58bbf3eac') |
Ghanshyam | 4541bd1 | 2016-02-29 13:46:39 +0900 | [diff] [blame] | 49 | def test_admin_manage_keypairs_for_other_users(self): |
| 50 | user_id = self.non_admin_client.user_id |
| 51 | key_list = self._create_and_check_keypairs(user_id) |
| 52 | first_keyname = key_list[0]['name'] |
| 53 | keypair_detail = self.client.show_keypair(first_keyname, |
| 54 | user_id=user_id)['keypair'] |
| 55 | self.assertEqual(first_keyname, keypair_detail['name']) |
| 56 | self.assertEqual(user_id, keypair_detail['user_id'], |
| 57 | "The fetched keypair is not for requested user!") |
| 58 | # Create a admin keypair |
zhufl | 5bcd7ee | 2017-01-13 17:47:14 +0800 | [diff] [blame] | 59 | admin_keypair = self.create_keypair(keypair_type='ssh') |
Ghanshyam | 4541bd1 | 2016-02-29 13:46:39 +0900 | [diff] [blame] | 60 | admin_keypair.pop('private_key', None) |
| 61 | admin_keypair.pop('user_id') |
| 62 | |
| 63 | # Admin fetch keypairs list of non admin user |
| 64 | keypairs = self.client.list_keypairs(user_id=user_id)['keypairs'] |
| 65 | fetched_list = [keypair['keypair'] for keypair in keypairs] |
| 66 | |
| 67 | # Check admin keypair is not present in non admin user keypairs list |
| 68 | self.assertNotIn(admin_keypair, fetched_list, |
| 69 | "The fetched user keypairs has admin keypair!") |
| 70 | |
| 71 | # Now check if all the created keypairs are in the fetched list |
| 72 | missing_kps = [kp for kp in key_list if kp not in fetched_list] |
| 73 | self.assertFalse(missing_kps, |
| 74 | "Failed to find keypairs %s in fetched list" |
| 75 | % ', '.join(m_key['name'] for m_key in missing_kps)) |