blob: d0599944f19665707d140f2b6e38702e7cb43ade [file] [log] [blame]
Jay Pipes13b479b2012-06-11 14:52:27 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
Jay Pipes13b479b2012-06-11 14:52:27 -04004# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Sean Dague1937d092013-05-17 16:36:38 -040018from tempest.api.compute import base
Jay Pipes13b479b2012-06-11 14:52:27 -040019from tempest.common.utils.data_utils import rand_name
Matthew Treinisha83a16e2012-12-07 13:44:02 -050020from tempest import exceptions
Chris Yeoh9465b0b2013-02-09 22:19:15 +103021from tempest.test import attr
kavan-patil2eb350f2012-01-19 11:17:26 +000022
23
ivan-zhuf2b00502013-10-18 10:06:52 +080024class KeyPairsTestJSON(base.BaseV2ComputeTest):
Attila Fazekas19044d52013-02-16 07:35:06 +010025 _interface = 'json'
26
27 @classmethod
28 def setUpClass(cls):
29 super(KeyPairsTestJSON, cls).setUpClass()
30 cls.client = cls.keypairs_client
kavan-patil2eb350f2012-01-19 11:17:26 +000031
Giulio Fidenteba3985a2013-05-29 01:46:36 +020032 @attr(type='gate')
kavan-patil2eb350f2012-01-19 11:17:26 +000033 def test_keypairs_create_list_delete(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050034 # Keypairs created should be available in the response list
Attila Fazekasf7f34f92013-08-01 17:01:44 +020035 # Create 3 keypairs
kavan-patil2eb350f2012-01-19 11:17:26 +000036 key_list = list()
37 for i in range(3):
38 k_name = rand_name('keypair-')
39 resp, keypair = self.client.create_keypair(k_name)
Attila Fazekasf7f34f92013-08-01 17:01:44 +020040 # Need to pop these keys so that our compare doesn't fail later,
41 # as the keypair dicts from list API doesn't have them.
kavan-patil2eb350f2012-01-19 11:17:26 +000042 keypair.pop('private_key')
43 keypair.pop('user_id')
44 self.assertEqual(200, resp.status)
45 key_list.append(keypair)
Attila Fazekasf7f34f92013-08-01 17:01:44 +020046 # Fetch all keypairs and verify the list
47 # has all created keypairs
kavan-patil2eb350f2012-01-19 11:17:26 +000048 resp, fetched_list = self.client.list_keypairs()
49 self.assertEqual(200, resp.status)
Attila Fazekasf7f34f92013-08-01 17:01:44 +020050 # We need to remove the extra 'keypair' element in the
51 # returned dict. See comment in keypairs_client.list_keypairs()
kavan-patil2eb350f2012-01-19 11:17:26 +000052 new_list = list()
53 for keypair in fetched_list:
54 new_list.append(keypair['keypair'])
55 fetched_list = new_list
Attila Fazekasf7f34f92013-08-01 17:01:44 +020056 # Now check if all the created keypairs are in the fetched list
kavan-patil2eb350f2012-01-19 11:17:26 +000057 missing_kps = [kp for kp in key_list if kp not in fetched_list]
58 self.assertFalse(missing_kps,
59 "Failed to find keypairs %s in fetched list"
60 % ', '.join(m_key['name'] for m_key in missing_kps))
Attila Fazekasf7f34f92013-08-01 17:01:44 +020061 # Delete all the keypairs created
kavan-patil2eb350f2012-01-19 11:17:26 +000062 for keypair in key_list:
63 resp, _ = self.client.delete_keypair(keypair['name'])
64 self.assertEqual(202, resp.status)
65
Giulio Fidenteba3985a2013-05-29 01:46:36 +020066 @attr(type='gate')
kavan-patil2eb350f2012-01-19 11:17:26 +000067 def test_keypair_create_delete(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050068 # Keypair should be created, verified and deleted
kavan-patil2eb350f2012-01-19 11:17:26 +000069 k_name = rand_name('keypair-')
70 resp, keypair = self.client.create_keypair(k_name)
71 self.assertEqual(200, resp.status)
72 private_key = keypair['private_key']
73 key_name = keypair['name']
74 self.assertEqual(key_name, k_name,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080075 "The created keypair name is not equal "
76 "to the requested name")
kavan-patil2eb350f2012-01-19 11:17:26 +000077 self.assertTrue(private_key is not None,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080078 "Field private_key is empty or not found.")
kavan-patil2eb350f2012-01-19 11:17:26 +000079 resp, _ = self.client.delete_keypair(k_name)
80 self.assertEqual(202, resp.status)
81
Giulio Fidenteba3985a2013-05-29 01:46:36 +020082 @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
rajalakshmi-ganesanb74a11a2012-05-16 10:37:58 +053085 k_name = rand_name('keypair-')
86 resp, keypair = self.client.create_keypair(k_name)
Giulio Fidente2732fca2013-08-29 13:40:22 +020087 self.addCleanup(self.client.delete_keypair, k_name)
Giulio Fidente92f77192013-08-26 17:13:28 +020088 resp, keypair_detail = self.client.get_keypair(k_name)
89 self.assertEqual(200, resp.status)
Giulio Fidente92f77192013-08-26 17:13:28 +020090 self.assertIn('name', keypair_detail)
91 self.assertIn('public_key', keypair_detail)
92 self.assertEqual(keypair_detail['name'], k_name,
93 "The created keypair name is not equal "
94 "to requested name")
95 public_key = keypair_detail['public_key']
96 self.assertTrue(public_key is not None,
97 "Field public_key is empty or not found.")
rajalakshmi-ganesanb74a11a2012-05-16 10:37:58 +053098
Giulio Fidenteba3985a2013-05-29 01:46:36 +020099 @attr(type='gate')
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
kavan-patil2eb350f2012-01-19 11:17:26 +0000102 k_name = rand_name('keypair-')
103 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")
112 resp, keypair = self.client.create_keypair(k_name, pub_key)
113 self.assertEqual(200, resp.status)
114 self.assertFalse('private_key' in keypair,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800115 "Field private_key is not empty!")
kavan-patil2eb350f2012-01-19 11:17:26 +0000116 key_name = keypair['name']
117 self.assertEqual(key_name, k_name,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800118 "The created keypair name is not equal "
119 "to the requested name!")
kavan-patil2eb350f2012-01-19 11:17:26 +0000120 resp, _ = self.client.delete_keypair(k_name)
121 self.assertEqual(202, resp.status)
122
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400123 @attr(type=['negative', 'gate'])
kavan-patil2eb350f2012-01-19 11:17:26 +0000124 def test_keypair_create_with_invalid_pub_key(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500125 # Keypair should not be created with a non RSA public key
kavan-patil2eb350f2012-01-19 11:17:26 +0000126 k_name = rand_name('keypair-')
127 pub_key = "ssh-rsa JUNK nova@ubuntu"
Chris Yeohe04628e2013-02-25 17:12:21 +1030128 self.assertRaises(exceptions.BadRequest,
129 self.client.create_keypair, k_name, pub_key)
kavan-patil2eb350f2012-01-19 11:17:26 +0000130
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400131 @attr(type=['negative', 'gate'])
kavan-patil2eb350f2012-01-19 11:17:26 +0000132 def test_keypair_delete_nonexistant_key(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500133 # Non-existant key deletion should throw a proper error
kavan-patil2eb350f2012-01-19 11:17:26 +0000134 k_name = rand_name("keypair-non-existant-")
Chris Yeohe04628e2013-02-25 17:12:21 +1030135 self.assertRaises(exceptions.NotFound, self.client.delete_keypair,
136 k_name)
kavan-patil2eb350f2012-01-19 11:17:26 +0000137
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400138 @attr(type=['negative', 'gate'])
Rohit Karajgib4825c32012-04-24 13:52:34 -0700139 def test_create_keypair_with_empty_public_key(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500140 # Keypair should not be created with an empty public key
Rohit Karajgib4825c32012-04-24 13:52:34 -0700141 k_name = rand_name("keypair-")
142 pub_key = ' '
Chris Yeohe04628e2013-02-25 17:12:21 +1030143 self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
144 k_name, pub_key)
Rohit Karajgib4825c32012-04-24 13:52:34 -0700145
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400146 @attr(type=['negative', 'gate'])
Rohit Karajgib4825c32012-04-24 13:52:34 -0700147 def test_create_keypair_when_public_key_bits_exceeds_maximum(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500148 # Keypair should not be created when public key bits are too long
Rohit Karajgib4825c32012-04-24 13:52:34 -0700149 k_name = rand_name("keypair-")
150 pub_key = 'ssh-rsa ' + 'A' * 2048 + ' openstack@ubuntu'
Chris Yeohe04628e2013-02-25 17:12:21 +1030151 self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
152 k_name, pub_key)
Rohit Karajgib4825c32012-04-24 13:52:34 -0700153
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400154 @attr(type=['negative', 'gate'])
kavan-patil2eb350f2012-01-19 11:17:26 +0000155 def test_create_keypair_with_duplicate_name(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500156 # Keypairs with duplicate names should not be created
kavan-patil2eb350f2012-01-19 11:17:26 +0000157 k_name = rand_name('keypair-')
158 resp, _ = self.client.create_keypair(k_name)
159 self.assertEqual(200, resp.status)
Chang Bo Guof099f802013-09-13 19:01:46 -0700160 # Now try the same keyname to create another key
Anju5c3e510c2013-10-18 06:40:29 +0530161 self.assertRaises(exceptions.Conflict, self.client.create_keypair,
Chris Yeohe04628e2013-02-25 17:12:21 +1030162 k_name)
David Kranz5a23d862012-02-14 09:48:55 -0500163 resp, _ = self.client.delete_keypair(k_name)
164 self.assertEqual(202, resp.status)
kavan-patil2eb350f2012-01-19 11:17:26 +0000165
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400166 @attr(type=['negative', 'gate'])
kavan-patil2eb350f2012-01-19 11:17:26 +0000167 def test_create_keypair_with_empty_name_string(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500168 # Keypairs with name being an empty string should not be created
Chris Yeohe04628e2013-02-25 17:12:21 +1030169 self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
170 '')
kavan-patil2eb350f2012-01-19 11:17:26 +0000171
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400172 @attr(type=['negative', 'gate'])
kavan-patil2eb350f2012-01-19 11:17:26 +0000173 def test_create_keypair_with_long_keynames(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500174 # Keypairs with name longer than 255 chars should not be created
kavan-patil2eb350f2012-01-19 11:17:26 +0000175 k_name = 'keypair-'.ljust(260, '0')
Chris Yeohe04628e2013-02-25 17:12:21 +1030176 self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
177 k_name)
Rohit Karajgib4825c32012-04-24 13:52:34 -0700178
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400179 @attr(type=['negative', 'gate'])
Rohit Karajgib4825c32012-04-24 13:52:34 -0700180 def test_create_keypair_invalid_name(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500181 # Keypairs with name being an invalid name should not be created
Rohit Karajgib4825c32012-04-24 13:52:34 -0700182 k_name = 'key_/.\@:'
Chris Yeohe04628e2013-02-25 17:12:21 +1030183 self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
184 k_name)
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -0400185
186
Attila Fazekas19044d52013-02-16 07:35:06 +0100187class KeyPairsTestXML(KeyPairsTestJSON):
188 _interface = 'xml'