blob: 4c0398ea1c4767bdae0050089ab05d5cc11b385a [file] [log] [blame]
Jay Pipes13b479b2012-06-11 14:52:27 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# 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
Attila Fazekas19044d52013-02-16 07:35:06 +010024class KeyPairsTestJSON(base.BaseComputeTest):
25 _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
Giampaolo Lauriae9c77022013-05-22 01:23:58 -040032 @attr(type=['positive', '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
kavan-patil2eb350f2012-01-19 11:17:26 +000035 #Create 3 keypairs
36 key_list = list()
37 for i in range(3):
38 k_name = rand_name('keypair-')
39 resp, keypair = self.client.create_keypair(k_name)
40 #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.
42 keypair.pop('private_key')
43 keypair.pop('user_id')
44 self.assertEqual(200, resp.status)
45 key_list.append(keypair)
46 #Fetch all keypairs and verify the list
47 #has all created keypairs
48 resp, fetched_list = self.client.list_keypairs()
49 self.assertEqual(200, resp.status)
50 #We need to remove the extra 'keypair' element in the
51 #returned dict. See comment in keypairs_client.list_keypairs()
52 new_list = list()
53 for keypair in fetched_list:
54 new_list.append(keypair['keypair'])
55 fetched_list = new_list
56 #Now check if all the created keypairs are in the fetched list
57 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))
61 #Delete all the keypairs created
62 for keypair in key_list:
63 resp, _ = self.client.delete_keypair(keypair['name'])
64 self.assertEqual(202, resp.status)
65
Giampaolo Lauriae9c77022013-05-22 01:23:58 -040066 @attr(type=['positive', '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
Giampaolo Lauriae9c77022013-05-22 01:23:58 -040082 @attr(type=['positive', '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)
87 try:
88 resp, keypair_detail = self.client.get_keypair(k_name)
89 self.assertEqual(200, resp.status)
90 self.assertTrue('name' in keypair_detail)
91 self.assertTrue('public_key' in keypair_detail)
92 self.assertEqual(keypair_detail['name'], k_name,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080093 "The created keypair name is not equal "
94 "to requested name")
rajalakshmi-ganesanb74a11a2012-05-16 10:37:58 +053095 public_key = keypair_detail['public_key']
96 self.assertTrue(public_key is not None,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080097 "Field public_key is empty or not found.")
Matthew Treinish05d9fb92012-12-07 16:14:05 -050098 except Exception:
Zhongyue Luoe0884a32012-09-25 17:24:17 +080099 self.fail("GET keypair details requested by keypair name "
100 "has failed")
rajalakshmi-ganesanb74a11a2012-05-16 10:37:58 +0530101 finally:
102 resp, _ = self.client.delete_keypair(k_name)
103 self.assertEqual(202, resp.status)
104
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400105 @attr(type=['positive', 'gate'])
kavan-patil2eb350f2012-01-19 11:17:26 +0000106 def test_keypair_create_with_pub_key(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500107 # Keypair should be created with a given public key
kavan-patil2eb350f2012-01-19 11:17:26 +0000108 k_name = rand_name('keypair-')
109 pub_key = ("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCs"
110 "Ne3/1ILNCqFyfYWDeTKLD6jEXC2OQHLmietMWW+/vd"
111 "aZq7KZEwO0jhglaFjU1mpqq4Gz5RX156sCTNM9vRbw"
112 "KAxfsdF9laBYVsex3m3Wmui3uYrKyumsoJn2g9GNnG1P"
113 "I1mrVjZ61i0GY3khna+wzlTpCCmy5HNlrmbj3XLqBUpip"
114 "TOXmsnr4sChzC53KCd8LXuwc1i/CZPvF+3XipvAgFSE53pCt"
115 "LOeB1kYMOBaiUPLQTWXR3JpckqFIQwhIH0zoHlJvZE8hh90"
116 "XcPojYN56tI0OlrGqojbediJYD0rUsJu4weZpbn8vilb3JuDY+jws"
117 "snSA8wzBx3A/8y9Pp1B nova@ubuntu")
118 resp, keypair = self.client.create_keypair(k_name, pub_key)
119 self.assertEqual(200, resp.status)
120 self.assertFalse('private_key' in keypair,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800121 "Field private_key is not empty!")
kavan-patil2eb350f2012-01-19 11:17:26 +0000122 key_name = keypair['name']
123 self.assertEqual(key_name, k_name,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800124 "The created keypair name is not equal "
125 "to the requested name!")
kavan-patil2eb350f2012-01-19 11:17:26 +0000126 resp, _ = self.client.delete_keypair(k_name)
127 self.assertEqual(202, resp.status)
128
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400129 @attr(type=['negative', 'gate'])
kavan-patil2eb350f2012-01-19 11:17:26 +0000130 def test_keypair_create_with_invalid_pub_key(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500131 # Keypair should not be created with a non RSA public key
kavan-patil2eb350f2012-01-19 11:17:26 +0000132 k_name = rand_name('keypair-')
133 pub_key = "ssh-rsa JUNK nova@ubuntu"
Chris Yeohe04628e2013-02-25 17:12:21 +1030134 self.assertRaises(exceptions.BadRequest,
135 self.client.create_keypair, k_name, pub_key)
kavan-patil2eb350f2012-01-19 11:17:26 +0000136
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400137 @attr(type=['negative', 'gate'])
kavan-patil2eb350f2012-01-19 11:17:26 +0000138 def test_keypair_delete_nonexistant_key(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500139 # Non-existant key deletion should throw a proper error
kavan-patil2eb350f2012-01-19 11:17:26 +0000140 k_name = rand_name("keypair-non-existant-")
Chris Yeohe04628e2013-02-25 17:12:21 +1030141 self.assertRaises(exceptions.NotFound, self.client.delete_keypair,
142 k_name)
kavan-patil2eb350f2012-01-19 11:17:26 +0000143
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400144 @attr(type=['negative', 'gate'])
Rohit Karajgib4825c32012-04-24 13:52:34 -0700145 def test_create_keypair_with_empty_public_key(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500146 # Keypair should not be created with an empty public key
Rohit Karajgib4825c32012-04-24 13:52:34 -0700147 k_name = rand_name("keypair-")
148 pub_key = ' '
Chris Yeohe04628e2013-02-25 17:12:21 +1030149 self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
150 k_name, pub_key)
Rohit Karajgib4825c32012-04-24 13:52:34 -0700151
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400152 @attr(type=['negative', 'gate'])
Rohit Karajgib4825c32012-04-24 13:52:34 -0700153 def test_create_keypair_when_public_key_bits_exceeds_maximum(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500154 # Keypair should not be created when public key bits are too long
Rohit Karajgib4825c32012-04-24 13:52:34 -0700155 k_name = rand_name("keypair-")
156 pub_key = 'ssh-rsa ' + 'A' * 2048 + ' openstack@ubuntu'
Chris Yeohe04628e2013-02-25 17:12:21 +1030157 self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
158 k_name, pub_key)
Rohit Karajgib4825c32012-04-24 13:52:34 -0700159
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400160 @attr(type=['negative', 'gate'])
kavan-patil2eb350f2012-01-19 11:17:26 +0000161 def test_create_keypair_with_duplicate_name(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500162 # Keypairs with duplicate names should not be created
kavan-patil2eb350f2012-01-19 11:17:26 +0000163 k_name = rand_name('keypair-')
164 resp, _ = self.client.create_keypair(k_name)
165 self.assertEqual(200, resp.status)
166 #Now try the same keyname to ceate another key
Chris Yeohe04628e2013-02-25 17:12:21 +1030167 self.assertRaises(exceptions.Duplicate, self.client.create_keypair,
168 k_name)
David Kranz5a23d862012-02-14 09:48:55 -0500169 resp, _ = self.client.delete_keypair(k_name)
170 self.assertEqual(202, resp.status)
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_empty_name_string(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500174 # Keypairs with name being an empty string should not be created
Chris Yeohe04628e2013-02-25 17:12:21 +1030175 self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
176 '')
kavan-patil2eb350f2012-01-19 11:17:26 +0000177
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400178 @attr(type=['negative', 'gate'])
kavan-patil2eb350f2012-01-19 11:17:26 +0000179 def test_create_keypair_with_long_keynames(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500180 # Keypairs with name longer than 255 chars should not be created
kavan-patil2eb350f2012-01-19 11:17:26 +0000181 k_name = 'keypair-'.ljust(260, '0')
Chris Yeohe04628e2013-02-25 17:12:21 +1030182 self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
183 k_name)
Rohit Karajgib4825c32012-04-24 13:52:34 -0700184
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400185 @attr(type=['negative', 'gate'])
Rohit Karajgib4825c32012-04-24 13:52:34 -0700186 def test_create_keypair_invalid_name(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500187 # Keypairs with name being an invalid name should not be created
Rohit Karajgib4825c32012-04-24 13:52:34 -0700188 k_name = 'key_/.\@:'
Chris Yeohe04628e2013-02-25 17:12:21 +1030189 self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
190 k_name)
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -0400191
192
Attila Fazekas19044d52013-02-16 07:35:06 +0100193class KeyPairsTestXML(KeyPairsTestJSON):
194 _interface = 'xml'