5
5
"""Base class for RPC testing."""
6
6
7
7
from collections import deque
8
+ from enum import Enum
8
9
import logging
9
10
import optparse
10
11
import os
41
42
)
42
43
from .authproxy import JSONRPCException
43
44
45
+ class TestStatus (Enum ):
46
+ PASSED = 1
47
+ FAILED = 2
48
+ SKIPPED = 3
49
+
50
+ TEST_EXIT_PASSED = 0
51
+ TEST_EXIT_FAILED = 1
52
+ TEST_EXIT_SKIPPED = 77
53
+
44
54
class BitcoinTestFramework (object ):
45
55
"""Base class for a bitcoin test script.
46
56
@@ -57,11 +67,6 @@ class BitcoinTestFramework(object):
57
67
This class also contains various public and private helper methods."""
58
68
59
69
# Methods to override in subclass test scripts.
60
-
61
- TEST_EXIT_PASSED = 0
62
- TEST_EXIT_FAILED = 1
63
- TEST_EXIT_SKIPPED = 77
64
-
65
70
def __init__ (self ):
66
71
self .num_nodes = 4
67
72
self .setup_clean_chain = False
@@ -139,15 +144,18 @@ def main(self):
139
144
self .options .tmpdir = tempfile .mkdtemp (prefix = "test" )
140
145
self ._start_logging ()
141
146
142
- success = False
147
+ success = TestStatus . FAILED
143
148
144
149
try :
145
150
self .setup_chain ()
146
151
self .setup_network ()
147
152
self .run_test ()
148
- success = True
153
+ success = TestStatus . PASSED
149
154
except JSONRPCException as e :
150
155
self .log .exception ("JSONRPC error" )
156
+ except SkipTest as e :
157
+ self .log .warning ("Test Skipped: %s" % e .message )
158
+ success = TestStatus .SKIPPED
151
159
except AssertionError as e :
152
160
self .log .exception ("Assertion failed" )
153
161
except KeyError as e :
@@ -159,11 +167,12 @@ def main(self):
159
167
160
168
if not self .options .noshutdown :
161
169
self .log .info ("Stopping nodes" )
162
- self .stop_nodes ()
170
+ if self .nodes :
171
+ self .stop_nodes ()
163
172
else :
164
173
self .log .info ("Note: bitcoinds were not stopped and may still be running" )
165
174
166
- if not self .options .nocleanup and not self .options .noshutdown and success :
175
+ if not self .options .nocleanup and not self .options .noshutdown and success != TestStatus . FAILED :
167
176
self .log .info ("Cleaning up" )
168
177
shutil .rmtree (self .options .tmpdir )
169
178
else :
@@ -183,13 +192,17 @@ def main(self):
183
192
except OSError :
184
193
print ("Opening file %s failed." % fn )
185
194
traceback .print_exc ()
186
- if success :
195
+
196
+ if success == TestStatus .PASSED :
187
197
self .log .info ("Tests successful" )
188
- sys .exit (self .TEST_EXIT_PASSED )
198
+ sys .exit (TEST_EXIT_PASSED )
199
+ elif success == TestStatus .SKIPPED :
200
+ self .log .info ("Test skipped" )
201
+ sys .exit (TEST_EXIT_SKIPPED )
189
202
else :
190
203
self .log .error ("Test failed. Test logging available at %s/test_framework.log" , self .options .tmpdir )
191
204
logging .shutdown ()
192
- sys .exit (self . TEST_EXIT_FAILED )
205
+ sys .exit (TEST_EXIT_FAILED )
193
206
194
207
# Public helper methods. These can be accessed by the subclass test scripts.
195
208
@@ -346,6 +359,11 @@ def _initialize_chain_clean(self, test_dir, num_nodes):
346
359
# 2 binaries: 1 test binary, 1 ref binary
347
360
# n>2 binaries: 1 test binary, n-1 ref binaries
348
361
362
+ class SkipTest (Exception ):
363
+ """This exception is raised to skip a test"""
364
+ def __init__ (self , message ):
365
+ self .message = message
366
+
349
367
class ComparisonTestFramework (BitcoinTestFramework ):
350
368
351
369
def __init__ (self ):
0 commit comments