Browse Source

Use get_notification_transport() for notifications

In oslo.messaging 2.9.0, the RPC and notification buses were decoupled
into separate transports. This changes over the Nova notifier to use the
notification transport instead of the original transport (which is now
used for RPC).

Change-Id: I595d9dd7986a87dfc93e3579d4498b2d9838a4d8
Partially-Implements: bp oslo-for-mitaka
tags/13.0.0.0b3
Ryan Rossiter 3 years ago
parent
commit
2356f90e0b
3 changed files with 57 additions and 25 deletions
  1. 20
    10
      nova/rpc.py
  2. 2
    0
      nova/tests/unit/test_notifier.py
  3. 35
    15
      nova/tests/unit/test_rpc.py

+ 20
- 10
nova/rpc.py View File

@@ -47,6 +47,7 @@ CONF.register_opts(notification_opts)
47 47
 
48 48
 TRANSPORT = None
49 49
 LEGACY_NOTIFIER = None
50
+NOTIFICATION_TRANSPORT = None
50 51
 NOTIFIER = None
51 52
 
52 53
 ALLOWED_EXMODS = [
@@ -68,34 +69,43 @@ TRANSPORT_ALIASES = {
68 69
 
69 70
 
70 71
 def init(conf):
71
-    global TRANSPORT, LEGACY_NOTIFIER, NOTIFIER
72
+    global TRANSPORT, NOTIFICATION_TRANSPORT, LEGACY_NOTIFIER, NOTIFIER
72 73
     exmods = get_allowed_exmods()
73 74
     TRANSPORT = messaging.get_transport(conf,
74 75
                                         allowed_remote_exmods=exmods,
75 76
                                         aliases=TRANSPORT_ALIASES)
77
+    NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
78
+        conf, allowed_remote_exmods=exmods, aliases=TRANSPORT_ALIASES)
76 79
     serializer = RequestContextSerializer(JsonPayloadSerializer())
77 80
     if conf.notification_format == 'unversioned':
78
-        LEGACY_NOTIFIER = messaging.Notifier(TRANSPORT, serializer=serializer)
79
-        NOTIFIER = messaging.Notifier(TRANSPORT, serializer=serializer,
80
-                                      driver='noop')
81
+        LEGACY_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
82
+                                             serializer=serializer)
83
+        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
84
+                                      serializer=serializer, driver='noop')
81 85
     elif conf.notification_format == 'both':
82
-        LEGACY_NOTIFIER = messaging.Notifier(TRANSPORT, serializer=serializer)
83
-        NOTIFIER = messaging.Notifier(TRANSPORT, serializer=serializer,
86
+        LEGACY_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
87
+                                             serializer=serializer)
88
+        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
89
+                                      serializer=serializer,
84 90
                                       topic='versioned_notifications')
85 91
     else:
86
-        LEGACY_NOTIFIER = messaging.Notifier(TRANSPORT, serializer=serializer,
92
+        LEGACY_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
93
+                                             serializer=serializer,
87 94
                                              driver='noop')
88
-        NOTIFIER = messaging.Notifier(TRANSPORT, serializer=serializer,
95
+        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
96
+                                      serializer=serializer,
89 97
                                       topic='versioned_notifications')
90 98
 
91 99
 
92 100
 def cleanup():
93
-    global TRANSPORT, LEGACY_NOTIFIER, NOTIFIER
101
+    global TRANSPORT, NOTIFICATION_TRANSPORT, LEGACY_NOTIFIER, NOTIFIER
94 102
     assert TRANSPORT is not None
103
+    assert NOTIFICATION_TRANSPORT is not None
95 104
     assert LEGACY_NOTIFIER is not None
96 105
     assert NOTIFIER is not None
97 106
     TRANSPORT.cleanup()
98
-    TRANSPORT = LEGACY_NOTIFIER = NOTIFIER = None
107
+    NOTIFICATION_TRANSPORT.cleanup()
108
+    TRANSPORT = NOTIFICATION_TRANSPORT = LEGACY_NOTIFIER = NOTIFIER = None
99 109
 
100 110
 
101 111
 def set_defaults(control_exchange):

+ 2
- 0
nova/tests/unit/test_notifier.py View File

@@ -22,9 +22,11 @@ from nova import test
22 22
 class TestNotifier(test.NoDBTestCase):
23 23
 
24 24
     @mock.patch('oslo_messaging.get_transport')
25
+    @mock.patch('oslo_messaging.get_notification_transport')
25 26
     @mock.patch('oslo_messaging.Notifier')
