Browse Source

test_rpc: Stop f****** with global state

We're occasionally seeing stacktraces like this in our tests:

  Fatal Python error: Cannot recover from stack overflow.

  Current thread 0x00007fe66549f740 (most recent call first):
    File "...nova/.tox/py36/lib/python3.6/site-packages/oslo_config/cfg.py", line 2614 in _get
    File "...nova/.tox/py36/lib/python3.6/site-packages/oslo_config/cfg.py", line 2183 in __getattr__
    File "...nova/.tox/py36/lib/python3.6/site-packages/oslo_config/cfg.py", line 2614 in _get
    File "...nova/.tox/py36/lib/python3.6/site-packages/oslo_config/cfg.py", line 2183 in __getattr__
    File "...nova/.tox/py36/lib/python3.6/site-packages/oslo_config/cfg.py", line 2614 in _get
    ...

From a look at the oslo.config source, this seems to be occurring
because 'ConfigOpts.__cache' is somehow undefined, which results in the
'_get' method attempting to call '__getattr__' [1], which calls '_get'
[2], which calls '__getattr__' and so on.

The exact reason this is happening isn't clear, but what is clear is
that how we handle global config options in the tests that are failing
is very strange and potentially subject to race conditions. We have a
clear pattern for mocking everything and anything - the mock module -
and we should be using this here. Start doing so, reworking a lot of the
tests in the process, in order to avoid messing with oslo.config and
triggering the issue entirely.

[1] https://github.com/openstack/oslo.config/blob/6.8.1/oslo_config/cfg.py#L2614
[2] https://github.com/openstack/oslo.config/blob/6.8.1/oslo_config/cfg.py#L2183

Change-Id: I468cef94185a1b59f379ca527050450e03664c67
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
Closes-Bug: #1825435
changes/43/655843/2
Stephen Finucane 4 months ago
parent
commit
1a4a521fef
2 changed files with 146 additions and 171 deletions
  1. 1
    0
      nova/rpc.py
  2. 145
    171
      nova/tests/unit/test_rpc.py

+ 1
- 0
nova/rpc.py View File

@@ -47,6 +47,7 @@ CONF = nova.conf.CONF
47 47
 
48 48
 LOG = logging.getLogger(__name__)
49 49
 
50
+# TODO(stephenfin): These should be private
50 51
 TRANSPORT = None
51 52
 LEGACY_NOTIFIER = None
52 53
 NOTIFICATION_TRANSPORT = None

+ 145
- 171
nova/tests/unit/test_rpc.py View File

@@ -11,38 +11,19 @@
11 11
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 12
 #    License for the specific language governing permissions and limitations
13 13
 #    under the License.
14
-import copy
15 14
 
16
-import fixtures
17 15
 import mock
18 16
 import oslo_messaging as messaging
19 17
 from oslo_messaging.rpc import dispatcher
20 18
 from oslo_serialization import jsonutils
21 19
 import six
22 20
 
21
+import nova.conf
23 22
 from nova import context
24 23
 from nova import rpc
25 24
 from nova import test
26 25
 
27
-
28
-# Make a class that resets all of the global variables in nova.rpc
29
-class RPCResetFixture(fixtures.Fixture):
30
-    def _setUp(self):
31
-        self.trans = copy.copy(rpc.TRANSPORT)
32
-        self.noti_trans = copy.copy(rpc.NOTIFICATION_TRANSPORT)
33
-        self.noti = copy.copy(rpc.NOTIFIER)
34
-        self.all_mods = copy.copy(rpc.ALLOWED_EXMODS)
35
-        self.ext_mods = copy.copy(rpc.EXTRA_EXMODS)
36
-        self.conf = copy.copy(rpc.CONF)
37
-        self.addCleanup(self._reset_everything)
38
-
39
-    def _reset_everything(self):
40
-        rpc.TRANSPORT = self.trans
41
-        rpc.NOTIFICATION_TRANSPORT = self.noti_trans
42
-        rpc.NOTIFIER = self.noti
43
-        rpc.ALLOWED_EXMODS = self.all_mods
44
-        rpc.EXTRA_EXMODS = self.ext_mods
45
-        rpc.CONF = self.conf
26
+CONF = nova.conf.CONF
46 27
 
47 28
 
48 29
 class TestRPC(test.NoDBTestCase):
