Skip to content

Commit 1aefc94

Browse files
author
MarcoFalke
committed
Merge #10423: [tests] skipped tests should clean up after themselves
930deb9 [tests] skipped tests should clean up after themselves (John Newbery) Tree-SHA512: ed486c1cf144c223efa738a66e8bb407089a23002871ef5b15c43d4c08641038a20253fc286853b457d66f37a52c81c16f7988aef90635ac262df6ce7bab9b18
2 parents 00d3692 + 930deb9 commit 1aefc94

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

test/functional/rpcbind_test.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import socket
88
import sys
99

10-
from test_framework.test_framework import BitcoinTestFramework
10+
from test_framework.test_framework import BitcoinTestFramework, SkipTest
1111
from test_framework.util import *
1212
from test_framework.netutil import *
1313

@@ -56,24 +56,21 @@ def run_allowip_test(self, allow_ips, rpchost, rpcport):
5656
def run_test(self):
5757
# due to OS-specific network stats queries, this test works only on Linux
5858
if not sys.platform.startswith('linux'):
59-
self.log.warning("This test can only be run on linux. Skipping test.")
60-
sys.exit(self.TEST_EXIT_SKIPPED)
59+
raise SkipTest("This test can only be run on linux.")
6160
# find the first non-loopback interface for testing
6261
non_loopback_ip = None
6362
for name,ip in all_interfaces():
6463
if ip != '127.0.0.1':
6564
non_loopback_ip = ip
6665
break
6766
if non_loopback_ip is None:
68-
self.log.warning("This test requires at least one non-loopback IPv4 interface. Skipping test.")
69-
sys.exit(self.TEST_EXIT_SKIPPED)
67+
raise SkipTest("This test requires at least one non-loopback IPv4 interface.")
7068
try:
7169
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
7270
s.connect(("::1",1))
7371
s.close
7472
except OSError:
75-
self.log.warning("This test requires IPv6 support. Skipping test.")
76-
sys.exit(self.TEST_EXIT_SKIPPED)
73+
raise SkipTest("This test requires IPv6 support.")
7774

7875
self.log.info("Using interface %s for testing" % non_loopback_ip)
7976

test/functional/test_framework/test_framework.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""Base class for RPC testing."""
66

77
from collections import deque
8+
from enum import Enum
89
import logging
910
import optparse
1011
import os
@@ -41,6 +42,15 @@
4142
)
4243
from .authproxy import JSONRPCException
4344

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+
4454
class BitcoinTestFramework(object):
4555
"""Base class for a bitcoin test script.
4656
@@ -57,11 +67,6 @@ class BitcoinTestFramework(object):
5767
This class also contains various public and private helper methods."""
5868

5969
# Methods to override in subclass test scripts.
60-
61-
TEST_EXIT_PASSED = 0
62-
TEST_EXIT_FAILED = 1
63-
TEST_EXIT_SKIPPED = 77
64-
6570
def __init__(self):
6671
self.num_nodes = 4
6772
self.setup_clean_chain = False
@@ -139,15 +144,18 @@ def main(self):
139144
self.options.tmpdir = tempfile.mkdtemp(prefix="test")
140145
self._start_logging()
141146

142-
success = False
147+
success = TestStatus.FAILED
143148

144149
try:
145150
self.setup_chain()
146151
self.setup_network()
147152
self.run_test()
148-
success = True
153+
success = TestStatus.PASSED
149154
except JSONRPCException as e:
150155
self.log.exception("JSONRPC error")
156+
except SkipTest as e:
157+
self.log.warning("Test Skipped: %s" % e.message)
158+
success = TestStatus.SKIPPED
151159
except AssertionError as e:
152160
self.log.exception("Assertion failed")
153161
except KeyError as e:
@@ -159,11 +167,12 @@ def main(self):
159167

160168
if not self.options.noshutdown:
161169
self.log.info("Stopping nodes")
162-
self.stop_nodes()
170+
if self.nodes:
171+
self.stop_nodes()
163172
else:
164173
self.log.info("Note: bitcoinds were not stopped and may still be running")
165174

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:
167176
self.log.info("Cleaning up")
168177
shutil.rmtree(self.options.tmpdir)
169178
else:
@@ -183,13 +192,17 @@ def main(self):
183192
except OSError:
184193
print("Opening file %s failed." % fn)
185194
traceback.print_exc()
186-
if success:
195+
196+
if success == TestStatus.PASSED:
187197
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)
189202
else:
190203
self.log.error("Test failed. Test logging available at %s/test_framework.log", self.options.tmpdir)
191204
logging.shutdown()
192-
sys.exit(self.TEST_EXIT_FAILED)
205+
sys.exit(TEST_EXIT_FAILED)
193206

194207
# Public helper methods. These can be accessed by the subclass test scripts.
195208

@@ -346,6 +359,11 @@ def _initialize_chain_clean(self, test_dir, num_nodes):
346359
# 2 binaries: 1 test binary, 1 ref binary
347360
# n>2 binaries: 1 test binary, n-1 ref binaries
348361

362+
class SkipTest(Exception):
363+
"""This exception is raised to skip a test"""
364+
def __init__(self, message):
365+
self.message = message
366+
349367
class ComparisonTestFramework(BitcoinTestFramework):
350368

351369
def __init__(self):

test/functional/zmq_test.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
import configparser
77
import os
88
import struct
9-
import sys
109

11-
from test_framework.test_framework import BitcoinTestFramework
10+
from test_framework.test_framework import BitcoinTestFramework, SkipTest
1211
from test_framework.util import *
1312

1413
class ZMQTest (BitcoinTestFramework):
@@ -24,8 +23,7 @@ def setup_nodes(self):
2423
try:
2524
import zmq
2625
except ImportError:
27-
self.log.warning("python3-zmq module not available. Skipping zmq tests!")
28-
sys.exit(self.TEST_EXIT_SKIPPED)
26+
raise SkipTest("python3-zmq module not available.")
2927

3028
# Check that bitcoin has been built with ZMQ enabled
3129
config = configparser.ConfigParser()
@@ -34,8 +32,7 @@ def setup_nodes(self):
3432
config.read_file(open(self.options.configfile))
3533

3634
if not config["components"].getboolean("ENABLE_ZMQ"):
37-
self.log.warning("bitcoind has not been built with zmq enabled. Skipping zmq tests!")
38-
sys.exit(self.TEST_EXIT_SKIPPED)
35+
raise SkipTest("bitcoind has not been built with zmq enabled.")
3936

4037
self.zmqContext = zmq.Context()
4138
self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)

0 commit comments

Comments
 (0)