~ubuntu-cloud-archive/ubuntu/precise/nova/trunk

« back to all changes in this revision

Viewing changes to .pc/security-fix-tenant-bypass.patch/nova/tests/api/openstack/v2/contrib/test_keypairs.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Adam Gandleman
  • Date: 2012-01-13 09:51:10 UTC
  • mfrom: (1.1.40)
  • Revision ID: package-import@ubuntu.com-20120113095110-ffd6163drcg77wez
Tags: 2012.1~e3~20120113.12049-0ubuntu1
[Chuck Short]
* New upstream version.
* debian/nova_sudoers, debian/nova-common.install, 
  Switch out to nova-rootwrap. (LP: #681774)
* Add "get-origsource-git" which allows developers to 
  generate a tarball from github, by doing:
  fakeroot debian/rules get-orig-source-git
* debian/debian/nova-objectstore.logrotate: Dont determine
  if we are running Debian or Ubuntu. (LP: #91379)

[Adam Gandleman]
* Removed python-nova.postinst, let dh_python2 generate instead since
  python-support is not a dependency. (LP: #907543)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2011 Eldar Nugaev
2
 
# 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
 
 
16
 
import json
17
 
 
18
 
import webob
19
 
from lxml import etree
20
 
 
21
 
from nova.api.openstack import wsgi
22
 
from nova.api.openstack.v2.contrib import keypairs
23
 
from nova import context
24
 
from nova import db
25
 
from nova import test
26
 
from nova.tests.api.openstack import fakes
27
 
 
28
 
 
29
 
def fake_keypair(name):
30
 
    return {'public_key': 'FAKE_KEY',
31
 
            'fingerprint': 'FAKE_FINGERPRINT',
32
 
            'name': name}
33
 
 
34
 
 
35
 
def db_key_pair_get_all_by_user(self, user_id):
36
 
    return [fake_keypair('FAKE')]
37
 
 
38
 
 
39
 
def db_key_pair_create(self, keypair):
40
 
    pass
41
 
 
42
 
 
43
 
def db_key_pair_destroy(context, user_id, name):
44
 
    if not (user_id and name):
45
 
        raise Exception()
46
 
 
47
 
 
48
 
class KeypairsTest(test.TestCase):
49
 
 
50
 
    def setUp(self):
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",
58
 
                       db_key_pair_create)
59
 
        self.stubs.Set(db, "key_pair_destroy",
60
 
                       db_key_pair_destroy)
61
 
        self.context = context.get_admin_context()
62
 
 
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)
70
 
 
71
 
    def test_keypair_create(self):
72
 
        body = {'keypair': {'name': 'create_test'}}
73
 
        req = webob.Request.blank('/v2/123/os-keypairs')
74
 
        req.method = 'POST'
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)
82
 
 
83
 
    def test_keypair_import(self):
84
 
        body = {
85
 
            'keypair': {
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',
96
 
            },
97
 
        }
98
 
 
99
 
        req = webob.Request.blank('/v2/123/os-keypairs')
100
 
        req.method = 'POST'
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'])
109
 
 
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)
116
 
 
117
 
 
118
 
class KeypairsXMLSerializerTest(test.TestCase):
119
 
    def setUp(self):
120
 
        super(KeypairsXMLSerializerTest, self).setUp()
121
 
        self.serializer = keypairs.KeypairsSerializer()
122
 
        self.deserializer = wsgi.XMLDeserializer()
123
 
 
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)
132
 
 
133
 
        print text
134
 
        tree = etree.fromstring(text)
135
 
 
136
 
        self.assertEqual('keypair', tree.tag)
137
 
        for child in tree:
138
 
            self.assertTrue(child.tag in exemplar['keypair'])
139
 
            self.assertEqual(child.text, exemplar['keypair'][child.tag])
140
 
 
141
 
    def test_index_serializer(self):
142
 
        exemplar = dict(keypairs=[
143
 
                dict(keypair=dict(
144
 
                        name='key1_name',
145
 
                        public_key='key1_key',
146
 
                        fingerprint='key1_fingerprint')),
147
 
                dict(keypair=dict(
148
 
                        name='key2_name',
149
 
                        public_key='key2_key',
150
 
                        fingerprint='key2_fingerprint'))])
151
 
        text = self.serializer.serialize(exemplar, 'index')
152
 
 
153
 
        print text
154
 
        tree = etree.fromstring(text)
155
 
 
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])
164
 
 
165
 
    def test_deserializer(self):
166
 
        exemplar = dict(keypair=dict(
167
 
                name='key_name',
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>')
172
 
 
173
 
        result = self.deserializer.deserialize(intext)['body']
174
 
        self.assertEqual(result, exemplar)