@@ -50,93 +31,127 @@ class TestRPC(test.NoDBTestCase):
50 31
     # We're testing the rpc code so we can't use the RPCFixture.
51 32
     STUB_RPC = False
52 33
 
53
-    def setUp(self):
54
-        super(TestRPC, self).setUp()
55
-        self.useFixture(RPCResetFixture())
56
-
34
+    @mock.patch.object(rpc, 'TRANSPORT')
35
+    @mock.patch.object(rpc, 'NOTIFICATION_TRANSPORT')
36
+    @mock.patch.object(rpc, 'LEGACY_NOTIFIER')
37
+    @mock.patch.object(rpc, 'NOTIFIER')
57 38
     @mock.patch.object(rpc, 'get_allowed_exmods')
58 39
     @mock.patch.object(rpc, 'RequestContextSerializer')
59 40
     @mock.patch.object(messaging, 'get_notification_transport')
60 41
     @mock.patch.object(messaging, 'Notifier')
61
-    def test_init_unversioned(self, mock_notif, mock_noti_trans,
62
-                              mock_ser, mock_exmods):
63
-        # The expected call to get the legacy notifier will require no new
64
-        # kwargs, and we expect the new notifier will need the noop driver
65
-        expected = [{}, {'driver': 'noop'}]
66
-        self._test_init(mock_notif, mock_noti_trans, mock_ser,
67
-                        mock_exmods, 'unversioned', expected)
42
+    def _test_init(self, notification_format, expected_driver_topic_kwargs,
43
+            mock_notif, mock_noti_trans, mock_ser, mock_exmods,
44
+            mock_NOTIFIER, mock_LEGACY_NOTIFIER, mock_NOTIFICATION_TRANSPORT,
45
+            mock_TRANSPORT,
46
+            versioned_notifications_topics=None):
68 47
 
69
-    @mock.patch.object(rpc, 'get_allowed_exmods')
70
-    @mock.patch.object(rpc, 'RequestContextSerializer')
71
-    @mock.patch.object(messaging, 'get_notification_transport')
72
-    @mock.patch.object(messaging, 'Notifier')
73
-    def test_init_both(self, mock_notif, mock_noti_trans,
74
-                       mock_ser, mock_exmods):
75
-        expected = [{}, {'topics': ['versioned_notifications']}]
76
-        self._test_init(mock_notif, mock_noti_trans, mock_ser,
77
-                        mock_exmods, 'both', expected)
48
+        if not versioned_notifications_topics:
49
+            versioned_notifications_topics = ['versioned_notifications']
78 50
 
79
-    @mock.patch.object(rpc, 'get_allowed_exmods')
80
-    @mock.patch.object(rpc, 'RequestContextSerializer')
81
-    @mock.patch.object(messaging, 'get_notification_transport')
82
-    @mock.patch.object(messaging, 'Notifier')
83
-    def test_init_versioned(self, mock_notif, mock_noti_trans,
84
-                            mock_ser, mock_exmods):
85
-        expected = [{'driver': 'noop'},
86
-                    {'topics': ['versioned_notifications']}]
87
-        self._test_init(mock_notif, mock_noti_trans, mock_ser,
88
-                        mock_exmods, 'versioned', expected)
51
+        self.flags(
52
+            notification_format=notification_format,
53
+            versioned_notifications_topics=versioned_notifications_topics,
54
+            group='notifications')
89 55
 
