Exemplo n.º 1
0
    def generate_invalid_tests_expected(self, schema_id):
        """Generate expected invalid test results for a given schema.

        Args:
            schema_id (str): unique identifier of a schema
        """
        # TODO: Refactor this into a method. Exists in multiple places
        schema = self.schemas.get(schema_id, None)
        if schema is None:
            raise ValueError(f"Could not find schema ID {schema_id}")

        invalid_test_dir = self._get_test_dir_absolute(test_type="invalid",
                                                       schema_id=schema_id)
        test_dirs = next(os.walk(invalid_test_dir))[1]

        # For each test, load the data file, test the data against the schema and save the results
        for test_dir in test_dirs:

            data_file = find_file(
                os.path.join(invalid_test_dir, test_dir, "data"))

            if not data_file:
                warn("Could not find data file {}".format(
                    os.path.join(invalid_test_dir, test_dir, "data")))

            data = load_file(data_file)
            results = schema.validate_to_dict(data)
            self._ensure_results_invalid(results, data_file)

            result_file = os.path.join(invalid_test_dir, test_dir,
                                       "results.yml")
            dump_data_to_yaml({"results": results}, result_file)
            print(f"Generated/Updated results file: {result_file}")
Exemplo n.º 2
0
    def test_schema_valid(self, schema_id, strict=False):
        """Execute all valid tests for a given schema.

        Args:
            schema_id (str): The unique identifier of a schema.

        Returns:
            list of ValidationResult.
        """
        schema = self.schemas[schema_id]

        valid_test_dir = self._get_test_dir_absolute(test_type="valid",
                                                     schema_id=schema_id)

        valid_files = find_files(
            file_extensions=[".yaml", ".yml", ".json"],
            search_directories=[valid_test_dir],
            excluded_filenames=[],
            return_dir=True,
        )

        results = []

        for root, filename in valid_files:

            test_data = load_file(os.path.join(root, filename))

            for result in schema.validate(test_data, strict=strict):
                result.instance_name = filename
                result.instance_location = root
                result.instance_type = "TEST"
                results.append(result)

        return results
Exemplo n.º 3
0
    def get_content(self):
        """Return the content of the instance file in structured format.

        Content returned can be either dict or list depending on the content of the file

        Returns:
            dict or list: Content of the instance file.
        """
        return load_file(os.path.join(self.full_path, self.filename))
Exemplo n.º 4
0
    def create_schema_from_file(self, root, filename):  # pylint: disable=no-self-use
        """Create a new JsonSchema object for a given file.

        Load the content from disk and resolve all JSONRef within the schema file.

        Args:
            root (string): Absolute location of the file in the filesystem.
            filename (string): Name of the file.

        Returns:
            JsonSchema: JsonSchema object newly created.
        """
        file_data = load_file(os.path.join(root, filename))

        # TODO Find the type of Schema based on the Type, currently only jsonschema is supported
        # schema_type = "jsonschema"
        base_uri = f"file:{root}/"
        schema_full = jsonref.JsonRef.replace_refs(file_data,
                                                   base_uri=base_uri,
                                                   jsonschema=True,
                                                   loader=load_file)
        return JsonSchema(schema=schema_full, filename=filename, root=root)
Exemplo n.º 5
0
def strict_invalid_instance_data():
    """Invalid instance data when strict mode is used. Loaded from YAML file."""
    return load_file(os.path.join(FIXTURES_DIR, "hostvars", "eng-london-rt1", "dns.yml"))
Exemplo n.º 6
0
def invalid_instance_data():
    """Invalid instance data loaded from YAML file."""
    return load_file(os.path.join(FIXTURES_DIR, "hostvars", "can-vancouver-rt1", "dns.yml"))
Exemplo n.º 7
0
def valid_instance_data():
    """Valid instance data loaded from YAML file."""
    return load_file(os.path.join(FIXTURES_DIR, "hostvars", "chi-beijing-rt1", "dns.yml"))
