1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00
checktheroads-hyperglass/validate_examples.py

109 lines
2.8 KiB
Python
Raw Normal View History

2020-02-16 19:14:11 -07:00
"""Validate example files."""
# Standard Library
2020-03-21 15:00:37 -07:00
import re
2020-02-16 19:14:11 -07:00
import sys
from pathlib import Path
# Third Party
import yaml
# Project
2020-03-21 15:00:37 -07:00
from hyperglass.util import set_app_path
2020-02-16 19:14:11 -07:00
from hyperglass.configuration.models.params import Params
from hyperglass.configuration.models.routers import Routers
from hyperglass.configuration.models.commands import Commands
2020-02-16 19:20:22 -07:00
EXAMPLES = Path(__file__).parent.parent / "hyperglass" / "examples"
2020-02-16 19:14:11 -07:00
DEVICES = EXAMPLES / "devices.yaml"
COMMANDS = EXAMPLES / "commands.yaml"
MAIN = EXAMPLES / "hyperglass.yaml"
2020-03-21 15:00:37 -07:00
def _uncomment_files():
"""Uncomment out files."""
for file in (MAIN, COMMANDS):
output = []
with file.open("r") as f:
for line in f.readlines():
2020-04-13 01:00:42 -07:00
commented = re.compile(r"^(#\s*#?\s?).*$")
if re.match(commented, line):
output.append(re.sub(r"^#\s*#?\s?$", "", line))
2020-03-21 15:00:37 -07:00
else:
output.append(line)
with file.open("w") as f:
f.write("".join(output))
return True
def _comment_optional_files():
"""Comment out files."""
for file in (MAIN, COMMANDS):
output = []
with file.open("r") as f:
for line in f.readlines():
2020-04-13 01:00:42 -07:00
if not re.match(r"^(#\s*#?\s?).*$|(^\-{3})", line):
2020-03-21 15:00:37 -07:00
output.append("# " + line)
else:
output.append(line)
with file.open("w") as f:
f.write("".join(output))
return True
2020-02-16 19:14:11 -07:00
def _validate_devices():
with DEVICES.open() as raw:
devices_dict = yaml.safe_load(raw.read()) or {}
try:
Routers._import(devices_dict.get("routers", []))
except Exception as e:
raise ValueError(str(e))
return True
def _validate_commands():
with COMMANDS.open() as raw:
commands_dict = yaml.safe_load(raw.read()) or {}
try:
Commands.import_params(commands_dict)
except Exception as e:
raise ValueError(str(e))
return True
def _validate_main():
with MAIN.open() as raw:
main_dict = yaml.safe_load(raw.read()) or {}
try:
Params(**main_dict)
except Exception as e:
2020-03-21 15:00:37 -07:00
raise
2020-02-16 19:14:11 -07:00
raise ValueError(str(e))
return True
def validate_all():
"""Validate all example configs against configuration models."""
2020-03-21 15:00:37 -07:00
_uncomment_files()
2020-02-16 19:14:11 -07:00
for validator in (_validate_main, _validate_commands, _validate_devices):
try:
validator()
except ValueError as e:
raise RuntimeError(str(e))
return True
if __name__ == "__main__":
2020-04-16 00:26:40 -07:00
set_app_path(required=True)
2020-02-16 19:14:11 -07:00
try:
all_passed = validate_all()
message = "All tests passed"
status = 0
except RuntimeError as e:
message = str(e)
status = 1
2020-03-21 15:00:37 -07:00
if status == 0:
_comment_optional_files()
2020-02-16 19:14:11 -07:00
print(message)
sys.exit(status)