def test_complex_policy(self): tests = koji.policy.findSimpleTests(koji.policy.__dict__) data = {} policy = ''' # This is a comment in the test policy #^blank line # commented test && true :: some result # First some rules that should never match false :: ERROR none :: ERROR true !! ERROR all !! ERROR false && true && true :: ERROR none && true && true :: ERROR has NOSUCHFIELD :: ERROR # nesting has DEPTH :: { match DEPTH 1 :: 1 all :: { match DEPTH 2 :: 2 all :: { match DEPTH 3 :: 3 all :: { match DEPTH 4 :: 4 all :: END } } } } ''' lines = policy.splitlines() for depth in ['1', '2', '3', '4']: data = {'DEPTH': depth} obj = koji.policy.SimpleRuleSet(lines, tests) action = obj.apply(data) self.assertEqual(action, depth) data = {'DEPTH': '99'} obj = koji.policy.SimpleRuleSet(lines, tests) action = obj.apply(data) self.assertEqual(action, 'END') actions = set(obj.all_actions()) self.assertEquals(actions, set(['1', '2', '3', '4', 'ERROR', 'END']))
def test_last_rule(self): tests = koji.policy.findSimpleTests(koji.policy.__dict__) data = {} # no match rules = ['none :: allow'] obj = koji.policy.SimpleRuleSet(rules, tests) self.assertEquals(obj.last_rule(), None) action = obj.apply(data) self.assertEquals(obj.last_rule(), '(no match)') # simple rule rules = ['all :: allow'] obj = koji.policy.SimpleRuleSet(rules, tests) action = obj.apply(data) self.assertEquals(obj.last_rule(), rules[0]) # negate rule rules = ['none !! allow'] obj = koji.policy.SimpleRuleSet(rules, tests) action = obj.apply(data) self.assertEquals(obj.last_rule(), rules[0]) # nested rule policy = ''' all :: { all :: { all :: allow } } ''' rules = policy.splitlines() obj = koji.policy.SimpleRuleSet(rules, tests) action = obj.apply(data) expected = 'all :: ... all :: ... all :: allow' self.assertEquals(obj.last_rule(), expected)