#!/usr/bin/env python3

#
# Development Order #3:
# 
# This file will determine if this tool can run a test based on a test spec.
#
# Be sure to edit line 19, inserting the names of the tests the tool
# should be compatible with.
# 

# exit statuses should be different based on error

import pscheduler

json = pscheduler.json_load(exit_on_error=True);

try:
    if json['type'] != 'latency':
        pscheduler.succeed_json({
            "can-run": False,
            "reasons": [ "Unsupported test type" ]
        })
except KeyError:
    pscheduler.succeed_json({
        "can-run": False,
        "reasons": [ "Missing test type" ]
    })

spec = json['spec']
SUPPORTED_OPTIONS = ["schema",
                     "source", "source-node", "dest", "dest-node",
                     "protocol", "packet-count", "packet-interval",
                     "packet-timeout", "packet-padding",
                     "ip-tos", "ip-version",
                     "bucket-width"
                     ]

errors = []

for option in spec:
    if option not in SUPPORTED_OPTIONS:
        errors.append(f"halfping does not support {option}")

try:
    protocol = spec['protocol']
    if protocol != 'icmp':
        errors.append(f'Protocol "{protocol}" is not supported.')
except KeyError:
    pass

result = {"can-run": len(errors) == 0}
if len(errors) > 0:
    result["reasons"] = errors

pscheduler.succeed_json(result)
