#!/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:
    test_type = json["type"]
except KeyError:
    pscheduler.succeed_json({
        "can-run": False,
        "reasons": [ "Missing test type" ]
    })

if test_type != "disk-to-disk":
    pscheduler.succeed_json({
        "can-run": False,
        "reasons": [ "Unsupported test type" ]
    })


MAX_SCHEMA = 1

try:
    pscheduler.json_check_schema(json["spec"], MAX_SCHEMA)
except ValueError as ex:
    # Schema check failed
    pscheduler.succeed_json({
        "can-run": False,
        "reasons": [str(ex)]
    })
except KeyError:
    # Data was missing
    pscheduler.succeed_json({
        "can-run": False,
        "reasons": ["Missing data in test specification"]
    })


reasons = []

spec = json['spec']

if spec.get('source', '').lower().startswith('gopher:') or spec.get('dest', '').lower().startswith('gopher:'):
    reasons.append('Will not operate on Gopher URLs.  See https://hackerone.com/reports/3477023.')


if spec.get('dest','') == '-':
    reasons.append('Will not write to standard output')


result = {
    'can-run': not len(reasons)
}

if reasons:
    result['reasons'] = reasons
    
pscheduler.succeed_json(result)
