From 2abe46ae21fe0fa83587328ffb2f44075d3a44fc Mon Sep 17 00:00:00 2001 From: Xing Zhang Date: Sun, 27 Mar 2022 13:54:52 +0800 Subject: [PATCH] Update json module to jsonutils oslo project provide jsonutils, the others project use it this PS to update the json moudule to oslo jsonutils. Closes-bug: #1966401 Change-Id: I9cbe9f03c32e4f42822def727aa2871d502641f4 --- .../html/confirmation_web_service_sample.py | 4 +- samples/zaqar/sendmail.py | 4 +- samples/zaqar/subscriber_service_sample.py | 6 +- zaqar/bench/conductor.py | 4 +- zaqar/bench/producer.py | 4 +- zaqar/notification/tasks/mailto.py | 4 +- zaqar/notification/tasks/webhook.py | 7 +- zaqar/storage/swift/claims.py | 4 +- zaqar/storage/utils.py | 4 +- zaqar/tests/functional/http.py | 9 ++- .../tests/functional/websocket/test_queues.py | 18 ++--- .../tests/functional/wsgi/v1/test_messages.py | 5 +- .../functional/wsgi/v1_1/test_messages.py | 5 +- .../tests/unit/notification/test_notifier.py | 29 ++++---- zaqar/tests/unit/storage/base.py | 4 +- .../unit/transport/websocket/test_protocol.py | 8 +-- zaqar/tests/unit/transport/websocket/utils.py | 9 +-- .../unit/transport/websocket/v2/test_auth.py | 56 +++++++-------- .../transport/websocket/v2/test_claims.py | 64 ++++++++--------- .../transport/websocket/v2/test_messages.py | 72 +++++++++---------- .../websocket/v2/test_queue_lifecycle.py | 72 +++++++++---------- .../websocket/v2/test_subscriptions.py | 22 +++--- zaqar/tests/unit/transport/wsgi/test_utils.py | 12 ++-- .../unit/transport/wsgi/v1/test_validation.py | 6 +- .../unit/transport/wsgi/v1_1/test_claims.py | 11 ++- .../transport/wsgi/v1_1/test_validation.py | 7 +- .../unit/transport/wsgi/v2_0/test_claims.py | 11 ++- .../transport/wsgi/v2_0/test_validation.py | 7 +- zaqar/transport/utils.py | 6 +- zaqar/transport/websocket/factory.py | 5 +- zaqar/transport/websocket/protocol.py | 9 +-- zaqar/transport/wsgi/v1_0/homedoc.py | 4 +- zaqar/transport/wsgi/v1_1/homedoc.py | 4 +- zaqar/transport/wsgi/v2_0/homedoc.py | 4 +- 34 files changed, 246 insertions(+), 254 deletions(-) diff --git a/samples/html/confirmation_web_service_sample.py b/samples/html/confirmation_web_service_sample.py index c7bb09172..570164fd5 100644 --- a/samples/html/confirmation_web_service_sample.py +++ b/samples/html/confirmation_web_service_sample.py @@ -9,8 +9,8 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations under # the License. -import json import logging +from oslo_serialization import jsonutils from oslo_utils import uuidutils import requests import sys @@ -79,7 +79,7 @@ class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): 'URL-Expires': self.headers['url-expires'], } data = {'confirmed': confirmed_value} - requests.put(url=url, data=json.dumps(data), headers=headers) + requests.put(url=url, data=jsonutils.dumps(data), headers=headers) Handler = ServerHandler diff --git a/samples/zaqar/sendmail.py b/samples/zaqar/sendmail.py index ff172189e..3cbcba0cc 100644 --- a/samples/zaqar/sendmail.py +++ b/samples/zaqar/sendmail.py @@ -14,13 +14,13 @@ from email.mime.text import MIMEText from email.parser import Parser -import json import smtplib import sys from keystoneauth1 import loading from keystoneauth1 import session as ks_session from oslo_config import cfg +from oslo_serialization import jsonutils import requests import retrying @@ -178,7 +178,7 @@ def prepare_msg(msg_str): msg_subject = headers['subject'] if not headers['subject']: - alarm_info = json.loads(payload)['body'] + alarm_info = jsonutils.loads(payload)['body'] subject = msg_subject + alarm_info['alarm_name'] template = generate_subbody(mail_alarm_info, reason=alarm_info['reason'], diff --git a/samples/zaqar/subscriber_service_sample.py b/samples/zaqar/subscriber_service_sample.py index 9a19872c1..f6ae6bc5c 100644 --- a/samples/zaqar/subscriber_service_sample.py +++ b/samples/zaqar/subscriber_service_sample.py @@ -9,8 +9,8 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations under # the License. -import json import logging +from oslo_serialization import jsonutils from oslo_utils import uuidutils import requests import sys @@ -48,7 +48,7 @@ class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): logging.warning('=================== POST =====================') data_string = str( self.rfile.read(int(self.headers['Content-Length']))) - self.data = json.loads(data_string) + self.data = jsonutils.loads(data_string) if _AUTO_CONFIRM: self._send_confirm_request() message = 'OK' @@ -72,7 +72,7 @@ class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): 'URL-Expires': self.data['URL-Expires'], } data = {'confirmed': True} - requests.put(url=url, data=json.dumps(data), headers=headers) + requests.put(url=url, data=jsonutils.dumps(data), headers=headers) Handler = ServerHandler diff --git a/zaqar/bench/conductor.py b/zaqar/bench/conductor.py index a10dd956e..a516d5437 100644 --- a/zaqar/bench/conductor.py +++ b/zaqar/bench/conductor.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json import multiprocessing as mp import os +from oslo_serialization import jsonutils # NOTE(Eva-i): See https://github.com/gevent/gevent/issues/349. Let's keep # it until the new stable version of gevent(>=1.1) will be released. os.environ["GEVENT_RESOLVER"] = "ares" @@ -100,4 +100,4 @@ def main(): }, } - print(json.dumps(stats)) + print(jsonutils.dumps(stats)) diff --git a/zaqar/bench/producer.py b/zaqar/bench/producer.py index 9787faa97..b4f124af2 100644 --- a/zaqar/bench/producer.py +++ b/zaqar/bench/producer.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json import multiprocessing as mp import random import sys @@ -22,6 +21,7 @@ from gevent import monkey as curious_george curious_george.patch_all(thread=False, select=False) import gevent import marktime +from oslo_serialization import jsonutils from zaqarclient.transport import errors from zaqar.bench import config @@ -50,7 +50,7 @@ def load_messages(): messages_path = CONF.messages_path or CONF.find_file(default_file_name) if messages_path: with open(messages_path) as f: - message_pool = json.load(f) + message_pool = jsonutils.load(f) message_pool.sort(key=lambda msg: msg['weight']) return message_pool else: diff --git a/zaqar/notification/tasks/mailto.py b/zaqar/notification/tasks/mailto.py index 46fdc5f40..5b9caed7a 100644 --- a/zaqar/notification/tasks/mailto.py +++ b/zaqar/notification/tasks/mailto.py @@ -14,12 +14,12 @@ # limitations under the License. from email.mime import text -import json import smtplib import subprocess from urllib import parse as urllib_parse from oslo_log import log as logging +from oslo_serialization import jsonutils from zaqar.i18n import _ from zaqar.notification.notifier import MessageType @@ -92,7 +92,7 @@ class MailtoTask(object): # to our original messages(dicts) which will be later # consumed in the storage controller. It seems safe though. message['queue_name'] = subscription['source'] - msg = text.MIMEText(json.dumps(message)) + msg = text.MIMEText(jsonutils.dumps(message)) msg["to"] = subscriber.path msg["from"] = subscription['options'].get('from', '') subject_opt = subscription['options'].get('subject', '') diff --git a/zaqar/notification/tasks/webhook.py b/zaqar/notification/tasks/webhook.py index a601324d7..9adc25a54 100644 --- a/zaqar/notification/tasks/webhook.py +++ b/zaqar/notification/tasks/webhook.py @@ -16,8 +16,8 @@ import math import time -import json from oslo_log import log as logging +from oslo_serialization import jsonutils import requests from zaqar.common import consts @@ -129,9 +129,10 @@ class WebhookTask(object): msg['queue_name'] = subscription['source'] if 'post_data' in subscription['options']: data = subscription['options']['post_data'] - data = data.replace('"$zaqar_message$"', json.dumps(msg)) + data = data.replace('"$zaqar_message$"', + jsonutils.dumps(msg)) else: - data = json.dumps(msg) + data = jsonutils.dumps(msg) response = requests.post(subscription['subscriber'], data=data, headers=headers) diff --git a/zaqar/storage/swift/claims.py b/zaqar/storage/swift/claims.py index efa594e95..f9456beb0 100644 --- a/zaqar/storage/swift/claims.py +++ b/zaqar/storage/swift/claims.py @@ -118,10 +118,10 @@ class ClaimController(storage.Claim): claim_count = msg.get('claim_count', 0) md5 = hashlib.md5() md5.update( - jsonutils.dumps( + jsonutils.dump_as_bytes( {'body': msg['body'], 'claim_id': None, 'ttl': msg['ttl'], - 'claim_count': claim_count}).encode('utf-8')) + 'claim_count': claim_count})) md5 = md5.hexdigest() msg_ttl = max(msg['ttl'], msg_ts) move_to_dlq = False diff --git a/zaqar/storage/utils.py b/zaqar/storage/utils.py index 769e3f017..ec2fbb877 100644 --- a/zaqar/storage/utils.py +++ b/zaqar/storage/utils.py @@ -14,10 +14,10 @@ import copy import hashlib -import json from oslo_config import cfg from oslo_log import log +from oslo_serialization import jsonutils from osprofiler import profiler from stevedore import driver from urllib import parse as urllib_parse @@ -229,7 +229,7 @@ def get_checksum(body, algorithm='MD5'): if body is None: return '' else: - checksum_body = json.dumps(body).encode('utf-8') + checksum_body = jsonutils.dump_as_bytes(body) # TODO(yangzhenyu): We may support other algorithms in future # versions, including SHA1, SHA256, SHA512, and so on. if algorithm == 'MD5': diff --git a/zaqar/tests/functional/http.py b/zaqar/tests/functional/http.py index f1af249b0..9a38f63c9 100644 --- a/zaqar/tests/functional/http.py +++ b/zaqar/tests/functional/http.py @@ -14,7 +14,6 @@ # limitations under the License. import functools -import json from falcon import testing as ftest from oslo_serialization import jsonutils @@ -66,7 +65,7 @@ class Client(object): """Does http POST.""" if "data" in kwargs: - kwargs['data'] = json.dumps(kwargs["data"]) + kwargs['data'] = jsonutils.dumps(kwargs["data"]) return self.session.post(url, **kwargs) @@ -75,7 +74,7 @@ class Client(object): """Does http PUT.""" if "data" in kwargs: - kwargs['data'] = json.dumps(kwargs["data"]) + kwargs['data'] = jsonutils.dumps(kwargs["data"]) return self.session.put(url, **kwargs) @@ -88,7 +87,7 @@ class Client(object): def patch(self, url=None, **kwargs): """Does http PATCH.""" if "data" in kwargs: - kwargs['data'] = json.dumps(kwargs["data"]) + kwargs['data'] = jsonutils.dumps(kwargs["data"]) return self.session.patch(url, **kwargs) @@ -149,7 +148,7 @@ class WSGIClient(object): elif isinstance(data, str): body = data else: - body = json.dumps(data, ensure_ascii=False) + body = jsonutils.dumps(data, ensure_ascii=False) parsed_url = urllib_parse.urlparse(url) diff --git a/zaqar/tests/functional/websocket/test_queues.py b/zaqar/tests/functional/websocket/test_queues.py index 5bb17c2e9..c9a9d32c7 100644 --- a/zaqar/tests/functional/websocket/test_queues.py +++ b/zaqar/tests/functional/websocket/test_queues.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils from oslo_utils import uuidutils from testtools import testcase @@ -39,8 +39,8 @@ class TestQueues(base.V1_1FunctionalTestBase): def test_list_empty(self): self.client.send( - json.dumps({'action': 'queue_list', 'headers': self.headers})) - response = json.loads(self.client.recv()) + jsonutils.dumps({'action': 'queue_list', 'headers': self.headers})) + response = jsonutils.loads(self.client.recv()) self.assertEqual( {'body': {'queues': []}, 'headers': {'status': 200}, @@ -50,10 +50,10 @@ class TestQueues(base.V1_1FunctionalTestBase): def test_list(self): self.client.send( - json.dumps({'action': 'queue_create', - 'body': {'queue_name': 'my_queue'}, - 'headers': self.headers})) - response = json.loads(self.client.recv()) + jsonutils.dumps({'action': 'queue_create', + 'body': {'queue_name': 'my_queue'}, + 'headers': self.headers})) + response = jsonutils.loads(self.client.recv()) self.assertEqual( {'body': 'Queue my_queue created.', 'headers': {'status': 201}, @@ -62,8 +62,8 @@ class TestQueues(base.V1_1FunctionalTestBase): 'headers': self.headers}}, response) self.client.send( - json.dumps({'action': 'queue_list', 'headers': self.headers})) - response = json.loads(self.client.recv()) + jsonutils.dumps({'action': 'queue_list', 'headers': self.headers})) + response = jsonutils.loads(self.client.recv()) self.assertEqual( {'body': {'queues': [{'name': 'my_queue'}]}, 'headers': {'status': 200}, diff --git a/zaqar/tests/functional/wsgi/v1/test_messages.py b/zaqar/tests/functional/wsgi/v1/test_messages.py index f10054046..29790b6a9 100644 --- a/zaqar/tests/functional/wsgi/v1/test_messages.py +++ b/zaqar/tests/functional/wsgi/v1/test_messages.py @@ -13,11 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json import uuid import ddt +from oslo_serialization import jsonutils + from zaqar.tests.functional import base from zaqar.tests.functional import helpers @@ -54,7 +55,7 @@ class TestMessages(base.V1FunctionalTestBase): message2 = {"body": '', "ttl": 120} doc = [message1, message2] - overhead = len(json.dumps(doc)) + overhead = len(jsonutils.dumps(doc)) half_size = (self.limits.max_messages_post_size - overhead) // 2 message1['body'] = helpers.generate_random_string(half_size) diff --git a/zaqar/tests/functional/wsgi/v1_1/test_messages.py b/zaqar/tests/functional/wsgi/v1_1/test_messages.py index ebfcd8630..3f5521f12 100644 --- a/zaqar/tests/functional/wsgi/v1_1/test_messages.py +++ b/zaqar/tests/functional/wsgi/v1_1/test_messages.py @@ -13,11 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json import uuid import ddt +from oslo_serialization import jsonutils + from zaqar.common import consts from zaqar.tests.functional import base from zaqar.tests.functional import helpers @@ -56,7 +57,7 @@ class TestMessages(base.V1_1FunctionalTestBase): message2 = {"body": '', "ttl": 120} doc = {'messages': [message1, message2]} - overhead = len(json.dumps(doc)) + overhead = len(jsonutils.dumps(doc)) half_size = (self.limits.max_messages_post_size - overhead) // 2 message1['body'] = helpers.generate_random_string(half_size) diff --git a/zaqar/tests/unit/notification/test_notifier.py b/zaqar/tests/unit/notification/test_notifier.py index bb66e718c..9d7f22346 100644 --- a/zaqar/tests/unit/notification/test_notifier.py +++ b/zaqar/tests/unit/notification/test_notifier.py @@ -13,12 +13,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json from unittest import mock import uuid import ddt +from oslo_serialization import jsonutils + from zaqar.common import urls from zaqar.notification import notifier from zaqar.notification.tasks import webhook @@ -86,7 +87,7 @@ class NotifierTest(testing.TestBase): # often fail, because dict keys can be serialized in different # order inside the string. for call in mock_post.call_args_list: - call[1]['data'] = json.loads(call[1]['data']) + call[1]['data'] = jsonutils.loads(call[1]['data']) # These are not real calls. In real calls each "data" argument is # serialized by json.dumps. But we made a substitution before, # so it will work. @@ -116,7 +117,7 @@ class NotifierTest(testing.TestBase): post_data = {'foo': 'bar', 'egg': '$zaqar_message$'} subscription = [{'subscriber': 'http://trigger_me', 'source': 'fake_queue', - 'options': {'post_data': json.dumps(post_data)}}] + 'options': {'post_data': jsonutils.dumps(post_data)}}] ctlr = mock.MagicMock() ctlr.list = mock.Mock(return_value=iter([subscription, {}])) queue_ctlr = mock.MagicMock() @@ -134,7 +135,7 @@ class NotifierTest(testing.TestBase): # often fail, because dict keys can be serialized in different # order inside the string. for call in mock_post.call_args_list: - call[1]['data'] = json.loads(call[1]['data']) + call[1]['data'] = jsonutils.loads(call[1]['data']) # These are not real calls. In real calls each "data" argument is # serialized by json.dumps. But we made a substitution before, # so it will work. @@ -179,7 +180,7 @@ class NotifierTest(testing.TestBase): # often fail, because dict keys can be serialized in different # order inside the string. for call in mock_post.call_args_list: - call[1]['data'] = json.loads(call[1]['data']) + call[1]['data'] = jsonutils.loads(call[1]['data']) # These are not real calls. In real calls each "data" argument is # serialized by json.dumps. But we made a substitution before, # so it will work. @@ -216,16 +217,16 @@ class NotifierTest(testing.TestBase): ' %(to)s\nfrom: %(from)s\nsubject: %(subject)s\n\n%(body)s') mail1 = msg % {'to': subscription[0]['subscriber'][7:], 'from': 'zaqar@example.com', 'subject': 'Hello', - 'body': json.dumps(self.notifications[0])} + 'body': jsonutils.dumps(self.notifications[0])} mail2 = msg % {'to': subscription[0]['subscriber'][7:], 'from': 'zaqar@example.com', 'subject': 'Hello', - 'body': json.dumps(self.notifications[1])} + 'body': jsonutils.dumps(self.notifications[1])} mail3 = msg % {'to': subscription[1]['subscriber'][7:], 'from': 'zaqar@example.com', 'subject': 'Hello', - 'body': json.dumps(self.notifications[0])} + 'body': jsonutils.dumps(self.notifications[0])} mail4 = msg % {'to': subscription[1]['subscriber'][7:], 'from': 'zaqar@example.com', 'subject': 'Hello', - 'body': json.dumps(self.notifications[1])} + 'body': jsonutils.dumps(self.notifications[1])} def _communicate(msg): called.add(msg) @@ -246,13 +247,15 @@ class NotifierTest(testing.TestBase): for mail in mails: options, body = mail.split('\n\n') mail_options.append(options) - mail_bodies.append(json.dumps(json.loads(body), sort_keys=True)) + mail_bodies.append(jsonutils.dumps(jsonutils.loads(body), + sort_keys=True)) called_options = [] called_bodies = [] for call in called: options, body = call.split('\n\n') called_options.append(options) - called_bodies.append(json.dumps(json.loads(body), sort_keys=True)) + called_bodies.append(jsonutils.dumps(jsonutils.loads(body), + sort_keys=True)) self.assertEqual(sorted(mail_options), sorted(called_options)) self.assertEqual(sorted(mail_bodies), sorted(called_bodies)) @@ -286,7 +289,7 @@ class NotifierTest(testing.TestBase): driver.executor.shutdown() self.assertEqual(2, mock_post.call_count) self.assertEqual(self.notifications[1], - json.loads(mock_post.call_args[1]['data'])) + jsonutils.loads(mock_post.call_args[1]['data'])) @mock.patch('requests.post') def test_send_confirm_notification(self, mock_request): @@ -310,7 +313,7 @@ class NotifierTest(testing.TestBase): 'X-Project-ID', 'URL-Signature', 'URL-Paths', 'Message', 'URL-Expires', 'Message_Type', 'WSGISubscribeURL', 'WebSocketSubscribeURL' 'UnsubscribeBody'] - actual_args = json.loads(mock_request.call_args[1]['data']).keys() + actual_args = jsonutils.loads(mock_request.call_args[1]['data']).keys() self.assertEqual(expect_args.sort(), list(actual_args).sort()) diff --git a/zaqar/tests/unit/storage/base.py b/zaqar/tests/unit/storage/base.py index 694d33dc7..f7b9bf8d3 100644 --- a/zaqar/tests/unit/storage/base.py +++ b/zaqar/tests/unit/storage/base.py @@ -17,7 +17,6 @@ from collections import abc import datetime import hashlib -import json import math import os import random @@ -26,6 +25,7 @@ from unittest import mock import uuid import ddt +from oslo_serialization import jsonutils from oslo_utils import timeutils import testtools from testtools import matchers @@ -488,7 +488,7 @@ class MessageControllerTest(ControllerBaseTest): expected_checksum = '' if algorithm == 'MD5': md5 = hashlib.md5() - md5.update(json.dumps(message['body']).encode('utf-8')) + md5.update(jsonutils.dump_as_bytes(message['body'])) expected_checksum = md5.hexdigest() self.assertEqual(expected_checksum, checksum) diff --git a/zaqar/tests/unit/transport/websocket/test_protocol.py b/zaqar/tests/unit/transport/websocket/test_protocol.py index a30edbf96..2a4b09e19 100644 --- a/zaqar/tests/unit/transport/websocket/test_protocol.py +++ b/zaqar/tests/unit/transport/websocket/test_protocol.py @@ -13,10 +13,10 @@ # License for the specific language governing permissions and limitations # under the License. -import json from unittest import mock import ddt +from oslo_serialization import jsonutils from oslo_utils import uuidutils import zaqar @@ -43,13 +43,13 @@ class TestMessagingProtocol(base.TestBase): self.protocol.sendMessage = send_mock self.protocol.onMessage(payload, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) payload = "123" self.protocol.onMessage(payload, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) def test_on_message_with_invalid_input_binary(self): @@ -92,8 +92,6 @@ class TestMessagingProtocol(base.TestBase): self.protocol.sendMessage = send_mock self.protocol.onMessage(req, in_binary) arg = send_mock.call_args[0][0] - if not in_binary: - arg = arg.decode() resp = loads(arg) self.assertEqual(200, resp['headers']['status']) diff --git a/zaqar/tests/unit/transport/websocket/utils.py b/zaqar/tests/unit/transport/websocket/utils.py index 589a676ca..e2443e7d0 100644 --- a/zaqar/tests/unit/transport/websocket/utils.py +++ b/zaqar/tests/unit/transport/websocket/utils.py @@ -13,12 +13,13 @@ # the License. import functools -import json import msgpack +from oslo_serialization import jsonutils def create_request(action, body, headers): - return json.dumps({"action": action, "body": body, "headers": headers}) + return jsonutils.dumps({"action": action, "body": body, + "headers": headers}) def create_binary_request(action, body, headers): @@ -42,7 +43,7 @@ def get_pack_tools(binary=None): loads = functools.partial(msgpack.unpackb) create_request_function = create_binary_request else: - dumps = json.dumps - loads = json.loads + dumps = jsonutils.dumps + loads = jsonutils.loads create_request_function = create_request return dumps, loads, create_request_function diff --git a/zaqar/tests/unit/transport/websocket/v2/test_auth.py b/zaqar/tests/unit/transport/websocket/v2/test_auth.py index bcc31fd8d..beaeb0080 100644 --- a/zaqar/tests/unit/transport/websocket/v2/test_auth.py +++ b/zaqar/tests/unit/transport/websocket/v2/test_auth.py @@ -13,11 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json from unittest import mock import ddt from keystonemiddleware import auth_token +from oslo_serialization import jsonutils from oslo_utils import uuidutils from zaqar.common import consts @@ -51,7 +51,7 @@ class AuthTest(base.V2Base): def test_post(self): headers = self.headers.copy() headers['X-Auth-Token'] = 'mytoken1' - req = json.dumps({'action': 'authenticate', 'headers': headers}) + req = jsonutils.dumps({'action': 'authenticate', 'headers': headers}) msg_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(msg_mock.stop) @@ -69,9 +69,9 @@ class AuthTest(base.V2Base): self.assertEqual('200 OK', responses[0]) # Check that the env is available to future requests - req = json.dumps({'action': consts.MESSAGE_LIST, - 'body': {'queue_name': 'myqueue'}, - 'headers': self.headers}) + req = jsonutils.dumps({'action': consts.MESSAGE_LIST, + 'body': {'queue_name': 'myqueue'}, + 'headers': self.headers}) process_request = mock.patch.object(self.protocol._handler, 'process_request').start() process_request.return_value = self.protocol._handler.create_response( @@ -83,7 +83,7 @@ class AuthTest(base.V2Base): def test_post_between_auth(self): headers = self.headers.copy() headers['X-Auth-Token'] = 'mytoken1' - req = json.dumps({'action': 'authenticate', 'headers': headers}) + req = jsonutils.dumps({'action': 'authenticate', 'headers': headers}) msg_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(msg_mock.stop) @@ -94,7 +94,7 @@ class AuthTest(base.V2Base): self.protocol.onMessage(req, False) self.assertEqual(1, msg_mock.call_count) - resp = json.loads(msg_mock.call_args[0][0].decode()) + resp = jsonutils.loads(msg_mock.call_args[0][0]) self.assertEqual(403, resp['headers']['status']) def test_failed_auth(self): @@ -104,14 +104,14 @@ class AuthTest(base.V2Base): self.protocol._auth_in_binary = False self.protocol._auth_response('401 error', 'Failed') self.assertEqual(1, msg_mock.call_count) - resp = json.loads(msg_mock.call_args[0][0].decode()) + resp = jsonutils.loads(msg_mock.call_args[0][0]) self.assertEqual(401, resp['headers']['status']) self.assertEqual('authenticate', resp['request']['action']) def test_reauth(self): headers = self.headers.copy() headers['X-Auth-Token'] = 'mytoken1' - req = json.dumps({'action': 'authenticate', 'headers': headers}) + req = jsonutils.dumps({'action': 'authenticate', 'headers': headers}) msg_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(msg_mock.stop) @@ -128,7 +128,7 @@ class AuthTest(base.V2Base): headers = self.headers.copy() headers['X-Auth-Token'] = 'mytoken2' - req = json.dumps({'action': 'authenticate', 'headers': headers}) + req = jsonutils.dumps({'action': 'authenticate', 'headers': headers}) self.protocol.onMessage(req, False) self.protocol._auth_start(self.env, lambda x, y: responses.append(x)) @@ -140,7 +140,7 @@ class AuthTest(base.V2Base): def test_reauth_after_auth_failure(self): headers = self.headers.copy() headers['X-Auth-Token'] = 'wrong_token' - req = json.dumps({'action': 'authenticate', 'headers': headers}) + req = jsonutils.dumps({'action': 'authenticate', 'headers': headers}) msg_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(msg_mock.stop) @@ -149,7 +149,7 @@ class AuthTest(base.V2Base): # request will raise 401 error. self.protocol.onMessage(req, False) self.protocol._auth_response('401 error', 'Failed') - resp = json.loads(msg_mock.call_args[0][0].decode()) + resp = jsonutils.loads(msg_mock.call_args[0][0]) self.assertEqual(401, resp['headers']['status']) self.assertEqual('authenticate', resp['request']['action']) @@ -158,11 +158,11 @@ class AuthTest(base.V2Base): # try to authenticate again, "onMessage" should not return 403 because # that the _auth_app was cleaned after auth failure. headers['X-Auth-Token'] = 'mytoken' - req = json.dumps({'action': 'authenticate', 'headers': headers}) + req = jsonutils.dumps({'action': 'authenticate', 'headers': headers}) self.protocol.onMessage(req, False) self.protocol._auth_response('200 OK', 'authenticate success') - resp = json.loads(msg_mock.call_args[0][0].decode()) + resp = jsonutils.loads(msg_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) @ddt.data(True, False) @@ -182,8 +182,6 @@ class AuthTest(base.V2Base): self.protocol._auth_response('401 error', 'Failed') self.assertEqual(1, msg_mock.call_count) arg = msg_mock.call_args[0][0] - if not in_binary: - arg = arg.decode() resp = loads(arg) self.assertEqual(401, resp['headers']['status']) @@ -202,13 +200,13 @@ class AuthTest(base.V2Base): 'URL-Methods': ['GET'], 'URL-Paths': ['/v2/queues/myqueue/messages'] }) - req = json.dumps({'action': consts.MESSAGE_LIST, - 'body': {'queue_name': 'myqueue'}, - 'headers': headers}) + req = jsonutils.dumps({'action': consts.MESSAGE_LIST, + 'body': {'queue_name': 'myqueue'}, + 'headers': headers}) self.protocol.onMessage(req, False) self.assertEqual(1, send_mock.call_count) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) def test_signed_url_wrong_queue(self): @@ -226,13 +224,13 @@ class AuthTest(base.V2Base): 'URL-Methods': ['GET'], 'URL-Paths': ['/v2/queues/otherqueue/messages'] }) - req = json.dumps({'action': consts.MESSAGE_LIST, - 'body': {'queue_name': 'otherqueue'}, - 'headers': headers}) + req = jsonutils.dumps({'action': consts.MESSAGE_LIST, + 'body': {'queue_name': 'otherqueue'}, + 'headers': headers}) self.protocol.onMessage(req, False) self.assertEqual(1, send_mock.call_count) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(403, resp['headers']['status']) def test_signed_url_wrong_method(self): @@ -250,12 +248,12 @@ class AuthTest(base.V2Base): 'URL-Methods': ['GET'], 'URL-Paths': ['/v2/queues/myqueue/messages'] }) - req = json.dumps({'action': consts.MESSAGE_DELETE, - 'body': {'queue_name': 'myqueue', - 'message_id': '123'}, - 'headers': headers}) + req = jsonutils.dumps({'action': consts.MESSAGE_DELETE, + 'body': {'queue_name': 'myqueue', + 'message_id': '123'}, + 'headers': headers}) self.protocol.onMessage(req, False) self.assertEqual(1, send_mock.call_count) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(403, resp['headers']['status']) diff --git a/zaqar/tests/unit/transport/websocket/v2/test_claims.py b/zaqar/tests/unit/transport/websocket/v2/test_claims.py index d2473286f..6faa3d1a1 100644 --- a/zaqar/tests/unit/transport/websocket/v2/test_claims.py +++ b/zaqar/tests/unit/transport/websocket/v2/test_claims.py @@ -12,10 +12,10 @@ # License for the specific language governing permissions and limitations under # the License. -import json from unittest import mock import ddt +from oslo_serialization import jsonutils from oslo_utils import timeutils from oslo_utils import uuidutils @@ -46,7 +46,7 @@ class ClaimsBaseTest(base.V1_1Base): with mock.patch.object(self.protocol, 'sendMessage') as msg_mock: self.protocol.onMessage(req, False) - resp = json.loads(msg_mock.call_args[0][0].decode()) + resp = jsonutils.loads(msg_mock.call_args[0][0]) self.assertIn(resp['headers']['status'], [201, 204]) action = consts.MESSAGE_POST @@ -70,7 +70,7 @@ class ClaimsBaseTest(base.V1_1Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(201, resp['headers']['status']) def tearDown(self): @@ -84,7 +84,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) @ddt.data('[', '[]', '.', '"fail"') @@ -97,7 +97,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) action = consts.CLAIM_UPDATE @@ -105,7 +105,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) def test_exceeded_claim(self): @@ -120,7 +120,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) @ddt.data((-1, -1), (59, 60), (60, 59), (60, 43201), (43201, 60)) @@ -136,7 +136,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) @ddt.data(-1, 59, 43201) @@ -153,7 +153,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) def test_default_ttl_and_grace(self): @@ -165,7 +165,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(201, resp['headers']['status']) action = consts.CLAIM_GET @@ -174,7 +174,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) self.assertEqual(self.defaults.claim_ttl, resp['body']['ttl']) @@ -191,7 +191,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(201, resp['headers']['status']) claimed_messages = resp['body']['messages'] claim_id = resp['body']['claim_id'] @@ -203,7 +203,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) # Listing messages, by default, won't include claimed, will echo @@ -213,7 +213,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) self.assertEqual([], resp['body']['messages']) @@ -224,7 +224,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) self.assertEqual([], resp['body']['messages']) @@ -236,7 +236,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) self.assertEqual(resp['body']['messages'], []) @@ -253,7 +253,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) # Include claimed messages this time, and echo @@ -264,7 +264,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) self.assertEqual(len(claimed_messages), len(resp['body']['messages'])) @@ -278,7 +278,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(403, resp['headers']['status']) # Delete the message and its associated claim @@ -288,7 +288,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) # Try to get it from the wrong project @@ -302,7 +302,7 @@ class ClaimsBaseTest(base.V1_1Base): "message_id": message_id_2} req = test_utils.create_request(action, body, headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(404, resp['headers']['status']) # Get the message @@ -311,7 +311,7 @@ class ClaimsBaseTest(base.V1_1Base): "message_id": message_id_2} req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) # Update the claim @@ -323,7 +323,7 @@ class ClaimsBaseTest(base.V1_1Base): "claim_id": claim_id} req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) # Get the claimed messages (again) @@ -333,7 +333,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) query = timeutils.utcnow() - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) self.assertEqual(60, resp['body']['ttl']) @@ -352,7 +352,7 @@ class ClaimsBaseTest(base.V1_1Base): "claim_id": claim_id} req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) # Try to delete a message with an invalid claim ID @@ -363,7 +363,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) # Make sure it wasn't deleted! @@ -372,7 +372,7 @@ class ClaimsBaseTest(base.V1_1Base): "message_id": message_id_2} req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) # Try to get a claim that doesn't exist @@ -381,7 +381,7 @@ class ClaimsBaseTest(base.V1_1Base): "claim_id": claim_id} req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(404, resp['headers']['status']) # Try to update a claim that doesn't exist @@ -392,7 +392,7 @@ class ClaimsBaseTest(base.V1_1Base): "claim_id": claim_id} req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(404, resp['headers']['status']) def test_post_claim_nonexistent_queue(self): @@ -406,7 +406,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) def test_get_claim_nonexistent_queue(self): @@ -419,7 +419,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(404, resp['headers']['status']) def _get_a_claim(self): @@ -433,7 +433,7 @@ class ClaimsBaseTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(201, resp['headers']['status']) return resp diff --git a/zaqar/tests/unit/transport/websocket/v2/test_messages.py b/zaqar/tests/unit/transport/websocket/v2/test_messages.py index 8a9a5b2fb..3bd73f17e 100644 --- a/zaqar/tests/unit/transport/websocket/v2/test_messages.py +++ b/zaqar/tests/unit/transport/websocket/v2/test_messages.py @@ -14,10 +14,10 @@ # limitations under the License. import datetime -import json from unittest import mock import ddt +from oslo_serialization import jsonutils from oslo_utils import timeutils from oslo_utils import uuidutils from testtools import matchers @@ -51,7 +51,7 @@ class MessagesBaseTest(base.V2Base): with mock.patch.object(self.protocol, 'sendMessage') as msg_mock: self.protocol.onMessage(req, False) - resp = json.loads(msg_mock.call_args[0][0].decode()) + resp = jsonutils.loads(msg_mock.call_args[0][0]) self.assertIn(resp['headers']['status'], [201, 204]) def tearDown(self): @@ -66,7 +66,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) def _test_post(self, sample_messages, in_binary=False): @@ -82,8 +82,6 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, in_binary) arg = send_mock.call_args[0][0] - if not in_binary: - arg = arg.decode() resp = loads(arg) self.assertEqual(201, resp['headers']['status']) self.msg_ids = resp['body']['message_ids'] @@ -109,8 +107,6 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, in_binary) arg = send_mock.call_args[0][0] - if not in_binary: - arg = arg.decode() resp = loads(arg) self.assertEqual(404, resp['headers']['status']) @@ -119,8 +115,6 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, in_binary) arg = send_mock.call_args[0][0] - if not in_binary: - arg = arg.decode() resp = loads(arg) self.assertEqual(200, resp['headers']['status']) @@ -143,8 +137,6 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, in_binary) arg = send_mock.call_args[0][0] - if not in_binary: - arg = arg.decode() resp = loads(arg) self.assertEqual(200, resp['headers']['status']) expected_ttls = set(m['ttl'] for m in sample_messages) @@ -170,14 +162,14 @@ class MessagesBaseTest(base.V2Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) # Listing restriction body['limit'] = 21 req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) # Bulk deletion restriction @@ -187,7 +179,7 @@ class MessagesBaseTest(base.V2Base): "message_ids": del_msg_ids} req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) @ddt.data(True, False) @@ -222,7 +214,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(201, resp['headers']['status']) msg_id = resp['body']['message_ids'][0] @@ -233,7 +225,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) self.assertEqual(self.default_message_ttl, resp['body']['messages']['ttl']) @@ -278,7 +270,7 @@ class MessagesBaseTest(base.V2Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) self.assertEqual( 'Bad request. The value of the "ttl" field must be a int.', @@ -299,7 +291,7 @@ class MessagesBaseTest(base.V2Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) self.assertEqual( 'Bad request. Missing "body" field.', resp['body']['exception']) @@ -314,7 +306,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) self.assertEqual([], resp['body']['messages']) @@ -338,7 +330,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) action = consts.MESSAGE_GET @@ -351,7 +343,7 @@ class MessagesBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) @ddt.data(None, '[', '[]', '{}', '.') @@ -369,7 +361,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) @ddt.data(-1, 59, 1209601) @@ -385,7 +377,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) def test_exceeded_message_posting(self): @@ -404,7 +396,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) @ddt.data('{"overflow": 9223372036854775808}', @@ -423,7 +415,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) def test_delete(self): @@ -441,7 +433,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) # Delete queue @@ -450,7 +442,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) # Get non existent queue @@ -458,7 +450,7 @@ class MessagesBaseTest(base.V2Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(404, resp['headers']['status']) # Safe to delete non-existing ones @@ -466,7 +458,7 @@ class MessagesBaseTest(base.V2Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) def test_bulk_delete(self): @@ -484,7 +476,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) action = consts.MESSAGE_GET @@ -492,7 +484,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) # Safe to delete non-existing ones @@ -501,7 +493,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) # Even after the queue is gone @@ -510,7 +502,7 @@ class MessagesBaseTest(base.V2Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) action = consts.MESSAGE_DELETE_MANY @@ -519,7 +511,7 @@ class MessagesBaseTest(base.V2Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) def test_pop_delete(self): @@ -535,7 +527,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) self.assertEqual(2, len(resp['body']['messages'])) self.assertEqual(239, resp['body']['messages'][0]['body']) @@ -552,7 +544,7 @@ class MessagesBaseTest(base.V2Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(404, resp['headers']['status']) def test_get_multiple_invalid_messages_404s(self): @@ -566,7 +558,7 @@ class MessagesBaseTest(base.V2Base): req = test_utils.create_request(action, body, self.headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) def test_delete_multiple_invalid_messages_204s(self): @@ -581,7 +573,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) def _post_messages(self, queue_name, repeat=1): @@ -598,7 +590,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage(req, False) - return json.loads(send_mock.call_args[0][0].decode()) + return jsonutils.loads(send_mock.call_args[0][0]) def test_invalid_request(self): send_mock = mock.Mock() @@ -606,7 +598,7 @@ class MessagesBaseTest(base.V2Base): self.protocol.onMessage('foo', False) self.assertEqual(1, send_mock.call_count) - response = json.loads(send_mock.call_args[0][0].decode()) + response = jsonutils.loads(send_mock.call_args[0][0]) self.assertIn('error', response['body']) self.assertEqual({'status': 400}, response['headers']) self.assertEqual( diff --git a/zaqar/tests/unit/transport/websocket/v2/test_queue_lifecycle.py b/zaqar/tests/unit/transport/websocket/v2/test_queue_lifecycle.py index c5c1e4bc4..8b676ec4d 100644 --- a/zaqar/tests/unit/transport/websocket/v2/test_queue_lifecycle.py +++ b/zaqar/tests/unit/transport/websocket/v2/test_queue_lifecycle.py @@ -12,11 +12,11 @@ # License for the specific language governing permissions and limitations under # the License. -import json from unittest import mock import ddt +from oslo_serialization import jsonutils from oslo_utils import uuidutils from zaqar.common import consts from zaqar.storage import errors as storage_errors @@ -47,7 +47,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(400, resp['headers']['status']) with mock.patch.object(self.protocol, 'sendMessage') as msg_mock: @@ -71,7 +71,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(404, resp['headers']['status']) sender.side_effect = validator @@ -90,7 +90,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(201, resp['headers']['status']) sender.side_effect = validator @@ -107,7 +107,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(200, resp['headers']['status']) self.assertEqual(meta, resp['body']) @@ -120,7 +120,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(200, resp['headers']['status']) sender.side_effect = validator @@ -132,7 +132,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(204, resp['headers']['status']) sender.side_effect = validator @@ -144,7 +144,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(404, resp['headers']['status']) sender.side_effect = validator @@ -172,7 +172,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertIn(resp['headers']['status'], [201, 204]) sender.side_effect = validator @@ -182,7 +182,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(400, resp['headers']['status']) sender.side_effect = validator @@ -207,7 +207,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(400, resp['headers']['status']) sender.side_effect = validator @@ -217,7 +217,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertIn(resp['headers']['status'], [201, 204]) sender.side_effect = validator @@ -241,7 +241,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(400, resp['headers']['status']) sender.side_effect = validator @@ -267,14 +267,14 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertIn(resp['headers']['status'], [201, 204]) sender.side_effect = validator self.protocol.onMessage(req, False) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(204, resp['headers']['status']) sender.side_effect = validator @@ -297,7 +297,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(400, resp['headers']['status']) sender.side_effect = validator @@ -324,7 +324,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(400, resp['headers']['status']) sender.side_effect = validator @@ -351,7 +351,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(400, resp['headers']['status']) sender.side_effect = validator @@ -374,7 +374,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(201, resp['headers']['status']) sender.side_effect = validator @@ -387,7 +387,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(204, resp['headers']['status']) sender.side_effect = validator @@ -400,7 +400,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(204, resp['headers']['status']) self.assertEqual(meta1, resp['body']) @@ -415,7 +415,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(204, resp['headers']['status']) sender.side_effect = validator @@ -428,7 +428,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(200, resp['headers']['status']) self.assertEqual(meta2, resp['body']) @@ -461,7 +461,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(200, resp['headers']['status']) self.assertEqual([], resp['body']['queues']) @@ -473,7 +473,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(400, resp['headers']['status']) sender.side_effect = validator @@ -491,7 +491,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, altheaders) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(201, resp['headers']['status']) sender.side_effect = validator @@ -508,7 +508,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(2, len(resp['body']['queues'])) sender.side_effect = validator @@ -519,7 +519,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(200, resp['headers']['status']) # Ensure we didn't pick up the queue from the alt project. self.assertEqual(3, len(resp['body']['queues'])) @@ -532,7 +532,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(200, resp['headers']['status']) sender.side_effect = validator @@ -543,7 +543,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(200, resp['headers']['status']) self.assertEqual({"node": 31}, resp['body']) @@ -556,7 +556,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(200, resp['headers']['status']) sender.side_effect = validator @@ -582,7 +582,7 @@ class QueueLifecycleBaseTest(base.V2Base): req = test_utils.create_request(action, body, headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(503, resp['headers']['status']) sender.side_effect = validator @@ -616,7 +616,7 @@ class QueueLifecycleBaseTest(base.V2Base): self.protocol.onMessage(req, False) - return json.loads(send_mock.call_args[0][0].decode()) + return jsonutils.loads(send_mock.call_args[0][0]) def test_purge(self): arbitrary_number = 644079696574693 @@ -637,14 +637,14 @@ class QueueLifecycleBaseTest(base.V2Base): body = {"queue_name": queue_name, "message_id": msg_id} req = test_utils.create_request(action, body, headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status']) action = consts.QUEUE_PURGE body = {"queue_name": queue_name, "resource_types": ["messages"]} req = test_utils.create_request(action, body, headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(204, resp['headers']['status']) for msg_id in msg_ids: @@ -652,7 +652,7 @@ class QueueLifecycleBaseTest(base.V2Base): body = {"queue_name": queue_name, "message_id": msg_id} req = test_utils.create_request(action, body, headers) self.protocol.onMessage(req, False) - resp = json.loads(send_mock.call_args[0][0].decode()) + resp = jsonutils.loads(send_mock.call_args[0][0]) self.assertEqual(404, resp['headers']['status']) diff --git a/zaqar/tests/unit/transport/websocket/v2/test_subscriptions.py b/zaqar/tests/unit/transport/websocket/v2/test_subscriptions.py index bd5bb4b12..f6949c6ef 100644 --- a/zaqar/tests/unit/transport/websocket/v2/test_subscriptions.py +++ b/zaqar/tests/unit/transport/websocket/v2/test_subscriptions.py @@ -13,11 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json import time from unittest import mock import msgpack +from oslo_serialization import jsonutils from oslo_utils import uuidutils from zaqar.common import auth @@ -47,7 +47,7 @@ class SubscriptionTest(base.V1_1Base): body, self.headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertIn(resp['headers']['status'], [201, 204]) with mock.patch.object(self.protocol, 'sendMessage') as msg_mock: @@ -66,7 +66,7 @@ class SubscriptionTest(base.V1_1Base): body, self.headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(resp['headers']['status'], 204) sender.side_effect = validator @@ -110,7 +110,7 @@ class SubscriptionTest(base.V1_1Base): 'api': 'v2', 'headers': self.headers}} self.assertEqual(1, sender.call_count) - self.assertEqual(response, json.loads(sender.call_args[0][0].decode())) + self.assertEqual(response, jsonutils.loads(sender.call_args[0][0])) # Trigger protocol close self.protocol.onClose(True, 100, None) @@ -180,7 +180,7 @@ class SubscriptionTest(base.V1_1Base): 'subscription_id': str(sub)}, 'api': 'v2', 'headers': self.headers}} self.assertEqual(1, sender.call_count) - self.assertEqual(response, json.loads(sender.call_args[0][0].decode())) + self.assertEqual(response, jsonutils.loads(sender.call_args[0][0])) def test_subscription_create_no_queue(self): action = consts.SUBSCRIPTION_CREATE @@ -214,7 +214,7 @@ class SubscriptionTest(base.V1_1Base): 'api': 'v2', 'headers': self.headers}} self.assertEqual(1, sender.call_count) - self.assertEqual(response, json.loads(sender.call_args[0][0].decode())) + self.assertEqual(response, jsonutils.loads(sender.call_args[0][0])) def test_subscription_get(self): sub = self.boot.storage.subscription_controller.create( @@ -246,7 +246,7 @@ class SubscriptionTest(base.V1_1Base): 'api': 'v2', 'headers': self.headers}} self.assertEqual(1, sender.call_count) - response = json.loads(sender.call_args[0][0].decode()) + response = jsonutils.loads(sender.call_args[0][0]) # Get and remove age from the actual response. actual_sub_age = response['body'].pop('age') self.assertLessEqual(0, actual_sub_age) @@ -282,7 +282,7 @@ class SubscriptionTest(base.V1_1Base): 'body': {'queue_name': 'kitkat'}, 'api': 'v2', 'headers': self.headers}} self.assertEqual(1, sender.call_count) - response = json.loads(sender.call_args[0][0].decode()) + response = jsonutils.loads(sender.call_args[0][0]) # Get and remove age from the actual response. actual_sub_age = response['body']['subscriptions'][0].pop('age') self.assertLessEqual(0, actual_sub_age) @@ -334,8 +334,8 @@ class SubscriptionTest(base.V1_1Base): # Check that the server responded in text format to the message # creation request - message_create_response = json.loads( - sender.call_args_list[1][0][0].decode()) + message_create_response = jsonutils.loads( + sender.call_args_list[1][0][0]) self.assertEqual(201, message_create_response['headers']['status']) # Fetch webhook notification that was intended to arrive to @@ -368,7 +368,7 @@ class SubscriptionTest(base.V1_1Base): req = test_utils.create_request(action, body, self.headers) def validator(resp, isBinary): - resp = json.loads(resp.decode()) + resp = jsonutils.loads(resp) self.assertEqual(503, resp['headers']['status']) sender.side_effect = validator diff --git a/zaqar/tests/unit/transport/wsgi/test_utils.py b/zaqar/tests/unit/transport/wsgi/test_utils.py index 1721007e7..ea113446b 100644 --- a/zaqar/tests/unit/transport/wsgi/test_utils.py +++ b/zaqar/tests/unit/transport/wsgi/test_utils.py @@ -13,9 +13,11 @@ # the License. import io -import json import falcon + +from oslo_serialization import jsonutils + import testtools from zaqar.transport.wsgi import utils @@ -113,7 +115,7 @@ class TestUtils(testtools.TestCase): def test_no_spec(self): obj = {u'body': {'event': 'start_backup'}, 'ttl': 300} - document = str(json.dumps(obj, ensure_ascii=False)) + document = str(jsonutils.dumps(obj, ensure_ascii=False)) doc_stream = io.StringIO(document) deserialized = utils.deserialize(doc_stream, len(document)) @@ -126,7 +128,7 @@ class TestUtils(testtools.TestCase): def test_no_spec_array(self): things = [{u'body': {'event': 'start_backup'}, 'ttl': 300}] - document = str(json.dumps(things, ensure_ascii=False)) + document = str(jsonutils.dumps(things, ensure_ascii=False)) doc_stream = io.StringIO(document) deserialized = utils.deserialize(doc_stream, len(document)) @@ -145,7 +147,7 @@ class TestUtils(testtools.TestCase): def test_deserialize_and_sanitize_json_obj(self): obj = {u'body': {'event': 'start_backup'}, 'id': 'DEADBEEF'} - document = str(json.dumps(obj, ensure_ascii=False)) + document = str(jsonutils.dumps(obj, ensure_ascii=False)) stream = io.StringIO(document) spec = [('body', dict, None), ('id', str, None)] @@ -162,7 +164,7 @@ class TestUtils(testtools.TestCase): def test_deserialize_and_sanitize_json_array(self): array = [{u'body': {u'x': 1}}, {u'body': {u'x': 2}}] - document = str(json.dumps(array, ensure_ascii=False)) + document = str(jsonutils.dumps(array, ensure_ascii=False)) stream = io.StringIO(document) spec = [('body', dict, None)] diff --git a/zaqar/tests/unit/transport/wsgi/v1/test_validation.py b/zaqar/tests/unit/transport/wsgi/v1/test_validation.py index ad50c6784..f04d63712 100644 --- a/zaqar/tests/unit/transport/wsgi/v1/test_validation.py +++ b/zaqar/tests/unit/transport/wsgi/v1/test_validation.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils from oslo_utils import uuidutils import falcon @@ -78,11 +78,11 @@ class TestValidation(base.V1Base): max_messages_post_size = 256 obj = {'a': 0, 'b': ''} - envelope_length = len(json.dumps(obj, separators=(',', ':'))) + envelope_length = len(jsonutils.dumps(obj, separators=(',', ':'))) obj['b'] = 'x' * (max_messages_post_size - envelope_length + 1) for long_body in ('a' * (max_messages_post_size - 2 + 1), obj): - doc = json.dumps([{'body': long_body, 'ttl': 100}]) + doc = jsonutils.dumps([{'body': long_body, 'ttl': 100}]) self.simulate_post(self.queue_path + '/messages', self.project_id, body=doc, diff --git a/zaqar/tests/unit/transport/wsgi/v1_1/test_claims.py b/zaqar/tests/unit/transport/wsgi/v1_1/test_claims.py index a52fced90..b763a26e8 100644 --- a/zaqar/tests/unit/transport/wsgi/v1_1/test_claims.py +++ b/zaqar/tests/unit/transport/wsgi/v1_1/test_claims.py @@ -14,7 +14,6 @@ # limitations under the License. import datetime -import json from unittest import mock import ddt @@ -47,12 +46,12 @@ class TestClaimsMongoDB(base.V1_1Base): self.claims_path = self.queue_path + '/claims' self.messages_path = self.queue_path + '/messages' - doc = json.dumps({"_ttl": 60}) + doc = jsonutils.dumps({"_ttl": 60}) self.simulate_put(self.queue_path, body=doc, headers=self.headers) self.assertEqual(falcon.HTTP_201, self.srmock.status) - doc = json.dumps({'messages': [{'body': 239, 'ttl': 300}] * 10}) + doc = jsonutils.dumps({'messages': [{'body': 239, 'ttl': 300}] * 10}) self.simulate_post(self.queue_path + '/messages', body=doc, headers=self.headers) self.assertEqual(falcon.HTTP_201, self.srmock.status) @@ -91,7 +90,7 @@ class TestClaimsMongoDB(base.V1_1Base): def test_unacceptable_ttl_or_grace(self, ttl_grace): ttl, grace = ttl_grace self.simulate_post(self.claims_path, - body=json.dumps({'ttl': ttl, 'grace': grace}), + body=jsonutils.dumps({'ttl': ttl, 'grace': grace}), headers=self.headers) self.assertEqual(falcon.HTTP_400, self.srmock.status) @@ -101,7 +100,7 @@ class TestClaimsMongoDB(base.V1_1Base): href = self._get_a_claim() self.simulate_patch(href, - body=json.dumps({'ttl': ttl}), + body=jsonutils.dumps({'ttl': ttl}), headers=self.headers) self.assertEqual(falcon.HTTP_400, self.srmock.status) @@ -281,7 +280,7 @@ class TestClaimsMongoDB(base.V1_1Base): self.assertEqual(falcon.HTTP_204, self.srmock.status) def test_patch_nonexistent_claim_404s(self): - patch_data = json.dumps({'ttl': 100}) + patch_data = jsonutils.dumps({'ttl': 100}) self.simulate_patch(self.claims_path + '/a', body=patch_data, headers=self.headers) self.assertEqual(falcon.HTTP_404, self.srmock.status) diff --git a/zaqar/tests/unit/transport/wsgi/v1_1/test_validation.py b/zaqar/tests/unit/transport/wsgi/v1_1/test_validation.py index 5f188e993..f0c239936 100644 --- a/zaqar/tests/unit/transport/wsgi/v1_1/test_validation.py +++ b/zaqar/tests/unit/transport/wsgi/v1_1/test_validation.py @@ -13,10 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - import falcon +from oslo_serialization import jsonutils from oslo_utils import uuidutils from zaqar.tests.unit.transport.wsgi import base @@ -78,11 +77,11 @@ class TestValidation(base.V1_1Base): max_messages_post_size = 256 obj = {'a': 0, 'b': ''} - envelope_length = len(json.dumps(obj, separators=(',', ':'))) + envelope_length = len(jsonutils.dumps(obj, separators=(',', ':'))) obj['b'] = 'x' * (max_messages_post_size - envelope_length + 1) for long_body in ('a' * (max_messages_post_size - 2 + 1), obj): - doc = json.dumps([{'body': long_body, 'ttl': 100}]) + doc = jsonutils.dumps([{'body': long_body, 'ttl': 100}]) self.simulate_post(self.queue_path + '/messages', self.project_id, body=doc, diff --git a/zaqar/tests/unit/transport/wsgi/v2_0/test_claims.py b/zaqar/tests/unit/transport/wsgi/v2_0/test_claims.py index 0a478b076..c946db716 100644 --- a/zaqar/tests/unit/transport/wsgi/v2_0/test_claims.py +++ b/zaqar/tests/unit/transport/wsgi/v2_0/test_claims.py @@ -14,7 +14,6 @@ # limitations under the License. import datetime -import json from unittest import mock import ddt @@ -47,12 +46,12 @@ class TestClaimsMongoDB(base.V2Base): self.claims_path = self.queue_path + '/claims' self.messages_path = self.queue_path + '/messages' - doc = json.dumps({"_ttl": 60}) + doc = jsonutils.dumps({"_ttl": 60}) self.simulate_put(self.queue_path, body=doc, headers=self.headers) self.assertEqual(falcon.HTTP_201, self.srmock.status) - doc = json.dumps({'messages': [{'body': 239, 'ttl': 300}] * 10}) + doc = jsonutils.dumps({'messages': [{'body': 239, 'ttl': 300}] * 10}) self.simulate_post(self.queue_path + '/messages', body=doc, headers=self.headers) self.assertEqual(falcon.HTTP_201, self.srmock.status) @@ -92,7 +91,7 @@ class TestClaimsMongoDB(base.V2Base): def test_unacceptable_ttl_or_grace(self, ttl_grace): ttl, grace = ttl_grace self.simulate_post(self.claims_path, - body=json.dumps({'ttl': ttl, 'grace': grace}), + body=jsonutils.dumps({'ttl': ttl, 'grace': grace}), headers=self.headers) self.assertEqual(falcon.HTTP_400, self.srmock.status) @@ -102,7 +101,7 @@ class TestClaimsMongoDB(base.V2Base): href = self._get_a_claim() self.simulate_patch(href, - body=json.dumps({'ttl': ttl}), + body=jsonutils.dumps({'ttl': ttl}), headers=self.headers) self.assertEqual(falcon.HTTP_400, self.srmock.status) @@ -282,7 +281,7 @@ class TestClaimsMongoDB(base.V2Base): self.assertEqual(falcon.HTTP_204, self.srmock.status) def test_patch_nonexistent_claim_404s(self): - patch_data = json.dumps({'ttl': 100}) + patch_data = jsonutils.dumps({'ttl': 100}) self.simulate_patch(self.claims_path + '/a', body=patch_data, headers=self.headers) self.assertEqual(falcon.HTTP_404, self.srmock.status) diff --git a/zaqar/tests/unit/transport/wsgi/v2_0/test_validation.py b/zaqar/tests/unit/transport/wsgi/v2_0/test_validation.py index c689f7e33..3b33252aa 100644 --- a/zaqar/tests/unit/transport/wsgi/v2_0/test_validation.py +++ b/zaqar/tests/unit/transport/wsgi/v2_0/test_validation.py @@ -13,10 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - import falcon +from oslo_serialization import jsonutils from oslo_utils import uuidutils from zaqar.tests.unit.transport.wsgi import base @@ -78,11 +77,11 @@ class TestValidation(base.V2Base): max_messages_post_size = 256 obj = {'a': 0, 'b': ''} - envelope_length = len(json.dumps(obj, separators=(',', ':'))) + envelope_length = len(jsonutils.dumps(obj, separators=(',', ':'))) obj['b'] = 'x' * (max_messages_post_size - envelope_length + 1) for long_body in ('a' * (max_messages_post_size - 2 + 1), obj): - doc = json.dumps([{'body': long_body, 'ttl': 100}]) + doc = jsonutils.dumps([{'body': long_body, 'ttl': 100}]) self.simulate_post(self.queue_path + '/messages', self.project_id, body=doc, diff --git a/zaqar/transport/utils.py b/zaqar/transport/utils.py index 9757ddf3e..aea2813ea 100644 --- a/zaqar/transport/utils.py +++ b/zaqar/transport/utils.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils from oslo_utils import encodeutils @@ -45,7 +45,7 @@ def read_json(stream, len): """ try: content = encodeutils.safe_decode(stream.read(len), 'utf-8') - result = json.loads(content, parse_int=_json_int) + result = jsonutils.loads(content, parse_int=_json_int) if not isinstance(result, dict) and not isinstance(result, list): raise MalformedJSON() return result @@ -60,4 +60,4 @@ def to_json(obj): :param obj: a JSON-serializable object """ - return json.dumps(obj, ensure_ascii=False) + return jsonutils.dumps(obj, ensure_ascii=False) diff --git a/zaqar/transport/websocket/factory.py b/zaqar/transport/websocket/factory.py index 193073007..a5f79edeb 100644 --- a/zaqar/transport/websocket/factory.py +++ b/zaqar/transport/websocket/factory.py @@ -13,10 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - from autobahn.asyncio import websocket import msgpack +from oslo_serialization import jsonutils from oslo_utils import uuidutils from zaqar.transport.websocket import protocol @@ -67,7 +66,7 @@ class NotificationFactory(object): # NOTE(Eva-i): incoming data is encoded in JSON, let's convert it # to MsgPack, if notification should be encoded in binary format. if instance.notify_in_binary: - data = msgpack.packb(json.loads(data)) + data = msgpack.packb(jsonutils.loads(data)) instance.sendMessage(data, instance.notify_in_binary) def __call__(self): diff --git a/zaqar/transport/websocket/protocol.py b/zaqar/transport/websocket/protocol.py index b00a51ad8..649d39377 100644 --- a/zaqar/transport/websocket/protocol.py +++ b/zaqar/transport/websocket/protocol.py @@ -15,12 +15,12 @@ import datetime import io -import json import sys from autobahn.asyncio import websocket import msgpack from oslo_log import log as logging +from oslo_serialization import jsonutils from oslo_utils import timeutils import pytz import txaio @@ -84,7 +84,7 @@ class MessagingProtocol(websocket.WebSocketServerProtocol): else: if isinstance(payload, bytes): payload = payload.decode() - payload = json.loads(payload) + payload = jsonutils.loads(payload) except Exception: if isBinary: pack_name = 'binary (MessagePack)' @@ -209,13 +209,14 @@ class MessagingProtocol(websocket.WebSocketServerProtocol): self.sendMessage(msgpack.packb(resp.get_response()), True) else: pack_name = 'txt' - self.sendMessage(json.dumps(resp.get_response()).encode(), False) + self.sendMessage(jsonutils.dump_as_bytes(resp.get_response()), + False) if LOG.isEnabledFor(logging.INFO): api = resp._request._api status = resp._headers['status'] action = resp._request._action # Dump to JSON to print body without unicode prefixes on Python 2 - body = json.dumps(resp._request._body) + body = jsonutils.dumps(resp._request._body) var_dict = {'api': api, 'pack_name': pack_name, 'status': status, 'action': action, 'body': body} LOG.info('Response: API %(api)s %(pack_name)s, %(status)s. ' diff --git a/zaqar/transport/wsgi/v1_0/homedoc.py b/zaqar/transport/wsgi/v1_0/homedoc.py index 013871606..a4a7b335e 100644 --- a/zaqar/transport/wsgi/v1_0/homedoc.py +++ b/zaqar/transport/wsgi/v1_0/homedoc.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations under # the License. -import json +from oslo_serialization import jsonutils # NOTE(kgriffs): http://tools.ietf.org/html/draft-nottingham-json-home-03 @@ -131,7 +131,7 @@ JSON_HOME = { class Resource(object): def __init__(self): - document = json.dumps(JSON_HOME, ensure_ascii=False, indent=4) + document = jsonutils.dumps(JSON_HOME, ensure_ascii=False, indent=4) self.document_utf8 = document.encode('utf-8') def on_get(self, req, resp, project_id): diff --git a/zaqar/transport/wsgi/v1_1/homedoc.py b/zaqar/transport/wsgi/v1_1/homedoc.py index d5f0c5a24..042f92cc3 100644 --- a/zaqar/transport/wsgi/v1_1/homedoc.py +++ b/zaqar/transport/wsgi/v1_1/homedoc.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations under # the License. -import json +from oslo_serialization import jsonutils # NOTE(kgriffs): http://tools.ietf.org/html/draft-nottingham-json-home-03 @@ -281,7 +281,7 @@ class Resource(object): if conf.admin_mode: JSON_HOME['resources'].update(ADMIN_RESOURCES) - document = json.dumps(JSON_HOME, ensure_ascii=False, indent=4) + document = jsonutils.dumps(JSON_HOME, ensure_ascii=False, indent=4) self.document_utf8 = document.encode('utf-8') def on_get(self, req, resp, project_id): diff --git a/zaqar/transport/wsgi/v2_0/homedoc.py b/zaqar/transport/wsgi/v2_0/homedoc.py index f019a2fa4..01250db89 100644 --- a/zaqar/transport/wsgi/v2_0/homedoc.py +++ b/zaqar/transport/wsgi/v2_0/homedoc.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations under # the License. -import json +from oslo_serialization import jsonutils # NOTE(kgriffs): http://tools.ietf.org/html/draft-nottingham-json-home-03 @@ -381,7 +381,7 @@ class Resource(object): if conf.admin_mode: JSON_HOME['resources'].update(ADMIN_RESOURCES) - document = json.dumps(JSON_HOME, ensure_ascii=False, indent=4) + document = jsonutils.dumps(JSON_HOME, ensure_ascii=False, indent=4) self.document_utf8 = document.encode('utf-8') def on_get(self, req, resp, project_id):