From ce58e93ec0bf4ef4a503379e98bd209dd99447eb Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 21 Mar 2017 13:54:02 -0400 Subject: Change bitcoin-util-test.py to use Python3 --- src/Makefile.test.include | 2 +- test/util/bitcoin-util-test.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Makefile.test.include b/src/Makefile.test.include index 10cb7e775a..c3302f983c 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -147,7 +147,7 @@ bitcoin_test_clean : FORCE check-local: @echo "Running test/util/bitcoin-util-test.py..." - $(PYTHON) $(top_builddir)/test/util/bitcoin-util-test.py + $(top_builddir)/test/util/bitcoin-util-test.py $(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C secp256k1 check if EMBEDDED_UNIVALUE $(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C univalue check diff --git a/test/util/bitcoin-util-test.py b/test/util/bitcoin-util-test.py index e09a25159d..55d89c3b92 100755 --- a/test/util/bitcoin-util-test.py +++ b/test/util/bitcoin-util-test.py @@ -1,9 +1,8 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright 2014 BitPay Inc. # Copyright 2016 The Bitcoin Core developers # 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 os import sys import argparse -- cgit v1.2.3 From e9265df15b04178b40ab3bacfe6a944f0fb9ad27 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 21 Mar 2017 13:57:07 -0400 Subject: Change help_text in bitcoin-util-test.py to a docstring. --- test/util/bitcoin-util-test.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/util/bitcoin-util-test.py b/test/util/bitcoin-util-test.py index 55d89c3b92..0c67663edb 100755 --- a/test/util/bitcoin-util-test.py +++ b/test/util/bitcoin-util-test.py @@ -3,23 +3,23 @@ # Copyright 2016 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. +"""Test framework for bitcoin utils. + +Runs automatically during `make check`. + +Can also be run manually.""" + import os import sys import argparse import logging -help_text="""Test framework for bitcoin utils. - -Runs automatically during `make check`. - -Can also be run manually.""" - if __name__ == '__main__': sys.path.append(os.path.dirname(os.path.abspath(__file__))) import buildenv import bctest - parser = argparse.ArgumentParser(description=help_text) + parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-v', '--verbose', action='store_true') args = parser.parse_args() verbose = args.verbose -- cgit v1.2.3 From 89fcd3586c9714a923b6a3147f60e43d5de74942 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 21 Mar 2017 14:38:09 -0400 Subject: Use an .ini config file for environment vars in bitcoin-util-test.py --- .gitignore | 2 -- Makefile.am | 3 --- configure.ac | 3 +-- test/util/bitcoin-util-test.py | 9 ++++++++- test/util/buildenv.py.in | 4 ---- test/util/config.ini.in | 11 +++++++++++ 6 files changed, 20 insertions(+), 12 deletions(-) delete mode 100644 test/util/buildenv.py.in create mode 100644 test/util/config.ini.in diff --git a/.gitignore b/.gitignore index f1e9ca20c1..aa37e381e2 100644 --- a/.gitignore +++ b/.gitignore @@ -80,7 +80,6 @@ Bitcoin-Qt.app # Unit-tests Makefile.test bitcoin-qt_test -src/test/buildenv.py # Resources cpp qrc_*.cpp @@ -102,7 +101,6 @@ linux-coverage-build linux-build win32-build test/functional/config.ini -test/util/buildenv.py test/cache/* !src/leveldb*/Makefile diff --git a/Makefile.am b/Makefile.am index 3a56eea0c0..b0ba0c8504 100644 --- a/Makefile.am +++ b/Makefile.am @@ -277,9 +277,6 @@ EXTRA_DIST += \ CLEANFILES = $(OSX_DMG) $(BITCOIN_WIN_INSTALLER) -# This file is problematic for out-of-tree builds if it exists. -DISTCLEANFILES = test/util/buildenv.pyc - .INTERMEDIATE: $(COVERAGE_INFO) DISTCHECK_CONFIGURE_FLAGS = --enable-man diff --git a/configure.ac b/configure.ac index 3672700488..26a9d082c6 100644 --- a/configure.ac +++ b/configure.ac @@ -1160,8 +1160,7 @@ AC_SUBST(EVENT_PTHREADS_LIBS) AC_SUBST(ZMQ_LIBS) AC_SUBST(PROTOBUF_LIBS) AC_SUBST(QR_LIBS) -AC_CONFIG_FILES([Makefile src/Makefile doc/man/Makefile share/setup.nsi share/qt/Info.plist test/functional/config.ini]) -AC_CONFIG_FILES([test/util/buildenv.py],[chmod +x test/util/buildenv.py]) +AC_CONFIG_FILES([Makefile src/Makefile doc/man/Makefile share/setup.nsi share/qt/Info.plist test/functional/config.ini test/util/config.ini]) AC_CONFIG_FILES([contrib/devtools/split-debug.sh],[chmod +x contrib/devtools/split-debug.sh]) AC_CONFIG_FILES([doc/Doxyfile]) AC_CONFIG_LINKS([test/functional/test_runner.py:test/functional/test_runner.py]) diff --git a/test/util/bitcoin-util-test.py b/test/util/bitcoin-util-test.py index 0c67663edb..f85bd13f93 100755 --- a/test/util/bitcoin-util-test.py +++ b/test/util/bitcoin-util-test.py @@ -9,6 +9,7 @@ Runs automatically during `make check`. Can also be run manually.""" +import configparser import os import sys import argparse @@ -16,9 +17,15 @@ import logging if __name__ == '__main__': sys.path.append(os.path.dirname(os.path.abspath(__file__))) - import buildenv import bctest + config = configparser.ConfigParser() + config.read_file(open(os.path.dirname(__file__) + "/config.ini")) + + buildenv = argparse.Namespace(exeext=config["environment"]["EXEEXT"], + SRCDIR=config["environment"]["SRCDIR"], + BUILDDIR=config["environment"]["BUILDDIR"]) + parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-v', '--verbose', action='store_true') args = parser.parse_args() diff --git a/test/util/buildenv.py.in b/test/util/buildenv.py.in deleted file mode 100644 index 33030b0348..0000000000 --- a/test/util/buildenv.py.in +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env python -exeext="@EXEEXT@" -SRCDIR="@abs_top_srcdir@" -BUILDDIR="@abs_top_builddir@" diff --git a/test/util/config.ini.in b/test/util/config.ini.in new file mode 100644 index 0000000000..a1f8f09cec --- /dev/null +++ b/test/util/config.ini.in @@ -0,0 +1,11 @@ +# Copyright (c) 2013-2017 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +# These environment variables are set by the build process and read by +# test/util/bitcoin-util-test.py + +[environment] +SRCDIR=@abs_top_srcdir@ +BUILDDIR=@abs_top_builddir@ +EXEEXT=@EXEEXT@ -- cgit v1.2.3 From 95836c5eba4a6cdc835e72189e4b0fe3e9458c5a Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 21 Mar 2017 14:47:20 -0400 Subject: Use shared config file for functional and util tests The functional tests and util tests both require a config file that is generated by ./configure. This commit merges those two config files into a single configuration file that can be shared by both tests. The config from config.ini is put into a Namespace object to maintain the interface with bctest.py. A future commit could change this interface to use a dictionary instead of a namespace. --- .gitignore | 2 +- configure.ac | 6 +++--- test/config.ini.in | 18 ++++++++++++++++++ test/functional/config.ini.in | 18 ------------------ test/functional/test_runner.py | 2 +- test/util/bitcoin-util-test.py | 2 +- test/util/config.ini.in | 11 ----------- 7 files changed, 24 insertions(+), 35 deletions(-) create mode 100644 test/config.ini.in delete mode 100644 test/functional/config.ini.in delete mode 100644 test/util/config.ini.in diff --git a/.gitignore b/.gitignore index aa37e381e2..60c26dae8b 100644 --- a/.gitignore +++ b/.gitignore @@ -100,7 +100,7 @@ coverage_percent.txt linux-coverage-build linux-build win32-build -test/functional/config.ini +test/config.ini test/cache/* !src/leveldb*/Makefile diff --git a/configure.ac b/configure.ac index 26a9d082c6..13de015151 100644 --- a/configure.ac +++ b/configure.ac @@ -1160,7 +1160,7 @@ AC_SUBST(EVENT_PTHREADS_LIBS) AC_SUBST(ZMQ_LIBS) AC_SUBST(PROTOBUF_LIBS) AC_SUBST(QR_LIBS) -AC_CONFIG_FILES([Makefile src/Makefile doc/man/Makefile share/setup.nsi share/qt/Info.plist test/functional/config.ini test/util/config.ini]) +AC_CONFIG_FILES([Makefile src/Makefile doc/man/Makefile share/setup.nsi share/qt/Info.plist test/config.ini]) AC_CONFIG_FILES([contrib/devtools/split-debug.sh],[chmod +x contrib/devtools/split-debug.sh]) AC_CONFIG_FILES([doc/Doxyfile]) AC_CONFIG_LINKS([test/functional/test_runner.py:test/functional/test_runner.py]) @@ -1213,8 +1213,8 @@ esac dnl Replace the BUILDDIR path with the correct Windows path if compiling on Native Windows case ${OS} in *Windows*) - sed 's/BUILDDIR="\/\([[a-z]]\)/BUILDDIR="\1:/' test/functional/config.ini > test/functional/config-2.ini - mv test/functional/config-2.ini test/functional/config.ini + sed 's/BUILDDIR="\/\([[a-z]]\)/BUILDDIR="\1:/' test/config.ini > test/config-2.ini + mv test/config-2.ini test/config.ini ;; esac diff --git a/test/config.ini.in b/test/config.ini.in new file mode 100644 index 0000000000..35ee092be4 --- /dev/null +++ b/test/config.ini.in @@ -0,0 +1,18 @@ +# Copyright (c) 2013-2016 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +# These environment variables are set by the build process and read by +# test/functional/test_runner.py and test/util/bitcoin-util-test.py + +[environment] +SRCDIR=@abs_top_srcdir@ +BUILDDIR=@abs_top_builddir@ +EXEEXT=@EXEEXT@ + +[components] +# Which components are enabled. These are commented out by `configure` if they were disabled when running config. +@ENABLE_WALLET_TRUE@ENABLE_WALLET=true +@BUILD_BITCOIN_UTILS_TRUE@ENABLE_UTILS=true +@BUILD_BITCOIND_TRUE@ENABLE_BITCOIND=true +@ENABLE_ZMQ_TRUE@ENABLE_ZMQ=true diff --git a/test/functional/config.ini.in b/test/functional/config.ini.in deleted file mode 100644 index 29586c555d..0000000000 --- a/test/functional/config.ini.in +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright (c) 2013-2016 The Bitcoin Core developers -# Distributed under the MIT software license, see the accompanying -# file COPYING or http://www.opensource.org/licenses/mit-license.php. - -# These environment variables are set by the build process and read by -# test/functional/test_runner.py - -[environment] -SRCDIR=@abs_top_srcdir@ -BUILDDIR=@abs_top_builddir@ -EXEEXT=@EXEEXT@ - -[components] -# Which components are enabled. These are commented out by `configure` if they were disabled when running config. -@ENABLE_WALLET_TRUE@ENABLE_WALLET=true -@BUILD_BITCOIN_UTILS_TRUE@ENABLE_UTILS=true -@BUILD_BITCOIND_TRUE@ENABLE_BITCOIND=true -@ENABLE_ZMQ_TRUE@ENABLE_ZMQ=true diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 467e1668d1..a805f50557 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -179,7 +179,7 @@ def main(): # Read config generated by configure. config = configparser.ConfigParser() - configfile = os.path.abspath(os.path.dirname(__file__)) + "/config.ini" + configfile = os.path.abspath(os.path.dirname(__file__)) + "/../config.ini" config.read_file(open(configfile)) passon_args.append("--configfile=%s" % configfile) diff --git a/test/util/bitcoin-util-test.py b/test/util/bitcoin-util-test.py index f85bd13f93..ce0b9ca938 100755 --- a/test/util/bitcoin-util-test.py +++ b/test/util/bitcoin-util-test.py @@ -20,7 +20,7 @@ if __name__ == '__main__': import bctest config = configparser.ConfigParser() - config.read_file(open(os.path.dirname(__file__) + "/config.ini")) + config.read_file(open(os.path.dirname(__file__) + "/../config.ini")) buildenv = argparse.Namespace(exeext=config["environment"]["EXEEXT"], SRCDIR=config["environment"]["SRCDIR"], diff --git a/test/util/config.ini.in b/test/util/config.ini.in deleted file mode 100644 index a1f8f09cec..0000000000 --- a/test/util/config.ini.in +++ /dev/null @@ -1,11 +0,0 @@ -# Copyright (c) 2013-2017 The Bitcoin Core developers -# Distributed under the MIT software license, see the accompanying -# file COPYING or http://www.opensource.org/licenses/mit-license.php. - -# These environment variables are set by the build process and read by -# test/util/bitcoin-util-test.py - -[environment] -SRCDIR=@abs_top_srcdir@ -BUILDDIR=@abs_top_builddir@ -EXEEXT=@EXEEXT@ -- cgit v1.2.3 From 8ad5bdef781d9b3009030287e5c99341e6933007 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 21 Mar 2017 15:01:48 -0400 Subject: Merge bctest.py into bitcoin-util-test.py bctest.py is only used as an import by bitcoin-util-test.py. There's no value in keeping it as a separate module, so let's merge them into a single module to keep building and packaging simpler. bitcoin-test-util is importable as a module, so if any future modules really want to import the code from bctest.py, they can import bitcoin-test-util and call the bctest functions by name. --- Makefile.am | 1 - configure.ac | 1 - test/util/bctest.py | 139 ------------------------------------- test/util/bitcoin-util-test.py | 153 +++++++++++++++++++++++++++++++++++++---- 4 files changed, 140 insertions(+), 154 deletions(-) delete mode 100644 test/util/bctest.py diff --git a/Makefile.am b/Makefile.am index b0ba0c8504..40114a551f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -223,7 +223,6 @@ dist_noinst_SCRIPTS = autogen.sh EXTRA_DIST = $(top_srcdir)/share/genbuild.sh test/functional/test_runner.py test/functional $(DIST_CONTRIB) $(DIST_DOCS) $(WINDOWS_PACKAGING) $(OSX_PACKAGING) $(BIN_CHECKS) EXTRA_DIST += \ - test/util/bctest.py \ test/util/bitcoin-util-test.py \ test/util/data/bitcoin-util-test.json \ test/util/data/blanktxv1.hex \ diff --git a/configure.ac b/configure.ac index 13de015151..ebc01ed42f 100644 --- a/configure.ac +++ b/configure.ac @@ -1165,7 +1165,6 @@ AC_CONFIG_FILES([contrib/devtools/split-debug.sh],[chmod +x contrib/devtools/spl AC_CONFIG_FILES([doc/Doxyfile]) AC_CONFIG_LINKS([test/functional/test_runner.py:test/functional/test_runner.py]) AC_CONFIG_LINKS([test/util/bitcoin-util-test.py:test/util/bitcoin-util-test.py]) -AC_CONFIG_LINKS([test/util/bctest.py:test/util/bctest.py]) dnl boost's m4 checks do something really nasty: they export these vars. As a dnl result, they leak into secp256k1's configure and crazy things happen. diff --git a/test/util/bctest.py b/test/util/bctest.py deleted file mode 100644 index b17cf77ae3..0000000000 --- a/test/util/bctest.py +++ /dev/null @@ -1,139 +0,0 @@ -# Copyright 2014 BitPay Inc. -# Copyright 2016 The Bitcoin Core developers -# 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 -import pprint - -def parse_output(a, fmt): - """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) - -def bctest(testDir, testObj, buildenv): - """Runs a single test, comparing output and RC to expected output and RC. - - 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 - execprog = buildenv.BUILDDIR + "/src/" + testObj['exec'] + buildenv.exeext - execargs = testObj['args'] - execrun = [execprog] + execargs - - # 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 - - # 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 - - # 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 - - if outputData: - data_mismatch, formatting_mismatch = False, False - # 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 + ")") - data_mismatch = True - # 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) - formatting_mismatch = True - - assert not data_mismatch and not formatting_mismatch - - # 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 - - if "error_txt" in testObj: - want_error = testObj["error_txt"] - # Compare error text - # TODO: ideally, we'd compare the strings exactly and also assert - # That stderr is empty if no errors are expected. However, bitcoin-tx - # emits DISPLAY errors when running as a windows application on - # linux through wine. Just assert that the expected error text appears - # somewhere in stderr. - if want_error not in outs[1]: - logging.error("Error mismatch:\n" + "Expected: " + want_error + "\nReceived: " + outs[1].rstrip()) - raise Exception - -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) - - failed_testcases = [] - - for testObj in input_data: - try: - bctest(testDir, testObj, buildenv) - logging.info("PASSED: " + testObj["description"]) - except: - logging.info("FAILED: " + testObj["description"]) - failed_testcases.append(testObj["description"]) - - if failed_testcases: - error_message = "FAILED_TESTCASES:\n" - error_message += pprint.pformat(failed_testcases, width=400) - logging.error(error_message) - sys.exit(1) - else: - sys.exit(0) diff --git a/test/util/bitcoin-util-test.py b/test/util/bitcoin-util-test.py index ce0b9ca938..d15d6a6011 100755 --- a/test/util/bitcoin-util-test.py +++ b/test/util/bitcoin-util-test.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # Copyright 2014 BitPay Inc. -# Copyright 2016 The Bitcoin Core developers +# Copyright 2016-2017 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test framework for bitcoin utils. @@ -9,23 +9,21 @@ Runs automatically during `make check`. Can also be run manually.""" +import argparse +import binascii import configparser +import difflib +import json +import logging import os +import pprint +import subprocess import sys -import argparse -import logging - -if __name__ == '__main__': - sys.path.append(os.path.dirname(os.path.abspath(__file__))) - import bctest +def main(): config = configparser.ConfigParser() config.read_file(open(os.path.dirname(__file__) + "/../config.ini")) - buildenv = argparse.Namespace(exeext=config["environment"]["EXEEXT"], - SRCDIR=config["environment"]["SRCDIR"], - BUILDDIR=config["environment"]["BUILDDIR"]) - parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-v', '--verbose', action='store_true') args = parser.parse_args() @@ -37,6 +35,135 @@ if __name__ == '__main__': level = logging.ERROR formatter = '%(asctime)s - %(levelname)s - %(message)s' # Add the format/level to the logger - logging.basicConfig(format = formatter, level=level) + logging.basicConfig(format=formatter, level=level) + + bctester(config["environment"]["SRCDIR"] + "/test/util/data", "bitcoin-util-test.json", config["environment"]) + +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) + + failed_testcases = [] + + for testObj in input_data: + try: + bctest(testDir, testObj, buildenv) + logging.info("PASSED: " + testObj["description"]) + except: + logging.info("FAILED: " + testObj["description"]) + failed_testcases.append(testObj["description"]) + + if failed_testcases: + error_message = "FAILED_TESTCASES:\n" + error_message += pprint.pformat(failed_testcases, width=400) + logging.error(error_message) + sys.exit(1) + else: + sys.exit(0) - bctest.bctester(buildenv.SRCDIR + "/test/util/data", "bitcoin-util-test.json", buildenv) +def bctest(testDir, testObj, buildenv): + """Runs a single test, comparing output and RC to expected output and RC. + + 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 + execprog = buildenv["BUILDDIR"] + "/src/" + testObj['exec'] + buildenv["EXEEXT"] + execargs = testObj['args'] + execrun = [execprog] + execargs + + # 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 + + # 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 + + # 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 + + if outputData: + data_mismatch, formatting_mismatch = False, False + # 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 + ")") + data_mismatch = True + # 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) + formatting_mismatch = True + + assert not data_mismatch and not formatting_mismatch + + # 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 + + if "error_txt" in testObj: + want_error = testObj["error_txt"] + # Compare error text + # TODO: ideally, we'd compare the strings exactly and also assert + # That stderr is empty if no errors are expected. However, bitcoin-tx + # emits DISPLAY errors when running as a windows application on + # linux through wine. Just assert that the expected error text appears + # somewhere in stderr. + if want_error not in outs[1]: + logging.error("Error mismatch:\n" + "Expected: " + want_error + "\nReceived: " + outs[1].rstrip()) + raise Exception + +def parse_output(a, fmt): + """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) + +if __name__ == '__main__': + main() -- cgit v1.2.3