class LibDocLib(object): def __init__(self, interpreter=None): self.interpreter = interpreter self.schema = XMLSchema(join(ROOT, 'doc', 'schema', 'libdoc.02.xsd')) @property def libdoc(self): return self.interpreter.libdoc @property def encoding(self): return SYSTEM_ENCODING \ if not self.interpreter.is_ironpython else CONSOLE_ENCODING def run_libdoc(self, args): cmd = self.libdoc + self._split_args(args) cmd[-1] = cmd[-1].replace('/', os.sep) logger.info(' '.join(cmd)) result = run(cmd, cwd=join(ROOT, 'src'), stdout=PIPE, stderr=STDOUT, encoding=self.encoding, timeout=120, universal_newlines=True) logger.info(result.stdout) return result.stdout def _split_args(self, args): lexer = shlex.shlex(args, posix=True) lexer.escape = '' lexer.whitespace_split = True return list(lexer) def get_libdoc_model_from_html(self, path): with open(path, encoding='UTF-8') as html_file: model_string = self._find_model(html_file) model = json.loads(model_string.replace('\\x3c/', '</')) logger.info(pprint.pformat(model)) return model def _find_model(self, html_file): for line in html_file: if line.startswith('libdoc = '): return line.split('=', 1)[1].strip(' \n;') raise RuntimeError('No model found from HTML') def validate_spec(self, path): self.schema.validate(path) def relative_source(self, path, start): if not exists(path): return path try: return relpath(path, start) except ValueError: return normpath(path)
def validate_single_file(file_to_test: ConnectorFile, path_to_file: Path, xml_violations_buffer: List[str], properties: ConnectorProperties) -> bool: """ Arguments: file_to_test {ConnectorFile} -- path to a single file to test path_to_file {Path} -- path to the file xml_violations_buffer {list[str]} -- a list of strings that holds the xml violation messages properties {ConnectorProperties} -- an object contating properties that apply to the entire connector Returns: bool -- True if the xml file passes validation, false if it does not or there is an error Any xml violation messages will be appended to xml_violations_buffer """ logger.debug("Validating " + str(path_to_file)) if file_to_test.file_type == 'connection-dialog': properties.uses_tcd = True xsd_file = get_xsd_file(file_to_test) if not xsd_file: xml_violations_buffer.append("Error: No valid XSD for file type:" + file_to_test.file_type) return False manifest_schema = XMLSchema(str(PATH_TO_XSD_FILES / Path(xsd_file))) # If the file is too big, we shouldn't try and parse it, just log the violation and move on if path_to_file.stat().st_size > MAX_FILE_SIZE: xml_violations_buffer.append(file_to_test.file_name + " exceeds maximum size of " + str(int(MAX_FILE_SIZE / 1024)) + " KB") return False # Try to validate the xml. If the xml validation error is thrown, save the violation information to the buffer try: manifest_schema.validate(str(path_to_file)) except Exception: saved_error_type = sys.exc_info()[0] saved_error = sys.exc_info()[1] xml_violations_buffer.append("File: " + file_to_test.file_name + " Error Type: " + str(saved_error_type) + "\n" + str(saved_error)) logger.error("XML Validation failed for " + file_to_test.file_name) return False if not validate_file_specific_rules(file_to_test, path_to_file, xml_violations_buffer, properties): logger.error("XML Validation failed for " + file_to_test.file_name) return False return True
def test_exception_repr(self): xs = XMLSchema( os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xsd')) error = XMLSchemaValidatorError(xs, 'unknown error') self.assertEqual(str(error), 'unknown error') self.assertEqual(error.msg, 'unknown error') error = XMLSchemaValidatorError(xs, 'unknown error', elem=xs.root) lines = str(error).split('\n') self.assertEqual(lines[0], 'unknown error:') self.assertEqual(lines[2], 'Schema:') self.assertTrue(lines[4].strip().startswith('<xs:schema ')) self.assertEqual(lines[-2].strip(), '</xs:schema>') schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root" type="xs:integer"/> </xs:schema>""") root = lxml.etree.XML('<root a="10"/>') with self.assertRaises(XMLSchemaValidationError) as ctx: schema.validate(root) lines = str(ctx.exception).split('\n') self.assertEqual( lines[0], "failed validating {'a': '10'} with XsdAttributeGroup():") self.assertEqual(lines[2], "Reason: 'a' attribute not allowed for element.") self.assertEqual(lines[8], "Instance (line 1):") self.assertEqual(lines[12], "Path: /root") self.assertEqual( repr(ctx.exception), "XMLSchemaValidationError(reason=\"'a' " "attribute not allowed for element.\")") error = XMLSchemaValidationError(schema.elements['root'], root) self.assertIsNone(error.reason) self.assertNotIn("Reason:", str(error)) self.assertIn("Schema:", str(error)) error = XMLSchemaValidationError(schema, root) self.assertNotIn("Reason:", str(error)) self.assertNotIn("Schema:", str(error)) error = XMLSchemaValidationError(schema, 10) self.assertEqual( str(error), "failed validating 10 with XMLSchema10(namespace='')")
def parser(self, xml, xsd, pattern=None): """ Validate xml to match with xsd. Calling parsers to get Model from xml. If provided pattern, filtering Model. :param xml: path to MOBILE_API.xml :param xsd: path to MOBILE_API.xsd :param pattern: regex-pattern from command-line arguments to filter element from initial Model :return: initial Model """ self.logger.info( '''Validating XML and generating model with following parameters: Source xml : %s Source xsd : %s''', xml, xsd) try: schema = XMLSchema(xsd) if not schema.is_valid(xml): raise GenerateError(schema.validate(xml)) interface = Parser().parse(xml) except (InterfaceError, XMLSchemaError, GenerateError) as message1: self.logger.critical('Invalid XML file content: %s, %s', xml, message1) sys.exit(1) enum_names = tuple(interface.enums.keys()) struct_names = tuple(interface.structs.keys()) if pattern: intermediate = OrderedDict() intermediate.update({'params': interface.params}) for kind, content in vars(interface).items(): if kind == 'params': continue for name, item in content.items(): if re.match(pattern, item.name): self.logger.info('%s/%s match with %s', kind, item.name, pattern) if kind in intermediate: intermediate[kind].update({name: item}) else: intermediate.update({kind: {name: item}}) interface = Interface(**intermediate) self.logger.debug({ 'enums': tuple(interface.enums.keys()), 'structs': tuple(interface.structs.keys()), 'functions': tuple( map(lambda i: i.function_id.name, interface.functions.values())), 'params': interface.params }) return enum_names, struct_names, interface
def validateFile(validateFilePath: str, schemaPath: str, bcfFile: str): """ Validates `validateFileName` against the XSD file referenced by `schemaPath`. If successful an empty string is returned, else an error string is returned. """ logger.debug("Validating file {} against {}".format( validateFilePath, schemaPath)) schema = XMLSchema(schemaPath) try: schema.validate(validateFilePath) except Exception as e: # get parent directory of file, useful for the user if the file is a # markup.bcf file inside some topic parentDir = os.path.abspath(os.path.join(validateFilePath, os.pardir)) return "{} file inside {} of {} could not be validated against"\ " {}\nError:{}".format(validateFilePath, parentDir, bcfFile, os.path.basename(schemaPath), str(e)) return ""
def test_setattr(self): schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root" type="xs:integer"/> </xs:schema>""") root = ElementTree.XML('<root a="10"/>') with self.assertRaises(XMLSchemaValidationError) as ctx: schema.validate(root) self.assertIsInstance(ctx.exception.source, XMLResource) self.assertFalse(ctx.exception.source.is_lazy()) resource = XMLResource(io.StringIO('<root a="10"/>'), lazy=True) with self.assertRaises(XMLSchemaValidationError) as ctx: schema.validate(resource) self.assertIsInstance(ctx.exception.source, XMLResource) self.assertTrue(ctx.exception.source.is_lazy()) self.assertIsNone(ctx.exception.elem) self.assertEqual(ctx.exception.source, resource) self.assertEqual(ctx.exception.path, '/root')
def test_properties(self): schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root" type="xs:integer"/> </xs:schema>""") root = lxml.etree.XML('<root a="10"/>') with self.assertRaises(XMLSchemaValidationError) as ctx: schema.validate(root) self.assertEqual(ctx.exception.sourceline, 1) self.assertEqual(ctx.exception.root, root) xsd_file = os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xsd') xs = XMLSchema(xsd_file) with self.assertRaises(XMLSchemaValidatorError) as ctx: raise XMLSchemaValidatorError(xs, 'unknown error') self.assertIsNone(ctx.exception.root) self.assertIsNone(ctx.exception.schema_url) self.assertEqual(ctx.exception.origin_url, xs.source.url) self.assertIsNone(XMLSchemaValidatorError(None, 'unknown error').origin_url)
def parse(self, filename, xsd=None): filename = str(filename) if not xsd: if not Path(filename).exists(): raise modelParseError('File not found: ' + filename) replace = filename.replace('.xml', '.xsd') if Path(replace).exists(): xsd = replace else: raise modelParseError('File not found: ' + replace) try: schema = XMLSchema(xsd) if not schema.is_valid(filename): raise modelParseError('Invalid XML file content:\n' + schema.validate(filename)) return super(Parser, self).parse(filename) except xmlParseError as error: raise modelParseError(error)
class TestCheckerLibrary: ROBOT_LIBRARY_SCOPE = 'GLOBAL' def __init__(self): self.schema = XMLSchema('doc/schema/robot.03.xsd') def process_output(self, path, validate=None): set_suite_variable = BuiltIn().set_suite_variable if not path or path.upper() == 'NONE': set_suite_variable('$SUITE', None) logger.info("Not processing output.") return path = path.replace('/', os.sep) if validate is None: validate = os.getenv('ATEST_VALIDATE_OUTPUT', False) if utils.is_truthy(validate): self._validate_output(path) try: logger.info("Processing output '%s'." % path) result = Result(root_suite=NoSlotsTestSuite()) ExecutionResultBuilder(path).build(result) except: set_suite_variable('$SUITE', None) msg, details = utils.get_error_details() logger.info(details) raise RuntimeError('Processing output failed: %s' % msg) result.visit(ProcessResults()) set_suite_variable('$SUITE', result.suite) set_suite_variable('$STATISTICS', result.statistics) set_suite_variable('$ERRORS', result.errors) def _validate_output(self, path): schema_version = self._get_schema_version(path) if schema_version != self.schema.version: raise AssertionError( 'Incompatible schema versions. Schema has `version="%s"` ' 'but output file has `schemaversion="%s"`.' % (self.schema.version, schema_version)) self.schema.validate(path) def _get_schema_version(self, path): with open(path, encoding='UTF-8') as f: for line in f: if line.startswith('<robot'): return re.search(r'schemaversion="(\d+)"', line).group(1) def get_test_case(self, name): suite = BuiltIn().get_variable_value('${SUITE}') return self._get_test_from_suite(suite, name) def _get_test_from_suite(self, suite, name): tests = self.get_tests_from_suite(suite, name) if len(tests) == 1: return tests[0] err = "No test '%s' found from suite '%s'" if not tests \ else "More than one test '%s' found from suite '%s'" raise RuntimeError(err % (name, suite.name)) def get_tests_from_suite(self, suite, name=None): tests = [ test for test in suite.tests if name is None or utils.eq(test.name, name) ] for subsuite in suite.suites: tests.extend(self.get_tests_from_suite(subsuite, name)) return tests def get_test_suite(self, name): suite = BuiltIn().get_variable_value('${SUITE}') suites = self._get_suites_from_suite(suite, name) if len(suites) == 1: return suites[0] err = "No suite '%s' found from suite '%s'" if not suites \ else "More than one suite '%s' found from suite '%s'" raise RuntimeError(err % (name, suite.name)) def _get_suites_from_suite(self, suite, name): suites = [suite] if utils.eq(suite.name, name) else [] for subsuite in suite.suites: suites.extend(self._get_suites_from_suite(subsuite, name)) return suites def check_test_case(self, testname, status=None, message=None): test = self._get_test_from_suite( BuiltIn().get_variable_value('${SUITE}'), testname) self._check_test_status(test, status=status, message=message) return test def _check_test_status(self, test, status=None, message=None): """Verifies that test's status and message are as expected. Expected status and message can be given as parameters. If expected status is not given, expected status and message are read from test's documentation. If documentation doesn't contain any of PASS, FAIL or ERROR, test's status is expected to be PASS. If status is given that is used. Expected message is documentation after given status. Expected message can also be regular expression. In that case expected match starts with REGEXP: , which is ignored in the regexp match. """ if status is not None: test.exp_status = status if message is not None: test.exp_message = message if test.exp_status != test.status: if test.exp_status == 'PASS': if test.status == 'FAIL': msg = f"Error message:\n{test.message}" else: msg = f"Test message:\n{test.message}" else: msg = f"Expected message:\n{test.exp_message}" raise AssertionError( f"Status of '{test.name}' should have been {test.exp_status} " f"but it was {test.status}.\n\n{msg}") if test.exp_message == test.message: return if test.exp_message.startswith('REGEXP:'): pattern = self._get_pattern(test, 'REGEXP:') if re.match('^%s$' % pattern, test.message, re.DOTALL): return if test.exp_message.startswith('GLOB:'): pattern = self._get_pattern(test, 'GLOB:') matcher = utils.Matcher(pattern, caseless=False, spaceless=False) if matcher.match(test.message): return if test.exp_message.startswith('STARTS:'): start = self._get_pattern(test, 'STARTS:') if test.message.startswith(start): return raise AssertionError("Test '%s' had wrong message.\n\n" "Expected:\n%s\n\nActual:\n%s\n" % (test.name, test.exp_message, test.message)) def _get_pattern(self, test, prefix): pattern = test.exp_message[len(prefix):].strip() if not pattern: raise RuntimeError("Empty '%s' is not allowed!") return pattern def should_contain_tests(self, suite, *names, **names_and_statuses): """Verify that specified tests exists in suite. 'names' contains test names to check. These tests are expected to pass/fail as their documentation says. Is same name is given multiple times, test ought to be executed multiple times too. 'names_and_statuses' contains test names with associated custom status in format `STATUS:Message`. Test is given both in 'names' and in 'names_and_statuses', only the latter has an effect. """ tests = self.get_tests_from_suite(suite) expected = [(n, None) for n in names if n not in names_and_statuses] expected.extend((n, s) for n, s in names_and_statuses.items()) tests_msg = "\nExpected tests : %s\nActual tests : %s" \ % (', '.join(sorted([e[0] for e in expected], key=lambda s: s.lower())), ', '.join(sorted([t.name for t in tests], key=lambda s: s.lower()))) if len(tests) != len(expected): raise AssertionError("Wrong number of tests." + tests_msg) for test in tests: logger.info("Verifying test '%s'" % test.name) try: status = self._find_expected_status(test.name, expected) except IndexError: raise AssertionError( "Test '%s' was not expected to be run.%s" % (test.name, tests_msg)) expected.pop(expected.index((test.name, status))) if status and ':' in status: status, message = status.split(':', 1) else: message = None self._check_test_status(test, status, message) assert not expected def _find_expected_status(self, test, expected): for name, status in expected: if name == test: return status raise IndexError def should_not_contain_tests(self, suite, *test_names): actual_names = [t.name for t in suite.tests] for name in test_names: if name in actual_names: raise AssertionError( 'Suite should not have contained test "%s"' % name) def should_contain_suites(self, suite, *expected): logger.info('Suite has suites', suite.suites) expected = sorted(expected) actual = sorted(s.name for s in suite.suites) if len(actual) != len(expected): raise AssertionError("Wrong number of suites.\n" "Expected (%d): %s\n" "Actual (%d): %s" % (len(expected), ', '.join(expected), len(actual), ', '.join(actual))) for name in expected: if not utils.Matcher(name).match_any(actual): raise AssertionError('Suite %s not found' % name) def should_contain_tags(self, test, *tags): logger.info('Test has tags', test.tags) assert_equal(len(test.tags), len(tags), 'Wrong number of tags') tags = sorted( tags, key=lambda s: s.lower().replace('_', '').replace(' ', '')) for act, exp in zip(test.tags, tags): assert_equal(act, exp) def should_contain_keywords(self, item, *kw_names): actual_names = [kw.name for kw in item.kws] assert_equal(len(actual_names), len(kw_names), 'Wrong number of keywords') for act, exp in zip(actual_names, kw_names): assert_equal(act, exp) def test_should_have_correct_keywords(self, *kw_names, **config): get_var = BuiltIn().get_variable_value suite = get_var('${SUITE}') name = config.get('name', get_var('${TEST NAME}')) kw_index = int(config.get('kw_index', 0)) test = self._get_test_from_suite(suite, name) self._check_test_status(test) self.should_contain_keywords(test.body[kw_index], *kw_names) return test def check_log_message(self, item, msg, level='INFO', html=False, pattern=False): b = BuiltIn() matcher = b.should_match if pattern else b.should_be_equal matcher(item.message.rstrip(), msg.rstrip(), 'Wrong log message') b.should_be_equal(item.level, 'INFO' if level == 'HTML' else level, 'Wrong log level') b.should_be_equal(str(item.html), str(html or level == 'HTML'), 'Wrong HTML status')
class LibDocLib: def __init__(self, interpreter=None): self.interpreter = interpreter self.schema = XMLSchema(join(ROOT, 'doc', 'schema', 'libdoc.04.xsd')) @property def libdoc(self): return self.interpreter.libdoc def run_libdoc(self, args): cmd = self.libdoc + self._split_args(args) cmd[-1] = cmd[-1].replace('/', os.sep) logger.info(' '.join(cmd)) result = run(cmd, cwd=join(ROOT, 'src'), stdout=PIPE, stderr=STDOUT, encoding=SYSTEM_ENCODING, timeout=120, universal_newlines=True) logger.info(result.stdout) return result.stdout def _split_args(self, args): lexer = shlex.shlex(args, posix=True) lexer.escape = '' lexer.whitespace_split = True return list(lexer) def get_libdoc_model_from_html(self, path): with open(path, encoding='UTF-8') as html_file: model_string = self._find_model(html_file) model = json.loads(model_string) logger.info(pprint.pformat(model)) return model def _find_model(self, html_file): for line in html_file: if line.startswith('libdoc = '): return line.split('=', 1)[1].strip(' \n;') raise RuntimeError('No model found from HTML') def validate_spec(self, path): self.schema.validate(path) def relative_source(self, path, start): if not exists(path): return path try: return relpath(path, start) except ValueError: return normpath(path) def get_repr_from_arg_model(self, model): return str( ArgInfo(kind=model['kind'], name=model['name'], types=tuple(model['type']), default=model['default'] or ArgInfo.NOTSET)) def get_repr_from_json_arg_model(self, model): return str( ArgInfo(kind=model['kind'], name=model['name'], types=tuple(model['types']), default=model['defaultValue'] or ArgInfo.NOTSET))
def test_children_validation_error(self): schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="a"> <xs:complexType> <xs:sequence> <xs:element name="b1" type="xs:string"/> <xs:element name="b2" type="xs:string"/> <xs:element name="b3" type="xs:string" minOccurs="2" maxOccurs="3"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>""") with self.assertRaises(XMLSchemaChildrenValidationError) as ctx: schema.validate('<a><b1/><b2/><b3/><b3/><b3/><b3/></a>') lines = str(ctx.exception).split('\n') self.assertEqual( lines[2], "Reason: Unexpected child with tag 'b3' at position 6.") self.assertEqual(lines[-2], "Path: /a") with self.assertRaises(XMLSchemaChildrenValidationError) as ctx: schema.validate('<a><b1/><b2/><b3/></a>') lines = str(ctx.exception).split('\n') self.assertEqual( lines[2][:51], "Reason: The content of element 'a' is not complete.") self.assertEqual(lines[-2], "Path: /a") root = ElementTree.XML('<a><b1/><b2/><b2/><b3/><b3/><b3/></a>') validator = schema.elements['a'].type.content_type with self.assertRaises(XMLSchemaChildrenValidationError) as ctx: raise XMLSchemaChildrenValidationError(validator, root, 2, validator[1], 2) lines = str(ctx.exception).split('\n') self.assertTrue( lines[2].endswith("occurs 2 times but the maximum is 1.")) schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="a"> <xs:complexType> <xs:sequence> <xs:element name="b1" type="xs:string"/> <xs:any/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>""") with self.assertRaises(XMLSchemaChildrenValidationError) as ctx: schema.validate('<a><b1/></a>') lines = str(ctx.exception).split('\n') self.assertTrue( lines[2].endswith("Tag from \'##any\' namespace/s expected.")) schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="a"> <xs:complexType> <xs:sequence> <xs:element name="b1" type="xs:string"/> <xs:choice> <xs:any namespace="tns0" processContents="lax"/> <xs:element name="b2" type="xs:string"/> </xs:choice> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>""") with self.assertRaises(XMLSchemaChildrenValidationError) as ctx: schema.validate('<a><b1/></a>') lines = str(ctx.exception).split('\n') self.assertTrue(lines[2].endswith("Tag b2 expected."))
if not self._disk is None: del self._disk # free loop-device del self._lodev ### ### MAIN ### if __name__ == "__main__": # load configuration config = None print(f'Loading configuration ... ', end='') try: schema = XMLSchema('config.xsd') schema.validate('config.xml') config = schema.to_dict('config.xml') print(f'Ok') except XMLSchemaException as error: print(f'Error') print(error) exit(1) # create top-level build directory pwd = os.path.dirname(os.path.realpath(__file__)) if not os.path.isdir(f'{ pwd }/build'): os.mkdir(path=f'{ pwd }/build') # configure kiwi logger = logging.getLogger('kiwi') logger.setLogLevel(logging.INFO)