Refactored Watcher codebase to add py34 support

Even though Watcher was mentioning python 3.4 as supported, it
really wasn't the case as all the unit tests were not passing in this
version of Python.

This patchset fixes all the failing tests in Python 3.4 while
keeping Watcher Python 2.7 compatible.

DocImpact
BugImpact
Change-Id: Ie74acc08ef0a2899349a4b419728c89e416a18cb
This commit is contained in:
Vincent Françoise
2015-12-08 11:06:52 +01:00
committed by Jean-Emile DARTOIS
parent b1fe7a5f3d
commit d934971458
21 changed files with 134 additions and 57 deletions

View File

@@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals
import json
import mock
@@ -68,14 +70,14 @@ class TestContextHook(base.BaseTestCase):
class TestNoExceptionTracebackHook(api_base.FunctionalTest):
TRACE = [
u'Traceback (most recent call last):',
u' File "/opt/stack/watcher/watcher/openstack/common/rpc/amqp.py",'
'Traceback (most recent call last):',
' File "/opt/stack/watcher/watcher/openstack/common/rpc/amqp.py",'
' line 434, in _process_data\\n **args)',
u' File "/opt/stack/watcher/watcher/openstack/common/rpc/'
' File "/opt/stack/watcher/watcher/openstack/common/rpc/'
'dispatcher.py", line 172, in dispatch\\n result ='
' getattr(proxyobj, method)(context, **kwargs)']
MSG_WITHOUT_TRACE = "Test exception message."
MSG_WITH_TRACE = MSG_WITHOUT_TRACE + "\n" + "\n".join(TRACE)
MSG_WITH_TRACE = "{0}\n{1}".format(MSG_WITHOUT_TRACE, "\n".join(TRACE))
def setUp(self):
super(TestNoExceptionTracebackHook, self).setUp()
@@ -94,7 +96,7 @@ class TestNoExceptionTracebackHook(api_base.FunctionalTest):
def test_hook_remote_error_success(self):
test_exc_type = 'TestException'
self.root_convert_mock.side_effect = messaging.rpc.RemoteError(
test_exc_type, self.MSG_WITHOUT_TRACE, self.TRACE)
test_exc_type, self.MSG_WITHOUT_TRACE, "\n".join(self.TRACE))
response = self.get_json('/', path_prefix='', expect_errors=True)
@@ -104,7 +106,7 @@ class TestNoExceptionTracebackHook(api_base.FunctionalTest):
# rare thing (happens due to wrong deserialization settings etc.)
# we don't care about this garbage.
expected_msg = ("Remote error: %s %s"
% (test_exc_type, self.MSG_WITHOUT_TRACE) + "\n[u'")
% (test_exc_type, self.MSG_WITHOUT_TRACE))
actual_msg = json.loads(response.json['error_message'])['faultstring']
self.assertEqual(expected_msg, actual_msg)

View File

@@ -0,0 +1,59 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# 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 __future__ import unicode_literals
from oslo_config import cfg
import wsme
from watcher.api.controllers.v1 import utils as v1_utils
from watcher.tests import base
class TestApiUtilsValidScenarios(base.TestCase):
scenarios = [
("limit=None + max_limit=None",
{"limit": None, "max_limit": None, "expected": None}),
("limit=None + max_limit=1",
{"limit": None, "max_limit": 1, "expected": 1}),
# ("limit=0 + max_limit=None",
# {"limit": 0, "max_limit": None, "expected": 0}),
("limit=1 + max_limit=None",
{"limit": 1, "max_limit": None, "expected": 1}),
("limit=1 + max_limit=1",
{"limit": 1, "max_limit": 1, "expected": 1}),
("limit=2 + max_limit=1",
{"limit": 2, "max_limit": 1, "expected": 1}),
]
def test_validate_limit(self):
cfg.CONF.set_override("max_limit", self.max_limit, group="api")
actual_limit = v1_utils.validate_limit(self.limit)
self.assertEqual(actual_limit, self.expected)
class TestApiUtilsInvalidScenarios(base.TestCase):
scenarios = [
("limit=0 + max_limit=None", {"limit": 0, "max_limit": None}),
]
def test_validate_limit_invalid_cases(self):
cfg.CONF.set_override("max_limit", self.max_limit, group="api")
self.assertRaises(
wsme.exc.ClientSideError, v1_utils.validate_limit, self.limit
)

View File

@@ -18,6 +18,7 @@ Utils for testing the API service.
import datetime
import json
import six
from watcher.api.controllers.v1 import action as action_ctrl
from watcher.api.controllers.v1 import action_plan as action_plan_ctrl
from watcher.api.controllers.v1 import audit as audit_ctrl
@@ -76,7 +77,9 @@ class FakeMemcache(object):
def remove_internal(values, internal):
# NOTE(yuriyz): internal attributes should not be posted, except uuid
int_attr = [attr.lstrip('/') for attr in internal if attr != '/uuid']
return dict([(k, v) for (k, v) in values.iteritems() if k not in int_attr])
return dict(
(k, v) for (k, v) in six.iteritems(values) if k not in int_attr
)
def audit_post_data(**kw):

View File

@@ -367,20 +367,21 @@ class TestListAction(api_base.FunctionalTest):
uuid=utils.generate_uuid(),
next=id_ + 1)
response = self.get_json('/actions/')
reference_uuids = [(s['next_uuid'] if 'next_uuid' in s else None)
for s in response['actions']]
reference_uuids = [
s.get('next_uuid', '') for s in response['actions']
]
response = self.get_json('/actions/?sort_key=next_uuid')
self.assertEqual(5, len(response['actions']))
uuids = [(s['next_uuid'] if 'next_uuid' in s else None)
uuids = [(s['next_uuid'] if 'next_uuid' in s else '')
for s in response['actions']]
self.assertEqual(sorted(reference_uuids), uuids)
response = self.get_json('/actions/?sort_key=next_uuid&sort_dir=desc')
self.assertEqual(5, len(response['actions']))
uuids = [(s['next_uuid'] if 'next_uuid' in s else None)
uuids = [(s['next_uuid'] if 'next_uuid' in s else '')
for s in response['actions']]
self.assertEqual(sorted(reference_uuids, reverse=True), uuids)

View File

@@ -38,19 +38,19 @@ class TestListGoal(api_base.FunctionalTest):
self._assert_goal_fields(response['goals'][0])
def test_get_one(self):
goal_name = CONF.watcher_goals.goals.keys()[0]
goal_name = list(CONF.watcher_goals.goals.keys())[0]
response = self.get_json('/goals/%s' % goal_name)
self.assertEqual(goal_name, response['name'])
self._assert_goal_fields(response)
def test_detail(self):
goal_name = CONF.watcher_goals.goals.keys()[0]
goal_name = list(CONF.watcher_goals.goals.keys())[0]
response = self.get_json('/goals/detail')
self.assertEqual(goal_name, response['goals'][0]["name"])
self._assert_goal_fields(response['goals'][0])
def test_detail_against_single(self):
goal_name = CONF.watcher_goals.goals.keys()[0]
goal_name = list(CONF.watcher_goals.goals.keys())[0]
response = self.get_json('/goals/%s/detail' % goal_name,
expect_errors=True)
self.assertEqual(404, response.status_int)

View File

@@ -123,7 +123,7 @@ class TestJsonPatchType(base.TestCase):
'value': {'cat': 'meow'}}]
ret = self._patch_json(valid_patches, False)
self.assertEqual(200, ret.status_int)
self.assertEqual(sorted(valid_patches), sorted(ret.json))
self.assertEqual(valid_patches, ret.json)
def test_cannot_update_internal_attr(self):
patch = [{'path': '/internal', 'op': 'replace', 'value': 'foo'}]
@@ -244,7 +244,6 @@ class TestJsonType(base.TestCase):
vts = str(types.jsontype)
self.assertIn(str(wtypes.text), vts)
self.assertIn(str(int), vts)
self.assertIn(str(long), vts)
self.assertIn(str(float), vts)
self.assertIn(str(types.BooleanType), vts)
self.assertIn(str(list), vts)