diff options
author | serena.spinoso <serena.spinoso@polito.it> | 2017-02-25 12:00:55 +0100 |
---|---|---|
committer | serena.spinoso <serena.spinoso@polito.it> | 2017-02-27 13:39:18 +0100 |
commit | 53d83244c1bf36af86e90ce5fe758a369f73563e (patch) | |
tree | f5780b3033763d6b3cd62c40f5d4a63dfac35f7a /verigraph/tester/test.py | |
parent | f0108f26fb1f62fefbbc4d5484b87241d563737b (diff) |
Add verigraph code base
JIRA: PARSER-111
Change-Id: Ie76e14fabbb6c388ebc89d9a15dd3021b25fa892
Signed-off-by: serena.spinoso <serena.spinoso@polito.it>
Diffstat (limited to 'verigraph/tester/test.py')
-rw-r--r-- | verigraph/tester/test.py | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/verigraph/tester/test.py b/verigraph/tester/test.py new file mode 100644 index 0000000..ad0b82a --- /dev/null +++ b/verigraph/tester/test.py @@ -0,0 +1,115 @@ +#!/usr/bin/python + +############################################################################## +# Copyright (c) 2017 Politecnico di Torino and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from __future__ import print_function +from jsonschema import validate +from pprint import pprint +import sys +import requests +from requests.exceptions import * +from jsonschema.exceptions import * +import json +import getopt +import os +import subprocess + +# Constants (change them, if appropriate) +VERIGRAPH_PORT = "8080" +TEST_CASES_DIR = "testcases" +BASE_URL = "http://localhost:"+VERIGRAPH_PORT+"/verify/api/graphs/" +SCHEMA_FILE = "testcase_schema.json" + +# Variables +success = 0 +run = 0 + +# Utils +def eprint(toPrint): + sys.stdout.flush() + print(toPrint, file=sys.stderr) + sys.stderr.flush() + +# Print PYTHON ver +print("PYTHON " + sys.version) + +# Loading schema file +try: + schema = json.load(open(SCHEMA_FILE)) +except ValueError: + eprint("Invalid json schema (check your "+SCHEMA_FILE+")!\nExiting.") + exit(-1) + +# Iterate over .json files contained in the TEST_CASES_DIR +for i in os.listdir(TEST_CASES_DIR): + if i.endswith(".json"): + with open(TEST_CASES_DIR+os.path.sep+i) as data_file: + try: + # Load json file (raise exception if malformed) + data = json.load(data_file) + + # Validate input json against schema (raise exception if invalid) + validate(data, schema) + + run += 1 + print("Test case ID: "+str(data["id"])) + print("\tFILE NAME: "+i) + print("\tTEST NAME: "+data["name"]) + print("\tTEST DESCRIPTION: "+data["description"]) + + # POST the graph + r = requests.post(BASE_URL, json=data["graph"]) + if r.status_code == 201: + graph_id = r.json()["id"] + print("\tCreated Graph has ID " + str(graph_id) + " on VeriGraph") + + # GET the policy verification result + policy = requests.get(BASE_URL+str(graph_id)+"/policy"+data["policy_url_parameters"]) + + # Check the response + if policy.status_code == 200: + print("\tVerification result is " + policy.json()["result"]) + + # Check the result with the expected one + if policy.json()["result"] == data["result"]: + # SUCCESS + print("\t+++ Test passed +++") + success += 1 + else: + # FAIL + eprint("\t[ERROR] Expected result was " + data["result"] + " but VeriGraph returned " + policy.json()["result"]) + print("\t--- Test failed ---") + else: + print("\tVeriGraph returned an unexpected response -> " + str(policy.status_code), policy.reason) + print("\t--- Test failed ---") + print() + except ValueError: + print("Malformed json!\nSkipping "+i+" file") + print("\t--- Test failed ---") + except ValidationError: + print("Invalid json (see Schema file)!\nSkipping "+i+" file") + print("\t--- Test failed ---") + except ConnectionError: + print("Connection refused!") + print("\t--- Test failed ---") + except HTTPError: + print("HTTP error!") + print("\t--- Test failed ---") + +# Final output +print("\nTest run = "+str(run)) +print("Test succeded = "+str(success)) +if run != 0: + if success != run: + print("\n --- Some tests failed. See the output. ---") + else: + print("\n +++ All tests passed +++") +else: + print("\n\n +++ 0 tests executed +++")
\ No newline at end of file |