def test_delete_policy(self): db_library_policies.delete_policy('policy1') policy1 = db_library_policies.add_policy({ 'name': 'policy1', 'abbreviation': 'abbr', 'kind': 'database', 'description': 'descrip', 'rules': [{ 'rule': 'p(x) :- q(x)', 'comment': 'test comment', 'name': 'testname' }] }) policy2 = db_library_policies.add_policy({ 'name': 'policy2', 'abbreviation': 'abbr', 'kind': 'database', 'description': 'descrip', 'rules': [{ 'rule': 'p(x) :- q(x)', 'comment': 'test comment', 'name': 'testname' }] }) res = db_library_policies.get_policies() self.assertEqual(len(res), 2) db_library_policies.delete_policy('no_such_policy') res = db_library_policies.get_policies() self.assertEqual(len(res), 2) db_library_policies.delete_policy(policy1['id']) res = db_library_policies.get_policies() self.assertEqual(len(res), 1) db_library_policies.delete_policy(policy2['id']) res = db_library_policies.get_policies() self.assertEqual(len(res), 0)
def test_add_policy(self): res = db_library_policies.add_policy({ 'name': 'policy1', 'abbreviation': 'abbr', 'kind': 'database', 'description': 'descrip', 'rules': [{ 'rule': 'p(x) :- q(x)', 'comment': 'test comment', 'name': 'testname' }] }) self.assertEqual( res.to_dict(include_rules=True), { 'id': res['id'], 'abbreviation': 'abbr', 'kind': 'database', 'name': 'policy1', 'description': 'descrip', 'rules': [{ 'rule': 'p(x) :- q(x)', 'comment': 'test comment', 'name': 'testname' }] })
def create_policy(self, policy_dict): policy_dict = copy.deepcopy(policy_dict) validate_policy_item(policy_dict) policy_name = policy_dict['name'] # check name is valid if not compile.string_is_servicename(policy_name): raise exception.PolicyException( 'name `%s` is not a valid policy name' % policy_name) # make defaults if 'kind' not in policy_dict: policy_dict['kind'] = 'nonrecursive' if 'abbreviation' not in policy_dict: policy_dict['abbreviation'] = policy_name[:5] if 'description' not in policy_dict: policy_dict['description'] = '' try: # Note(thread-safety): blocking call policy = db_library_policies.add_policy(policy_dict=policy_dict) return policy.to_dict() except db_exc.DBError: LOG.exception('Creating a new library policy failed due to ' 'backend database error.') raise
def test_add_policy_duplicate(self): db_library_policies.add_policy({ 'name': 'policy1', 'abbreviation': 'abbr', 'kind': 'database', 'description': 'descrip', 'rules': [] }) self.assertRaises( KeyError, db_library_policies.add_policy, { 'name': 'policy1', 'rules': [{ 'rule': 'p(x) :- q(x)', 'comment': 'test comment', 'name': 'testname' }] })
def test_create_get_policy(self): policy1 = db_library_policies.add_policy({ 'name': 'policy1', 'abbreviation': 'abbr', 'kind': 'database', 'description': 'descrip', 'rules': [{ 'rule': 'p(x) :- q(x)', 'comment': 'test comment', 'name': 'testname' }] }) res = db_library_policies.get_policies() self.assertEqual([p.to_dict(include_rules=True) for p in res], [{ 'id': policy1['id'], 'abbreviation': 'abbr', 'kind': 'database', 'name': 'policy1', 'description': 'descrip', 'rules': [{ 'rule': 'p(x) :- q(x)', 'comment': 'test comment', 'name': 'testname' }] }]) res = db_library_policies.get_policy(policy1['id']) self.assertEqual( res.to_dict(include_rules=True), { 'id': policy1['id'], 'abbreviation': 'abbr', 'kind': 'database', 'name': 'policy1', 'description': 'descrip', 'rules': [{ 'rule': 'p(x) :- q(x)', 'comment': 'test comment', 'name': 'testname' }] }) res = db_library_policies.get_policy_by_name(policy1['name']) self.assertEqual( res.to_dict(include_rules=True), { 'id': policy1['id'], 'abbreviation': 'abbr', 'kind': 'database', 'name': 'policy1', 'description': 'descrip', 'rules': [{ 'rule': 'p(x) :- q(x)', 'comment': 'test comment', 'name': 'testname' }] }) self.assertRaises(KeyError, db_library_policies.get_policy, 'no_such_policy') self.assertRaises(KeyError, db_library_policies.get_policy_by_name, 'no_such_policy')