3 ##############################################################################
4 # Copyright (c) 2017 Politecnico di Torino and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
12 from __future__ import print_function
13 from jsonschema import validate
14 from pprint import pprint
17 from requests.exceptions import *
18 from jsonschema.exceptions import *
24 # Constants (change them, if appropriate)
25 VERIGRAPH_PORT = "8080"
26 TEST_CASES_DIR = "testcases"
27 BASE_URL = "http://localhost:"+VERIGRAPH_PORT+"/verify/api/graphs/"
28 SCHEMA_FILE = "testcase_schema.json"
37 print(toPrint, file=sys.stderr)
41 print("PYTHON " + sys.version)
45 schema = json.load(open(SCHEMA_FILE))
47 eprint("Invalid json schema (check your "+SCHEMA_FILE+")!\nExiting.")
50 # Iterate over .json files contained in the TEST_CASES_DIR
51 for i in os.listdir(TEST_CASES_DIR):
52 if i.endswith(".json"):
53 with open(TEST_CASES_DIR+os.path.sep+i) as data_file:
55 # Load json file (raise exception if malformed)
56 data = json.load(data_file)
58 # Validate input json against schema (raise exception if invalid)
59 validate(data, schema)
62 print("Test case ID: "+str(data["id"]))
63 print("\tFILE NAME: "+i)
64 print("\tTEST NAME: "+data["name"])
65 print("\tTEST DESCRIPTION: "+data["description"])
68 r = requests.post(BASE_URL, json=data["graph"])
69 if r.status_code == 201:
70 graph_id = r.json()["id"]
71 print("\tCreated Graph has ID " + str(graph_id) + " on VeriGraph")
73 # GET the policy verification result
74 policy = requests.get(BASE_URL+str(graph_id)+"/policy"+data["policy_url_parameters"])
77 if policy.status_code == 200:
78 print("\tVerification result is " + policy.json()["result"])
80 # Check the result with the expected one
81 if policy.json()["result"] == data["result"]:
83 print("\t+++ Test passed +++")
87 eprint("\t[ERROR] Expected result was " + data["result"] + " but VeriGraph returned " + policy.json()["result"])
88 print("\t--- Test failed ---")
90 print("\tVeriGraph returned an unexpected response -> " + str(policy.status_code), policy.reason)
91 print("\t--- Test failed ---")
94 print("Malformed json!\nSkipping "+i+" file")
95 print("\t--- Test failed ---")
96 except ValidationError:
97 print("Invalid json (see Schema file)!\nSkipping "+i+" file")
98 print("\t--- Test failed ---")
99 except ConnectionError:
100 print("Connection refused!")
101 print("\t--- Test failed ---")
104 print("\t--- Test failed ---")
107 print("\nTest run = "+str(run))
108 print("Test succeded = "+str(success))
111 print("\n --- Some tests failed. See the output. ---")
113 print("\n +++ All tests passed +++")
115 print("\n\n +++ 0 tests executed +++")