Handle null JSON blobs

The JSON deserializer doesn't handle null values properly; it raises
a TypeError because it expects a string or buffer. This became apparent
after the supplier info update which added columns that would be null
for existing packages. Patch returns None from the JSON deserializer
in that case, and also refactors unittest base classes.

Change-Id: I6f31500d2dd1aaaf6a1fd26c1901666072d79776
Closes-Bug: #1342306
This commit is contained in:
Steve McLellan 2014-07-15 15:33:26 -05:00
parent ad4cb489a2
commit 3509b530d0
5 changed files with 42 additions and 12 deletions

View File

@ -101,7 +101,9 @@ class JsonBlob(sa.TypeDecorator):
return anyjson.serialize(value)
def process_result_value(self, value, dialect):
return anyjson.deserialize(value)
if value is not None:
return anyjson.deserialize(value)
return None
class Environment(BASE, ModificationsTrackedObject):

View File

@ -16,8 +16,6 @@
import fixtures
import logging
import mock
from oslo.config import cfg
import testtools
import urllib
import webob
@ -25,6 +23,7 @@ from murano.api.v1 import request_statistics
from murano.common import rpc
from murano.openstack.common import timeutils
from murano.openstack.common import wsgi
from murano.tests import base
from murano.tests import utils
TEST_DEFAULT_LOGLEVELS = {'migrate': logging.WARN, 'sqlalchemy': logging.WARN}
@ -60,13 +59,13 @@ class FakeLogMixin:
name=base))
class MuranoTestCase(testtools.TestCase, FakeLogMixin):
class MuranoApiTestCase(base.MuranoWithDBTestCase, FakeLogMixin):
# Set this if common.rpc is imported into other scopes so that
# it can be mocked properly
RPC_IMPORT = 'murano.common.rpc'
def setUp(self):
super(MuranoTestCase, self).setUp()
super(MuranoApiTestCase, self).setUp()
self.setup_logging()
@ -79,12 +78,9 @@ class MuranoTestCase(testtools.TestCase, FakeLogMixin):
return_value=self.mock_api_rpc).start()
self.addCleanup(mock.patch.stopall)
self.addCleanup(cfg.CONF.reset)
utils.setup_dummy_db()
self.addCleanup(utils.reset_dummy_db)
def tearDown(self):
super(MuranoTestCase, self).tearDown()
super(MuranoApiTestCase, self).tearDown()
timeutils.utcnow.override_time = None
def _stub_uuid(self, values=[]):

View File

@ -24,7 +24,7 @@ import os
@mock.patch.object(policy, 'check')
class TestCatalogApi(test_base.ControllerTest, test_base.MuranoTestCase):
class TestCatalogApi(test_base.ControllerTest, test_base.MuranoApiTestCase):
def setUp(self):
super(TestCatalogApi, self).setUp()
self.controller = catalog.Controller()

View File

@ -21,12 +21,12 @@ from murano.api.v1 import environments
from murano.common import policy
from murano.db import models
from murano.openstack.common import timeutils
import murano.tests.api.base as test_base
import murano.tests.api.base as tb
import murano.tests.utils as test_utils
@mock.patch.object(policy, 'check')
class TestEnvironmentApi(test_base.ControllerTest, test_base.MuranoTestCase):
class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase):
RPC_IMPORT = 'murano.db.services.environments.rpc'
def setUp(self):

View File

@ -0,0 +1,32 @@
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from murano.db import models
from murano.db import session
from murano.tests import base
class TestModels(base.MuranoWithDBTestCase):
def test_missing_blob(self):
"""Fake a package with NULL supplier JSON blob to test bug 1342306"""
con = session.get_session().connection()
con.execute("INSERT INTO package(id, fully_qualified_name, "
"owner_id, name, description, created, updated, type, "
"supplier) "
"VALUES (1, 'blob.test', 1, 'blob test', 'Desc', "
"'2014-07-15 00:00:00', '2014-07-15 00:00:00', "
"'Application', NULL)")
loaded_e = session.get_session().query(models.Package).get(1)
self.assertEqual(None, loaded_e.supplier)