From 73cb32238a3443cc171f85ecc6767d81f4dd927b Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 31 Mar 2016 16:53:45 +0200 Subject: [PATCH] Remove oslo.context dependency oslo.context is only used to generate a request id, something we can easily do since it's just about generating a random string. There's no reason to depends on this library just for that, especially since we're never going to use the rest of it. Change-Id: I6b8618bdc32264f9f5e66a4697d1886e4f292dc8 --- aodh/notifier/rest.py | 8 ++-- aodh/tests/unit/test_notifier.py | 70 +++++++++++++++++++++++--------- requirements.txt | 1 - 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/aodh/notifier/rest.py b/aodh/notifier/rest.py index fe61a2e0e..8683b445c 100644 --- a/aodh/notifier/rest.py +++ b/aodh/notifier/rest.py @@ -14,8 +14,9 @@ # under the License. """Rest alarm notifier.""" +import uuid + from oslo_config import cfg -from oslo_context import context from oslo_log import log from oslo_serialization import jsonutils import requests @@ -58,8 +59,9 @@ class RestAlarmNotifier(notifier.AlarmNotifier): def notify(self, action, alarm_id, alarm_name, severity, previous, current, reason, reason_data, headers=None): headers = headers or {} - if not headers.get('x-openstack-request-id'): - headers['x-openstack-request-id'] = context.generate_request_id() + if 'x-openstack-request-id' not in headers: + headers['x-openstack-request-id'] = b'req-' + str( + uuid.uuid4()).encode('ascii') LOG.info(_( "Notifying alarm %(alarm_name)s %(alarm_id)s with severity" diff --git a/aodh/tests/unit/test_notifier.py b/aodh/tests/unit/test_notifier.py index 509121cb4..55417c919 100644 --- a/aodh/tests/unit/test_notifier.py +++ b/aodh/tests/unit/test_notifier.py @@ -66,9 +66,6 @@ class TestAlarmNotifier(tests_base.BaseTestCase): 'aodh.notifier.zaqar.ZaqarAlarmNotifier.get_zaqar_client', return_value=self.zaqar)) self.service = notifier.AlarmNotifierService(self.CONF) - self.useFixture(mockpatch.Patch( - 'oslo_context.context.generate_request_id', - self._fake_generate_request_id)) def test_notify_alarm(self): data = { @@ -111,12 +108,6 @@ class TestAlarmNotifier(tests_base.BaseTestCase): notification['actions'] = [action] return notification - HTTP_HEADERS = {'x-openstack-request-id': 'fake_request_id', - 'content-type': 'application/json'} - - def _fake_generate_request_id(self): - return self.HTTP_HEADERS['x-openstack-request-id'] - def test_notify_alarm_rest_action_ok(self): action = 'http://host/action' @@ -126,7 +117,13 @@ class TestAlarmNotifier(tests_base.BaseTestCase): poster.assert_called_with(action, data=mock.ANY, headers=mock.ANY) args, kwargs = poster.call_args - self.assertEqual(self.HTTP_HEADERS, kwargs['headers']) + self.assertEqual( + { + 'x-openstack-request-id': + kwargs['headers']['x-openstack-request-id'], + 'content-type': 'application/json' + }, + kwargs['headers']) self.assertEqual(DATA_JSON, jsonutils.loads(kwargs['data'])) def test_notify_alarm_rest_action_with_ssl_client_cert(self): @@ -142,7 +139,13 @@ class TestAlarmNotifier(tests_base.BaseTestCase): headers=mock.ANY, cert=certificate, verify=True) args, kwargs = poster.call_args - self.assertEqual(self.HTTP_HEADERS, kwargs['headers']) + self.assertEqual( + { + 'x-openstack-request-id': + kwargs['headers']['x-openstack-request-id'], + 'content-type': 'application/json' + }, + kwargs['headers']) self.assertEqual(DATA_JSON, jsonutils.loads(kwargs['data'])) def test_notify_alarm_rest_action_with_ssl_client_cert_and_key(self): @@ -160,7 +163,12 @@ class TestAlarmNotifier(tests_base.BaseTestCase): headers=mock.ANY, cert=(certificate, key), verify=True) args, kwargs = poster.call_args - self.assertEqual(self.HTTP_HEADERS, kwargs['headers']) + self.assertEqual( + { + 'x-openstack-request-id': + kwargs['headers']['x-openstack-request-id'], + 'content-type': 'application/json'}, + kwargs['headers']) self.assertEqual(DATA_JSON, jsonutils.loads(kwargs['data'])) def test_notify_alarm_rest_action_with_ssl_verify_disable_by_cfg(self): @@ -175,7 +183,13 @@ class TestAlarmNotifier(tests_base.BaseTestCase): headers=mock.ANY, verify=False) args, kwargs = poster.call_args - self.assertEqual(self.HTTP_HEADERS, kwargs['headers']) + self.assertEqual( + { + 'x-openstack-request-id': + kwargs['headers']['x-openstack-request-id'], + 'content-type': 'application/json' + }, + kwargs['headers']) self.assertEqual(DATA_JSON, jsonutils.loads(kwargs['data'])) def test_notify_alarm_rest_action_with_ssl_verify_disable(self): @@ -188,7 +202,13 @@ class TestAlarmNotifier(tests_base.BaseTestCase): headers=mock.ANY, verify=False) args, kwargs = poster.call_args - self.assertEqual(self.HTTP_HEADERS, kwargs['headers']) + self.assertEqual( + { + 'x-openstack-request-id': + kwargs['headers']['x-openstack-request-id'], + 'content-type': 'application/json' + }, + kwargs['headers']) self.assertEqual(DATA_JSON, jsonutils.loads(kwargs['data'])) def test_notify_alarm_rest_action_with_ssl_verify_enable_by_user(self): @@ -203,7 +223,13 @@ class TestAlarmNotifier(tests_base.BaseTestCase): headers=mock.ANY, verify=True) args, kwargs = poster.call_args - self.assertEqual(self.HTTP_HEADERS, kwargs['headers']) + self.assertEqual( + { + 'x-openstack-request-id': + kwargs['headers']['x-openstack-request-id'], + 'content-type': 'application/json' + }, + kwargs['headers']) self.assertEqual(DATA_JSON, jsonutils.loads(kwargs['data'])) @staticmethod @@ -242,8 +268,6 @@ class TestAlarmNotifier(tests_base.BaseTestCase): client = mock.MagicMock() client.session.auth.get_access.return_value.auth_token = 'token_1234' - headers = {'X-Auth-Token': 'token_1234'} - headers.update(self.HTTP_HEADERS) self.useFixture(mockpatch.Patch('keystoneclient.v3.client.Client', lambda **kwargs: client)) @@ -251,12 +275,18 @@ class TestAlarmNotifier(tests_base.BaseTestCase): with mock.patch.object(requests.Session, 'post') as poster: self.service.notify_alarm({}, self._notification(action)) - headers = {'X-Auth-Token': 'token_1234'} - headers.update(self.HTTP_HEADERS) poster.assert_called_with( url, data=mock.ANY, headers=mock.ANY) args, kwargs = poster.call_args - self.assertEqual(headers, kwargs['headers']) + self.assertEqual( + { + 'X-Auth-Token': 'token_1234', + 'x-openstack-request-id': + kwargs['headers']['x-openstack-request-id'], + 'content-type': 'application/json' + }, + kwargs['headers']) + self.assertEqual(DATA_JSON, jsonutils.loads(kwargs['data'])) def test_zaqar_notifier_action(self): diff --git a/requirements.txt b/requirements.txt index 233ab892a..e5bba1d70 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,6 @@ jsonschema!=2.5.0,<3.0.0,>=2.0.0 keystonemiddleware>=2.2.0 gnocchiclient>=2.1.0 # Apache-2.0 lxml>=2.3 -oslo.context>=0.2.0 # Apache-2.0 oslo.db>=1.12.0 # Apache-2.0 oslo.config>=2.6.0 # Apache-2.0 oslo.i18n>=1.5.0 # Apache-2.0