Commit a4f41d85 authored by Aaron Jones's avatar Aaron Jones

Copied in test.py changes from f600ca5f to fix errors in finesse/finesse/master

HEAD detached at master_testpoint1
  Changes to be committed:
     modified:   test.py
parent 6150765e
......@@ -3,11 +3,39 @@ from __future__ import print_function
import sys
import subprocess as sub
import numpy as np
import os
import os.path as path
import requests
import traceback
################ Exceptions ################
class RunException(Exception):
# ===========
# Exceptions
# ===========
# Setting the exception hooks
# ---------------------------
class ExitCodeException(Exception):
""" Base class for all exceptions which set the exit code"""
def getExitCode(self):
"meant to be overridden in subclass"
return 1
def handleUncaughtException(exctype, value, trace):
""" Function to exit with error code from exception object """
oldHook(exctype, value, trace)
if isinstance(value, ExitCodeException):
sys.exit(value.getExitCode())
# Set the exception hook to be our new function which exits with a error code
# Store old hook as a variable
sys.excepthook, oldHook = handleUncaughtException, sys.excepthook
# Definning new exceptions
# --------------------------
class RunException(ExitCodeException):
""" Exception for failures of finesse to run """
def __init__(self, returncode, args, err, out):
self.returncode = returncode
self.args = args
......@@ -15,16 +43,25 @@ class RunException(Exception):
self.out = out
super(RunException, self).__init__(returncode, args, err, out)
class DiffException(Exception):
def getExitCode(self):
return 3
class DiffException(ExitCodeException):
""" Exception for differances against the referance file """
def __init__(self, msg, outfile):
self.msg = msg
self.outfile = outfile
################ Functions ################
def runkat(katfile):
def getExitCode(self):
return 5
# ===========
# Functions
# ===========
def runkat(katfile):
""" Run the katfile """
try:
p = sub.Popen(["kat",katfile], stdout=sub.PIPE, stderr=sub.PIPE)
p = sub.Popen(["kat","--test","--noheader",katfile], stdout=sub.PIPE, stderr=sub.PIPE)
out, err = p.communicate()
finally:
if 'p' in locals():
......@@ -33,21 +70,26 @@ def runkat(katfile):
except OSError:
pass
# BrumSoftTest once we leave this file, stdout and stderr get mapped though
# docker back to the system stdout and stderr on the host server
# This can only handel ascii, so here we re-encode the data stream as ascii
encoding=sys.stdout.encoding
if encoding==None:
encoding='utf-8'
out = out.decode(encoding)
err = err.decode(encoding)
out = str(out).decode(encoding)
err = str(err).decode(encoding)
print(str(out))
print(str(err), file=sys.stderr)
print(out.encode('ascii','replace'))
print(err.encode('ascii','replace'), file=sys.stderr)
# if finesse exited with a non-zero error code we raise a run exception (which maps to error code 3)
if p.returncode != 0:
raise RunException(p.returncode, args, err, out)
raise RunException(p.returncode, "kat "+katfile, err, out)
return [out,err,p.returncode]
def diff(katfile):
""" If everything ran okay we will calculate the differance """
diffFound = False
katdir=path.dirname(katfile)
......@@ -103,7 +145,7 @@ def diff(katfile):
print('Refernce Array: '+str(ref_arr[ix]))
print('Output Array: '+str(out_arr[ix]))
print('Differance: '+str(np.max(rel_diff)))
print('Max relative difference: '+str(np.max(rel_diff)))
raise DiffException("Difference larger than " + str(diff_rel_eps), katfile)
else:
......@@ -117,15 +159,38 @@ def diff(katfile):
if __name__ == "__main__":
print("Kat Test Started, Arguments: "+str(sys.argv))
test_kat = sys.argv[1]
runkat(test_kat)
try:
runkat(test_kat)
except Exception as e:
errtext = 'Error running finesse. Output file not produced, referance file not used. These will not be uploaded'
print(errtext)
print(errtext, file=sys.stderr)
raise e
else:
print('Finesse ran without error')
try:
diff(test_kat)
finally:
if len(sys.argv) > 2:
katdir=path.dirname(test_kat)
katname=str(path.basename(test_kat)).split('.')[0]
out_file = path.join(katdir,katname+'.out')
print('Uploading: '+str(path.basename(out_file))+" To: "+str(sys.argv[2]))
with open(out_file, 'rb') as f: r = requests.post(sys.argv[2], files={'file': f},verify=False)
print("Server Returns: " + str(r.text))
r.raise_for_status()
try:
katdir=path.dirname(test_kat)
katname=str(path.basename(test_kat)).split('.')[0]
out_file = path.join(katdir,katname+'.out')
ref_file = path.join(katdir,'reference',katname)
os.rename(str(ref_file)+'.out',str(ref_file)+'.ref')
ref_file = str(ref_file)+'.ref'
print('Uploading: '+str(out_file)+" To: "+str(sys.argv[2]))
with open(out_file, 'rb') as f: r = requests.post(sys.argv[2], files={'file': f},verify=False)
print("Server Returns: " + str(r.text))
r.raise_for_status()
print('Uploading: '+str(ref_file)+" To: "+str(sys.argv[2]))
with open(ref_file, 'rb') as f: r = requests.post(sys.argv[2], files={'file': f},verify=False)
print("Server Returns: " + str(r.text))
r.raise_for_status()
except Exception:
print(traceback.format_exc())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment