~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/tests/rpc_unittest.py

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-01-21 11:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110121114806-v8fvnnl6az4m4ohv
Tags: upstream-2011.1~bzr597
ImportĀ upstreamĀ versionĀ 2011.1~bzr597

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
 
Unit Tests for remote procedure calls using queue
20
 
"""
21
 
import logging
22
 
 
23
 
from twisted.internet import defer
24
 
 
25
 
from nova import context
26
 
from nova import flags
27
 
from nova import rpc
28
 
from nova import test
29
 
 
30
 
 
31
 
FLAGS = flags.FLAGS
32
 
 
33
 
 
34
 
class RpcTestCase(test.TrialTestCase):
35
 
    """Test cases for rpc"""
36
 
    def setUp(self):
37
 
        super(RpcTestCase, self).setUp()
38
 
        self.conn = rpc.Connection.instance()
39
 
        self.receiver = TestReceiver()
40
 
        self.consumer = rpc.AdapterConsumer(connection=self.conn,
41
 
                                            topic='test',
42
 
                                            proxy=self.receiver)
43
 
        self.consumer.attach_to_twisted()
44
 
        self.context = context.get_admin_context()
45
 
 
46
 
    def test_call_succeed(self):
47
 
        """Get a value through rpc call"""
48
 
        value = 42
49
 
        result = yield rpc.call_twisted(self.context,
50
 
                                        'test', {"method": "echo",
51
 
                                                 "args": {"value": value}})
52
 
        self.assertEqual(value, result)
53
 
 
54
 
    def test_context_passed(self):
55
 
        """Makes sure a context is passed through rpc call"""
56
 
        value = 42
57
 
        result = yield rpc.call_twisted(self.context,
58
 
                                        'test', {"method": "context",
59
 
                                                 "args": {"value": value}})
60
 
        self.assertEqual(self.context.to_dict(), result)
61
 
 
62
 
    def test_call_exception(self):
63
 
        """Test that exception gets passed back properly
64
 
 
65
 
        rpc.call returns a RemoteError object.  The value of the
66
 
        exception is converted to a string, so we convert it back
67
 
        to an int in the test.
68
 
        """
69
 
        value = 42
70
 
        self.assertFailure(rpc.call_twisted(self.context, 'test',
71
 
                                            {"method": "fail",
72
 
                                             "args": {"value": value}}),
73
 
                           rpc.RemoteError)
74
 
        try:
75
 
            yield rpc.call_twisted(self.context,
76
 
                                   'test', {"method": "fail",
77
 
                                            "args": {"value": value}})
78
 
            self.fail("should have thrown rpc.RemoteError")
79
 
        except rpc.RemoteError as exc:
80
 
            self.assertEqual(int(exc.value), value)
81
 
 
82
 
 
83
 
class TestReceiver(object):
84
 
    """Simple Proxy class so the consumer has methods to call
85
 
 
86
 
    Uses static methods because we aren't actually storing any state"""
87
 
 
88
 
    @staticmethod
89
 
    def echo(context, value):
90
 
        """Simply returns whatever value is sent in"""
91
 
        logging.debug("Received %s", value)
92
 
        return defer.succeed(value)
93
 
 
94
 
    @staticmethod
95
 
    def context(context, value):
96
 
        """Returns dictionary version of context"""
97
 
        logging.debug("Received %s", context)
98
 
        return defer.succeed(context.to_dict())
99
 
 
100
 
    @staticmethod
101
 
    def fail(context, value):
102
 
        """Raises an exception with the value sent in"""
103
 
        raise Exception(value)