Exemplo n.º 8
0
# pylint: disable=redefined-outer-name
"""Tests to validate functions defined in jsonschema.py"""
import os

import pytest

from schema_enforcer.schemas.jsonschema import JsonSchema
from schema_enforcer.validation import RESULT_PASS, RESULT_FAIL
from schema_enforcer.utils import load_file

FIXTURES_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "fixtures", "test_jsonschema")
LOADED_SCHEMA_DATA = load_file(os.path.join(FIXTURES_DIR, "schema", "schemas", "dns.yml"))
LOADED_INSTANCE_DATA = load_file(os.path.join(FIXTURES_DIR, "hostvars", "chi-beijing-rt1", "dns.yml"))


@pytest.fixture
def schema_instance():
    """JSONSchema schema instance."""
    schema_instance = JsonSchema(
        schema=LOADED_SCHEMA_DATA, filename="dns.yml", root=os.path.join(FIXTURES_DIR, "schema", "schemas"),
    )
    return schema_instance


@pytest.fixture
def valid_instance_data():
    """Valid instance data loaded from YAML file."""
    return load_file(os.path.join(FIXTURES_DIR, "hostvars", "chi-beijing-rt1", "dns.yml"))


@pytest.fixture
Exemplo n.º 9
0
    def test_schema_invalid(self, schema_id):  # pylint: disable=too-many-locals
        """Execute all invalid tests for a given schema.

        - Acquire structured data to be validated against a given schema. Do this by searching for a file named
          "data.yml", "data.yaml", or "data.json" within a directory of hierarchy
          "./<test_directory>/<schema_id>/invalid/"
        - Acquire results expected after data is validated against a giveh schema. Do this by searching for a file
          named "results.yml", "results.yaml", or "results.json" within a directory of hierarchy
          "./<test_directory>/<schema_id>/results/"
        - Validate expected results match actual results of data after it is checked for adherence to schema.

        Args:
            schema_id (str): The unique identifier of a schema.

        Returns:
            list of ValidationResult objects.
        """
        schema = self.schemas.get(schema_id, None)

        if schema is None:
            raise ValueError(f"Could not find schema ID {schema_id}")

        invalid_test_dir = self._get_test_dir_absolute(test_type="invalid",
                                                       schema_id=schema_id)
        test_dirs = next(os.walk(invalid_test_dir))[1]

        results = []
        for test_dir in test_dirs:
            data_file = find_file(
                os.path.join(invalid_test_dir, test_dir, "data"))
            expected_results_file = find_file(
                os.path.join(invalid_test_dir, test_dir, "results"))

            if not data_file:
                warn("Could not find data file {}. Skipping...".format(
                    os.path.join(invalid_test_dir, test_dir, "data")))
                continue

            if not expected_results_file:
                warn("Could not find expected_results_file {}. Skipping...".
                     format(os.path.join(invalid_test_dir, test_dir,
                                         "results")))
                continue

            data = load_file(data_file)
            expected_results = load_file(expected_results_file)

            tmp_results = schema.validate_to_dict(data)

            # Currently the expected results are using OrderedDict instead of Dict
            # the easiest way to remove that is to dump into JSON and convert back into a "normal" dict
            expected_results = json.loads(
                json.dumps(expected_results["results"]))

            results_sorted = sorted(tmp_results,
                                    key=lambda i: i.get("message", ""))
            expected_results_sorted = sorted(
                expected_results, key=lambda i: i.get("message", ""))

            params = dict(schema_id=schema_id,
                          instance_type="TEST",
                          instance_name=test_dir,
                          instance_location=invalid_test_dir)
            if results_sorted != expected_results_sorted:
                params["result"] = RESULT_FAIL
                params[
                    "message"] = "Invalid test results do not match expected test results from {}".format(
                        expected_results_file)
            else:
                params["result"] = RESULT_PASS

            val = ValidationResult(**params)
            results.append(val)

        return results  # [ ValidationResult(**result) for result in results ]