forked from JoinMarket-Org/joinmarket-clientserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
126 lines (111 loc) · 4.88 KB
/
conftest.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from builtins import *
import pytest
import re
import os
import time
import subprocess
bitcoin_path = None
bitcoin_conf = None
bitcoin_rpcpassword = None
bitcoin_rpcusername = None
miniircd_procs = []
def get_bitcoind_version(version_string):
# this utility function returns the version number
# as a tuple in the form (major, minor, patch)
version_tuple = re.match(
b'.*v(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)',
version_string).groups()
return tuple(map(lambda x: int(x), version_tuple))
def local_command(command, bg=False, redirect=''):
if redirect == 'NULL':
if OS == 'Windows':
command.append(' > NUL 2>&1')
elif OS == 'Linux':
command.extend(['>', '/dev/null', '2>&1'])
else:
print("OS not recognised, quitting.")
elif redirect:
command.extend(['>', redirect])
if bg:
#using subprocess.PIPE seems to cause problems
FNULL = open(os.devnull, 'w')
return subprocess.Popen(command,
stdout=FNULL,
stderr=subprocess.STDOUT,
close_fds=True)
else:
#in case of foreground execution, we can use the output; if not
#it doesn't matter
return subprocess.check_output(command)
def root_path():
# returns the directory in which this file is contained
return os.path.dirname(os.path.realpath(__file__))
def pytest_addoption(parser):
parser.addoption("--btcroot", action="store", default='',
help="the fully qualified path to the directory containing "+\
"the bitcoin binaries, e.g. /home/user/bitcoin/bin/")
parser.addoption("--btcconf", action="store",
default=os.path.join(root_path(), 'test/bitcoin.conf'),
help="the fully qualified path to the location of the "+\
"bitcoin configuration file you use for testing, e.g. "+\
"/home/user/.bitcoin/bitcoin.conf")
parser.addoption("--btcpwd",
action="store",
help="the RPC password for your test bitcoin instance")
parser.addoption("--btcuser",
action="store",
default='bitcoinrpc',
help="the RPC username for your test bitcoin instance (default=bitcoinrpc)")
parser.addoption("--nirc",
type="int",
action="store",
default=1,
help="the number of local miniircd instances")
def teardown():
#didn't find a stop command in miniircd, so just kill
global miniircd_procs
for m in miniircd_procs:
m.kill()
#shut down bitcoin and remove the regtest dir
local_command([bitcoin_path + "bitcoin-cli", "-regtest", "-rpcuser=" + bitcoin_rpcusername,
"-rpcpassword=" + bitcoin_rpcpassword, "stop"])
#note, it is better to clean out ~/.bitcoin/regtest but too
#dangerous to automate it here perhaps
@pytest.fixture(scope="session", autouse=True)
def setup(request):
request.addfinalizer(teardown)
global bitcoin_conf, bitcoin_path, bitcoin_rpcpassword, bitcoin_rpcusername
bitcoin_path = request.config.getoption("--btcroot")
bitcoin_conf = request.config.getoption("--btcconf")
print("Here is the bitcoin_conf path:")
print(bitcoin_conf)
bitcoin_rpcpassword = request.config.getoption("--btcpwd")
bitcoin_rpcusername = request.config.getoption("--btcuser")
#start up miniircd
#minor bug in miniircd (seems); need *full* unqualified path for motd file
cwd = os.getcwd()
n_irc = request.config.getoption("--nirc")
global miniircd_procs
for i in range(n_irc):
miniircd_proc = local_command(
["./miniircd/miniircd", "--ports=" + str(6667+i),
"--motd=" + cwd + "/miniircd/testmotd"],
bg=True)
miniircd_procs.append(miniircd_proc)
# determine bitcoind version
bitcoind_version_string = subprocess.check_output([bitcoin_path + "bitcoind", "-version"]).split(b'\n')[0]
bitcoind_version = get_bitcoind_version(bitcoind_version_string)
#start up regtest blockchain
bitcoin_args = ["-regtest", "-daemon", "-conf=" + bitcoin_conf]
btc_proc = subprocess.call([bitcoin_path + "bitcoind"] + bitcoin_args)
time.sleep(4)
#generate blocks; segwit activates around block 500-600
root_cmd = [bitcoin_path + "bitcoin-cli", "-regtest",
"-rpcuser=" + bitcoin_rpcusername,
"-rpcpassword=" + bitcoin_rpcpassword]
for i in range(2):
destn_addr = local_command(root_cmd + ["getnewaddress"])
local_command(root_cmd + ["generatetoaddress", "301", destn_addr])
time.sleep(1)