예제 #1
0
    def test_cluster_policy_to_dict(self):
        values = {
            'priority': 12,
            'cooldown': 34,
            'level': 56,
            'enabled': True,
        }
        cp = cpm.ClusterPolicy('fake-cluster', 'fake-policy', **values)
        self.assertIsNone(cp.id)
        expected = {
            'id': None,
            'cluster_id': 'fake-cluster',
            'policy_id': 'fake-policy',
            'priority': 12,
            'cooldown': 34,
            'level': 56,
            'enabled': True,
            'data': {},
            'last_op': None,
            'cluster_name': '',
            'policy_type': '',
            'policy_name': '',
        }

        self.assertEqual(expected, cp.to_dict())
예제 #2
0
    def test_cluster_policy_load(self):
        ex = self.assertRaises(exception.PolicyNotAttached,
                               cpm.ClusterPolicy.load, self.context,
                               'some-cluster', 'any-policy')
        self.assertEqual(
            "The policy 'any-policy' is not attached to the "
            "specified cluster 'some-cluster'.", six.text_type(ex))

        cluster = utils.create_cluster(self.context, CLUSTER_ID, PROFILE_ID)
        policy = utils.create_policy(self.context, POLICY_ID)

        values = {
            'priority': 12,
            'enabled': True,
        }
        cp = cpm.ClusterPolicy(cluster.id, policy.id, **values)
        cp_id = cp.store(self.context)

        result = cpm.ClusterPolicy.load(self.context, CLUSTER_ID, POLICY_ID)

        self.assertEqual(cp_id, result.id)
        self.assertEqual(cluster.id, result.cluster_id)
        self.assertEqual(policy.id, result.policy_id)
        self.assertTrue(True, result.enabled)
        self.assertEqual(12, result.priority)
        self.assertEqual({}, result.data)
        self.assertIsNone(result.last_op)
        self.assertEqual('test-cluster', result.cluster_name)
        self.assertEqual('senlin.policy.dummy-1.0', result.policy_type)
        self.assertEqual('test_policy', result.policy_name)
예제 #3
0
    def test_cluster_policy_load(self):
        ex = self.assertRaises(exception.PolicyNotAttached,
                               cpm.ClusterPolicy.load,
                               self.context, 'some-cluster', 'any-policy')
        self.assertEqual('The policy (any-policy) is not attached to the '
                         'specified cluster (some-cluster).',
                         six.text_type(ex))

        cluster = self._create_cluster('CLUSTER_ID')
        policy = self._create_policy('POLICY_ID')

        values = {
            'priority': 12,
            'cooldown': 34,
            'level': 56,
            'enabled': True,
        }
        cp = cpm.ClusterPolicy(cluster.id, policy.id, **values)
        cp_id = cp.store(self.context)

        result = cpm.ClusterPolicy.load(self.context, 'CLUSTER_ID',
                                        'POLICY_ID')

        self.assertEqual(cp_id, result.id)
        self.assertEqual(cluster.id, result.cluster_id)
        self.assertEqual(policy.id, result.policy_id)
        self.assertEqual(12, result.priority)
        self.assertEqual(34, result.cooldown)
        self.assertEqual(56, result.level)
        self.assertTrue(True, result.enabled)
        self.assertEqual({}, result.data)
        self.assertIsNone(result.last_op)
        self.assertEqual('test_cluster', result.cluster_name)
        self.assertEqual('policy-type', result.policy_type)
        self.assertEqual('test_policy', result.policy_name)
예제 #4
0
    def test_cluster_policy_load_all(self):
        result = cpm.ClusterPolicy.load_all(self.context, 'non-existent')
        self.assertEqual([], result)

        cluster = self._create_cluster('CLUSTER')
        policy1 = self._create_policy('P1')
        policy2 = self._create_policy('P2')

        b1 = cpm.ClusterPolicy(cluster.id, policy1.id, enabled=True)
        b1.store(self.context)
        b2 = cpm.ClusterPolicy(cluster.id, policy2.id, enabled=False)
        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')
        self.assertEqual(2, len(result))
예제 #5
0
 def test_cooldown_inprogress(self):
     values = {
         'enabled': True,
         'last_op': timeutils.utcnow(),
     }
     cp = cpm.ClusterPolicy('fake-cluster', 'fake-policy', **values)
     self.assertTrue(cp.cooldown_inprogress(60))
     cp.last_op -= timedelta(hours=1)
     self.assertFalse(cp.cooldown_inprogress(60))
