From 07c7ba9b2e289e75578aea15e863ade2ad71ca25 Mon Sep 17 00:00:00 2001 From: Vladimir Ostroverkhov Date: Mon, 20 Jun 2016 10:58:45 +0300 Subject: [PATCH] Add goal_name field in strategy This patch set adds goal_name field in strategy Related-Bug: #1590416. Change-Id: Ia381dd882b244487f8beec4d04dabddd1ac7dbc7 --- watcher/api/controllers/v1/strategy.py | 19 +++- watcher/tests/objects/test_strategy.py | 130 +++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 watcher/tests/objects/test_strategy.py diff --git a/watcher/api/controllers/v1/strategy.py b/watcher/api/controllers/v1/strategy.py index adc861ba9..b23fbaa41 100644 --- a/watcher/api/controllers/v1/strategy.py +++ b/watcher/api/controllers/v1/strategy.py @@ -54,6 +54,7 @@ class Strategy(base.APIBase): between the internal object model and the API representation of a strategy. """ _goal_uuid = None + _goal_name = None def _get_goal(self, value): if value == wtypes.Unset: @@ -81,6 +82,16 @@ class Strategy(base.APIBase): if goal: self._goal_uuid = goal.uuid + def _get_goal_name(self): + return self._goal_name + + def _set_goal_name(self, value): + if value and self._goal_name != value: + self._goal_name = None + goal = self._get_goal(value) + if goal: + self._goal_name = goal.name + uuid = types.uuid """Unique UUID for this strategy""" @@ -97,6 +108,10 @@ class Strategy(base.APIBase): mandatory=True) """The UUID of the goal this audit refers to""" + goal_name = wsme.wsproperty(wtypes.text, _get_goal_name, _set_goal_name, + mandatory=False) + """The name of the goal this audit refers to""" + def __init__(self, **kwargs): super(Strategy, self).__init__() @@ -105,16 +120,18 @@ class Strategy(base.APIBase): self.fields.append('name') self.fields.append('display_name') self.fields.append('goal_uuid') + self.fields.append('goal_name') setattr(self, 'uuid', kwargs.get('uuid', wtypes.Unset)) setattr(self, 'name', kwargs.get('name', wtypes.Unset)) setattr(self, 'display_name', kwargs.get('display_name', wtypes.Unset)) setattr(self, 'goal_uuid', kwargs.get('goal_id', wtypes.Unset)) + setattr(self, 'goal_name', kwargs.get('goal_id', wtypes.Unset)) @staticmethod def _convert_with_links(strategy, url, expand=True): if not expand: strategy.unset_fields_except( - ['uuid', 'name', 'display_name', 'goal_uuid']) + ['uuid', 'name', 'display_name', 'goal_uuid', 'goal_name']) strategy.links = [ link.Link.make_link('self', url, 'strategies', strategy.uuid), diff --git a/watcher/tests/objects/test_strategy.py b/watcher/tests/objects/test_strategy.py new file mode 100644 index 000000000..be3bbbc77 --- /dev/null +++ b/watcher/tests/objects/test_strategy.py @@ -0,0 +1,130 @@ +# Copyright 2015 OpenStack Foundation +# All 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. + +import mock +from testtools.matchers import HasLength + +from watcher.common import exception +from watcher import objects +from watcher.tests.db import base +from watcher.tests.db import utils + + +class TestStrategyObject(base.DbTestCase): + + def setUp(self): + super(TestStrategyObject, self).setUp() + self.fake_strategy = utils.get_test_strategy() + + def test_get_by_id(self): + strategy_id = self.fake_strategy['id'] + with mock.patch.object(self.dbapi, 'get_strategy_by_id', + autospec=True) as mock_get_strategy: + mock_get_strategy.return_value = self.fake_strategy + strategy = objects.Strategy.get(self.context, strategy_id) + mock_get_strategy.assert_called_once_with(self.context, + strategy_id) + self.assertEqual(self.context, strategy._context) + + def test_get_by_uuid(self): + uuid = self.fake_strategy['uuid'] + with mock.patch.object(self.dbapi, 'get_strategy_by_uuid', + autospec=True) as mock_get_strategy: + mock_get_strategy.return_value = self.fake_strategy + strategy = objects.Strategy.get(self.context, uuid) + mock_get_strategy.assert_called_once_with(self.context, uuid) + self.assertEqual(self.context, strategy._context) + + def test_get_bad_uuid(self): + self.assertRaises(exception.InvalidIdentity, + objects.Strategy.get, self.context, 'not-a-uuid') + + def test_list(self): + with mock.patch.object(self.dbapi, 'get_strategy_list', + autospec=True) as mock_get_list: + mock_get_list.return_value = [self.fake_strategy] + strategies = objects.Strategy.list(self.context) + self.assertEqual(1, mock_get_list.call_count, 1) + self.assertThat(strategies, HasLength(1)) + self.assertIsInstance(strategies[0], objects.Strategy) + self.assertEqual(self.context, strategies[0]._context) + + def test_create(self): + with mock.patch.object(self.dbapi, 'create_strategy', + autospec=True) as mock_create_strategy: + mock_create_strategy.return_value = self.fake_strategy + strategy = objects.Strategy(self.context, **self.fake_strategy) + + strategy.create() + mock_create_strategy.assert_called_once_with(self.fake_strategy) + self.assertEqual(self.context, strategy._context) + + def test_destroy(self): + _id = self.fake_strategy['id'] + with mock.patch.object(self.dbapi, 'get_strategy_by_id', + autospec=True) as mock_get_strategy: + mock_get_strategy.return_value = self.fake_strategy + with mock.patch.object(self.dbapi, 'destroy_strategy', + autospec=True) as mock_destroy_strategy: + strategy = objects.Strategy.get_by_id(self.context, _id) + strategy.destroy() + mock_get_strategy.assert_called_once_with(self.context, _id) + mock_destroy_strategy.assert_called_once_with(_id) + self.assertEqual(self.context, strategy._context) + + def test_save(self): + _id = self.fake_strategy['id'] + with mock.patch.object(self.dbapi, 'get_strategy_by_id', + autospec=True) as mock_get_strategy: + mock_get_strategy.return_value = self.fake_strategy + with mock.patch.object(self.dbapi, 'update_strategy', + autospec=True) as mock_update_strategy: + strategy = objects.Strategy.get_by_id(self.context, _id) + strategy.name = 'UPDATED NAME' + strategy.save() + + mock_get_strategy.assert_called_once_with(self.context, _id) + mock_update_strategy.assert_called_once_with( + _id, {'name': 'UPDATED NAME'}) + self.assertEqual(self.context, strategy._context) + + def test_refresh(self): + _id = self.fake_strategy['id'] + returns = [dict(self.fake_strategy, name="first name"), + dict(self.fake_strategy, name="second name")] + expected = [mock.call(self.context, _id), + mock.call(self.context, _id)] + with mock.patch.object(self.dbapi, 'get_strategy_by_id', + side_effect=returns, + autospec=True) as mock_get_strategy: + strategy = objects.Strategy.get(self.context, _id) + self.assertEqual("first name", strategy.name) + strategy.refresh() + self.assertEqual("second name", strategy.name) + self.assertEqual(expected, mock_get_strategy.call_args_list) + self.assertEqual(self.context, strategy._context) + + def test_soft_delete(self): + _id = self.fake_strategy['id'] + with mock.patch.object(self.dbapi, 'get_strategy_by_id', + autospec=True) as mock_get_strategy: + mock_get_strategy.return_value = self.fake_strategy + with mock.patch.object(self.dbapi, 'soft_delete_strategy', + autospec=True) as mock_soft_delete: + strategy = objects.Strategy.get_by_id(self.context, _id) + strategy.soft_delete() + mock_get_strategy.assert_called_once_with(self.context, _id) + mock_soft_delete.assert_called_once_with(_id) + self.assertEqual(self.context, strategy._context)