def testSkuIdOutOfBound(self): config = BASIC_CONFIG.replace('$sku-id: 0', '$sku-id: 0x80000000') with self.assertRaises(jsonschema.ValidationError) as ctx: libcros_schema.ValidateConfigSchema( self._schema, cros_config_schema.TransformConfig(config)) self.assertIn("'sku-id': %i" % 0x80000000, str(ctx.exception)) self.assertIn('is not valid', str(ctx.exception))
def Start(config, filter_name, output, schema): """Transforms and validates a cros config test file for use on the system Applies consistent transforms to covert a source YAML configuration into a JSON file that will be used on the system by cros_config tast tests. Verifies that the file complies with the schema verification rules and performs additional verification checks for config consistency. Args: config: Source config file that will be transformed/verified. filter_name: Device name to filter on. output: Output file that will be generated by the transform. schema: Schema file used to verify the config. """ json_transform = MergeConfig(config, filter_name) if schema is None: schema = default_test_schema with open(schema, 'r') as schema_stream: libcros_schema.ValidateConfigSchema( schema_stream.read(), libcros_schema.FormatJson(json_transform)) if output: with open(output, 'w') as output_stream: # Using print function adds proper trailing newline. print(json_transform, file=output_stream) else: print(json_transform)
def testReferencedNonExistentTemplateVariable(self): config = re.sub(r' *$card: .*', '', BASIC_CONFIG) try: libcros_schema.ValidateConfigSchema( self._schema, cros_config_schema.TransformConfig(config)) except cros_config_schema.ValidationError as err: self.assertIn('Referenced template variable', err.__str__()) self.assertIn('cras-config-dir', err.__str__())
def Main(schema, config, output, filter_build_details=False, gen_c_output_dir=None, configs=None): """Transforms and validates a cros config file for use on the system Applies consistent transforms to covert a source YAML configuration into a JSON file that will be used on the system by cros_config. Verifies that the file complies with the schema verification rules and performs additional verification checks for config consistency. Args: schema: Schema file used to verify the config. config: Source config file that will be transformed/verified. output: Output file that will be generated by the transform. filter_build_details: Whether build only details should be filtered or not. gen_c_output_dir: Output directory for generated C config files. configs: List of source config files that will be transformed/verified. """ if not schema: schema = os.path.join(this_dir, 'cros_config_schema.yaml') # TODO(shapiroc): Remove this once we no longer need backwards compatibility # for single config parameters. if config: configs = [config] full_json_transform = MergeConfigs(configs) json_transform = full_json_transform with open(schema, 'r') as schema_stream: libcros_schema.ValidateConfigSchema(schema_stream.read(), json_transform) ValidateConfig(json_transform) if filter_build_details: json_transform = FilterBuildElements(json_transform) if output: with open(output, 'w') as output_stream: # Using print function adds proper trailing newline. print(json_transform, file=output_stream) else: print(json_transform) if gen_c_output_dir: with open(os.path.join(gen_c_output_dir, MOSYS_OUTPUT_NAME), 'w') \ as output_stream: # Using print function adds proper trailing newline. print(GenerateMosysCBindings(full_json_transform), file=output_stream) h_output, c_output = GenerateEcCBindings(full_json_transform) with open(os.path.join(gen_c_output_dir, EC_OUTPUT_NAME + ".h"), 'w') \ as output_stream: print(h_output, file=output_stream) with open(os.path.join(gen_c_output_dir, EC_OUTPUT_NAME + ".c"), 'w') \ as output_stream: print(c_output, file=output_stream)
def Main(schema, config, output, filter_build_details=False, gen_c_output_dir=None, configfs_output=None, configs=None): """Transforms and validates a cros config file for use on the system Applies consistent transforms to covert a source YAML configuration into a JSON file that will be used on the system by cros_config. Verifies that the file complies with the schema verification rules and performs additional verification checks for config consistency. Args: schema: Schema file used to verify the config. config: Source config file that will be transformed/verified. output: Output file that will be generated by the transform. filter_build_details: Whether build only details should be filtered or not. gen_c_output_dir: Output directory for generated C config files. configfs_output: Output path to generated SquashFS for ConfigFS. configs: List of source config files that will be transformed/verified. """ # TODO(shapiroc): Remove this once we no longer need backwards compatibility # for single config parameters. if config: configs = [config] full_json_transform = MergeConfigs(configs) json_transform = full_json_transform schema_contents = ReadSchema(schema) libcros_schema.ValidateConfigSchema(schema_contents, json_transform) ValidateConfig(json_transform) schema_attrs = libcros_schema.GetSchemaPropertyAttrs( yaml.load(schema_contents, Loader=yaml.SafeLoader)) if filter_build_details: build_only_elements = [] for path in schema_attrs: if schema_attrs[path].build_only_element: build_only_elements.append(path) json_transform = FilterBuildElements(json_transform, build_only_elements) if output: with open(output, 'w') as output_stream: # Using print function adds proper trailing newline. print(json_transform, file=output_stream) else: print(json_transform) if gen_c_output_dir: with open(os.path.join(gen_c_output_dir, MOSYS_OUTPUT_NAME), 'w') \ as output_stream: # Using print function adds proper trailing newline. print(GenerateMosysCBindings(full_json_transform), file=output_stream) if configfs_output: configfs.GenerateConfigFSData(json.loads(json_transform), configfs_output)
def testMissingRequiredElement(self): config = re.sub(r' *cras-config-dir: .*', '', BASIC_CONFIG) config = re.sub(r' *volume: .*', '', BASIC_CONFIG) try: libcros_schema.ValidateConfigSchema( self._schema, cros_config_schema.TransformConfig(config)) except jsonschema.ValidationError as err: self.assertIn('required', err.__str__()) self.assertIn('cras-config-dir', err.__str__())
def testSkuIdOutOfBound(self): config = BASIC_CONFIG.replace('$sku-id: 0', '$sku-id: 0x80000000') with self.assertRaises(jsonschema.ValidationError) as ctx: libcros_schema.ValidateConfigSchema( self._schema, cros_config_schema.TransformConfig(config)) if version.parse(jsonschema.__version__) >= version.Version('3.0.0'): self.assertIn('%i is greater than the maximum' % 0x80000000, str(ctx.exception)) self.assertIn('sku-id', str(ctx.exception)) else: self.assertIn("'sku-id': %i" % 0x80000000, str(ctx.exception)) self.assertIn('is not valid', str(ctx.exception))
def testROVersion(self): config = { 'chromeos': { 'configs': [ { 'identity': { 'platform-name': 'foo', 'sku-id': 1 }, 'name': 'foo', 'fingerprint': { 'board': 'dartmonkey', 'ro-version': '123' } }, ], }, } libcros_schema.ValidateConfigSchema(self._schema, libcros_schema.FormatJson(config))
def testDevices(self): config = { 'chromeos': { 'configs': [ { 'identity': { 'platform-name': 'foo', 'sku-id': 1 }, 'name': 'foo', 'camera': { 'count': 2, 'devices': [ { 'interface': 'usb', 'facing': 'front', 'orientation': 180, 'flags': { 'support-1080p': False, 'support-autofocus': False, }, 'ids': ['0123:abcd', '4567:efef'], }, { 'interface': 'mipi', 'facing': 'back', 'orientation': 0, 'flags': { 'support-1080p': True, 'support-autofocus': True, }, }, ], } }, ], }, } libcros_schema.ValidateConfigSchema(self._schema, libcros_schema.FormatJson(config))
def testInvalidUsbId(self): if version.parse(jsonschema.__version__) < version.Version('3.0.0'): self.skipTest('jsonschema needs upgrade to support conditionals') for invalid_usb_id in ('0123-abcd', '0123:Abcd', '123:abcd'): config = { 'chromeos': { 'configs': [ { 'identity': { 'platform-name': 'foo', 'sku-id': 1 }, 'name': 'foo', 'camera': { 'count': 1, 'devices': [ { 'interface': 'usb', 'facing': 'front', 'orientation': 0, 'flags': { 'support-1080p': False, 'support-autofocus': True, }, 'ids': [invalid_usb_id], }, ], } }, ], }, } with self.assertRaises(jsonschema.ValidationError) as ctx: libcros_schema.ValidateConfigSchema( self._schema, libcros_schema.FormatJson(config)) self.assertIn('%r does not match' % invalid_usb_id, str(ctx.exception))
def testROVersionMissingBoardName(self): config = { 'chromeos': { 'configs': [ { 'identity': { 'platform-name': 'foo', 'sku-id': 1 }, 'name': 'foo', 'fingerprint': { # "ro-version" only allowed if "board" is also specified. 'ro-version': '123' } }, ], }, } with self.assertRaises(jsonschema.exceptions.ValidationError) as ctx: libcros_schema.ValidateConfigSchema( self._schema, libcros_schema.FormatJson(config)) self.assertEqual(ctx.exception.message, "'board' is a dependency of 'ro-version'")
def testBasicSchemaValidation(self): libcros_schema.ValidateConfigSchema( self._schema, cros_config_schema.TransformConfig(BASIC_CONFIG))