Fix AttributeError being raised in buckets controller

This PS fixes an AttributeError raised in buckets controller
when Deckhand tries to parse a malformed YAML which in turn
causes Deckhand to attempt to raise a 400, but accesses an
invalid attribute in the exception object, causing the
AttributeError.

Example error:

    AttributeError: 'ScannerError' object has no attribute 'format_message'

Change-Id: I54865e4830d3d34e813b1ecfa3105cf6243f2ca0
This commit is contained in:
Felipe Monteiro 2017-10-05 01:26:31 +01:00
parent 5e6b0bfe5c
commit 6e2a8adcf5
6 changed files with 85 additions and 11 deletions

View File

@ -16,6 +16,7 @@ import yaml
import falcon
from oslo_log import log as logging
import six
from deckhand.control import base as api_base
from deckhand.control.views import document as document_view
@ -42,7 +43,7 @@ class BucketsResource(api_base.BaseResource):
error_msg = ("Could not parse the document into YAML data. "
"Details: %s." % e)
LOG.error(error_msg)
raise falcon.HTTPBadRequest(description=e.format_message())
raise falcon.HTTPBadRequest(description=six.text_type(e))
# All concrete documents in the payload must successfully pass their
# JSON schema validations. Otherwise raise an error.

View File

@ -0,0 +1,27 @@
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
#
# 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 falcon import testing as falcon_testing
from deckhand.control import api
from deckhand.tests.unit import base as test_base
class BaseControllerTest(test_base.DeckhandWithDBTestCase,
falcon_testing.TestCase):
"""Base class for unit testing falcon controllers."""
def setUp(self):
super(BaseControllerTest, self).setUp()
self.app = falcon_testing.TestClient(api.start_api())

View File

@ -33,9 +33,9 @@ class TestApi(test_base.DeckhandTestCase):
for resource in (buckets, revision_diffing, revision_documents,
revision_tags, revisions, rollback, versions):
resource_name = resource.__name__.split('.')[-1]
resource_obj = mock.patch.object(
resource_obj = self.patchobject(
resource, '%sResource' % resource_name.title().replace(
'_', ''), autospec=True).start()
'_', ''), autospec=True)
setattr(self, '%s_resource' % resource_name, resource_obj)
@mock.patch.object(api, 'db_api', autospec=True)

View File

@ -15,21 +15,20 @@
import mock
from deckhand.control import base as api_base
from deckhand.tests.unit import base as test_base
from deckhand.tests.unit.control import base as test_base
class TestBaseResource(test_base.DeckhandTestCase):
class TestBaseResource(test_base.BaseControllerTest):
def setUp(self):
super(TestBaseResource, self).setUp()
self.base_resource = api_base.BaseResource()
def test_on_options(self):
# Override `dir` so that ``dir(self)`` returns `methods`.
expected_methods = ['on_get', 'on_heat', 'on_post', 'on_put',
'on_delete', 'on_patch']
api_base.BaseResource.__dir__ = lambda x: expected_methods
@mock.patch.object(api_base, 'dir') # noqa
def test_on_options(self, mock_dir):
expected_methods = ['on_get', 'on_post', 'on_put', 'on_delete',
'on_patch']
mock_dir.return_value = expected_methods
mock_resp = mock.Mock(headers={})
self.base_resource.on_options(None, mock_resp)

View File

@ -0,0 +1,40 @@
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
#
# 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 deckhand.tests.unit.control import base as test_base
class TestBucketsController(test_base.BaseControllerTest):
"""Test suite for validating positive scenarios bucket controller."""
class TestBucketsControllerNegative(test_base.BaseControllerTest):
"""Test suite for validating negative scenarios bucket controller."""
def test_put_bucket_with_invalid_document_payload(self):
no_colon_spaces = """
name:foo
schema:
layeringDefinition:
layer:site
"""
invalid_payloads = ['garbage', no_colon_spaces]
error_re = ['.*The provided YAML failed schema validation.*',
'.*mapping values are not allowed here.*']
for idx, payload in enumerate(invalid_payloads):
resp = self.app.simulate_put('/api/v1.0/bucket/mop/documents',
body=payload)
self.assertEqual(400, resp.status_code)
self.assertRegexpMatches(resp.text, error_re[idx])

View File

@ -0,0 +1,7 @@
---
fixes:
- |
Deckhand will no longer throw an ``AttributeError`` after a
``yaml.scanner.ScannerError`` is raised when attempting to parse a
malformed YAML document. Deckhand should now correctly raise a
"400 Bad Request" instead.