예제 #6
0
 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))
예제 #7
0
    def _create_cp_binding(self, cluster_id, policy_id, priority=30):
        values = {
            'priority': priority,
            'cooldown': 60,
            'level': 50,
            'enabled': True,
        }

        pb = cp_mod.ClusterPolicy(cluster_id, policy_id, **values)
        pb.id = 'FAKE_BINDING_ID'
        return pb
예제 #8
0
    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))
예제 #9
0
    def attach_policy(self, ctx, policy_id, values):
        """Attach policy object to the cluster.

        Note this method MUST be called with the cluster locked.

        :param ctx: A context for DB operation.
        :param policy_id: ID of the policy object.
        :param values: Optional dictionary containing binding properties.

        :returns: A tuple containing a boolean result and a reason string.
        """

        policy = pcb.Policy.load(ctx, policy_id)
        # Check if policy has already been attached
        for existing in self.rt['policies']:
            # Policy already attached
            if existing.id == policy_id:
                return True, 'Policy already attached.'

            # Detect policy type conflicts
            if (existing.type == policy.type) and policy.singleton:
                reason = ("Only one instance of policy type (%(ptype)s) can "
                          "be attached to a cluster, but another instance "
                          "(%(existing)s) is found attached to the cluster "
                          "(%(cluster)s) already.") % {
                              'ptype': policy.type,
                              'existing': existing.id,
                              'cluster': self.id
                          }
                return False, reason

        # invoke policy callback
        enabled = bool(values.get('enabled', True))
        res, data = policy.attach(self, enabled=enabled)
        if not res:
            return False, data

        kwargs = {
            'enabled': enabled,
            'data': data,
            'priority': policy.PRIORITY
        }

        cp = cpm.ClusterPolicy(self.id, policy_id, **kwargs)
        cp.store(ctx)

        # refresh cached runtime
        self.rt['policies'].append(policy)

        return True, 'Policy attached.'
예제 #10
0
    def test_cluster_policy_store(self):
        cluster = self._create_cluster('fake-cluster')
        policy = self._create_policy('fake-policy')
        values = {
            'priority': 12,
            'cooldown': 34,
            'level': 56,
            'enabled': True,
        }
        cp = cpm.ClusterPolicy(cluster.id, policy.id, **values)
        self.assertIsNone(cp.id)
        cp_id = cp.store(self.context)
        self.assertIsNotNone(cp_id)

        result = db_api.cluster_policy_get(self.context, 'fake-cluster',
                                           'fake-policy')

        self.assertIsNotNone(result)
        self.assertEqual(12, result.priority)
        self.assertEqual(34, result.cooldown)
        self.assertEqual(56, result.level)
        self.assertTrue(result.enabled)
        self.assertEqual({}, result.data)
        self.assertIsNone(result.last_op)

        # do an update
        cp.cooldown = 60
        cp.priority = 60
        cp.level = 40
        cp.enabled = False
        cp.data = {'foo': 'bar'}
        timestamp = timeutils.utcnow()
        cp.last_op = timestamp

        new_id = cp.store(self.context)
        self.assertEqual(cp_id, new_id)

        result = db_api.cluster_policy_get(self.context, 'fake-cluster',
                                           'fake-policy')

        self.assertIsNotNone(result)
        self.assertEqual(60, result.cooldown)
        self.assertEqual(60, result.priority)
        self.assertEqual(40, result.level)
        self.assertFalse(result.enabled)
        self.assertEqual({'foo': 'bar'}, result.data)
        self.assertEqual(timestamp, result.last_op)
예제 #11
0
    def do_attach_policy(self):
        """Handler for the CLUSTER_ATTACH_POLICY action.

        :returns: A tuple containing the result and the corresponding reason.
        """
        policy_id = self.inputs.get('policy_id', None)
        if not policy_id:
            return self.RES_ERROR, _('Policy not specified.')

        policy = policy_mod.Policy.load(self.context, policy_id)
        # Check if policy has already been attached
        for existing in self.cluster.policies:
            # Policy already attached
            if existing.id == policy_id:
                return self.RES_OK, _('Policy already attached.')

            # Detect policy type conflicts
            if (existing.type == policy.type) and policy.singleton:
                reason = _('Only one instance of policy type (%(ptype)s) can '
                           'be attached to a cluster, but another instance '
                           '(%(existing)s) is found attached to the cluster '
                           '(%(cluster)s) already.') % {
                               'ptype': policy.type,
                               'existing': existing.id,
                               'cluster': self.target
                           }
                return self.RES_ERROR, reason

        res, data = policy.attach(self.cluster)
        if not res:
            return self.RES_ERROR, data

        # Initialize data field of cluster_policy object with information
        # generated during policy attaching
        values = {
            'priority': self.inputs['priority'],
            'cooldown': self.inputs['cooldown'],
            'level': self.inputs['level'],
            'enabled': self.inputs['enabled'],
            'data': data,
        }

        cp = cp_mod.ClusterPolicy(self.cluster.id, policy_id, **values)
        cp.store(self.context)

        self.cluster.add_policy(policy)
        return self.RES_OK, _('Policy attached.')
