From a3011bf787a5a24333b7cbdfaf9d02d1d91ba411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Jeanneret?= Date: Thu, 4 Apr 2024 21:21:05 +0200 Subject: [PATCH] Add new CI validation: ensure files exist Since we now ensure the automation schema is valid, we also want to ensure linked directories and files exist in the various automation scenarios. This new script will load any YAML file nested in the automation/vars directory, and ensures all the `path` and `src_file` are present. --- .ci/validate-schema-paths.py | 46 ++++++++++++++++++++++++ .github/workflows/automation-schema.yaml | 16 +++++++++ 2 files changed, 62 insertions(+) create mode 100644 .ci/validate-schema-paths.py diff --git a/.ci/validate-schema-paths.py b/.ci/validate-schema-paths.py new file mode 100644 index 000000000..94b18c31b --- /dev/null +++ b/.ci/validate-schema-paths.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +import glob +import pathlib +import yaml + + +class TestSchema(): + def __init__(self, automation_dir): + cur_path = pathlib.Path(__file__).parent + + self.__autodir = automation_dir + self.__src_dir = pathlib.Path(cur_path, '../', self.__autodir) + self.__files = self.__src_dir.glob('*.yaml') + + def run(self): + for f in self.__files: + self.__run_file(f) + + def __run_file(self, f): + rel = pathlib.Path(self.__autodir, f.name) + print(f'Checking scenario file: {rel}') + with open(f, 'r') as fh: + content = yaml.safe_load(fh) + for scenario in content['vas']: + print(f' Checking scenario: {scenario}') + self.__validate(content['vas'][scenario]) + + def __validate(self, scenario): + for stage in scenario['stages']: + _path = stage['path'] + print(f' Checking path: {_path}', end=' ') + source = pathlib.Path(_path) + assert source.exists(), f'!! {source} does not exist' + assert source.is_dir(), f'!! {source} is not a directory' + print('[OK]') + for val in stage['values']: + f = val['src_file'] + _path = source / f + print(f' Checking source file: {_path}', end=' ') + assert _path.is_file(), f'!! {_path} does not exist' + print('[OK]') + +if __name__ == '__main__': + test = TestSchema('./automation/vars') + test.run() diff --git a/.github/workflows/automation-schema.yaml b/.github/workflows/automation-schema.yaml index a41901093..87b152a79 100644 --- a/.github/workflows/automation-schema.yaml +++ b/.github/workflows/automation-schema.yaml @@ -27,3 +27,19 @@ jobs: - name: Run yamale run: yamale -s .ci/automation-schema.yaml automation/vars/ + + files_exist: + runs-on: ubuntu-latest + needs: # Ensure schema is valid before reading it + - yamale + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + + - name: Run file checker + run: python3 .ci/validate-schema-paths.py