90
-    @mock.patch.object(rpc, 'get_allowed_exmods')
91
-    @mock.patch.object(rpc, 'RequestContextSerializer')
92
-    @mock.patch.object(messaging, 'get_notification_transport')
93
-    @mock.patch.object(messaging, 'Notifier')
94
-    def test_init_versioned_with_custom_topics(self, mock_notif,
95
-                                               mock_noti_trans, mock_ser,
96
-                                               mock_exmods):
97
-        expected = [{'driver': 'noop'},
98
-                    {'topics': ['custom_topic1', 'custom_topic2']}]
99
-        self._test_init(
100
-            mock_notif, mock_noti_trans, mock_ser, mock_exmods, 'versioned',
101
-            expected, versioned_notification_topics=['custom_topic1',
102
-                                                     'custom_topic2'])
56
+        legacy_notifier = mock.Mock()
57
+        notifier = mock.Mock()
58
+        notif_transport = mock.Mock()
59
+        transport = mock.Mock()
60
+        serializer = mock.Mock()
61
+
62
+        mock_exmods.return_value = ['foo']
63
+        mock_noti_trans.return_value = notif_transport
64
+        mock_ser.return_value = serializer
65
+        mock_notif.side_effect = [legacy_notifier, notifier]
66
+
67
+        with mock.patch.object(rpc, 'create_transport') as create_transport, \
68
+                mock.patch.object(rpc, 'get_transport_url') as get_url:
69
+            create_transport.return_value = transport
70
+            rpc.init(CONF)
71
+            create_transport.assert_called_once_with(get_url.return_value)
72
+
73
+        self.assertTrue(mock_exmods.called)
74
+        self.assertIsNotNone(mock_TRANSPORT)
75
+        self.assertIsNotNone(mock_LEGACY_NOTIFIER)
76
+        self.assertIsNotNone(mock_NOTIFIER)
77
+        self.assertEqual(legacy_notifier, rpc.LEGACY_NOTIFIER)
78
+        self.assertEqual(notifier, rpc.NOTIFIER)
79
+
80
+        expected_calls = []
81
+        for kwargs in expected_driver_topic_kwargs:
82
+            expected_kwargs = {'serializer': serializer}
83
+            expected_kwargs.update(kwargs)
84
+            expected_calls.append(((notif_transport,), expected_kwargs))
103 85
 
86
+        self.assertEqual(expected_calls, mock_notif.call_args_list,
87
+                         "The calls to messaging.Notifier() did not create "
88
+                         "the legacy and versioned notifiers properly.")
89
+
90
+    def test_init_unversioned(self):
91
+        # The expected call to get the legacy notifier will require no new
92
+        # kwargs, and we expect the new notifier will need the noop driver
93
+        expected_driver_topic_kwargs = [{}, {'driver': 'noop'}]
94
+        self._test_init('unversioned', expected_driver_topic_kwargs)
95
+
96
+    def test_init_both(self):
97
+        expected_driver_topic_kwargs = [
98
+            {},
99
+            {'topics': ['versioned_notifications']}]
100
+        self._test_init('both', expected_driver_topic_kwargs)
101
+
102
+    def test_init_versioned(self):
103
+        expected_driver_topic_kwargs = [
104
+            {'driver': 'noop'},
105
+            {'topics': ['versioned_notifications']}]
106
+        self._test_init('versioned', expected_driver_topic_kwargs)
107
+
108
+    def test_init_versioned_with_custom_topics(self):
109
+        expected_driver_topic_kwargs = [
110
+            {'driver': 'noop'},
111
+            {'topics': ['custom_topic1', 'custom_topic2']}]
112
+        versioned_notifications_topics = ['custom_topic1', 'custom_topic2']
113
+        self._test_init('versioned', expected_driver_topic_kwargs,
114
+            versioned_notifications_topics=versioned_notifications_topics)
115
+
116
+    @mock.patch.object(rpc, 'NOTIFICATION_TRANSPORT', new=mock.Mock())
117
+    @mock.patch.object(rpc, 'LEGACY_NOTIFIER', new=mock.Mock())
118
+    @mock.patch.object(rpc, 'NOTIFIER', new=mock.Mock())
104 119
     def test_cleanup_transport_null(self):
105
-        rpc.NOTIFICATION_TRANSPORT = mock.Mock()
106
-        rpc.LEGACY_NOTIFIER = mock.Mock()
107
-        rpc.NOTIFIER = mock.Mock()
120
+        """Ensure cleanup fails if 'rpc.TRANSPORT' wasn't set."""
108 121
         self.assertRaises(AssertionError, rpc.cleanup)
109 122
 
123
+    @mock.patch.object(rpc, 'TRANSPORT', new=mock.Mock())
124
+    @mock.patch.object(rpc, 'LEGACY_NOTIFIER', new=mock.Mock())
125
+    @mock.patch.object(rpc, 'NOTIFIER', new=mock.Mock())
110 126
     def test_cleanup_notification_transport_null(self):
111
-        rpc.TRANSPORT = mock.Mock()
112
-        rpc.NOTIFIER = mock.Mock()
127
+        """Ensure cleanup fails if 'rpc.NOTIFICATION_TRANSPORT' wasn't set."""
113 128
         self.assertRaises(AssertionError, rpc.cleanup)
