You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.7 KiB
128 lines
4.7 KiB
8 years ago
|
# Copyright 2014 BitPay Inc.
|
||
8 years ago
|
# Copyright 2016 The Bitcoin Core developers
|
||
10 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
10 years ago
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
9 years ago
|
from __future__ import division,print_function,unicode_literals
|
||
10 years ago
|
import subprocess
|
||
|
import os
|
||
|
import json
|
||
|
import sys
|
||
8 years ago
|
import binascii
|
||
8 years ago
|
import difflib
|
||
|
import logging
|
||
8 years ago
|
import pprint
|
||
8 years ago
|
|
||
|
def parse_output(a, fmt):
|
||
8 years ago
|
"""Parse the output according to specified format.
|
||
|
|
||
|
Raise an error if the output can't be parsed."""
|
||
|
if fmt == 'json': # json: compare parsed data
|
||
|
return json.loads(a)
|
||
|
elif fmt == 'hex': # hex: parse and compare binary data
|
||
|
return binascii.a2b_hex(a.strip())
|
||
|
else:
|
||
|
raise NotImplementedError("Don't know how to compare %s" % fmt)
|
||
10 years ago
|
|
||
8 years ago
|
def bctest(testDir, testObj, buildenv):
|
||
8 years ago
|
"""Runs a single test, comparing output and RC to expected output and RC.
|
||
10 years ago
|
|
||
8 years ago
|
Raises an error if input can't be read, executable fails, or output/RC
|
||
|
are not as expected. Error is caught by bctester() and reported.
|
||
|
"""
|
||
|
# Get the exec names and arguments
|
||
8 years ago
|
execprog = buildenv.BUILDDIR + "/src/" + testObj['exec'] + buildenv.exeext
|
||
8 years ago
|
execargs = testObj['args']
|
||
|
execrun = [execprog] + execargs
|
||
10 years ago
|
|
||
8 years ago
|
# Read the input data (if there is any)
|
||
|
stdinCfg = None
|
||
|
inputData = None
|
||
|
if "input" in testObj:
|
||
|
filename = testDir + "/" + testObj['input']
|
||
|
inputData = open(filename).read()
|
||
|
stdinCfg = subprocess.PIPE
|
||
8 years ago
|
|
||
8 years ago
|
# Read the expected output data (if there is any)
|
||
|
outputFn = None
|
||
|
outputData = None
|
||
|
if "output_cmp" in testObj:
|
||
|
outputFn = testObj['output_cmp']
|
||
|
outputType = os.path.splitext(outputFn)[1][1:] # output type from file extension (determines how to compare)
|
||
|
try:
|
||
|
outputData = open(testDir + "/" + outputFn).read()
|
||
|
except:
|
||
|
logging.error("Output file " + outputFn + " can not be opened")
|
||
|
raise
|
||
|
if not outputData:
|
||
|
logging.error("Output data missing for " + outputFn)
|
||
|
raise Exception
|
||
10 years ago
|
|
||
8 years ago
|
# Run the test
|
||
|
proc = subprocess.Popen(execrun, stdin=stdinCfg, stdout=subprocess.PIPE, stderr=subprocess.PIPE,universal_newlines=True)
|
||
|
try:
|
||
|
outs = proc.communicate(input=inputData)
|
||
|
except OSError:
|
||
|
logging.error("OSError, Failed to execute " + execprog)
|
||
|
raise
|
||
10 years ago
|
|
||
8 years ago
|
if outputData:
|
||
8 years ago
|
data_mismatch, formatting_mismatch = False, False
|
||
8 years ago
|
# Parse command output and expected output
|
||
|
try:
|
||
|
a_parsed = parse_output(outs[0], outputType)
|
||
|
except Exception as e:
|
||
|
logging.error('Error parsing command output as %s: %s' % (outputType,e))
|
||
|
raise
|
||
|
try:
|
||
|
b_parsed = parse_output(outputData, outputType)
|
||
|
except Exception as e:
|
||
|
logging.error('Error parsing expected output %s as %s: %s' % (outputFn,outputType,e))
|
||
|
raise
|
||
|
# Compare data
|
||
|
if a_parsed != b_parsed:
|
||
|
logging.error("Output data mismatch for " + outputFn + " (format " + outputType + ")")
|
||
8 years ago
|
data_mismatch = True
|
||
8 years ago
|
# Compare formatting
|
||
|
if outs[0] != outputData:
|
||
|
error_message = "Output formatting mismatch for " + outputFn + ":\n"
|
||
|
error_message += "".join(difflib.context_diff(outputData.splitlines(True),
|
||
|
outs[0].splitlines(True),
|
||
|
fromfile=outputFn,
|
||
|
tofile="returned"))
|
||
|
logging.error(error_message)
|
||
8 years ago
|
formatting_mismatch = True
|
||
|
|
||
|
assert not data_mismatch and not formatting_mismatch
|
||
10 years ago
|
|
||
8 years ago
|
# Compare the return code to the expected return code
|
||
|
wantRC = 0
|
||
|
if "return_code" in testObj:
|
||
|
wantRC = testObj['return_code']
|
||
|
if proc.returncode != wantRC:
|
||
|
logging.error("Return code mismatch for " + outputFn)
|
||
|
raise Exception
|
||
10 years ago
|
|
||
8 years ago
|
def bctester(testDir, input_basename, buildenv):
|
||
|
""" Loads and parses the input file, runs all tests and reports results"""
|
||
|
input_filename = testDir + "/" + input_basename
|
||
|
raw_data = open(input_filename).read()
|
||
|
input_data = json.loads(raw_data)
|
||
8 years ago
|
|
||
8 years ago
|
failed_testcases = []
|
||
10 years ago
|
|
||
8 years ago
|
for testObj in input_data:
|
||
|
try:
|
||
8 years ago
|
bctest(testDir, testObj, buildenv)
|
||
8 years ago
|
logging.info("PASSED: " + testObj["description"])
|
||
|
except:
|
||
|
logging.info("FAILED: " + testObj["description"])
|
||
|
failed_testcases.append(testObj["description"])
|
||
10 years ago
|
|
||
8 years ago
|
if failed_testcases:
|
||
8 years ago
|
error_message = "FAILED_TESTCASES:\n"
|
||
|
error_message += pprint.pformat(failed_testcases, width=400)
|
||
|
logging.error(error_message)
|
||
8 years ago
|
sys.exit(1)
|
||
|
else:
|
||
|
sys.exit(0)
|