|
| 1 | +import os |
| 2 | +import json |
| 3 | +import re |
| 4 | + |
| 5 | + |
| 6 | +def preprocess(i): |
| 7 | + |
| 8 | + env = i['env'] |
| 9 | + |
| 10 | + json_file = env.get('MLC_VALIDATE_JSON_FILE', '') |
| 11 | + if not json_file: |
| 12 | + return {'return': 1, 'error': 'json_file is required (--json_file=<path>)'} |
| 13 | + |
| 14 | + if not os.path.isfile(json_file): |
| 15 | + return {'return': 1, 'error': f'JSON file not found: {json_file}'} |
| 16 | + |
| 17 | + with open(json_file, 'r') as f: |
| 18 | + raw = f.read() |
| 19 | + |
| 20 | + # Extract JSON block from potentially mixed log+json output |
| 21 | + match = re.search(r'\{.*\}', raw, re.DOTALL) |
| 22 | + if not match: |
| 23 | + return {'return': 1, 'error': 'No JSON object found in file'} |
| 24 | + |
| 25 | + try: |
| 26 | + data = json.loads(match.group()) |
| 27 | + except json.JSONDecodeError as e: |
| 28 | + return {'return': 1, 'error': f'Invalid JSON: {e}'} |
| 29 | + |
| 30 | + errors = [] |
| 31 | + |
| 32 | + # Check return code |
| 33 | + expected_return = env.get('MLC_VALIDATE_EXPECTED_RETURN', '') |
| 34 | + if expected_return != '': |
| 35 | + actual = data.get('return') |
| 36 | + if actual != int(expected_return): |
| 37 | + errors.append(f'Expected return={expected_return}, got {actual}') |
| 38 | + |
| 39 | + # Check env keys exist |
| 40 | + env_keys = env.get('MLC_VALIDATE_ENV_KEYS', '') |
| 41 | + if env_keys: |
| 42 | + result_env = data.get('env', {}) |
| 43 | + for key in env_keys.split(','): |
| 44 | + key = key.strip() |
| 45 | + if key and key not in result_env: |
| 46 | + errors.append(f'Expected env key "{key}" not found') |
| 47 | + |
| 48 | + # Check env keys do NOT exist |
| 49 | + not_env_keys = env.get('MLC_VALIDATE_NOT_ENV_KEYS', '') |
| 50 | + if not_env_keys: |
| 51 | + result_env = data.get('env', {}) |
| 52 | + for key in not_env_keys.split(','): |
| 53 | + key = key.strip() |
| 54 | + if key and key in result_env: |
| 55 | + errors.append(f'Env key "{key}" should not be present but was found') |
| 56 | + |
| 57 | + # Check env key=value pairs (format: KEY=VALUE;KEY2=VALUE2) |
| 58 | + env_key_values = env.get('MLC_VALIDATE_ENV_KEY_VALUES', '') |
| 59 | + if env_key_values: |
| 60 | + result_env = data.get('env', {}) |
| 61 | + for pair in env_key_values.split(';'): |
| 62 | + if '=' in pair: |
| 63 | + k, v = pair.split('=', 1) |
| 64 | + k = k.strip() |
| 65 | + v = v.strip() |
| 66 | + actual_v = result_env.get(k) |
| 67 | + if actual_v is None: |
| 68 | + errors.append(f'Expected env key "{k}" not found') |
| 69 | + elif str(actual_v) != v: |
| 70 | + errors.append(f'Expected env {k}="{v}", got "{actual_v}"') |
| 71 | + |
| 72 | + # Check dep tags exist in deps list (semicolon-separated groups) |
| 73 | + dep_tags = env.get('MLC_VALIDATE_DEP_TAGS', '') |
| 74 | + if dep_tags: |
| 75 | + deps = data.get('deps', []) |
| 76 | + all_dep_tags = [d.get('tags', '') for d in deps] |
| 77 | + for tag_set in dep_tags.split(';'): |
| 78 | + tag_set = tag_set.strip() |
| 79 | + if tag_set and not any(tag_set in t for t in all_dep_tags): |
| 80 | + errors.append(f'Expected dep tags "{tag_set}" not found in deps: {all_dep_tags}') |
| 81 | + |
| 82 | + if errors: |
| 83 | + error_msg = 'JSON validation failed:\n' + '\n'.join(f' - {e}' for e in errors) |
| 84 | + return {'return': 1, 'error': error_msg} |
| 85 | + |
| 86 | + print('JSON validation PASSED') |
| 87 | + return {'return': 0} |
| 88 | + |
| 89 | + |
| 90 | +def postprocess(i): |
| 91 | + return {'return': 0} |
0 commit comments