blob: 6e59601b26513ab85db3eccd87755976628cca4f [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipes13b479b2012-06-11 14:52:27 -04002# 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
Matthew Treinish01472ff2015-02-20 17:26:52 -050016from tempest_lib.common.utils import data_utils
17
Sean Dague1937d092013-05-17 16:36:38 -040018from tempest.api.compute import base
ivan-zhu58d50772013-12-10 14:02:38 +080019from tempest import test
kavan-patil2eb350f2012-01-19 11:17:26 +000020
21
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +000022class KeyPairsV2TestJSON(base.BaseComputeTest):
Ghanshyam70a75b32014-05-07 17:09:41 +090023
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +000024 _api_version = 2
Attila Fazekas19044d52013-02-16 07:35:06 +010025
26 @classmethod
Rohan Kanade60b73092015-02-04 17:58:19 +053027 def setup_clients(cls):
28 super(KeyPairsV2TestJSON, cls).setup_clients()
Attila Fazekas19044d52013-02-16 07:35:06 +010029 cls.client = cls.keypairs_client
kavan-patil2eb350f2012-01-19 11:17:26 +000030
ivan-zhu58d50772013-12-10 14:02:38 +080031 def _delete_keypair(self, keypair_name):
David Kranz173f0e02015-02-06 13:47:57 -050032 self.client.delete_keypair(keypair_name)
ivan-zhu58d50772013-12-10 14:02:38 +080033
34 def _create_keypair(self, keypair_name, pub_key=None):
David Kranz173f0e02015-02-06 13:47:57 -050035 body = self.client.create_keypair(keypair_name, pub_key)
ivan-zhu58d50772013-12-10 14:02:38 +080036 self.addCleanup(self._delete_keypair, keypair_name)
David Kranz173f0e02015-02-06 13:47:57 -050037 return body
ivan-zhu58d50772013-12-10 14:02:38 +080038
39 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080040 @test.idempotent_id('1d1dbedb-d7a0-432a-9d09-83f543c3c19b')
kavan-patil2eb350f2012-01-19 11:17:26 +000041 def test_keypairs_create_list_delete(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050042 # Keypairs created should be available in the response list
Attila Fazekasf7f34f92013-08-01 17:01:44 +020043 # Create 3 keypairs
kavan-patil2eb350f2012-01-19 11:17:26 +000044 key_list = list()
45 for i in range(3):
Masayuki Igawa259c1132013-10-31 17:48:44 +090046 k_name = data_utils.rand_name('keypair-')
David Kranz173f0e02015-02-06 13:47:57 -050047 keypair = self._create_keypair(k_name)
Attila Fazekasf7f34f92013-08-01 17:01:44 +020048 # Need to pop these keys so that our compare doesn't fail later,
49 # as the keypair dicts from list API doesn't have them.
kavan-patil2eb350f2012-01-19 11:17:26 +000050 keypair.pop('private_key')
51 keypair.pop('user_id')
kavan-patil2eb350f2012-01-19 11:17:26 +000052 key_list.append(keypair)
Attila Fazekasf7f34f92013-08-01 17:01:44 +020053 # Fetch all keypairs and verify the list
54 # has all created keypairs
David Kranz173f0e02015-02-06 13:47:57 -050055 fetched_list = self.client.list_keypairs()
Attila Fazekasf7f34f92013-08-01 17:01:44 +020056 # We need to remove the extra 'keypair' element in the
57 # returned dict. See comment in keypairs_client.list_keypairs()
kavan-patil2eb350f2012-01-19 11:17:26 +000058 new_list = list()
59 for keypair in fetched_list:
60 new_list.append(keypair['keypair'])
61 fetched_list = new_list
Attila Fazekasf7f34f92013-08-01 17:01:44 +020062 # Now check if all the created keypairs are in the fetched list
kavan-patil2eb350f2012-01-19 11:17:26 +000063 missing_kps = [kp for kp in key_list if kp not in fetched_list]
64 self.assertFalse(missing_kps,
65 "Failed to find keypairs %s in fetched list"
66 % ', '.join(m_key['name'] for m_key in missing_kps))
kavan-patil2eb350f2012-01-19 11:17:26 +000067
ivan-zhu58d50772013-12-10 14:02:38 +080068 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080069 @test.idempotent_id('6c1d3123-4519-4742-9194-622cb1714b7d')
kavan-patil2eb350f2012-01-19 11:17:26 +000070 def test_keypair_create_delete(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050071 # Keypair should be created, verified and deleted
Masayuki Igawa259c1132013-10-31 17:48:44 +090072 k_name = data_utils.rand_name('keypair-')
David Kranz173f0e02015-02-06 13:47:57 -050073 keypair = self._create_keypair(k_name)
kavan-patil2eb350f2012-01-19 11:17:26 +000074 private_key = keypair['private_key']
75 key_name = keypair['name']
76 self.assertEqual(key_name, k_name,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080077 "The created keypair name is not equal "
78 "to the requested name")
kavan-patil2eb350f2012-01-19 11:17:26 +000079 self.assertTrue(private_key is not None,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080080 "Field private_key is empty or not found.")
kavan-patil2eb350f2012-01-19 11:17:26 +000081
ivan-zhu58d50772013-12-10 14:02:38 +080082 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080083 @test.idempotent_id('a4233d5d-52d8-47cc-9a25-e1864527e3df')
rajalakshmi-ganesanb74a11a2012-05-16 10:37:58 +053084 def test_get_keypair_detail(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050085 # Keypair should be created, Got details by name and deleted
Masayuki Igawa259c1132013-10-31 17:48:44 +090086 k_name = data_utils.rand_name('keypair-')
David Kranz173f0e02015-02-06 13:47:57 -050087 self._create_keypair(k_name)
88 keypair_detail = self.client.get_keypair(k_name)
Giulio Fidente92f77192013-08-26 17:13:28 +020089 self.assertIn('name', keypair_detail)
90 self.assertIn('public_key', keypair_detail)
91 self.assertEqual(keypair_detail['name'], k_name,
92 "The created keypair name is not equal "
93 "to requested name")
94 public_key = keypair_detail['public_key']
95 self.assertTrue(public_key is not None,
96 "Field public_key is empty or not found.")
rajalakshmi-ganesanb74a11a2012-05-16 10:37:58 +053097
ivan-zhu58d50772013-12-10 14:02:38 +080098 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080099 @test.idempotent_id('39c90c6a-304a-49dd-95ec-2366129def05')
kavan-patil2eb350f2012-01-19 11:17:26 +0000100 def test_keypair_create_with_pub_key(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500101 # Keypair should be created with a given public key
Masayuki Igawa259c1132013-10-31 17:48:44 +0900102 k_name = data_utils.rand_name('keypair-')
kavan-patil2eb350f2012-01-19 11:17:26 +0000103 pub_key = ("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCs"
104 "Ne3/1ILNCqFyfYWDeTKLD6jEXC2OQHLmietMWW+/vd"
105 "aZq7KZEwO0jhglaFjU1mpqq4Gz5RX156sCTNM9vRbw"
106 "KAxfsdF9laBYVsex3m3Wmui3uYrKyumsoJn2g9GNnG1P"
107 "I1mrVjZ61i0GY3khna+wzlTpCCmy5HNlrmbj3XLqBUpip"
108 "TOXmsnr4sChzC53KCd8LXuwc1i/CZPvF+3XipvAgFSE53pCt"
109 "LOeB1kYMOBaiUPLQTWXR3JpckqFIQwhIH0zoHlJvZE8hh90"
110 "XcPojYN56tI0OlrGqojbediJYD0rUsJu4weZpbn8vilb3JuDY+jws"
111 "snSA8wzBx3A/8y9Pp1B nova@ubuntu")
David Kranz173f0e02015-02-06 13:47:57 -0500112 keypair = self._create_keypair(k_name, pub_key)
kavan-patil2eb350f2012-01-19 11:17:26 +0000113 self.assertFalse('private_key' in keypair,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800114 "Field private_key is not empty!")
kavan-patil2eb350f2012-01-19 11:17:26 +0000115 key_name = keypair['name']
116 self.assertEqual(key_name, k_name,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800117 "The created keypair name is not equal "
118 "to the requested name!")