~ubuntu-branches/ubuntu/trusty/ceilometer/trusty-proposed

« back to all changes in this revision

Viewing changes to ceilometer/tests/publisher/test_rpc_publisher.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, James Page, Chuck Short
  • Date: 2014-01-23 15:08:11 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20140123150811-1zaismsuyh1hcl8y
Tags: 2014.1~b2-0ubuntu1
[ James Page ]
* d/control: Add python-jsonpath-rw to BD's.
* d/p/fix-setup-requirements.patch: Bump WebOb to support < 1.4.
 (LP: #1261101)

[ Chuck Short ]
* New upstream version.
* debian/control, debian/ceilometer-common.install: Split out
  ceilometer-alarm-evaluator and ceilometer-alarm-notifier into their
  own packages. (LP: #1250002)
* debian/ceilometer-agent-central.logrotate,
  debian/ceilometer-agent-compute.logrotate,
  debian/ceilometer-api.logrotate,
  debian/ceilometer-collector.logrotate: Add logrotate files, 
  thanks to Ahmed Rahal. (LP: #1224223)
* Fix typos in upstart files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import mock
26
26
 
27
27
from ceilometer.openstack.common.fixture import config
28
 
from ceilometer.openstack.common import jsonutils
29
28
from ceilometer.openstack.common import network_utils
30
29
from ceilometer.openstack.common import test
31
30
from ceilometer.publisher import rpc
32
31
from ceilometer import sample
33
32
 
34
33
 
35
 
class TestSignature(test.BaseTestCase):
36
 
 
37
 
    def test_compute_signature_change_key(self):
38
 
        sig1 = rpc.compute_signature({'a': 'A', 'b': 'B'},
39
 
                                     'not-so-secret')
40
 
        sig2 = rpc.compute_signature({'A': 'A', 'b': 'B'},
41
 
                                     'not-so-secret')
42
 
        self.assertNotEqual(sig1, sig2)
43
 
 
44
 
    def test_compute_signature_change_value(self):
45
 
        sig1 = rpc.compute_signature({'a': 'A', 'b': 'B'},
46
 
                                     'not-so-secret')
47
 
        sig2 = rpc.compute_signature({'a': 'a', 'b': 'B'},
48
 
                                     'not-so-secret')
49
 
        self.assertNotEqual(sig1, sig2)
50
 
 
51
 
    def test_compute_signature_same(self):
52
 
        sig1 = rpc.compute_signature({'a': 'A', 'b': 'B'},
53
 
                                     'not-so-secret')
54
 
        sig2 = rpc.compute_signature({'a': 'A', 'b': 'B'},
55
 
                                     'not-so-secret')
56
 
        self.assertEqual(sig1, sig2)
57
 
 
58
 
    def test_compute_signature_signed(self):
59
 
        data = {'a': 'A', 'b': 'B'}
60
 
        sig1 = rpc.compute_signature(data, 'not-so-secret')
61
 
        data['message_signature'] = sig1
62
 
        sig2 = rpc.compute_signature(data, 'not-so-secret')
63
 
        self.assertEqual(sig1, sig2)
64
 
 
65
 
    def test_compute_signature_use_configured_secret(self):
66
 
        data = {'a': 'A', 'b': 'B'}
67
 
        sig1 = rpc.compute_signature(data, 'not-so-secret')
68
 
        sig2 = rpc.compute_signature(data, 'different-value')
69
 
        self.assertNotEqual(sig1, sig2)
70
 
 
71
 
    def test_verify_signature_signed(self):
72
 
        data = {'a': 'A', 'b': 'B'}
73
 
        sig1 = rpc.compute_signature(data, 'not-so-secret')
74
 
        data['message_signature'] = sig1
75
 
        self.assertTrue(rpc.verify_signature(data, 'not-so-secret'))
76
 
 
77
 
    def test_verify_signature_unsigned(self):
78
 
        data = {'a': 'A', 'b': 'B'}
79
 
        self.assertFalse(rpc.verify_signature(data, 'not-so-secret'))
80
 
 
81
 
    def test_verify_signature_incorrect(self):
82
 
        data = {'a': 'A', 'b': 'B',
83
 
                'message_signature': 'Not the same'}
84
 
        self.assertFalse(rpc.verify_signature(data, 'not-so-secret'))
85
 
 
86
 
    def test_verify_signature_nested(self):
87
 
        data = {'a': 'A',
88
 
                'b': 'B',
89
 
                'nested': {'a': 'A',
90
 
                           'b': 'B',
91
 
                           },
92
 
                }
93
 
        data['message_signature'] = rpc.compute_signature(
94
 
            data,
95
 
            'not-so-secret')
96
 
        self.assertTrue(rpc.verify_signature(data, 'not-so-secret'))
97
 
 
98
 
    def test_verify_signature_nested_json(self):
99
 
        data = {'a': 'A',
100
 
                'b': 'B',
101
 
                'nested': {'a': 'A',
102
 
                           'b': 'B',
103
 
                           'c': ('c',),
104
 
                           'd': ['d']
105
 
                           },
106
 
                }
107
 
        data['message_signature'] = rpc.compute_signature(
108
 
            data,
109
 
            'not-so-secret')
110
 
        jsondata = jsonutils.loads(jsonutils.dumps(data))
111
 
        self.assertTrue(rpc.verify_signature(jsondata, 'not-so-secret'))
112
 
 
113
 
 
114
 
class TestCounter(test.BaseTestCase):
115
 
 
116
 
    TEST_COUNTER = sample.Sample(name='name',
117
 
                                 type='typ',
118
 
                                 unit='',
119
 
                                 volume=1,
120
 
                                 user_id='user',
121
 
                                 project_id='project',
122
 
                                 resource_id=2,
123
 
                                 timestamp='today',
124
 
                                 resource_metadata={'key': 'value'},
125
 
                                 source='rpc')
126
 
 
127
 
    def test_meter_message_from_counter_signed(self):
128
 
        msg = rpc.meter_message_from_counter(self.TEST_COUNTER,
129
 
                                             'not-so-secret')
130
 
        self.assertIn('message_signature', msg)
131
 
 
132
 
    def test_meter_message_from_counter_field(self):
133
 
        def compare(f, c, msg_f, msg):
134
 
            self.assertEqual(msg, c)
135
 
        msg = rpc.meter_message_from_counter(self.TEST_COUNTER,
136
 
                                             'not-so-secret')
137
 
        name_map = {'name': 'counter_name',
138
 
                    'type': 'counter_type',
139
 
                    'unit': 'counter_unit',
140
 
                    'volume': 'counter_volume'}
141
 
        for f in self.TEST_COUNTER._fields:
142
 
            msg_f = name_map.get(f, f)
143
 
            yield compare, f, getattr(self.TEST_COUNTER, f), msg_f, msg[msg_f]
144
 
 
145
 
 
146
34
class TestPublish(test.BaseTestCase):
147
 
 
148
35
    test_data = [
149
36
        sample.Sample(
150
37
            name='test',
219
106
        self.published = []
220
107
        self.rpc_unreachable = False
221
108
        self.useFixture(fixtures.MonkeyPatch(
222
 
                        "ceilometer.openstack.common.rpc.cast",
223
 
                        self.faux_cast))
 
109
            "ceilometer.openstack.common.rpc.cast",
 
110
            self.faux_cast))
224
111
 
225
112
    def test_published(self):
226
113
        publisher = rpc.RPCPublisher(
280
167
 
281
168
        def faux_cast_wait(context, topic, msg):
282
169
            self.useFixture(fixtures.MonkeyPatch(
283
 
                            "ceilometer.openstack.common.rpc.cast",
284
 
                            faux_cast_go))
 
170
                "ceilometer.openstack.common.rpc.cast",
 
171
                faux_cast_go))
285
172
            # Sleep to simulate concurrency and allow other threads to work
286
173
            eventlet.sleep(0)
287
174
            self.published.append((topic, msg))
288
175
 
289
176
        self.useFixture(fixtures.MonkeyPatch(
290
 
                        "ceilometer.openstack.common.rpc.cast",
291
 
                        faux_cast_wait))
 
177
            "ceilometer.openstack.common.rpc.cast",
 
178
            faux_cast_wait))
292
179
 
293
180
        publisher = rpc.RPCPublisher(network_utils.urlsplit('rpc://'))
294
181
        job1 = eventlet.spawn(publisher.publish_samples, None, self.test_data)