1
# Copyright 2011 Eldar Nugaev
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
8
# http://www.apache.org/licenses/LICENSE-2.0
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
19
from lxml import etree
21
from nova.api.openstack import wsgi
22
from nova.api.openstack.v2.contrib import keypairs
23
from nova import context
26
from nova.tests.api.openstack import fakes
29
def fake_keypair(name):
30
return {'public_key': 'FAKE_KEY',
31
'fingerprint': 'FAKE_FINGERPRINT',
35
def db_key_pair_get_all_by_user(self, user_id):
36
return [fake_keypair('FAKE')]
39
def db_key_pair_create(self, keypair):
43
def db_key_pair_destroy(context, user_id, name):
44
if not (user_id and name):
48
class KeypairsTest(test.TestCase):
51
super(KeypairsTest, self).setUp()
52
self.controller = keypairs.KeypairController()
53
fakes.stub_out_networking(self.stubs)
54
fakes.stub_out_rate_limiting(self.stubs)
55
self.stubs.Set(db, "key_pair_get_all_by_user",
56
db_key_pair_get_all_by_user)
57
self.stubs.Set(db, "key_pair_create",
59
self.stubs.Set(db, "key_pair_destroy",
61
self.context = context.get_admin_context()
63
def test_keypair_list(self):
64
req = webob.Request.blank('/v2/123/os-keypairs')
65
res = req.get_response(fakes.wsgi_app())
66
self.assertEqual(res.status_int, 200)
67
res_dict = json.loads(res.body)
68
response = {'keypairs': [{'keypair': fake_keypair('FAKE')}]}
69
self.assertEqual(res_dict, response)
71
def test_keypair_create(self):
72
body = {'keypair': {'name': 'create_test'}}
73
req = webob.Request.blank('/v2/123/os-keypairs')
75
req.body = json.dumps(body)
76
req.headers['Content-Type'] = 'application/json'
77
res = req.get_response(fakes.wsgi_app())
78
self.assertEqual(res.status_int, 200)
79
res_dict = json.loads(res.body)
80
self.assertTrue(len(res_dict['keypair']['fingerprint']) > 0)
81
self.assertTrue(len(res_dict['keypair']['private_key']) > 0)
83
def test_keypair_import(self):
86
'name': 'create_test',
87
'public_key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBYIznA'
88
'x9D7118Q1VKGpXy2HDiKyUTM8XcUuhQpo0srqb9rboUp4'
89
'a9NmCwpWpeElDLuva707GOUnfaBAvHBwsRXyxHJjRaI6Y'
90
'Qj2oLJwqvaSaWUbyT1vtryRqy6J3TecN0WINY71f4uymi'
91
'MZP0wby4bKBcYnac8KiCIlvkEl0ETjkOGUq8OyWRmn7lj'
92
'j5SESEUdBP0JnuTFKddWTU/wD6wydeJaUhBTqOlHn0kX1'
93
'GyqoNTE1UEhcM5ZRWgfUZfTjVyDF2kGj3vJLCJtJ8LoGc'
94
'j7YaN4uPg1rBle+izwE/tLonRrds+cev8p6krSSrxWOwB'
95
'bHkXa6OciiJDvkRzJXzf',
99
req = webob.Request.blank('/v2/123/os-keypairs')
101
req.body = json.dumps(body)
102
req.headers['Content-Type'] = 'application/json'
103
res = req.get_response(fakes.wsgi_app())
104
self.assertEqual(res.status_int, 200)
105
# FIXME(ja): sholud we check that public_key was sent to create?
106
res_dict = json.loads(res.body)
107
self.assertTrue(len(res_dict['keypair']['fingerprint']) > 0)
108
self.assertFalse('private_key' in res_dict['keypair'])
110
def test_keypair_delete(self):
111
req = webob.Request.blank('/v2/123/os-keypairs/FAKE')
112
req.method = 'DELETE'
113
req.headers['Content-Type'] = 'application/json'
114
res = req.get_response(fakes.wsgi_app())
115
self.assertEqual(res.status_int, 202)
118
class KeypairsXMLSerializerTest(test.TestCase):
120
super(KeypairsXMLSerializerTest, self).setUp()
121
self.serializer = keypairs.KeypairsSerializer()
122
self.deserializer = wsgi.XMLDeserializer()
124
def test_default_serializer(self):
125
exemplar = dict(keypair=dict(
126
public_key='fake_public_key',
127
private_key='fake_private_key',
128
fingerprint='fake_fingerprint',
129
user_id='fake_user_id',
130
name='fake_key_name'))
131
text = self.serializer.serialize(exemplar)
134
tree = etree.fromstring(text)
136
self.assertEqual('keypair', tree.tag)
138
self.assertTrue(child.tag in exemplar['keypair'])
139
self.assertEqual(child.text, exemplar['keypair'][child.tag])
141
def test_index_serializer(self):
142
exemplar = dict(keypairs=[
145
public_key='key1_key',
146
fingerprint='key1_fingerprint')),
149
public_key='key2_key',
150
fingerprint='key2_fingerprint'))])
151
text = self.serializer.serialize(exemplar, 'index')
154
tree = etree.fromstring(text)
156
self.assertEqual('keypairs', tree.tag)
157
self.assertEqual(len(exemplar['keypairs']), len(tree))
158
for idx, keypair in enumerate(tree):
159
self.assertEqual('keypair', keypair.tag)
160
kp_data = exemplar['keypairs'][idx]['keypair']
161
for child in keypair:
162
self.assertTrue(child.tag in kp_data)
163
self.assertEqual(child.text, kp_data[child.tag])
165
def test_deserializer(self):
166
exemplar = dict(keypair=dict(
168
public_key='public_key'))
169
intext = ("<?xml version='1.0' encoding='UTF-8'?>\n"
170
'<keypair><name>key_name</name>'
171
'<public_key>public_key</public_key></keypair>')
173
result = self.deserializer.deserialize(intext)['body']
174
self.assertEqual(result, exemplar)