26 27
     def test_notification_format_affects_notification_driver(self,
27 28
                                                              mock_notifier,
29
+                                                             mock_noti_trans,
28 30
                                                              mock_transport):
29 31
         conf = mock.Mock()
30 32
 

+ 35
- 15
nova/tests/unit/test_rpc.py View File

@@ -28,6 +28,7 @@ from nova import test
28 28
 class RPCResetFixture(fixtures.Fixture):
29 29
     def _setUp(self):
30 30
         self.trans = copy.copy(rpc.TRANSPORT)
31
+        self.noti_trans = copy.copy(rpc.NOTIFICATION_TRANSPORT)
31 32
         self.noti = copy.copy(rpc.NOTIFIER)
32 33
         self.all_mods = copy.copy(rpc.ALLOWED_EXMODS)
33 34
         self.ext_mods = copy.copy(rpc.EXTRA_EXMODS)
@@ -35,6 +36,7 @@ class RPCResetFixture(fixtures.Fixture):
35 36
 
36 37
     def _reset_everything(self):
37 38
         rpc.TRANSPORT = self.trans
39
+        rpc.NOTIFICATION_TRANSPORT = self.noti_trans
38 40
         rpc.NOTIFIER = self.noti
39 41
         rpc.ALLOWED_EXMODS = self.all_mods
40 42
         rpc.EXTRA_EXMODS = self.ext_mods
@@ -50,60 +52,76 @@ class TestRPC(testtools.TestCase):
50 52
     @mock.patch.object(rpc, 'get_allowed_exmods')
51 53
     @mock.patch.object(rpc, 'RequestContextSerializer')
52 54
     @mock.patch.object(messaging, 'get_transport')
55
+    @mock.patch.object(messaging, 'get_notification_transport')
53 56
     @mock.patch.object(messaging, 'Notifier')
54
-    def test_init_unversioned(self, mock_notif, mock_trans, mock_ser,
55
-                              mock_exmods):
57
+    def test_init_unversioned(self, mock_notif, mock_noti_trans, mock_trans,
58
+                              mock_ser, mock_exmods):
56 59
         # The expected call to get the legacy notifier will require no new
57 60
         # kwargs, and we expect the new notifier will need the noop driver
58 61
         expected = [{}, {'driver': 'noop'}]
59
-        self._test_init(mock_notif, mock_trans, mock_ser, mock_exmods,
60
-                        'unversioned', expected)
62
+        self._test_init(mock_notif, mock_noti_trans, mock_trans, mock_ser,
63
+                        mock_exmods, 'unversioned', expected)
61 64
 
62 65
     @mock.patch.object(rpc, 'get_allowed_exmods')
63 66
     @mock.patch.object(rpc, 'RequestContextSerializer')
64 67
     @mock.patch.object(messaging, 'get_transport')
68
+    @mock.patch.object(messaging, 'get_notification_transport')
65 69
     @mock.patch.object(messaging, 'Notifier')
66
-    def test_init_both(self, mock_notif, mock_trans, mock_ser, mock_exmods):
70
+    def test_init_both(self, mock_notif, mock_noti_trans, mock_trans,
71
+                       mock_ser, mock_exmods):
67 72
         expected = [{}, {'topic': 'versioned_notifications'}]
68
-        self._test_init(mock_notif, mock_trans, mock_ser, mock_exmods,
69
-                        'both', expected)
73
+        self._test_init(mock_notif, mock_noti_trans, mock_trans, mock_ser,
74
+                        mock_exmods, 'both', expected)
70 75
 
71 76
     @mock.patch.object(rpc, 'get_allowed_exmods')
72 77
     @mock.patch.object(rpc, 'RequestContextSerializer')
73 78
     @mock.patch.object(messaging, 'get_transport')
79
+    @mock.patch.object(messaging, 'get_notification_transport')
74 80
     @mock.patch.object(messaging, 'Notifier')
75
-    def test_init_versioned(self, mock_notif, mock_trans, mock_ser,
76
-                            mock_exmods):
81
+    def test_init_versioned(self, mock_notif, mock_noti_trans, mock_trans,
82
+                            mock_ser, mock_exmods):
77 83
         expected = [{'driver': 'noop'}, {'topic': 'versioned_notifications'}]
78
-        self._test_init(mock_notif, mock_trans, mock_ser, mock_exmods,
79
-                        'versioned', expected)
84
+        self._test_init(mock_notif, mock_noti_trans, mock_trans, mock_ser,
85
+                        mock_exmods, 'versioned', expected)
80 86
 
