blob: e24c7c100489b774443f8fb2b063a2a731c03a6e [file] [log] [blame]
Ghanshyam4541bd12016-02-29 13:46:39 +09001# 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
16from tempest.api.compute.keypairs import base
Ken'ichi Ohmichi757833a2017-03-10 10:30:30 -080017from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -080018from tempest.lib import decorators
Ghanshyam4541bd12016-02-29 13:46:39 +090019
20
21class 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 Pittier8160d312017-04-18 11:52:23 +020028 cls.client = cls.os_admin.keypairs_client
29 cls.non_admin_client = cls.os_primary.keypairs_client
Ghanshyam4541bd12016-02-29 13:46:39 +090030
31 def _create_and_check_keypairs(self, user_id):
32 key_list = list()
zhufl4311dc42017-01-26 16:26:18 +080033 for _ in range(2):
Ghanshyam4541bd12016-02-29 13:46:39 +090034 k_name = data_utils.rand_name('keypair')
zhufl5bcd7ee2017-01-13 17:47:14 +080035 keypair = self.create_keypair(k_name,
36 keypair_type='ssh',
37 user_id=user_id)
Ghanshyam4541bd12016-02-29 13:46:39 +090038 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 Ohmichiebbfd1c2017-01-27 16:37:00 -080048 @decorators.idempotent_id('3c8484af-cfb3-48f6-b8ba-d5d58bbf3eac')
Ghanshyam4541bd12016-02-29 13:46:39 +090049 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
zhufl5bcd7ee2017-01-13 17:47:14 +080059 admin_keypair = self.create_keypair(keypair_type='ssh')
Ghanshyam4541bd12016-02-29 13:46:39 +090060 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))