Skip to content

Commit

Permalink
completed issue-201 - validating included yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
JHofman728 committed Jul 7, 2022
1 parent c48c6a5 commit 5a5e884
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
2 changes: 1 addition & 1 deletion ait/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def check_yaml_timestamps(yaml_file_name, cache_name):
return True
except RecursionError as e:
print(f'ERROR: {e}: Infinite loop: check that yaml config files are not looping '
f'back and forth to one another thought the "!include" statements.')
f'back and forth on one another through the "!include" statements.')
return False


Expand Down
58 changes: 29 additions & 29 deletions ait/core/val.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,18 @@ def __init__(self, ymlfile, schemafile):
self._ymlfile = ymlfile
self._schemafile = schemafile
self._ymlproc = None

# Declare variables for testing schemas of nested yaml files
# Get the error handler ready, just in case
self.ehandler = ErrorHandler(ymlfile=self._ymlfile, schemafile=self._schemafile)
# Declare variables used for validating nested yaml files
self.validate_list = []
self.yml_dir = f"{ymlfile.rsplit('/', 1)[0]}/"
self.yml_files_to_validate = self.get_included_files(ymlfile)

# Get the error handler ready, just in case
self.ehandler = ErrorHandler(ymlfile=self._ymlfile, schemafile=self._schemafile)

def get_included_files(self, yml_file):
"""
Make a list of yaml files to test against the schema. The first member
of the list will be the full path to the yml file if it is part of the
file name. The next element will the module yml file (e.g. cmd.yml),
Make a list of yaml files to validate against the schema. The first member
of the list will include the full path to the yaml file.
The next element will the module yml file (e.g. cmd.yml),
followed by the names of any included files.
The assumption is made that all the included yml files are
found in the same directory as the module yml file.
Expand All @@ -400,12 +398,11 @@ def get_included_files(self, yml_file):
Returns
-------
self.validate_list: list
A list of all files that are required to be validated.
A set of all files that are to be validated.
"""

# The first yaml file to validate will include a full path
# Any included yaml files will need to have the path added.
# Any included yaml files will have the path added.
if '/' in yml_file:
self.validate_list.append(yml_file)
else:
Expand All @@ -421,16 +418,30 @@ def get_included_files(self, yml_file):
self.get_included_files(included_file_name)
# Check and flag multiple includes
if len(self.validate_list) != len(set(self.validate_list)):
log.info(f'Validate: Duplicate config files in the include tree. {self.validate_list}')
log.info(f'WARNING: Validate: Duplicate config files in the '
f'include tree. {self.validate_list}')
return set(self.validate_list)
except RecursionError as e:
print(f'ERROR: {e}: Infinite loop: check that yaml config files are not looping '
f'back and forth to one another thought the "!include" statements.')
f'back and forth on one another through the "!include" statements.')

def validate_schema(self, messages=None):
"""
Provides schema_val validation for objects that do not override
in domain-specific validator (evr.yaml, table.yaml, limits.yaml)
Returns
-------
valid boolean:
The result of the schema test (True(Pass)/False(Fail))
"""
valid = self.schema_val(messages)
return valid

def validate(self, ymldata=None, messages=None):
"""
Validates the Command Dictionary definitions
The method will validate module (e.g. tlm.yaml) and included yaml config files
Validates the Command or Telemetry Dictionary definitions
The method will validate module (cmd.yml or tlm.yaml) and included yaml config files
Returns
-------
Expand All @@ -439,7 +450,8 @@ def validate(self, ymldata=None, messages=None):
content_val boolean:
Results of the schema test on config and included files
For a True, all the tested yaml files must pass the schema test
True - all the tested yaml files passed the schema test
False - one or more yaml files did not pass the schema test
"""
content_val_list = []
Expand All @@ -457,14 +469,6 @@ def validate(self, ymldata=None, messages=None):

return schema_val and content_val

# def validate(self, messages=None):
# """
# Provides schema_val validation for objects that do not override
# in domain-specific validator
# """
# valid = self.schema_val(messages)
# return valid

