Remove 'load_all' from cluster_policy module

This removes the 'load_all' method from the cluster_policy module. We
are using the cluster_policy versioned object as an alternative.

Change-Id: I7f1df0441d808341dd50ffa6f20f36ccc1cb54ab
This commit is contained in:
tengqm 2017-01-30 07:06:51 -05:00
parent e05fb48e8d
commit 25c356a8cc
7 changed files with 93 additions and 100 deletions

View File

@ -22,9 +22,9 @@ from senlin.common import context as req_context
from senlin.common import exception
from senlin.common.i18n import _, _LE
from senlin.common import utils
from senlin.engine import cluster_policy as cp_mod
from senlin.engine import event as EVENT
from senlin.objects import action as ao
from senlin.objects import cluster_policy as cpo
from senlin.objects import dependency as dobj
from senlin.policies import base as policy_mod
@ -386,10 +386,9 @@ class Action(object):
if target not in ['BEFORE', 'AFTER']:
return
# TODO(Anyone): This could use the cluster's runtime data
bindings = cp_mod.ClusterPolicy.load_all(self.context, cluster_id,
sort='priority',
filters={'enabled': True})
bindings = cpo.ClusterPolicy.get_all(self.context, cluster_id,
sort='priority',
filters={'enabled': True})
# default values
self.data['status'] = policy_mod.CHECK_OK
self.data['reason'] = _('Completed policy checking.')
@ -400,7 +399,10 @@ class Action(object):
# cluster, no matter that policy is only interested in the
# "BEFORE" or "AFTER" or both.
if target == 'AFTER':
pb.record_last_op(self.context)
ts = timeutils.utcnow(True)
pb.last_op = ts
cpo.ClusterPolicy.update(self.context, pb.cluster_id,
pb.policy_id, {'last_op': ts})
if not policy.need_check(target, self):
continue

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from oslo_utils import timeutils
from senlin.common import exception
from senlin.objects import cluster_policy as cpo
@ -91,26 +89,6 @@ class ClusterPolicy(object):
return cls._from_object(context, binding)
@classmethod
def load_all(cls, context, cluster_id, filters=None, sort=None):
"""Retrieve all policies attached to a specific cluster."""
bindings = cpo.ClusterPolicy.get_all(context, cluster_id,
filters=filters,
sort=sort)
return [cls._from_object(context, b) for b in bindings]
def cooldown_inprogress(self, cooldown):
if self.last_op:
if not timeutils.is_older_than(self.last_op, cooldown):
return True
return False
def record_last_op(self, context):
self.last_op = timeutils.utcnow(True)
self.store(context)
def to_dict(self):
binding_dict = {
'id': self.id,

View File

@ -12,6 +12,8 @@
"""Cluster-policy binding object."""
from oslo_utils import timeutils
from senlin.db import api as db_api
from senlin.objects import base
from senlin.objects import cluster as cluster_obj
@ -84,6 +86,13 @@ class ClusterPolicy(base.SenlinObject, base.VersionedObjectDictCompat):
def delete(cls, context, cluster_id, policy_id):
db_api.cluster_policy_detach(context, cluster_id, policy_id)
def cooldown_inprogress(self, cooldown):
last_op = self.last_op
if last_op and not timeutils.is_older_than(last_op, cooldown):
return True
return False
def to_dict(self):
binding_dict = {
'id': self.id,

View File

@ -15,6 +15,7 @@ import copy
import mock
from oslo_config import cfg
from oslo_utils import timeutils
from oslo_utils import uuidutils
import six
from senlin.common import consts
@ -22,11 +23,11 @@ from senlin.common import exception
from senlin.common import utils as common_utils
from senlin.engine.actions import base as ab
from senlin.engine import cluster as cluster_mod
from senlin.engine import cluster_policy as cp_mod
from senlin.engine import environment
from senlin.engine import event as EVENT
from senlin.engine import node as node_mod
from senlin.objects import action as ao
from senlin.objects import cluster_policy as cpo
from senlin.objects import dependency as dobj
from senlin.policies import base as policy_mod
from senlin.tests.unit.common import base
@ -513,18 +514,22 @@ class ActionBaseTest(base.SenlinTestCase):
self.assertFalse(res)
mock_query.assert_called_once_with(action.context, 'FAKE_ID')
@mock.patch.object(cp_mod.ClusterPolicy, 'load_all')
@mock.patch.object(cpo.ClusterPolicy, 'get_all')
def test_policy_check_target_invalid(self, mock_load):
action = ab.Action(OBJID, 'OBJECT_ACTION', self.ctx)
res = action.policy_check('FAKE_CLUSTER', 'WHEN')
self.assertIsNone(res)
self.assertEqual(0, mock_load.call_count)
@mock.patch.object(cp_mod.ClusterPolicy, 'load_all')
@mock.patch.object(cpo.ClusterPolicy, 'get_all')
def test_policy_check_no_bindings(self, mock_load):
action = ab.Action(OBJID, 'OBJECT_ACTION', self.ctx)
mock_load.return_value = []
res = action.policy_check('FAKE_CLUSTER', 'BEFORE')
self.assertIsNone(res)
self.assertEqual(policy_mod.CHECK_OK, action.data['status'])
mock_load.assert_called_once_with(action.context, 'FAKE_CLUSTER',
@ -589,15 +594,13 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
return policy
def _create_cp_binding(self, cluster_id, policy_id):
values = {'enabled': True}
pb = cp_mod.ClusterPolicy(cluster_id, policy_id, **values)
pb.id = 'FAKE_BINDING_ID'
return pb
return cpo.ClusterPolicy(cluster_id=cluster_id, policy_id=policy_id,
enabled=True, id=uuidutils.generate_uuid(),
last_op=None)
@mock.patch.object(policy_mod.Policy, 'post_op')
@mock.patch.object(policy_mod.Policy, 'pre_op')
@mock.patch.object(cp_mod.ClusterPolicy, 'load_all')
@mock.patch.object(cpo.ClusterPolicy, 'get_all')
@mock.patch.object(policy_mod.Policy, 'load')
def test_policy_check_missing_target(self, mock_load, mock_load_all,
mock_pre_op, mock_post_op):
@ -609,7 +612,7 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
'properties': {'KEY2': 5},
}
policy = fakes.TestPolicy('test-policy', spec)
policy.id = 'FAKE_POLICY_ID'
policy.id = uuidutils.generate_uuid()
policy.TARGET = [('BEFORE', 'OBJECT_ACTION')]
# Note: policy binding is created but not stored
pb = self._create_cp_binding(cluster_id, policy.id)
@ -662,7 +665,7 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
mock_event.assert_called_once_with(action, 'error', reason)
@mock.patch.object(EVENT, 'debug')
@mock.patch.object(cp_mod.ClusterPolicy, 'load_all')
@mock.patch.object(cpo.ClusterPolicy, 'get_all')
@mock.patch.object(policy_mod.Policy, 'load')
def test_policy_check_pre_op(self, mock_load, mock_load_all, mock_event):
cluster_id = CLUSTER_ID
@ -673,7 +676,7 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
'properties': {'KEY2': 5},
}
policy = fakes.TestPolicy('test-policy', spec)
policy.id = 'FAKE_POLICY_ID'
policy.id = uuidutils.generate_uuid()
policy.TARGET = [('BEFORE', 'OBJECT_ACTION')]
# Note: policy binding is created but not stored
pb = self._create_cp_binding(cluster_id, policy.id)
@ -696,15 +699,13 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
self.assertIsNone(pb.last_op)
@mock.patch.object(EVENT, 'debug')
@mock.patch.object(cp_mod.ClusterPolicy, 'load_all')
@mock.patch.object(cpo.ClusterPolicy, 'get_all')
@mock.patch.object(policy_mod.Policy, 'load')
def test_policy_check_post_op(self, mock_load, mock_load_all, mock_event):
cluster_id = CLUSTER_ID
# Note: policy is mocked
policy = mock.Mock()
policy.id = 'FAKE_POLICY_ID'
policy.TARGET = [('AFTER', 'OBJECT_ACTION')]
policy.cooldown = 0
policy = mock.Mock(id=uuidutils.generate_uuid(), cooldown=0,
TARGET=[('AFTER', 'OBJECT_ACTION')])
# Note: policy binding is created but not stored
pb = self._create_cp_binding(cluster_id, policy.id)
self.assertIsNone(pb.last_op)
@ -728,28 +729,30 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
self.assertEqual(0, policy.pre_op.call_count)
policy.post_op.assert_called_once_with(cluster_id, action)
@mock.patch.object(cp_mod.ClusterPolicy, 'load_all')
@mock.patch.object(cpo.ClusterPolicy, 'cooldown_inprogress')
@mock.patch.object(cpo.ClusterPolicy, 'get_all')
@mock.patch.object(policy_mod.Policy, 'load')
def test_policy_check_cooldown_inprogress(self, mock_load, mock_load_all):
def test_policy_check_cooldown_inprogress(self, mock_load, mock_load_all,
mock_inprogress):
cluster_id = CLUSTER_ID
# Note: policy is mocked
policy = mock.Mock()
policy.id = 'FAKE_POLICY_ID'
policy.TARGET = [('AFTER', 'OBJECT_ACTION')]
policy_id = uuidutils.generate_uuid()
policy = mock.Mock(id=policy_id, TARGET=[('AFTER', 'OBJECT_ACTION')])
# Note: policy binding is created but not stored
pb = self._create_cp_binding(cluster_id, policy.id)
self.patchobject(pb, 'cooldown_inprogress', return_value=True)
self.assertIsNone(pb.last_op)
mock_inprogress.return_value = True
mock_load_all.return_value = [pb]
mock_load.return_value = policy
action = ab.Action(cluster_id, 'OBJECT_ACTION', self.ctx)
# Do it
res = action.policy_check(CLUSTER_ID, 'AFTER')
self.assertIsNone(res)
self.assertEqual(policy_mod.CHECK_ERROR, action.data['status'])
self.assertEqual('Policy FAKE_POLICY_ID cooldown is still in '
'progress.', six.text_type(action.data['reason']))
self.assertEqual(
'Policy %s cooldown is still in progress.' % policy_id,
six.text_type(action.data['reason']))
mock_load_all.assert_called_once_with(
action.context, cluster_id, sort='priority',
filters={'enabled': True})
@ -760,24 +763,19 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
self.assertEqual(0, policy.pre_op.call_count)
self.assertEqual(0, policy.post_op.call_count)
@mock.patch.object(cp_mod.ClusterPolicy, 'load_all')
@mock.patch.object(cpo.ClusterPolicy, 'get_all')
@mock.patch.object(policy_mod.Policy, 'load')
@mock.patch.object(ab.Action, '_check_result')
def test_policy_check_abort_in_middle(self, mock_check, mock_load,
mock_load_all):
cluster_id = CLUSTER_ID
# Note: both policies are mocked
policy1 = mock.Mock()
policy1.id = 'FAKE_POLICY_ID_1'
policy1 = mock.Mock(id=uuidutils.generate_uuid(), cooldown=0,
TARGET=[('AFTER', 'OBJECT_ACTION')])
policy1.name = 'P1'
policy1.cooldown = 0
policy1.TARGET = [('AFTER', 'OBJECT_ACTION')]
policy2 = mock.Mock()
policy2.id = 'FAKE_POLICY_ID_2'
policy2 = mock.Mock(id=uuidutils.generate_uuid(), cooldown=0,
TARGET=[('AFTER', 'OBJECT_ACTION')])
policy2.name = 'P2'
policy2.cooldown = 0
policy2.TARGET = [('AFTER', 'OBJECT_ACTION')]
action = ab.Action(cluster_id, 'OBJECT_ACTION', self.ctx)
# Note: policy binding is created but not stored

View File

@ -10,7 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from datetime import timedelta
from oslo_utils import timeutils
import six
@ -120,28 +119,6 @@ class TestClusterPolicy(base.SenlinTestCase):
self.assertEqual('senlin.policy.dummy-1.0', result.policy_type)
self.assertEqual('test_policy', result.policy_name)
def test_cluster_policy_load_all(self):
result = cpm.ClusterPolicy.load_all(self.context, 'non-existent')
self.assertEqual([], result)
cluster = utils.create_cluster(self.context, CLUSTER_ID, PROFILE_ID)
policy1 = utils.create_policy(self.context,
'd752f3e4-7be5-41a6-8a36-85bbbdbd7d4a')
policy2 = utils.create_policy(self.context,
'0a2e1240-d34f-4c96-8034-37388cdcdb7b')
b1 = cpm.ClusterPolicy(cluster.id, policy1.id, enabled=True,
priority=10)
b1.store(self.context)
b2 = cpm.ClusterPolicy(cluster.id, policy2.id, enabled=False,
priority=20)
b2.store(self.context)
# NOTE: we don't test all other parameters because the db api tests
# already covered that
result = cpm.ClusterPolicy.load_all(self.context, CLUSTER_ID)
self.assertEqual(2, len(result))
def test_cluster_policy_to_dict(self):
values = {
'priority': 12,
@ -162,13 +139,3 @@ class TestClusterPolicy(base.SenlinTestCase):
}
self.assertEqual(expected, cp.to_dict())
def test_cooldown_inprogress(self):
values = {
'enabled': True,
'last_op': timeutils.utcnow(True),
}
cp = cpm.ClusterPolicy(CLUSTER_ID, POLICY_ID, **values)
self.assertTrue(cp.cooldown_inprogress(60))
cp.last_op -= timedelta(hours=1)
self.assertFalse(cp.cooldown_inprogress(60))

View File

@ -28,8 +28,8 @@ class TestCluster(testtools.TestCase):
def test_find_by_uuid(self, mock_get):
x_cluster = mock.Mock()
mock_get.return_value = x_cluster
aid = uuidutils.generate_uuid()
result = co.Cluster.find(self.ctx, aid)
self.assertEqual(x_cluster, result)
@ -54,8 +54,8 @@ class TestCluster(testtools.TestCase):
def test_find_by_name(self, mock_get_name):
x_cluster = mock.Mock()
mock_get_name.return_value = x_cluster
aid = 'this-is-not-uuid'
result = co.Cluster.find(self.ctx, aid)
self.assertEqual(x_cluster, result)
@ -67,8 +67,8 @@ class TestCluster(testtools.TestCase):
x_cluster = mock.Mock()
mock_get_shortid.return_value = x_cluster
mock_get_name.return_value = None
aid = 'abcd-1234-abcd'
result = co.Cluster.find(self.ctx, aid, False)
self.assertEqual(x_cluster, result)

View File

@ -0,0 +1,39 @@
# 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 datetime import timedelta
import testtools
from oslo_utils import timeutils
from senlin.objects import cluster_policy as cpo
CLUSTER_ID = "8286fcaa-6474-44e2-873e-28b5cb2c204c"
POLICY_ID = "da958a16-f384-49a1-83a9-abac8b4ec46e"
class TestClusterPolicy(testtools.TestCase):
def test_cooldown_inprogress(self):
last_op = timeutils.utcnow(True)
cp = cpo.ClusterPolicy(cluster_id=CLUSTER_ID, policy_id=POLICY_ID,
last_op=last_op)
res = cp.cooldown_inprogress(60)
self.assertTrue(res)
cp.last_op -= timedelta(hours=1)
res = cp.cooldown_inprogress(60)
self.assertFalse(res)