#!/usr/bin/env python3
#
# Checker for 'http' task limits
#

import pscheduler

from validate import spec_is_valid
from validate import limit_is_valid

#
# Load and validate everything
#

try:
    json = pscheduler.json_load() # from stdin?
except ValueError as ex:
    pscheduler.fail("Invalid JSON %s" % str(ex))

spec = json["spec"]
valid, message = spec_is_valid(spec)
if not valid:
    raise ValueError("Invalid test spec: %s" % message)

limit = json["limit"]
valid, message = limit_is_valid(limit)
if not valid:
    raise ValueError("Invalid limit: %s" % message)


#
# Evaluate everything
#

errors = []


#
# Query host, host-node, timeout, url, strings to parse
#

for value in [ "host", "host-node", "timeout", "url", "parse", "always-succeed" ]:
    try:
        test = limit[value]
        match = pscheduler.StringMatcher(test["match"])
    except KeyError:
        continue
    try:
        invert = test["invert"]
    except KeyError:
        invert = False

    in_limit = match.matches(spec[value])
    if in_limit or (invert and in_limit):
        try:
            errors.append(test["fail-message"])
        except KeyError:
            errors.append("%s does not match limit" % (value.capitalize()))
        except KeyError:
            pass  # Don't care if not there.

#
# Finish up
#

result = { "passes": not errors }

if errors:
    result["errors"] = errors

pscheduler.succeed_json(result)
