From 711d4eae913f34e78aa10e8cd511f3b8e43cd54f Mon Sep 17 00:00:00 2001 From: Andrew Gauld Date: Tue, 28 Jul 2020 16:41:43 +0000 Subject: Pull JSON schemas at build/test not run time Change-Id: I4f095a8a80f1c2868319c58bb0e9db916044d067 Issue-ID: DCAEGEN2-2221 Signed-off-by: Andrew Gauld --- mod/onboardingapi/dcae_cli/catalog/mock/schema.py | 43 ++++++++++++---------- .../dcae_cli/catalog/mock/tests/test_schema.py | 12 ++++-- 2 files changed, 31 insertions(+), 24 deletions(-) (limited to 'mod/onboardingapi/dcae_cli/catalog/mock') diff --git a/mod/onboardingapi/dcae_cli/catalog/mock/schema.py b/mod/onboardingapi/dcae_cli/catalog/mock/schema.py index 640d125..abfd9d5 100644 --- a/mod/onboardingapi/dcae_cli/catalog/mock/schema.py +++ b/mod/onboardingapi/dcae_cli/catalog/mock/schema.py @@ -1,7 +1,7 @@ # ============LICENSE_START======================================================= # org.onap.dcae # ================================================================================ -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -65,16 +65,23 @@ log = get_logger('Schema') class FetchSchemaError(RuntimeError): pass -def _fetch_schema(schema_path): - try: - server_url = cli_config.get_server_url() - return fetch_file_from_web(server_url, schema_path) - except requests.HTTPError as e: - raise FetchSchemaError("HTTP error from fetching schema", e) - except Exception as e: - raise FetchSchemaError("Unexpected error from fetching schema", e) - - +class _Schema: + def __init__(self, path): + self.ret = None + self.path = path + + def get(self): + try: + if self.ret is None: + with open(self.path, 'r') as f: + self.ret = json.loads(f.read()) + return self.ret + except Exception as e: + raise FetchSchemaError("Unexpected error from fetching schema", e) + +component_schema = _Schema('schemas/compspec.json') +dataformat_schema = _Schema('schemas/dataformat.json') + def _safe_dict(obj): '''Returns a dict from a dict or json string''' if isinstance(obj, str): @@ -82,7 +89,7 @@ def _safe_dict(obj): else: return obj -def _validate(fetch_schema_func, schema_path, spec): +def _validate(schema, spec): '''Validate the given spec Fetch the schema and then validate. Upon a error from fetching or validation, @@ -100,16 +107,12 @@ def _validate(fetch_schema_func, schema_path, spec): Nothing, silence is golden ''' try: - schema = fetch_schema_func(schema_path) - validate(_safe_dict(spec), schema) + validate(_safe_dict(spec), schema.get()) except ValidationError as e: reraise_with_msg(e, as_dcae=True) except FetchSchemaError as e: reraise_with_msg(e, as_dcae=True) -_validate_using_nexus = partial(_validate, _fetch_schema) - - def apply_defaults(properties_definition, properties): """Utility method to enforce expected defaults @@ -157,7 +160,7 @@ def apply_defaults_docker_config(config): """ # Apply health check defaults healthcheck_type = config["healthcheck"]["type"] - component_spec = _fetch_schema(cli_config.get_path_component_spec()) + component_spec = component_schema.get() if healthcheck_type in ["http", "https"]: apply_defaults_func = partial(apply_defaults, @@ -174,7 +177,7 @@ def apply_defaults_docker_config(config): return config def validate_component(spec): - _validate_using_nexus(cli_config.get_path_component_spec(), spec) + _validate(component_schema, spec) # REVIEW: Could not determine how to do this nicely in json schema. This is # not ideal. We want json schema to be the "it" for validation. @@ -188,4 +191,4 @@ def validate_component(spec): def validate_format(spec): path = cli_config.get_path_data_format() - _validate_using_nexus(path, spec) + _validate(dataformat_schema, spec) diff --git a/mod/onboardingapi/dcae_cli/catalog/mock/tests/test_schema.py b/mod/onboardingapi/dcae_cli/catalog/mock/tests/test_schema.py index 90674d9..9270268 100644 --- a/mod/onboardingapi/dcae_cli/catalog/mock/tests/test_schema.py +++ b/mod/onboardingapi/dcae_cli/catalog/mock/tests/test_schema.py @@ -1,7 +1,7 @@ # ============LICENSE_START======================================================= # org.onap.dcae # ================================================================================ -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -394,6 +394,8 @@ def test_validate(): good_path = "/correct_path" + goodschema = schema._Schema(good_path) + goodschema.ret = fake_schema def fetch_schema(path): if path == good_path: return fake_schema @@ -404,18 +406,20 @@ def test_validate(): good_instance = { "foo": "hello", "bar": 1776 } - schema._validate(fetch_schema, good_path, good_instance) + schema._validate(goodschema, good_instance) # Error from validating bad_instance = {} with pytest.raises(DcaeException): - schema._validate(fetch_schema, good_path, bad_instance) + schema._validate(goodschema, bad_instance) # Error from fetching bad_path = "/wrong_path" + badschema = schema._Schema(bad_path) + with pytest.raises(DcaeException): - schema._validate(fetch_schema, bad_path, good_instance) + schema._validate(badschema, good_instance) -- cgit 1.2.3-korg