def testImportSchemaMissing(self): schema = '' empty_properties = '' try: properties = yaml.safe_load(empty_properties) schema_validation.Validate(properties, 'schema', 'template', {'wrong_name': schema}) self.fail('Validation should fail') except schema_validation.ValidationErrors as e: self.assertEqual(1, len(e.errors)) self.assertIn("Could not find schema file 'schema'", e.message)
def ImportsRawValidate(raw_properties, schema_name, import_map): """Takes raw properties, calls validate and returns yaml properties.""" properties = yaml.safe_load(raw_properties) return schema_validation.Validate(properties, schema_name, 'template.py', import_map)
def ExpandTemplate(resource, imports, env, validate_schema=False): """Expands a template, calling expansion mechanism based on type. Args: resource: resource object, the resource that contains parameters to the jinja file imports: map from string to string, the map of imported files names and contents env: map from string to string, the map of environment variable names to their values validate_schema: True to run schema validation; False otherwise Returns: The final expanded template Raises: ExpansionError: if there is any error occurred during expansion """ source_file = resource['type'] path = resource['type'] # Look for Template in imports. if source_file not in imports: raise ExpansionError( source_file, 'Unable to find source file %s in imports.' % (source_file)) # source_file could be a short version of the template # say github short name) so we need to potentially map this into # the fully resolvable name. if 'path' in imports[source_file] and imports[source_file]['path']: path = imports[source_file]['path'] resource['imports'] = imports # Populate the additional environment variables. if env is None: env = {} env['name'] = resource['name'] env['type'] = resource['type'] resource['env'] = env schema = source_file + '.schema' if validate_schema and schema in imports: properties = resource['properties'] if 'properties' in resource else {} try: resource['properties'] = schema_validation.Validate( properties, schema, source_file, imports) except schema_validation.ValidationErrors as e: raise ExpansionError(resource['name'], e.message) if path.endswith('jinja') or path.endswith('yaml'): expanded_template = ExpandJinja(source_file, imports[source_file]['content'], resource, imports) elif path.endswith('py'): # This is a Python template. expanded_template = ExpandPython(imports[source_file]['content'], source_file, resource) else: # The source file is not a jinja file or a python file. # This in fact should never happen due to the IsTemplate check above. raise ExpansionError(resource['source'], 'Unsupported source file: %s.' % (source_file)) parsed_template = yaml.safe_load(expanded_template) if parsed_template is None or 'resources' not in parsed_template: raise ExpansionError( resource['type'], 'Template did not return a \'resources:\' field.') return parsed_template