Ejemplo n.º 1
0
    def test_failure(self, mock_parseString, mock_getLogger):
        result = parser.parse_rule('test', 'rule text')

        self.assertEqual(result, Instructions([Constant(False), set_authz]))
        mock_parseString.assert_called_once_with('rule text', parseAll=True)
        mock_getLogger.assert_called_once_with('policies')
        mock_getLogger.return_value.assert_has_calls([
            mock.call.warn("Failed to parse rule 'test': "
                           "trial error (at char 5), (line:1, col:6)"),
            mock.call.warn("Rule line: test rule string"),
            mock.call.warn("Location :      ^"),
        ])
Ejemplo n.º 2
0
    def instructions(self):
        """
        Retrieve the instructions for the rule.
        """

        if self._instructions is None:
            # Compile the rule into an Instructions instance; we do
            # this lazily to amortize the cost of the compilation,
            # then cache that result for efficiency...
            self._instructions = parser.parse_rule(self.name, self.text)

        return self._instructions
Ejemplo n.º 3
0
    def test_evaluation(self):
        errors = 0
        for text, variables, expected in self.rules:
            try:
                insts = parser.parse_rule("test", text, do_raise=True)
            except pyparsing.ParseException as exc:
                # Print out a description of the unexpected failure
                print('')
                print("Failure to parse %r: %s" % (text, exc))
                print("  Line    : %s" % exc.line)
                print("  Location: %s^" % (" " * (exc.col - 1)))
                errors += 1
                continue

            # Allocate a context and inhibit error reporting
            ctxt = policy.PolicyContext(None, {}, variables)
            ctxt.reported = True

            try:
                with ctxt.push_rule('test'):
                    insts(ctxt, True)
            except Exception as exc:
                # Print out a description of the unexpected failure
                print('')
                print("Failure to evaluate %r: %s" % (text, exc))
                errors += 1
                continue

            # Compare the expected to the actual
            if ctxt.stack[-1] != expected:
                print('')
                print("Failure to evaluate %r: %r != %r" %
                      (text, ctxt.stack[-1], expected))
                errors += 1

        if errors > 0:
            self.fail("Evaluation failures encountered; see output "
                      "for information")
Ejemplo n.º 4
0
    def test_parse(self):
        errors = 0
        for text, expected in self.rules:
            try:
                result = parser.parse_rule("test", text, do_raise=True)
            except pyparsing.ParseException as exc:
                if expected is not None:
                    # Print out a description of the unexpected failure
                    print('')
                    print("Failure to parse %r: %s" % (text, exc))
                    print("  Line    : %s" % exc.line)
                    print("  Location: %s^" % (" " * (exc.col - 1)))
                    errors += 1
                continue

            # Compare the expected to the actual
            if result != expected:
                print('')
                print("Failure to parse %r: %r != %r" %
                      (text, result, expected))
                errors += 1

        if errors > 0:
            self.fail("Parse failures encountered; see output for information")
Ejemplo n.º 5
0
    def test_success(self, mock_parseString, mock_getLogger):
        result = parser.parse_rule('test', 'rule text')

        self.assertEqual(result, 'success')
        mock_parseString.assert_called_once_with('rule text', parseAll=True)
        self.assertFalse(mock_getLogger.called)