Debian Codegraph @

neutron_lib/tests/unit/callbacks/test_registry.py

raw
1 # Copyright 2015 Cisco Systems Inc
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 from unittest import mock
16
17 from oslotest import base
18 import testtools
19
20 from neutron_lib.callbacks import events
21 from neutron_lib.callbacks import priority_group
22 from neutron_lib.callbacks import registry
23 from neutron_lib.callbacks import resources
24 from neutron_lib import fixture
25
26 PRI_CALLBACK = 20
27
28
29 @registry.has_registry_receivers
30 class ObjectWithDecoratedCallback:
31
32 def __init__(self):
33 self.counter = 0
34
35 @registry.receives(resources.PORT, [events.AFTER_CREATE,
36 events.AFTER_UPDATE])
37 @registry.receives(resources.NETWORK, [events.AFTER_DELETE])
38 def callback(self, *args, **kwargs):
39 self.counter += 1
40
41
42 class MixinWithNew:
43 def __new__(cls):
44 i = super().__new__(cls)
45 i.new_called = True
46 return i
47
48
49 @registry.has_registry_receivers
50 class AnotherObjectWithDecoratedCallback(ObjectWithDecoratedCallback,
51 MixinWithNew):
52
53 def __init__(self):
54 super().__init__()
55 self.counter2 = 0
56
57 @registry.receives(resources.NETWORK, [events.AFTER_DELETE], PRI_CALLBACK)
58 def callback2(self, *args, **kwargs):
59 self.counter2 += 1
60
61
62 @registry.has_registry_receivers
63 class CallbackClassWithParameters:
64
65 def __init__(self, dummy):
66 pass
67
68
69 def my_callback():
70 pass
71
72
73 class CallBacksManagerTestCase(base.BaseTestCase):
74
75 def test_decorated_inst_method_receives(self):
76 i1 = ObjectWithDecoratedCallback()
77 event_payload = events.EventPayload(mock.ANY)
78 registry.publish(resources.PORT, events.BEFORE_CREATE, self,
79 payload=event_payload)
80 self.assertEqual(0, i1.counter)
81 registry.publish(resources.PORT, events.AFTER_CREATE, self,
82 payload=event_payload)
83 self.assertEqual(1, i1.counter)
84 registry.publish(resources.PORT, events.AFTER_UPDATE, self,
85 payload=event_payload)
86 self.assertEqual(2, i1.counter)
87 registry.publish(resources.NETWORK, events.AFTER_UPDATE, self,
88 payload=event_payload)
89 self.assertEqual(2, i1.counter)
90 registry.publish(resources.NETWORK, events.AFTER_DELETE, self,
91 payload=event_payload)
92 self.assertEqual(3, i1.counter)
93 i2 = ObjectWithDecoratedCallback()
94 self.assertEqual(0, i2.counter)
95 registry.publish(resources.NETWORK, events.AFTER_DELETE, self,
96 payload=event_payload)
97 self.assertEqual(4, i1.counter)
98 self.assertEqual(1, i2.counter)
99
100 def test_object_inheriting_others_no_double_subscribe(self):
101 with mock.patch.object(registry, 'subscribe') as sub:
102 callback = AnotherObjectWithDecoratedCallback()
103 # there are 3 methods (2 in parent and one in child) and 1
104 # subscribes to 2 events, so we expect 4 subscribes
105 priority_call = [mock.call(
106 callback.callback2,
107 resources.NETWORK, events.AFTER_DELETE, PRI_CALLBACK)]
108 self.assertEqual(4, len(sub.mock_calls))
109 sub.assert_has_calls(priority_call)
110
111 def test_new_inheritance_not_broken(self):
112 self.assertTrue(AnotherObjectWithDecoratedCallback().new_called)
113
114 def test_object_new_not_broken(self):
115 CallbackClassWithParameters('dummy')
116
117 def test_no_strings_in_events_arg(self):
118 with testtools.ExpectedException(AssertionError):
119 registry.receives(resources.PORT, events.AFTER_CREATE)
120
121
122 class TestCallbackRegistryDispatching(base.BaseTestCase):
123
124 def setUp(self):
125 super().setUp()
126 self.callback_manager = mock.Mock()
127 self.registry_fixture = fixture.CallbackRegistryFixture(
128 callback_manager=self.callback_manager)
129 self.useFixture(self.registry_fixture)
130
131 def test_subscribe(self):
132 registry.subscribe(my_callback, 'my-resource', 'my-event')
133 self.callback_manager.subscribe.assert_called_with(
134 my_callback, 'my-resource', 'my-event',
135 priority_group.PRIORITY_DEFAULT, False)
136
137 def test_subscribe_explicit_priority(self):
138 registry.subscribe(my_callback, 'my-resource', 'my-event',
139 PRI_CALLBACK)
140 self.callback_manager.subscribe.assert_called_with(
141 my_callback, 'my-resource', 'my-event', PRI_CALLBACK, False)
142
143 def test_unsubscribe(self):
144 registry.unsubscribe(my_callback, 'my-resource', 'my-event')
145 self.callback_manager.unsubscribe.assert_called_with(
146 my_callback, 'my-resource', 'my-event')
147
148 def test_unsubscribe_by_resource(self):
149 registry.unsubscribe_by_resource(my_callback, 'my-resource')
150 self.callback_manager.unsubscribe_by_resource.assert_called_with(
151 my_callback, 'my-resource')
152
153 def test_unsubscribe_all(self):
154 registry.unsubscribe_all(my_callback)
155 self.callback_manager.unsubscribe_all.assert_called_with(
156 my_callback)
157
158 def test_clear(self):
159 registry.clear()
160 self.callback_manager.clear.assert_called_with()
161
162 def test_publish_payload(self):
163 event_payload = events.EventPayload(mock.ANY)
164 registry.publish('x', 'y', self, payload=event_payload)
165 self.callback_manager.publish.assert_called_with(
166 'x', 'y', self, payload=event_payload)