blob: d4554bc66a89dd5573c4f41ff8fd69d7e200d5ba [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
Sean Dague1937d092013-05-17 16:36:38 -040016from tempest.api.compute import base
Masayuki Igawa259c1132013-10-31 17:48:44 +090017from tempest.common.utils import data_utils
ivan-zhu58d50772013-12-10 14:02:38 +080018from tempest import test
kavan-patil2eb350f2012-01-19 11:17:26 +000019
20
ivan-zhuf2b00502013-10-18 10:06:52 +080021class KeyPairsTestJSON(base.BaseV2ComputeTest):
Attila Fazekas19044d52013-02-16 07:35:06 +010022 _interface = 'json'
23
24 @classmethod
25 def setUpClass(cls):
26 super(KeyPairsTestJSON, cls).setUpClass()
27 cls.client = cls.keypairs_client
kavan-patil2eb350f2012-01-19 11:17:26 +000028
ivan-zhu58d50772013-12-10 14:02:38 +080029 def _delete_keypair(self, keypair_name):
30 resp, _ = self.client.delete_keypair(keypair_name)
31 self.assertEqual(202, resp.status)
32
33 def _create_keypair(self, keypair_name, pub_key=None):
34 resp, body = self.client.create_keypair(keypair_name, pub_key)
35 self.addCleanup(self._delete_keypair, keypair_name)
36 return resp, body
37
38 @test.attr(type='gate')
kavan-patil2eb350f2012-01-19 11:17:26 +000039 def test_keypairs_create_list_delete(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050040 # Keypairs created should be available in the response list
Attila Fazekasf7f34f92013-08-01 17:01:44 +020041 # Create 3 keypairs
kavan-patil2eb350f2012-01-19 11:17:26 +000042 key_list = list()
43 for i in range(3):
Masayuki Igawa259c1132013-10-31 17:48:44 +090044 k_name = data_utils.rand_name('keypair-')
ivan-zhu58d50772013-12-10 14:02:38 +080045 resp, keypair = self._create_keypair(k_name)
Attila Fazekasf7f34f92013-08-01 17:01:44 +020046 # Need to pop these keys so that our compare doesn't fail later,
47 # as the keypair dicts from list API doesn't have them.
kavan-patil2eb350f2012-01-19 11:17:26 +000048 keypair.pop('private_key')
49 keypair.pop('user_id')
50 self.assertEqual(200, resp.status)
51 key_list.append(keypair)
Attila Fazekasf7f34f92013-08-01 17:01:44 +020052 # Fetch all keypairs and verify the list
53 # has all created keypairs
kavan-patil2eb350f2012-01-19 11:17:26 +000054 resp, fetched_list = self.client.list_keypairs()
55 self.assertEqual(200, resp.status)
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')
kavan-patil2eb350f2012-01-19 11:17:26 +000069 def test_keypair_create_delete(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050070 # Keypair should be created, verified and deleted
Masayuki Igawa259c1132013-10-31 17:48:44 +090071 k_name = data_utils.rand_name('keypair-')
ivan-zhu58d50772013-12-10 14:02:38 +080072 resp, keypair = self._create_keypair(k_name)
kavan-patil2eb350f2012-01-19 11:17:26 +000073 self.assertEqual(200, resp.status)
74 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')
rajalakshmi-ganesanb74a11a2012-05-16 10:37:58 +053083 def test_get_keypair_detail(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050084 # Keypair should be created, Got details by name and deleted
Masayuki Igawa259c1132013-10-31 17:48:44 +090085 k_name = data_utils.rand_name('keypair-')
ivan-zhu58d50772013-12-10 14:02:38 +080086 resp, keypair = self._create_keypair(k_name)
Giulio Fidente92f77192013-08-26 17:13:28 +020087 resp, keypair_detail = self.client.get_keypair(k_name)
88 self.assertEqual(200, resp.status)
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')
kavan-patil2eb350f2012-01-19 11:17:26 +000099 def test_keypair_create_with_pub_key(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500100 # Keypair should be created with a given public key
Masayuki Igawa259c1132013-10-31 17:48:44 +0900101 k_name = data_utils.rand_name('keypair-')
kavan-patil2eb350f2012-01-19 11:17:26 +0000102 pub_key = ("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCs"
103 "Ne3/1ILNCqFyfYWDeTKLD6jEXC2OQHLmietMWW+/vd"
104 "aZq7KZEwO0jhglaFjU1mpqq4Gz5RX156sCTNM9vRbw"
105 "KAxfsdF9laBYVsex3m3Wmui3uYrKyumsoJn2g9GNnG1P"
106 "I1mrVjZ61i0GY3khna+wzlTpCCmy5HNlrmbj3XLqBUpip"
107 "TOXmsnr4sChzC53KCd8LXuwc1i/CZPvF+3XipvAgFSE53pCt"
108 "LOeB1kYMOBaiUPLQTWXR3JpckqFIQwhIH0zoHlJvZE8hh90"
109 "XcPojYN56tI0OlrGqojbediJYD0rUsJu4weZpbn8vilb3JuDY+jws"
110 "snSA8wzBx3A/8y9Pp1B nova@ubuntu")
ivan-zhu58d50772013-12-10 14:02:38 +0800111 resp, keypair = self._create_keypair(k_name, pub_key)
kavan-patil2eb350f2012-01-19 11:17:26 +0000112 self.assertEqual(200, resp.status)
113 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!")
kavan-patil2eb350f2012-01-19 11:17:26 +0000119
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -0400120
Attila Fazekas19044d52013-02-16 07:35:06 +0100121class KeyPairsTestXML(KeyPairsTestJSON):
122 _interface = 'xml'