114 129
 
130
+    @mock.patch.object(rpc, 'TRANSPORT', new=mock.Mock())
131
+    @mock.patch.object(rpc, 'NOTIFICATION_TRANSPORT', new=mock.Mock())
132
+    @mock.patch.object(rpc, 'NOTIFIER', new=mock.Mock())
115 133
     def test_cleanup_legacy_notifier_null(self):
116
-        rpc.TRANSPORT = mock.Mock()
117
-        rpc.NOTIFICATION_TRANSPORT = mock.Mock()
118
-        rpc.NOTIFIER = mock.Mock()
134
+        """Ensure cleanup fails if 'rpc.LEGACY_NOTIFIER' wasn't set."""
135
+        self.assertRaises(AssertionError, rpc.cleanup)
119 136
 
137
+    @mock.patch.object(rpc, 'TRANSPORT', new=mock.Mock())
138
+    @mock.patch.object(rpc, 'NOTIFICATION_TRANSPORT', new=mock.Mock())
139
+    @mock.patch.object(rpc, 'LEGACY_NOTIFIER', new=mock.Mock())
120 140
     def test_cleanup_notifier_null(self):
121
-        rpc.TRANSPORT = mock.Mock()
122
-        rpc.LEGACY_NOTIFIER = mock.Mock()
123
-        rpc.NOTIFICATION_TRANSPORT = mock.Mock()
141
+        """Ensure cleanup fails if 'rpc.NOTIFIER' wasn't set."""
124 142
         self.assertRaises(AssertionError, rpc.cleanup)
125 143
 
126
-    def test_cleanup(self):
127
-        rpc.LEGACY_NOTIFIER = mock.Mock()
128
-        rpc.NOTIFIER = mock.Mock()
129
-        rpc.NOTIFICATION_TRANSPORT = mock.Mock()
130
-        rpc.TRANSPORT = mock.Mock()
131
-        trans_cleanup = mock.Mock()
132
-        not_trans_cleanup = mock.Mock()
133
-        rpc.TRANSPORT.cleanup = trans_cleanup
134
-        rpc.NOTIFICATION_TRANSPORT.cleanup = not_trans_cleanup
135
-
144
+    @mock.patch.object(rpc, 'TRANSPORT')
145
+    @mock.patch.object(rpc, 'NOTIFICATION_TRANSPORT')
146
+    @mock.patch.object(rpc, 'LEGACY_NOTIFIER')
147
+    @mock.patch.object(rpc, 'NOTIFIER')
148
+    def test_cleanup(self, mock_NOTIFIER, mock_LEGACY_NOTIFIER,
149
+            mock_NOTIFICATION_TRANSPORT, mock_TRANSPORT):
136 150
         rpc.cleanup()
137 151
 
138
-        trans_cleanup.assert_called_once_with()
139
-        not_trans_cleanup.assert_called_once_with()
152
+        mock_TRANSPORT.cleanup.assert_called_once_with()
153
+        mock_NOTIFICATION_TRANSPORT.cleanup.assert_called_once_with()
154
+
140 155
         self.assertIsNone(rpc.TRANSPORT)
141 156
         self.assertIsNone(rpc.NOTIFICATION_TRANSPORT)
142 157
         self.assertIsNone(rpc.LEGACY_NOTIFIER)
@@ -151,54 +166,56 @@ class TestRPC(test.NoDBTestCase):
151 166
         mock_set.assert_called_once_with(control_exchange)
152 167
 
153 168
     def test_add_extra_exmods(self):
154
-        rpc.EXTRA_EXMODS = []
169
+        extra_exmods = []
155 170
 
156
-        rpc.add_extra_exmods('foo', 'bar')
157
-
158
-        self.assertEqual(['foo', 'bar'], rpc.EXTRA_EXMODS)
171
+        with mock.patch.object(
172
+                rpc, 'EXTRA_EXMODS', extra_exmods) as mock_EXTRA_EXMODS:
173
+            rpc.add_extra_exmods('foo', 'bar')
174
+            self.assertEqual(['foo', 'bar'], mock_EXTRA_EXMODS)
159 175
 
160 176
     def test_clear_extra_exmods(self):