81 87
     def test_cleanup_transport_null(self):
88
+        rpc.NOTIFICATION_TRANSPORT = mock.Mock()
82 89
         rpc.LEGACY_NOTIFIER = mock.Mock()
83 90
         rpc.NOTIFIER = mock.Mock()
84 91
         self.assertRaises(AssertionError, rpc.cleanup)
85 92
 
86
-    def test_cleanup_legacy_notifier_null(self):
93
+    def test_cleanup_notification_transport_null(self):
87 94
         rpc.TRANSPORT = mock.Mock()
88 95
         rpc.NOTIFIER = mock.Mock()
89 96
         self.assertRaises(AssertionError, rpc.cleanup)
90 97
 
98
+    def test_cleanup_legacy_notifier_null(self):
99
+        rpc.TRANSPORT = mock.Mock()
100
+        rpc.NOTIFICATION_TRANSPORT = mock.Mock()
101
+        rpc.NOTIFIER = mock.Mock()
102
+
91 103
     def test_cleanup_notifier_null(self):
92 104
         rpc.TRANSPORT = mock.Mock()
93 105
         rpc.LEGACY_NOTIFIER = mock.Mock()
106
+        rpc.NOTIFICATION_TRANSPORT = mock.Mock()
94 107
         self.assertRaises(AssertionError, rpc.cleanup)
95 108
 
96 109
     def test_cleanup(self):
97 110
         rpc.LEGACY_NOTIFIER = mock.Mock()
98 111
         rpc.NOTIFIER = mock.Mock()
112
+        rpc.NOTIFICATION_TRANSPORT = mock.Mock()
99 113
         rpc.TRANSPORT = mock.Mock()
100 114
         trans_cleanup = mock.Mock()
115
+        not_trans_cleanup = mock.Mock()
101 116
         rpc.TRANSPORT.cleanup = trans_cleanup
117
+        rpc.NOTIFICATION_TRANSPORT.cleanup = not_trans_cleanup
102 118
 
103 119
         rpc.cleanup()
104 120
 
105 121
         trans_cleanup.assert_called_once_with()
122
+        not_trans_cleanup.assert_called_once_with()
106 123
         self.assertIsNone(rpc.TRANSPORT)
124
+        self.assertIsNone(rpc.NOTIFICATION_TRANSPORT)
107 125
         self.assertIsNone(rpc.LEGACY_NOTIFIER)
108 126
         self.assertIsNone(rpc.NOTIFIER)
109 127
 
@@ -228,10 +246,11 @@ class TestRPC(testtools.TestCase):
228 246
         mock_prep.assert_called_once_with(publisher_id='service.foo')
229 247
         self.assertEqual('notifier', notifier)
230 248
 
231
-    def _test_init(self, mock_notif, mock_trans, mock_ser, mock_exmods,
232
-                   notif_format, expected_driver_topic_kwargs):
249
+    def _test_init(self, mock_notif, mock_noti_trans, mock_trans, mock_ser,
250
+                   mock_exmods, notif_format, expected_driver_topic_kwargs):
233 251
         legacy_notifier = mock.Mock()
234 252
         notifier = mock.Mock()
253
+        notif_transport = mock.Mock()
235 254
         transport = mock.Mock()
236 255
         serializer = mock.Mock()
237 256
         conf = mock.Mock()
@@ -239,6 +258,7 @@ class TestRPC(testtools.TestCase):
239 258
         conf.notification_format = notif_format
240 259
         mock_exmods.return_value = ['foo']
241 260
         mock_trans.return_value = transport
261
+        mock_noti_trans.return_value = notif_transport
242 262
         mock_ser.return_value = serializer
243 263
         mock_notif.side_effect = [legacy_notifier, notifier]
244 264
 
@@ -258,7 +278,7 @@ class TestRPC(testtools.TestCase):
258 278
         for kwargs in expected_driver_topic_kwargs:
259 279
             expected_kwargs = {'serializer': serializer}
260 280
             expected_kwargs.update(kwargs)
261
-            expected_calls.append(((transport,), expected_kwargs))
281
+            expected_calls.append(((notif_transport,), expected_kwargs))
262 282
 
263 283
         self.assertEqual(expected_calls, mock_notif.call_args_list,
264 284
                          "The calls to messaging.Notifier() did not create "

Loading…
Cancel
Save