예제 #12
0
    def test_cluster_policy_init(self):
        values = {
            'priority': 12,
            'enabled': True,
        }
        cp = cpm.ClusterPolicy(CLUSTER_ID, POLICY_ID, **values)

        self.assertIsNone(cp.id)
        self.assertEqual(CLUSTER_ID, cp.cluster_id)
        self.assertEqual(POLICY_ID, cp.policy_id)
        self.assertEqual(12, cp.priority)
        self.assertTrue(True, cp.enabled)
        self.assertEqual({}, cp.data)
        self.assertIsNone(cp.last_op)
        self.assertEqual('', cp.cluster_name)
        self.assertEqual('', cp.policy_type)
        self.assertEqual('', cp.policy_name)
예제 #13
0
    def test_cluster_policy_to_dict(self):
        values = {
            'priority': 12,
            'enabled': True,
        }
        cp = cpm.ClusterPolicy(CLUSTER_ID, POLICY_ID, **values)
        self.assertIsNone(cp.id)
        expected = {
            'id': None,
            'cluster_id': CLUSTER_ID,
            'policy_id': POLICY_ID,
            'enabled': True,
            'data': {},
            'last_op': None,
            'cluster_name': '',
            'policy_type': '',
            'policy_name': '',
        }

        self.assertEqual(expected, cp.to_dict())
예제 #14
0
    def test_cluster_policy_init(self):
        values = {
            'priority': 12,
            'cooldown': 34,
            'level': 56,
            'enabled': True,
        }
        cp = cpm.ClusterPolicy('fake-cluster', 'fake-policy', **values)

        self.assertIsNone(cp.id)
        self.assertEqual('fake-cluster', cp.cluster_id)
        self.assertEqual('fake-policy', cp.policy_id)
        self.assertEqual(12, cp.priority)
        self.assertEqual(34, cp.cooldown)
        self.assertEqual(56, cp.level)
        self.assertTrue(True, cp.enabled)
        self.assertEqual({}, cp.data)
        self.assertIsNone(cp.last_op)
        self.assertEqual('', cp.cluster_name)
        self.assertEqual('', cp.policy_type)
        self.assertEqual('', cp.policy_name)
예제 #15
0
    def test_cluster_policy_store(self):
        utils.create_profile(self.context, PROFILE_ID)
        cluster = utils.create_cluster(self.context, CLUSTER_ID, PROFILE_ID)
        policy = utils.create_policy(self.context, POLICY_ID)
        values = {
            'priority': 12,
            'enabled': True,
        }
        cp = cpm.ClusterPolicy(cluster.id, policy.id, **values)
        self.assertIsNone(cp.id)
        cp_id = cp.store(self.context)
        self.assertIsNotNone(cp_id)

        result = cpo.ClusterPolicy.get(self.context, CLUSTER_ID, POLICY_ID)

        self.assertIsNotNone(result)
        self.assertEqual(12, result.priority)
        self.assertTrue(result.enabled)
        self.assertEqual({}, result.data)
        self.assertIsNone(result.last_op)

        # do an update
        cp.enabled = False
        cp.priority = 60
        cp.data = {'foo': 'bar'}
        timestamp = timeutils.utcnow(True)
        cp.last_op = timestamp

        new_id = cp.store(self.context)
        self.assertEqual(cp_id, new_id)

        result = cpo.ClusterPolicy.get(self.context, CLUSTER_ID, POLICY_ID)

        self.assertIsNotNone(result)
        self.assertFalse(result.enabled)
        self.assertEqual(60, result.priority)
        self.assertEqual({'foo': 'bar'}, result.data)
        self.assertEqual(common_utils.isotime(timestamp),
                         common_utils.isotime(result.last_op))
예제 #16
0
    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