#!/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.
# 

import datetime
import sys

import pscheduler

logger = pscheduler.Log(prefix="tool-ethr", quiet=True)

json = pscheduler.json_load(exit_on_error=True)

logger.debug("can-run for %s" % json)

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


try:
    spec = json["spec"]
    pscheduler.json_check_schema(spec, 7)
except KeyError:
    pscheduler.succeed_json({
        "can-run": False,
        "reasons": ["Missing test specification"]
    })
except ValueError as ex:
    pscheduler.succeed_json({
        "can-run": False,
        "reasons": [str(ex)]
    })


errors = []

supported_options = [
    "buffer-length",
    "dest",
    "dest-node",
    "duration",
    "ip-version",
    "omit",
    "parallel",
    "reverse",
    "schema",
    "source",
    "source-node",
    "udp",
    "loopback"
]

for option in list(spec.keys()):
    if option not in supported_options:
        errors.append(f'''Option '{option}' is not supported.''')

logger.debug("can-run succeeded")

result = {
    "can-run": len(errors) == 0
}

if len(errors) > 0:
    result["reasons"] = errors

pscheduler.succeed_json(result)
