def test_policy_get_all_with_sort_key_are_used(self): values = { 'policy1': {'level': 40, 'priority': 40, 'cooldown': 1, 'enabled': True}, 'policy2': {'level': 30, 'priority': 60, 'cooldown': 2, 'enabled': True}, 'policy3': {'level': 50, 'priority': 10, 'cooldown': 3, 'enabled': True} } # prepare for key in values: value = values[key] policy_id = self.create_policy(id=key).id db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy_id, value) mock_paginate = self.patchobject(db_api.utils, 'paginate_query') sort_keys = ['level', 'priority', 'cooldown', 'enabled'] db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort_keys=sort_keys) # Check sort_keys used args = mock_paginate.call_args[0] used_sort_keys = set(args[3]) expected_keys = set(['id', 'level', 'priority', 'cooldown', 'enabled']) self.assertEqual(expected_keys, used_sort_keys)
def test_policy_enable_disable(self): policy = self.create_policy() fields = { 'enabled': True, } db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertTrue(bindings[0].enabled) db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, {'enabled': True}) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertTrue(bindings[0].enabled) db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, {'enabled': False}) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertFalse(bindings[0].enabled) db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, {'enabled': True}) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertTrue(bindings[0].enabled) # No policy binding found res = db_api.cluster_policy_update(self.ctx, self.cluster.id, 'BOGUS', {}) self.assertIsNone(res)
def test_policy_enable_disable(self): policy = self.create_policy() fields = { 'enabled': True, } db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertTrue(bindings[0].enabled) db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, {'enabled': True}) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertTrue(bindings[0].enabled) db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, {'enabled': False}) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertFalse(bindings[0].enabled) db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, {'enabled': True}) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertTrue(bindings[0].enabled) # No policy binding found res = db_api.cluster_policy_update(self.ctx, self.cluster.id, 'BOGUS', {}) self.assertIsNone(res)
def test_policy_get_all_with_empty_filters(self): for pid in ['policy1', 'policy2']: self.create_policy(id=pid) db_api.cluster_policy_attach(self.ctx, self.cluster.id, pid, {}) filters = None results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(2, len(results))
def test_policy_get_all_with_empty_filters(self): for pid in ['policy1', 'policy2']: self.create_policy(id=pid) db_api.cluster_policy_attach(self.ctx, self.cluster.id, pid, {}) filters = None results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(2, len(results))
def test_cluster_policy_get(self): policy = self.create_policy() db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, {}) binding = db_api.cluster_policy_get(self.ctx, self.cluster.id, policy.id) self.assertIsNotNone(binding) self.assertEqual(self.cluster.id, binding.cluster_id) self.assertEqual(policy.id, binding.policy_id)
def test_policy_get_all_by_policy_type(self): for pid in ['policy1', 'policy2']: self.create_policy(id=pid) db_api.cluster_policy_attach(self.ctx, self.cluster.id, pid, {}) results = db_api.cluster_policy_get_by_type(self.ctx, self.cluster.id, 'ScalingPolicy') self.assertEqual(2, len(results)) results = db_api.cluster_policy_get_by_type(self.ctx, self.cluster.id, 'UnknownPolicy') self.assertEqual(0, len(results))
def test_policy_get_all_by_policy_name(self): for pid in ['policy1', 'policy2']: self.create_policy(id=pid) db_api.cluster_policy_attach(self.ctx, self.cluster.id, pid, {}) results = db_api.cluster_policy_get_by_name(self.ctx, self.cluster.id, 'test_policy') self.assertEqual(2, len(results)) results = db_api.cluster_policy_get_by_name(self.ctx, self.cluster.id, 'unknown_policy') self.assertEqual(0, len(results))
def test_cluster_policy_ids_by_cluster(self): # prepare ids = [] for i in range(3): policy_id = self.create_policy().id ids.append(policy_id) db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy_id, {'enabled': True}) # sorted by enabled, the 2nd and 3rd are unpredictable results = db_api.cluster_policy_ids_by_cluster(self.ctx, self.cluster.id) self.assertEqual(set(ids), set(results))
def test_policy_delete_in_use(self): policy = db_api.policy_create(self.ctx, self.new_policy_data()) self.assertIsNotNone(policy) fields = {'enabled': True, 'level': 50} db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, fields) self.assertRaises(exception.ResourceBusyError, db_api.policy_delete, self.ctx, policy.id) db_api.cluster_policy_detach(self.ctx, self.cluster.id, policy.id) db_api.policy_delete(self.ctx, policy.id) policy = db_api.policy_get(self.ctx, policy.id) self.assertIsNone(policy)
def test_policy_get_all_with_default_sort_keys(self): values = {'policy1': {'level': 40, 'priority': 40}, 'policy2': {'level': 30, 'priority': 60}} for key in values: value = values[key] policy_id = self.create_policy(id=key).id db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy_id, value) filters = None results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(2, len(results))
def test_policy_update_last_op(self): policy = self.create_policy() db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, {}) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertIsNone(bindings[0].last_op) timestamp = tu.utcnow(True) fields = {'last_op': timestamp} db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertEqual(timestamp, bindings[0].last_op)
def test_policy_get_all_prioritized(self): policy = self.create_policy() fields = {'enabled': True, 'level': 50, 'priority': 20} binding1 = db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, fields) fields = {'enabled': True, 'level': 50, 'priority': 40} binding2 = db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(binding1.id, bindings[1].id) self.assertEqual(binding2.id, bindings[0].id)
def test_policy_update_last_op(self): policy = self.create_policy() db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, {}) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertIsNone(bindings[0].last_op) timestamp = tu.utcnow() fields = {'last_op': timestamp} db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertEqual(timestamp, bindings[0].last_op)
def test_policy_delete_in_use(self): policy = db_api.policy_create(self.ctx, self.new_policy_data()) self.assertIsNotNone(policy) fields = { 'enabled': True, } db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, fields) self.assertRaises(exception.ResourceBusyError, db_api.policy_delete, self.ctx, policy.id) db_api.cluster_policy_detach(self.ctx, self.cluster.id, policy.id) db_api.policy_delete(self.ctx, policy.id) policy = db_api.policy_get(self.ctx, policy.id) self.assertIsNone(policy)
def test_policy_get_all_by_policy_type_with_filter(self): for pid in ['policy1', 'policy2']: self.create_policy(id=pid) db_api.cluster_policy_attach(self.ctx, self.cluster.id, pid, {'enabled': True}) filters = {'enabled': True} results = db_api.cluster_policy_get_by_type(self.ctx, self.cluster.id, 'ScalingPolicy', filters=filters) self.assertEqual(2, len(results)) filters = {'enabled': False} results = db_api.cluster_policy_get_by_type(self.ctx, self.cluster.id, 'ScalingPolicy', filters=filters) self.assertEqual(0, len(results))
def test_policy_get_all_by_policy_name_with_filter(self): for pid in ['policy1', 'policy2']: self.create_policy(id=pid) db_api.cluster_policy_attach(self.ctx, self.cluster.id, pid, {'enabled': True}) filters = {'enabled': True} results = db_api.cluster_policy_get_by_name(self.ctx, self.cluster.id, 'test_policy', filters=filters) self.assertEqual(2, len(results)) filters = {'enabled': False} results = db_api.cluster_policy_get_by_name(self.ctx, self.cluster.id, 'test_policy', filters=filters) self.assertEqual(0, len(results))
def test_policy_get_all_with_all_filters(self): for pid in ['policy1', 'policy2']: self.create_policy(id=pid) db_api.cluster_policy_attach(self.ctx, self.cluster.id, pid, {'enabled': True}) filters = {'enabled': True, 'policy_name': 'test_policy', 'policy_type': 'ScalingPolicy'} results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(2, len(results)) filters = {'enabled': True, 'policy_type': 'ScalingPolicy'} results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(2, len(results)) filters = {'enabled': True, 'policy_name': 'test_policy'} results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(2, len(results)) filters = {'enabled': True, 'policy_name': 'wrong_name', 'policy_type': 'wrong_type'} results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(0, len(results)) filters = {'policy_name': 'test_policy'} results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(2, len(results)) filters = {'policy_type': 'ScalingPolicy'} results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(2, len(results)) filters = {'enabled': False} results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(0, len(results))
def test_policy_get_all_with_sorting(self): values = { 'policy1': {'enabled': True}, 'policy2': {'enabled': True}, 'policy3': {'enabled': False} } # prepare for key in values: value = values[key] policy_id = self.create_policy(id=key).id db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy_id, value) # sorted by enabled, the 2nd and 3rd are unpredictable results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort='enabled') self.assertEqual('policy3', results[0].policy_id)
def test_policy_get_all_with_sorting(self): values = { 'policy1': {'enabled': True}, 'policy2': {'enabled': True}, 'policy3': {'enabled': False} } # prepare for key in values: value = values[key] policy_id = self.create_policy(id=key).id db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy_id, value) # sorted by enabled, the 2nd and 3rd are unpredictable results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort='enabled') self.assertEqual('policy3', results[0].policy_id)
def test_policy_attach_detach(self): policy = self.create_policy() fields = { 'enabled': True, } db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertTrue(bindings[0].enabled) # This will succeed db_api.cluster_policy_detach(self.ctx, self.cluster.id, policy.id) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(0, len(bindings)) # This will fail silently res = db_api.cluster_policy_detach(self.ctx, self.cluster.id, 'BOGUS') self.assertIsNone(res)
def test_policy_attach_detach(self): policy = self.create_policy() fields = { 'enabled': True, } db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertTrue(bindings[0].enabled) # This will succeed db_api.cluster_policy_detach(self.ctx, self.cluster.id, policy.id) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(0, len(bindings)) # This will fail silently res = db_api.cluster_policy_detach(self.ctx, self.cluster.id, 'BOGUS') self.assertIsNone(res)
def test_cluster_delete_policies_deleted(self): # create cluster cluster = shared.create_cluster(self.ctx, self.profile) cluster_id = cluster.id # create policy policy_data = { 'name': 'test_policy', 'type': 'ScalingPolicy', 'user': self.ctx.user, 'project': self.ctx.project, 'spec': {'foo': 'bar'}, 'level': 50, 'cooldown': 60, 'data': None, } policy = db_api.policy_create(self.ctx, policy_data) self.assertIsNotNone(policy) # attach policy fields = { 'enabled': True, 'priority': 77, } db_api.cluster_policy_attach(self.ctx, cluster_id, policy.id, fields) binding = db_api.cluster_policy_get(self.ctx, cluster_id, policy.id) self.assertIsNotNone(binding) # now we delete the cluster db_api.cluster_delete(self.ctx, cluster_id) res = db_api.cluster_get(self.ctx, cluster_id) self.assertIsNone(res) # we check the cluster-policy binding binding = db_api.cluster_policy_get(self.ctx, cluster_id, policy.id) self.assertIsNone(binding) # but the policy is not deleted result = db_api.policy_get(self.ctx, policy.id) self.assertIsNotNone(result)
def test_policy_get_all_with_filters(self): values = {'policy1': {'level': 40, 'priority': 40}, 'policy2': {'level': 30, 'priority': 60}} for key in values: value = values[key] policy_id = self.create_policy(id=key).id db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy_id, value) filters = {'policy_id': ['policy1', 'policyx']} results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(1, len(results)) self.assertEqual('policy1', results[0].policy_id) filters = {'priority': 60} results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, filters=filters) self.assertEqual(1, len(results)) self.assertEqual('policy2', results[0].policy_id)
def test_policy_update_with_data(self): policy = self.create_policy() db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, {}) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertIsNone(bindings[0].data) fields = {'data': {'foo': 'bar'}} db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertEqual({'foo': 'bar'}, bindings[0].data) fields = {'data': {'foo': 'BAR'}} db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertEqual({'foo': 'BAR'}, bindings[0].data)
def test_policy_update_with_data(self): policy = self.create_policy() db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy.id, {}) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertIsNone(bindings[0].data) fields = {'data': {'foo': 'bar'}} db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertEqual({'foo': 'bar'}, bindings[0].data) fields = {'data': {'foo': 'BAR'}} db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id, fields) bindings = db_api.cluster_policy_get_all(self.ctx, self.cluster.id) self.assertEqual(1, len(bindings)) self.assertEqual({'foo': 'BAR'}, bindings[0].data)
def test_cluster_delete_policies_deleted(self): # create cluster cluster = shared.create_cluster(self.ctx, self.profile) cluster_id = cluster.id # create policy policy_data = { 'name': 'test_policy', 'type': 'ScalingPolicy', 'user': self.ctx.user_id, 'project': self.ctx.project_id, 'spec': { 'foo': 'bar' }, 'data': None, } policy = db_api.policy_create(self.ctx, policy_data) self.assertIsNotNone(policy) # attach policy fields = { 'enabled': True, } db_api.cluster_policy_attach(self.ctx, cluster_id, policy.id, fields) binding = db_api.cluster_policy_get(self.ctx, cluster_id, policy.id) self.assertIsNotNone(binding) # now we delete the cluster db_api.cluster_delete(self.ctx, cluster_id) res = db_api.cluster_get(self.ctx, cluster_id) self.assertIsNone(res) # we check the cluster-policy binding binding = db_api.cluster_policy_get(self.ctx, cluster_id, policy.id) self.assertIsNone(binding) # but the policy is not deleted result = db_api.policy_get(self.ctx, policy.id) self.assertIsNotNone(result)
def test_policy_get_all_with_sort_key_are_used(self, mock_paginate): values = { 'policy1': {'enabled': True}, 'policy2': {'enabled': True}, 'policy3': {'enabled': True} } # prepare for key in values: value = values[key] policy_id = self.create_policy(id=key).id db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy_id, value) sort = consts.CLUSTER_POLICY_SORT_KEYS db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort=','.join(sort)) # Check sort_keys used args = mock_paginate.call_args[0] sort.append('id') self.assertEqual(set(sort), set(args[3]))
def test_policy_get_all_with_sort_key_are_used(self, mock_paginate): values = { 'policy1': {'enabled': True}, 'policy2': {'enabled': True}, 'policy3': {'enabled': True} } # prepare for key in values: value = values[key] policy_id = self.create_policy(id=key).id db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy_id, value) sort = consts.CLUSTER_POLICY_SORT_KEYS db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort=','.join(sort)) # Check sort_keys used args = mock_paginate.call_args[0] sort.append('id') self.assertEqual(set(sort), set(args[3]))
def test_policy_get_all_with_sort_key_and_dir(self): values = { 'policy1': {'level': 40, 'priority': 40, 'cooldown': 10, 'enabled': True}, 'policy2': {'level': 30, 'priority': 60, 'cooldown': 20, 'enabled': True}, 'policy3': {'level': 50, 'priority': 10, 'cooldown': 30, 'enabled': False} } # prepare for key in values: value = values[key] policy_id = self.create_policy(id=key).id db_api.cluster_policy_attach(self.ctx, self.cluster.id, policy_id, value) # sorted by level sort_keys = ['level'] results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort_keys=sort_keys) self.assertEqual('policy2', results[0].policy_id) self.assertEqual('policy1', results[1].policy_id) self.assertEqual('policy3', results[2].policy_id) # sorted by priority sort_keys = ['priority'] results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort_keys=sort_keys) self.assertEqual('policy3', results[0].policy_id) self.assertEqual('policy1', results[1].policy_id) self.assertEqual('policy2', results[2].policy_id) # sorted by cooldown sort_keys = ['cooldown'] results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort_keys=sort_keys) self.assertEqual('policy1', results[0].policy_id) self.assertEqual('policy2', results[1].policy_id) self.assertEqual('policy3', results[2].policy_id) # sorted by enabled, the 2nd and 3rd are unpredictable sort_keys = ['enabled'] results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort_keys=sort_keys) self.assertEqual('policy3', results[0].policy_id) # sorted by enabled, the 2nd and 3rd are ordered by priority sort_keys = ['enabled', 'priority'] results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort_keys=sort_keys) self.assertEqual('policy3', results[0].policy_id) self.assertEqual('policy1', results[1].policy_id) self.assertEqual('policy2', results[2].policy_id) # sorted by cooldown, descending sort_keys = ['cooldown'] results = db_api.cluster_policy_get_all(self.ctx, self.cluster.id, sort_keys=sort_keys, sort_dir='desc') self.assertEqual('policy3', results[0].policy_id) self.assertEqual('policy2', results[1].policy_id) self.assertEqual('policy1', results[2].policy_id)