def test_versioned_cloud_config_schema_is_valid_json( self, schema, error_msg): version_schemafile = os.path.join(get_schema_dir(), VERSIONED_USERDATA_SCHEMA_FILE) version_schema = json.loads(load_file(version_schemafile)) # To avoid JSON resolver trying to pull the reference from our # upstream raw file in github. version_schema["$id"] = f"file://{version_schemafile}" if error_msg: with pytest.raises(SchemaValidationError) as context_mgr: try: validate_cloudconfig_schema(schema, schema=version_schema, strict=True) except jsonschema.exceptions.RefResolutionError: full_path_schema = self._relative_ref_to_local_file_path( version_schema) validate_cloudconfig_schema(schema, schema=full_path_schema, strict=True) assert error_msg in str(context_mgr.value) else: try: validate_cloudconfig_schema(schema, schema=version_schema, strict=True) except jsonschema.exceptions.RefResolutionError: full_path_schema = self._relative_ref_to_local_file_path( version_schema) validate_cloudconfig_schema(schema, schema=full_path_schema, strict=True)
def _relative_ref_to_local_file_path(self, source_schema): """Replace known relative ref URLs with full file path.""" # jsonschema 2.6.0 doesn't support relative URLs in $refs (bionic) full_path_schema = deepcopy(source_schema) relative_ref = full_path_schema["oneOf"][0]["allOf"][1]["$ref"] full_local_filepath = get_schema_dir() + relative_ref[1:] file_ref = f"file://{full_local_filepath}" full_path_schema["oneOf"][0]["allOf"][1]["$ref"] = file_ref return full_path_schema
def test_versioned_cloud_config_schema_is_valid_json( self, schema, error_msg): schema_dir = get_schema_dir() version_schemafile = os.path.join(schema_dir, VERSIONED_USERDATA_SCHEMA_FILE) # Point to local schema files avoid JSON resolver trying to pull the # reference from our upstream raw file in github. version_schema = json.loads( re.sub( r"https:\/\/raw.githubusercontent.com\/canonical\/" r"cloud-init\/main\/cloudinit\/config\/schemas\/", f"file://{schema_dir}/", load_file(version_schemafile), )) if error_msg: with pytest.raises(SchemaValidationError) as context_mgr: validate_cloudconfig_schema(schema, schema=version_schema, strict=True) assert error_msg in str(context_mgr.value) else: validate_cloudconfig_schema(schema, schema=version_schema, strict=True)