@ -5,6 +5,7 @@
@@ -5,6 +5,7 @@
""" Base class for RPC testing. """
from collections import deque
from enum import Enum
import logging
import optparse
import os
@ -41,6 +42,15 @@ from .util import (
@@ -41,6 +42,15 @@ from .util import (
)
from . authproxy import JSONRPCException
class TestStatus ( Enum ) :
PASSED = 1
FAILED = 2
SKIPPED = 3
TEST_EXIT_PASSED = 0
TEST_EXIT_FAILED = 1
TEST_EXIT_SKIPPED = 77
class BitcoinTestFramework ( object ) :
""" Base class for a bitcoin test script.
@ -57,11 +67,6 @@ class BitcoinTestFramework(object):
@@ -57,11 +67,6 @@ class BitcoinTestFramework(object):
This class also contains various public and private helper methods . """
# Methods to override in subclass test scripts.
TEST_EXIT_PASSED = 0
TEST_EXIT_FAILED = 1
TEST_EXIT_SKIPPED = 77
def __init__ ( self ) :
self . num_nodes = 4
self . setup_clean_chain = False
@ -139,15 +144,18 @@ class BitcoinTestFramework(object):
@@ -139,15 +144,18 @@ class BitcoinTestFramework(object):
self . options . tmpdir = tempfile . mkdtemp ( prefix = " test " )
self . _start_logging ( )
success = False
success = TestStatus . FAILED
try :
self . setup_chain ( )
self . setup_network ( )
self . run_test ( )
success = True
success = TestStatus . PASSED
except JSONRPCException as e :
self . log . exception ( " JSONRPC error " )
except SkipTest as e :
self . log . warning ( " Test Skipped: %s " % e . message )
success = TestStatus . SKIPPED
except AssertionError as e :
self . log . exception ( " Assertion failed " )
except KeyError as e :
@ -159,11 +167,12 @@ class BitcoinTestFramework(object):
@@ -159,11 +167,12 @@ class BitcoinTestFramework(object):
if not self . options . noshutdown :
self . log . info ( " Stopping nodes " )
self . stop_nodes ( )
if self . nodes :
self . stop_nodes ( )
else :
self . log . info ( " Note: bitcoinds were not stopped and may still be running " )
if not self . options . nocleanup and not self . options . noshutdown and success :
if not self . options . nocleanup and not self . options . noshutdown and success != TestStatus . FAILED :
self . log . info ( " Cleaning up " )
shutil . rmtree ( self . options . tmpdir )
else :
@ -183,13 +192,17 @@ class BitcoinTestFramework(object):
@@ -183,13 +192,17 @@ class BitcoinTestFramework(object):
except OSError :
print ( " Opening file %s failed. " % fn )
traceback . print_exc ( )
if success :
if success == TestStatus . PASSED :
self . log . info ( " Tests successful " )
sys . exit ( self . TEST_EXIT_PASSED )
sys . exit ( TEST_EXIT_PASSED )
elif success == TestStatus . SKIPPED :
self . log . info ( " Test skipped " )
sys . exit ( TEST_EXIT_SKIPPED )
else :
self . log . error ( " Test failed. Test logging available at %s /test_framework.log " , self . options . tmpdir )
logging . shutdown ( )
sys . exit ( self . TEST_EXIT_FAILED )
sys . exit ( TEST_EXIT_FAILED )
# Public helper methods. These can be accessed by the subclass test scripts.
@ -346,6 +359,11 @@ class BitcoinTestFramework(object):
@@ -346,6 +359,11 @@ class BitcoinTestFramework(object):
# 2 binaries: 1 test binary, 1 ref binary
# n>2 binaries: 1 test binary, n-1 ref binaries
class SkipTest ( Exception ) :
""" This exception is raised to skip a test """
def __init__ ( self , message ) :
self . message = message
class ComparisonTestFramework ( BitcoinTestFramework ) :
def __init__ ( self ) :