161
-        rpc.EXTRA_EXMODS = ['foo', 'bar']
162
-
163
-        rpc.clear_extra_exmods()
177
+        extra_exmods = ['foo', 'bar']
164 178
 
165
-        self.assertEqual(0, len(rpc.EXTRA_EXMODS))
179
+        with mock.patch.object(
180
+                rpc, 'EXTRA_EXMODS', extra_exmods) as mock_EXTRA_EXMODS:
181
+            rpc.clear_extra_exmods()
182
+            self.assertEqual([], mock_EXTRA_EXMODS)
166 183
 
167 184
     def test_get_allowed_exmods(self):
168
-        rpc.ALLOWED_EXMODS = ['foo']
169
-        rpc.EXTRA_EXMODS = ['bar']
185
+        allowed_exmods = ['foo']
186
+        extra_exmods = ['bar']
170 187
 
171
-        exmods = rpc.get_allowed_exmods()
188
+        with test.nested(
189
+                mock.patch.object(rpc, 'EXTRA_EXMODS', extra_exmods),
190
+                mock.patch.object(rpc, 'ALLOWED_EXMODS', allowed_exmods)
191
+                ) as (mock_EXTRA_EXMODS, mock_ALLOWED_EXMODS):
192
+            exmods = rpc.get_allowed_exmods()
172 193
 
173 194
         self.assertEqual(['foo', 'bar'], exmods)
174 195
 
175 196
     @mock.patch.object(messaging, 'TransportURL')
176 197
     def test_get_transport_url(self, mock_url):
177
-        conf = mock.Mock()
178
-        rpc.CONF = conf
179 198
         mock_url.parse.return_value = 'foo'
180 199
 
181 200
         url = rpc.get_transport_url(url_str='bar')
182 201
 
183 202
         self.assertEqual('foo', url)
184
-        mock_url.parse.assert_called_once_with(conf, 'bar')
203
+        mock_url.parse.assert_called_once_with(rpc.CONF, 'bar')
185 204
 
186 205
     @mock.patch.object(messaging, 'TransportURL')
187 206
     def test_get_transport_url_null(self, mock_url):
188
-        conf = mock.Mock()
189
-        rpc.CONF = conf
190 207
         mock_url.parse.return_value = 'foo'
191 208
 
192 209
         url = rpc.get_transport_url()
193 210
 
194 211
         self.assertEqual('foo', url)
195
-        mock_url.parse.assert_called_once_with(conf, None)
212
+        mock_url.parse.assert_called_once_with(rpc.CONF, None)
196 213
 
214
+    @mock.patch.object(rpc, 'TRANSPORT')
197 215
     @mock.patch.object(rpc, 'profiler', None)
198 216
     @mock.patch.object(rpc, 'RequestContextSerializer')
199 217
     @mock.patch.object(messaging, 'RPCClient')
200
-    def test_get_client(self, mock_client, mock_ser):
201
-        rpc.TRANSPORT = mock.Mock()
218
+    def test_get_client(self, mock_client, mock_ser, mock_TRANSPORT):
202 219
         tgt = mock.Mock()
203 220
         ser = mock.Mock()
204 221
         mock_client.return_value = 'client'
@@ -207,17 +224,17 @@ class TestRPC(test.NoDBTestCase):
207 224
         client = rpc.get_client(tgt, version_cap='1.0', serializer='foo')
208 225
 
209 226
         mock_ser.assert_called_once_with('foo')