def schema_val(self, messages=None):
"""Perform validation with processed YAML and Schema"""
self._ymlproc = YAMLProcessor(self._ymlfile)
Expand Down Expand Up @@ -515,7 +519,7 @@ def schema_val(self, messages=None):
return valid

def content_val(self, yaml_file, ymldata=None, messages=None):
""" temp"""
"""Simple base content_val method - needed for unit tests"""
self._ymlproc = YAMLProcessor(yaml_file, False)


Expand Down Expand Up @@ -549,7 +553,6 @@ def content_val(self, yaml_file, ymldata=None, messages=None):
rules = []

# set the command rules
#
# set uniqueness rule for command names
rules.append(UniquenessRule("name", "Duplicate command name: %s", messages))

Expand All @@ -565,7 +568,6 @@ def content_val(self, yaml_file, ymldata=None, messages=None):
argrules = []

# set rules for command arguments
#
# set uniqueness rule for opcodes
argrules.append(
UniquenessRule(
Expand Down Expand Up @@ -674,7 +676,6 @@ def content_val(self, yaml_file, ymldata=None, messages=None):
rules = []

# set the packet rules
#
# set uniqueness rule for packet names
rules.append(UniquenessRule("name", "Duplicate packet name: %s", messages))

Expand All @@ -689,7 +690,6 @@ def content_val(self, yaml_file, ymldata=None, messages=None):
fldrules = []

# set rules for telemetry fields
#
# set uniqueness rule for field name
fldrules.append(
UniquenessRule(
Expand Down
26 changes: 14 additions & 12 deletions tests/ait/core/test_val.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ def test_procces_with_single_doc(self, pretty_mock):
assert pretty_mock.called
pretty_mock.assert_called_with(1, 3, error, messages)

def validate_schema(args):
msgs = []

validator = val.Validator(*args)
v = validator.validate_schema(messages=msgs)
return msgs, v


def validate(args):
msgs = []
Expand All @@ -227,10 +234,8 @@ def cmdval(args):

def tlmval(args):
msgs = []

validator = val.TlmValidator(*args)
v = validator.validate(messages=msgs)

return msgs, v


Expand Down Expand Up @@ -266,14 +271,14 @@ def testValidatorCmd():
msgs = []

# test successful validation
msgs, v = validate(
msgs, v = validate_schema(
[os.path.join(DATA_PATH, "testValidCmd1.yaml"), cmd.getDefaultSchema()]
)
assert v
assert len(msgs) == 0

# test failed validation
msgs, v = validate(
msgs, v = validate_schema(
[os.path.join(DATA_PATH, "testInvalidCmd1.yaml"), cmd.getDefaultSchema()]
)

Expand Down Expand Up @@ -412,11 +417,8 @@ def testTlmValidator():
)

assert not v
# check this there are are 3 msgs
print(f'MESSAGE: {msgs}')
print(f'LEN: {len(msgs)}')

assert len(msgs) == 1
# Verify a new message has been added
assert len(msgs) == 3
assert "Missing value for desc." in msgs[0]


Expand All @@ -440,7 +442,7 @@ def testEvrValidation():
# Validation test of current telemetry dictionary
yml = ait.config.evrdict.filename
schema = os.path.join(os.path.dirname(yml), evr.getDefaultSchema())
msgs, v = validate([yml, schema])
msgs, v = validate_schema([yml, schema])
dispmsgs(msgs)
assert v
assert len(msgs) == 0
Expand All @@ -450,7 +452,7 @@ def testTableValidation():
# Validation test of current table configuration
yml = ait.config.table.filename
schema = pkg_resources.resource_filename("ait.core", "data/table_schema.json")
msgs, v = validate([yml, schema])
msgs, v = validate_schema([yml, schema])
dispmsgs(msgs)
assert v
assert len(msgs) == 0
Expand All @@ -460,7 +462,7 @@ def testLimitsValidation():
# Validation test of current table configuration
yml = ait.config.limits.filename
schema = pkg_resources.resource_filename("ait.core", "data/limits_schema.json")
msgs, v = validate([yml, schema])
msgs, v = validate_schema([yml, schema])
dispmsgs(msgs)
assert v
assert len(msgs) == 0

0 comments on commit 5a5e884

Please sign in to comment.