~termie/nova/db_migration

« back to all changes in this revision

Viewing changes to nova/tests/test_direct.py

  • Committer: Andy Smith
  • Date: 2011-01-18 23:51:13 UTC
  • mfrom: (556.2.23 nova)
  • Revision ID: code@term.ie-20110118235113-ivu21efg3h9z6niq
merge from upstream and fix small issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2010 United States Government as represented by the
 
4
# Administrator of the National Aeronautics and Space Administration.
 
5
# All Rights Reserved.
 
6
#
 
7
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
8
#    not use this file except in compliance with the License. You may obtain
 
9
#    a copy of the License at
 
10
#
 
11
#         http://www.apache.org/licenses/LICENSE-2.0
 
12
#
 
13
#    Unless required by applicable law or agreed to in writing, software
 
14
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
15
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
16
#    License for the specific language governing permissions and limitations
 
17
#    under the License.
 
18
 
 
19
"""Tests for Direct API."""
 
20
 
 
21
import json
 
22
import logging
 
23
 
 
24
import webob
 
25
 
 
26
from nova import compute
 
27
from nova import context
 
28
from nova import exception
 
29
from nova import test
 
30
from nova import utils
 
31
from nova.api import direct
 
32
from nova.tests import test_cloud
 
33
 
 
34
 
 
35
class FakeService(object):
 
36
    def echo(self, context, data):
 
37
        return {'data': data}
 
38
 
 
39
    def context(self, context):
 
40
        return {'user': context.user_id,
 
41
                'project': context.project_id}
 
42
 
 
43
 
 
44
class DirectTestCase(test.TestCase):
 
45
    def setUp(self):
 
46
        super(DirectTestCase, self).setUp()
 
47
        direct.register_service('fake', FakeService())
 
48
        self.router = direct.PostParamsMiddleware(
 
49
                direct.JsonParamsMiddleware(
 
50
                        direct.Router()))
 
51
        self.auth_router = direct.DelegatedAuthMiddleware(self.router)
 
52
        self.context = context.RequestContext('user1', 'proj1')
 
53
 
 
54
    def tearDown(self):
 
55
        direct.ROUTES = {}
 
56
 
 
57
    def test_delegated_auth(self):
 
58
        req = webob.Request.blank('/fake/context')
 
59
        req.headers['X-OpenStack-User'] = 'user1'
 
60
        req.headers['X-OpenStack-Project'] = 'proj1'
 
61
        resp = req.get_response(self.auth_router)
 
62
        data = json.loads(resp.body)
 
63
        self.assertEqual(data['user'], 'user1')
 
64
        self.assertEqual(data['project'], 'proj1')
 
65
 
 
66
    def test_json_params(self):
 
67
        req = webob.Request.blank('/fake/echo')
 
68
        req.environ['openstack.context'] = self.context
 
69
        req.method = 'POST'
 
70
        req.body = 'json=%s' % json.dumps({'data': 'foo'})
 
71
        resp = req.get_response(self.router)
 
72
        resp_parsed = json.loads(resp.body)
 
73
        self.assertEqual(resp_parsed['data'], 'foo')
 
74
 
 
75
    def test_post_params(self):
 
76
        req = webob.Request.blank('/fake/echo')
 
77
        req.environ['openstack.context'] = self.context
 
78
        req.method = 'POST'
 
79
        req.body = 'data=foo'
 
80
        resp = req.get_response(self.router)
 
81
        resp_parsed = json.loads(resp.body)
 
82
        self.assertEqual(resp_parsed['data'], 'foo')
 
83
 
 
84
    def test_proxy(self):
 
85
        proxy = direct.Proxy(self.router)
 
86
        rv = proxy.fake.echo(self.context, data='baz')
 
87
        self.assertEqual(rv['data'], 'baz')
 
88
 
 
89
 
 
90
class DirectCloudTestCase(test_cloud.CloudTestCase):
 
91
    def setUp(self):
 
92
        super(DirectCloudTestCase, self).setUp()
 
93
        compute_handle = compute.API(image_service=self.cloud.image_service,
 
94
                                     network_api=self.cloud.network_api,
 
95
                                     volume_api=self.cloud.volume_api)
 
96
        direct.register_service('compute', compute_handle)
 
97
        self.router = direct.JsonParamsMiddleware(direct.Router())
 
98
        proxy = direct.Proxy(self.router)
 
99
        self.cloud.compute_api = proxy.compute
 
100
 
 
101
    def tearDown(self):
 
102
        super(DirectCloudTestCase, self).tearDown()
 
103
        direct.ROUTES = {}