210
-        mock_client.assert_called_once_with(rpc.TRANSPORT,
227
+        mock_client.assert_called_once_with(mock_TRANSPORT,
211 228
                                             tgt, version_cap='1.0',
212 229
                                             call_monitor_timeout=None,
213 230
                                             serializer=ser)
214 231
         self.assertEqual('client', client)
215 232
 
233
+    @mock.patch.object(rpc, 'TRANSPORT')
216 234
     @mock.patch.object(rpc, 'profiler', None)
217 235
     @mock.patch.object(rpc, 'RequestContextSerializer')
218 236
     @mock.patch.object(messaging, 'get_rpc_server')
219
-    def test_get_server(self, mock_get, mock_ser):
220
-        rpc.TRANSPORT = mock.Mock()
237
+    def test_get_server(self, mock_get, mock_ser, mock_TRANSPORT):
221 238
         ser = mock.Mock()
222 239
         tgt = mock.Mock()
223 240
         ends = mock.Mock()
@@ -228,16 +245,17 @@ class TestRPC(test.NoDBTestCase):
228 245
 
229 246
         mock_ser.assert_called_once_with('foo')
230 247
         access_policy = dispatcher.DefaultRPCAccessPolicy
231
-        mock_get.assert_called_once_with(rpc.TRANSPORT, tgt, ends,
248
+        mock_get.assert_called_once_with(mock_TRANSPORT, tgt, ends,
232 249
                                          executor='eventlet', serializer=ser,
233 250
                                          access_policy=access_policy)
234 251
         self.assertEqual('server', server)
235 252
 
253
+    @mock.patch.object(rpc, 'TRANSPORT')
236 254
     @mock.patch.object(rpc, 'profiler', mock.Mock())
237 255
     @mock.patch.object(rpc, 'ProfilerRequestContextSerializer')
238 256
     @mock.patch.object(messaging, 'RPCClient')
239
-    def test_get_client_profiler_enabled(self, mock_client, mock_ser):
240
-        rpc.TRANSPORT = mock.Mock()
257
+    def test_get_client_profiler_enabled(self, mock_client, mock_ser,
258
+            mock_TRANSPORT):
241 259
         tgt = mock.Mock()
242 260
         ser = mock.Mock()
243 261
         mock_client.return_value = 'client'
@@ -246,17 +264,19 @@ class TestRPC(test.NoDBTestCase):
246 264
         client = rpc.get_client(tgt, version_cap='1.0', serializer='foo')
247 265
 
248 266
         mock_ser.assert_called_once_with('foo')
249
-        mock_client.assert_called_once_with(rpc.TRANSPORT,
267
+        mock_client.assert_called_once_with(mock_TRANSPORT,
250 268
                                             tgt, version_cap='1.0',
251 269
                                             call_monitor_timeout=None,
252 270
                                             serializer=ser)
253 271
         self.assertEqual('client', client)
254 272
 
273
+    @mock.patch.object(rpc, 'TRANSPORT')
274
+    @mock.patch.object(rpc, 'profiler', mock.Mock())
255 275
     @mock.patch.object(rpc, 'profiler', mock.Mock())
256 276
     @mock.patch.object(rpc, 'ProfilerRequestContextSerializer')
257 277
     @mock.patch.object(messaging, 'get_rpc_server')
258
-    def test_get_server_profiler_enabled(self, mock_get, mock_ser):
259
-        rpc.TRANSPORT = mock.Mock()
278
+    def test_get_server_profiler_enabled(self, mock_get, mock_ser,
279
+            mock_TRANSPORT):
260 280
         ser = mock.Mock()
261 281
         tgt = mock.Mock()
262 282
         ends = mock.Mock()
@@ -267,16 +287,16 @@ class TestRPC(test.NoDBTestCase):
267 287
 
268 288
         mock_ser.assert_called_once_with('foo')
269 289
         access_policy = dispatcher.DefaultRPCAccessPolicy
270
-        mock_get.assert_called_once_with(rpc.TRANSPORT, tgt, ends,
290
+        mock_get.assert_called_once_with(mock_TRANSPORT, tgt, ends,
271 291
                                          executor='eventlet', serializer=ser,
272 292
                                          access_policy=access_policy)
273 293
         self.assertEqual('server', server)
274 294
 
275
-    def test_get_notifier(self):
276
-        rpc.LEGACY_NOTIFIER = mock.Mock()
295
+    @mock.patch.object(rpc, 'LEGACY_NOTIFIER')
296
+    def test_get_notifier(self, mock_LEGACY_NOTIFIER):
277 297
         mock_prep = mock.Mock()
278 298
         mock_prep.return_value = 'notifier'
279
-        rpc.LEGACY_NOTIFIER.prepare = mock_prep
299
+        mock_LEGACY_NOTIFIER.prepare = mock_prep
280 300
 
281 301
         notifier = rpc.get_notifier('service', publisher_id='foo')
282 302
 
@@ -284,11 +304,11 @@ class TestRPC(test.NoDBTestCase):
284 304
         self.assertIsInstance(notifier, rpc.LegacyValidatingNotifier)
285 305
         self.assertEqual('notifier', notifier.notifier)
286 306
 
287
-    def test_get_notifier_null_publisher(self):
288
-        rpc.LEGACY_NOTIFIER = mock.Mock()
307
+    @mock.patch.object(rpc, 'LEGACY_NOTIFIER')
308
+    def test_get_notifier_null_publisher(self, mock_LEGACY_NOTIFIER):
289 309
         mock_prep = mock.Mock()
290 310
         mock_prep.return_value = 'notifier'
291
-        rpc.LEGACY_NOTIFIER.prepare = mock_prep
311
+        mock_LEGACY_NOTIFIER.prepare = mock_prep
292 312
 
293 313
         notifier = rpc.get_notifier('service', host='bar')
294 314
 
@@ -296,11 +316,11 @@ class TestRPC(test.NoDBTestCase):
296 316
         self.assertIsInstance(notifier, rpc.LegacyValidatingNotifier)
297 317
         self.assertEqual('notifier', notifier.notifier)
298 318
 
299
-    def test_get_versioned_notifier(self):
300
-        rpc.NOTIFIER = mock.Mock()
319
+    @mock.patch.object(rpc, 'NOTIFIER')
320
+    def test_get_versioned_notifier(self, mock_NOTIFIER):
301 321
         mock_prep = mock.Mock()
302 322
         mock_prep.return_value = 'notifier'
303
-        rpc.NOTIFIER.prepare = mock_prep
323
+        mock_NOTIFIER.prepare = mock_prep
304 324
 
305 325
         notifier = rpc.get_versioned_notifier('service.foo')
306 326
 
@@ -318,52 +338,6 @@ class TestRPC(test.NoDBTestCase):
318 338
                                                url=mock.sentinel.url,
319 339
                                                allowed_remote_exmods=exmods)
320 340
 
321
-    def _test_init(self, mock_notif, mock_noti_trans, mock_ser,
322
-                   mock_exmods, notif_format, expected_driver_topic_kwargs,
323
-                   versioned_notification_topics=['versioned_notifications']):
324
-        legacy_notifier = mock.Mock()
325
-        notifier = mock.Mock()
326
-        notif_transport = mock.Mock()
327
-        transport = mock.Mock()
328
-        serializer = mock.Mock()
329
-        conf = mock.Mock()
330
-
331
-        conf.transport_url = None
332
-        conf.notifications.notification_format = notif_format
333
-        conf.notifications.versioned_notifications_topics = (
334
-            versioned_notification_topics)
335
-        mock_exmods.return_value = ['foo']
336
-        mock_noti_trans.return_value = notif_transport
337
-        mock_ser.return_value = serializer
338
-        mock_notif.side_effect = [legacy_notifier, notifier]
339
-
340
-        @mock.patch.object(rpc, 'CONF', new=conf)
341
-        @mock.patch.object(rpc, 'create_transport')
342
-        @mock.patch.object(rpc, 'get_transport_url')
343
-        def _test(get_url, create_transport):
344
-            create_transport.return_value = transport
345
-            rpc.init(conf)
346
-            create_transport.assert_called_once_with(get_url.return_value)
347
-
348
-        _test()
349
-
350
-        self.assertTrue(mock_exmods.called)
351
-        self.assertIsNotNone(rpc.TRANSPORT)
352
-        self.assertIsNotNone(rpc.LEGACY_NOTIFIER)
353
-        self.assertIsNotNone(rpc.NOTIFIER)
354
-        self.assertEqual(legacy_notifier, rpc.LEGACY_NOTIFIER)
355
-        self.assertEqual(notifier, rpc.NOTIFIER)
356
-
357
-        expected_calls = []
358
-        for kwargs in expected_driver_topic_kwargs:
359
-            expected_kwargs = {'serializer': serializer}
360
-            expected_kwargs.update(kwargs)
361
-            expected_calls.append(((notif_transport,), expected_kwargs))
362
-
363
-        self.assertEqual(expected_calls, mock_notif.call_args_list,
364
-                         "The calls to messaging.Notifier() did not create "
365
-                         "the legacy and versioned notifiers properly.")
366
-
367 341
 
368 342
 class TestJsonPayloadSerializer(test.NoDBTestCase):
369 343
     def test_serialize_entity(self):

Loading…
Cancel
Save