aboutsummaryrefslogtreecommitdiff
path: root/src/test/bctest.py
blob: 00d96eff19947bee78b4cbe44a723a387b05ec4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright 2014 BitPay, Inc.
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
from __future__ import division,print_function,unicode_literals
import subprocess
import os
import json
import sys
import binascii
import difflib
import logging

def parse_output(a, fmt):
	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)

def bctest(testDir, testObj, exeext):

	execprog = testObj['exec'] + exeext
	execargs = testObj['args']
	execrun = [execprog] + execargs
	stdinCfg = None
	inputData = None
	if "input" in testObj:
		filename = testDir + "/" + testObj['input']
		inputData = open(filename).read()
		stdinCfg = subprocess.PIPE

	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

	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

	if outputData:
		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
		if a_parsed != b_parsed:
			logging.error("Output data mismatch for " + outputFn + " (format " + outputType + ")")
			raise Exception
		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)
			raise Exception

	wantRC = 0
	if "return_code" in testObj:
		wantRC = testObj['return_code']
	if proc.returncode != wantRC:
		logging.error("Return code mismatch for " + outputFn)
		raise Exception

def bctester(testDir, input_basename, buildenv):
	input_filename = testDir + "/" + input_basename
	raw_data = open(input_filename).read()
	input_data = json.loads(raw_data)

	failed_testcases = []

	for testObj in input_data:
		try:
			bctest(testDir, testObj, buildenv.exeext)
			logging.info("PASSED: " + testObj["description"])
		except:
			logging.info("FAILED: " + testObj["description"])
			failed_testcases.append(testObj["description"])

	if failed_testcases:
		logging.error("FAILED TESTCASES: [" + ", ".join(failed_testcases) + "]")
		sys.exit(1)
	else:
		sys.exit(0)