502 lines
15 KiB
Python
Executable file
502 lines
15 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
#
|
|
# mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
|
|
# Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
|
|
# Licensed under the GNU General Public License, Version 3:
|
|
# https://www.gnu.org/licenses
|
|
# Public project repositories:
|
|
# https://github.com/mmgen/mmgen-node-tools
|
|
# https://gitlab.com/mmgen/mmgen-node-tools
|
|
|
|
"""
|
|
test.cmdtest_d.misc: Miscellaneous test groups for the cmdtest.py test suite
|
|
"""
|
|
|
|
import os, shutil
|
|
|
|
from ..include.common import cfg
|
|
from .base import CmdTestBase
|
|
from .httpd.ticker import TickerServer
|
|
|
|
refdir = os.path.join('test','ref','ticker')
|
|
|
|
class CmdTestHelp(CmdTestBase):
|
|
'help, info and usage screens'
|
|
networks = ('btc','ltc','bch')
|
|
tmpdir_nums = []
|
|
passthru_opts = ('daemon_data_dir','rpc_port','coin','testnet')
|
|
cmd_group = (
|
|
('version', (1,'version message',[])),
|
|
('helpscreens', (1,'help screens', [])),
|
|
('longhelpscreens', (1,'help screens (--longhelp)',[])),
|
|
)
|
|
color = True
|
|
|
|
def version(self):
|
|
t = self.spawn('mmnode-netrate', ['--version'])
|
|
t.expect('MMNODE-NETRATE version')
|
|
return t
|
|
|
|
def helpscreens(self,arg='--help',scripts=(),expect='USAGE:.*OPTIONS:'):
|
|
|
|
scripts = list(scripts) or [s for s in os.listdir('cmds') if s.startswith('mmnode-')]
|
|
|
|
for s in sorted(scripts):
|
|
t = self.spawn(s,[arg],extra_desc=f'({s})')
|
|
t.expect(expect,regex=True)
|
|
t.read()
|
|
t.ok()
|
|
t.skip_ok = True
|
|
|
|
return t
|
|
|
|
def longhelpscreens(self):
|
|
return self.helpscreens(arg='--longhelp',expect='USAGE:.*GLOBAL OPTIONS:')
|
|
|
|
class CmdTestScripts(CmdTestBase):
|
|
'scripts not requiring a coin daemon'
|
|
networks = ('btc',)
|
|
tmpdir_nums = [2]
|
|
passthru_opts = ()
|
|
color = True
|
|
|
|
cmd_group_in = (
|
|
('subgroup.ticker', []),
|
|
)
|
|
cmd_subgroups = {
|
|
'ticker': (
|
|
"'mmnode-ticker' script",
|
|
('ticker1', 'ticker [--help]'),
|
|
('copy_cache_files', 'copying JSON files to cache'),
|
|
('ticker1a', 'ticker [--download=cc] (early caching)'),
|
|
('ticker1b', 'ticker [--download=cc] (late caching)'),
|
|
('ticker2', 'ticker (bad proxy)'),
|
|
('ticker3', 'ticker [--cached-data]'),
|
|
('ticker4', 'ticker [--cached-data --wide]'),
|
|
('ticker5', 'ticker [--cached-data --wide --adjust=-0.766] (usr cfg file)'),
|
|
('ticker6', 'ticker [--cached-data --wide --portfolio] (missing portfolio)'),
|
|
('ticker7', 'ticker [--cached-data --wide --portfolio]'),
|
|
('ticker8', 'ticker [--cached-data --wide --elapsed]'),
|
|
('ticker9', 'ticker [--cached-data --wide --portfolio --elapsed --add-rows=fake-fakecoin:0.0123 --add-precision=2]'),
|
|
('ticker10', 'ticker [--cached-data xmr:17.234]'),
|
|
('ticker11', 'ticker [--cached-data xmr:17.234:btc]'),
|
|
('ticker12', 'ticker [--cached-data --adjust=1.23 xmr:17.234:btc]'),
|
|
('ticker13', 'ticker [--cached-data --wide --elapsed -c inr-indian-rupee:79.5 inr:200000:btc:0.1]'),
|
|
('ticker14', 'ticker [--cached-data --wide --btc]'),
|
|
('ticker15', 'ticker [--cached-data --wide --btc btc:2:usd:45000]'),
|
|
('ticker16', 'ticker [--cached-data --wide --elapsed -c eur,omr-omani-rial:2.59r'),
|
|
('ticker17', 'ticker [--cached-data --wide --elapsed -c bgn-bulgarian-lev:0.5113r:eur'),
|
|
('ticker18', 'ticker [--cached-data --widest --add-columns eurusd=x 10]'),
|
|
('ticker19', 'ticker [--cached-data 1-5]'),
|
|
('ticker20', 'ticker [--cached-data 2-5]'),
|
|
('ticker21', 'ticker [--cached-data 5-5]'),
|
|
('ticker22', 'ticker [--sort=rp]'),
|
|
('ticker23', 'ticker [--sort=rp xmr:10]'),
|
|
('ticker24', 'ticker [--sort=p]'),
|
|
('ticker25', 'ticker [--sort=p 200]'),
|
|
('ticker26', 'ticker [--sort=c -r algo,ada]'),
|
|
('ticker27', 'ticker [--sort=rp -r algo,ada]'),
|
|
('ticker28', 'ticker [--sort=d -r algo,ada]'),
|
|
('ticker29', 'ticker [--sort=y -r algo,ada]'),
|
|
('ticker30', 'ticker [--cached-data --wide --pchg-unit=btc --sort=d] (cf with config file)'),
|
|
('ticker31', 'ticker [--cached-data --wide --pchg-unit=usd] (cf with no USD)'),
|
|
('ticker32', 'ticker [--cached-data --wide --pchg-unit=gc=f]'),
|
|
('ticker33', 'ticker [--cached-data --wide --pchg-unit=btc --sort=c] (cfg file with USD)'),
|
|
('ticker34', 'ticker [--cached-data --wide --pchg-unit=btc --sort=y] (cfg file with USD)'),
|
|
('ticker35', 'ticker [--cached-data --wide --pchg-unit=btc --sort=p] (cfg file with USD)'),
|
|
)
|
|
}
|
|
|
|
def __init__(self, cfg, trunner, cfgs, spawn):
|
|
if not trunner:
|
|
return
|
|
self.ticker_server = TickerServer(cfg)
|
|
self.ticker_server.start()
|
|
self.dests = {
|
|
'nt_datadir': os.path.join(cfg.data_dir_root, 'node_tools'),
|
|
'cache': self.tmpdir}
|
|
return super().__init__(cfg, trunner, cfgs, spawn)
|
|
|
|
def rm_file(self, fn, dest='nt_datadir'):
|
|
os.unlink(os.path.join(self.dests[dest], fn))
|
|
|
|
def copy_file(self, src_fn, dest_fn=None, dest='nt_datadir'):
|
|
shutil.copy2(
|
|
os.path.join(refdir, src_fn),
|
|
os.path.join(self.dests[dest], dest_fn or src_fn))
|
|
|
|
def copy_cache_files(self):
|
|
self.spawn('', msg_only=True)
|
|
self.copy_file('ticker-finance.json', dest='cache')
|
|
self.copy_file('ticker-finance-history.json', dest='cache')
|
|
self.copy_file('ticker-btc.json', dest='cache')
|
|
return 'ok'
|
|
|
|
def ticker(
|
|
self,
|
|
args = [],
|
|
expect_list = None,
|
|
cached_data = True,
|
|
add_opts = [],
|
|
use_proxy = True,
|
|
no_msg = False,
|
|
exit_val = None):
|
|
t = self.spawn(
|
|
'mmnode-ticker',
|
|
(['--cached-data'] if cached_data else [])
|
|
+ [f'--cachedir={self.tmpdir}']
|
|
+ (['--proxy=http://asdfzxcv:32459'] if use_proxy else [])
|
|
+ add_opts
|
|
+ args,
|
|
no_msg = no_msg,
|
|
exit_val = exit_val)
|
|
if expect_list:
|
|
t.match_expect_list(expect_list)
|
|
return t
|
|
|
|
def ticker1(self):
|
|
t = self.ticker(['--help'])
|
|
t.expect('USAGE:')
|
|
return t
|
|
|
|
def ticker1a(self, first_run=True):
|
|
t = self.ticker(
|
|
add_opts = ['--proxy', '', '--download=cc'],
|
|
cached_data = False,
|
|
use_proxy = False)
|
|
if first_run and not cfg.skipping_deps:
|
|
t.expect('Creating')
|
|
t.expect('Creating')
|
|
return t
|
|
|
|
def ticker1b(self):
|
|
return self.ticker1a(first_run=False)
|
|
|
|
def ticker2(self):
|
|
t = self.ticker(cached_data=False)
|
|
ret = t.expect(['proxy host could not be resolved', 'unexpected keyword'])
|
|
t.exit_val = 1 if ret else 3
|
|
return t
|
|
|
|
def ticker3(self):
|
|
return self.ticker(
|
|
[],
|
|
[
|
|
'USD BTC',
|
|
'BTC 23250.77 1.00000000 ETH 1659.66 0.07138094'
|
|
])
|
|
|
|
def ticker4(self):
|
|
return self.ticker(
|
|
['--widest','--add-columns=eurusd=x,inr-indian-rupee:79.5'],
|
|
[
|
|
r'EURUSD=X \(EUR/USD\) = 1.0642 USD ' +
|
|
r'INR \(INDIAN RUPEE\) = 0.012579 USD',
|
|
'USD EURUSD=X INR BTC CHG_1y CHG_30d CHG_7d CHG_24h UPDATED',
|
|
'BITCOIN',
|
|
r'ETHEREUM 1,659.66 1,559.5846 131,943.14 0.07138094 \+36.41 \+29.99 \+21.42 \+1.82',
|
|
r'MONERO 158.97 149.3870 12,638.36 0.00683732 \+12.38 \+10.19 \+7.28 \+1.21 2022-08-02 18:25:59',
|
|
r'S&P 500 4,320.06 4,059.5604 343,444.77 0.18580285 -1.71 \+12.93 \+9.05 -0.23',
|
|
r'INDIAN RUPEE 0.01 0.0118 1.00 0.00000054 -- -- -- --',
|
|
])
|
|
|
|
def ticker5(self):
|
|
self.copy_file('ticker-cfg.yaml')
|
|
t = self.ticker(
|
|
['--wide','--adjust=-0.766'],
|
|
[
|
|
'Adjusting prices by -0.77%',
|
|
'USD BTC CHG_7d CHG_24h UPDATED',
|
|
r'LITECOIN 58.56 0.00251869 \+12.79 \+0.40 2022-08-02 18:25:59',
|
|
r'MONERO 157.76 0.00678495 \+7.28 \+1.21'
|
|
])
|
|
self.rm_file('ticker-cfg.yaml')
|
|
return t
|
|
|
|
def ticker6(self):
|
|
t = self.ticker(['--wide','--portfolio'], None, exit_val=1)
|
|
t.expect('No portfolio')
|
|
return t
|
|
|
|
def ticker7(self): # demo
|
|
self.copy_file('ticker-portfolio.yaml')
|
|
t = self.ticker(
|
|
['--wide','--portfolio'],
|
|
[
|
|
'USD BTC CHG_7d CHG_24h UPDATED',
|
|
r'ETHEREUM 1,659.66 0.07138094 \+21.42 \+1.82 2022-08-02 18:25:59',
|
|
'CARDANO','ALGORAND',
|
|
'PORTFOLIO','BITCOIN','ETHEREUM','MONERO','CARDANO','ALGORAND','TOTAL'
|
|
])
|
|
self.rm_file('ticker-portfolio.yaml')
|
|
return t
|
|
|
|
def ticker8(self):
|
|
return self.ticker(
|
|
['--wide','--elapsed'],
|
|
[
|
|
'USD BTC CHG_7d CHG_24h UPDATED',
|
|
r'BITCOIN 23,250.77 1.00000000 \+11.15 \+0.89 10 minutes ago'
|
|
])
|
|
|
|
def ticker9(self):
|
|
self.copy_file('ticker-portfolio-bad.yaml', 'ticker-portfolio.yaml')
|
|
t = self.ticker(
|
|
['--wide','--portfolio','--elapsed','--add-rows=fake-fakecoin:0.0123','--add-precision=2'],
|
|
[
|
|
'USD BTC CHG_7d CHG_24h UPDATED',
|
|
r'BITCOIN 23,250.7741 1.0000000000 \+11.15 \+0.89 10 minutes ago',
|
|
r'FAKECOIN 81.3008 0.0034966927 -- -- --',
|
|
r'\(no data for noc-nocoin\)',
|
|
])
|
|
self.rm_file('ticker-portfolio.yaml')
|
|
return t
|
|
|
|
def ticker10(self):
|
|
return self.ticker(
|
|
['XMR:17.234'],
|
|
[
|
|
r'XMR \(MONERO\) = 158.97 USD ' +
|
|
'Amount: 17.234 XMR',
|
|
'SPOT PRICE',
|
|
'BTC 0.11783441',
|
|
'XMR 17.23400000',
|
|
'GC=F',r'\^IXIC',
|
|
])
|
|
|
|
def ticker11(self):
|
|
return self.ticker(
|
|
['XMR:17.234:BTC'],
|
|
[
|
|
r'XMR \(MONERO\) = 158.97 USD ' +
|
|
r'BTC \(BITCOIN\) = 23250.77 USD ' +
|
|
'Amount: 17.234 XMR',
|
|
'SPOT PRICE',
|
|
'XMR 17.23400000 BTC 0.11783441',
|
|
])
|
|
|
|
def ticker12(self):
|
|
return self.ticker(
|
|
['--adjust=1.23','--wide','XMR:17.234:BTC'],
|
|
[
|
|
r'XMR \(MONERO\) = 158.97 USD ' +
|
|
r'BTC \(BITCOIN\) = 23,250.77 USD ' +
|
|
'Amount: 17.234 XMR',
|
|
r'Adjusting prices by \+1.23%',
|
|
'SPOT PRICE ADJUSTED PRICE',
|
|
'MONERO 17.23400000 17.44597820 2022-08-02 18:25:59 ' +
|
|
'BITCOIN 0.11783441 0.11928377 2022-08-02 18:25:59',
|
|
])
|
|
|
|
def ticker13(self):
|
|
return self.ticker(
|
|
['-wE','-c','inr-indian-rupee:79.5','inr:200000:btc:0.1'],
|
|
[
|
|
'Offer: 200,000 INR',
|
|
'Offered price differs from spot by -7.58%',
|
|
'SPOT PRICE OFFERED PRICE UPDATED',
|
|
'INDIAN RUPEE 200,000.00000000 184,843.65372424 -- ' +
|
|
'BITCOIN 0.10819955 0.10000000 10 minutes ago'
|
|
])
|
|
|
|
def ticker14(self):
|
|
self.copy_file('ticker-portfolio.yaml')
|
|
t = self.ticker(
|
|
['--btc','--wide','--portfolio','--elapsed'],
|
|
[
|
|
'PRICES',
|
|
r'BITCOIN 23,368.86 \+6.05 -1.87 1 day 9 hours 2 minutes ago',
|
|
'PORTFOLIO',
|
|
r'BITCOIN 28,850.44 \+6.05 -1.87 1.23456789'
|
|
])
|
|
self.rm_file('ticker-portfolio.yaml')
|
|
return t
|
|
|
|
def ticker15(self):
|
|
return self.ticker(
|
|
['--btc','--wide','--elapsed','-r','inr:79.5','btc:2:usd:45000'],
|
|
[
|
|
r'BTC \(BITCOIN\) = 23,368.86 USD',
|
|
'Offered price differs from spot by -3.72%',
|
|
'SPOT PRICE OFFERED PRICE UPDATED',
|
|
'BITCOIN 2.00000000 1.92563954 1 day 9 hours 2 minutes ago ' +
|
|
'US DOLLAR 46,737.71911598 45,000.00000000 --',
|
|
])
|
|
|
|
def ticker16(self):
|
|
return self.ticker(
|
|
['--wide','--elapsed','-c','eurusd=x,omr-omani-rial:2.59r'],
|
|
[
|
|
r'EURUSD=X \(EUR/USD\) = 1.0642 USD ' +
|
|
r'OMR \(OMANI RIAL\) = 2.5900 USD',
|
|
'USD EURUSD=X OMR BTC CHG_7d CHG_24h UPDATED',
|
|
r'BITCOIN 23,250.77 21,848.7527 8,977.1328 1.00000000 \+11.15 \+0.89 10 minutes ago',
|
|
'OMANI RIAL 2.59 2.4338 1.0000 0.00011139 -- -- --'
|
|
])
|
|
|
|
def ticker17(self):
|
|
# BGN pegged at 0.5113 EUR
|
|
return self.ticker(
|
|
['--wide','--elapsed','-c','bgn-bulgarian-lev:0.5113r:eurusd=x'],
|
|
[
|
|
r'BGN \(BULGARIAN LEV\) = 0.54411 USD',
|
|
'USD BGN BTC CHG_7d CHG_24h UPDATED',
|
|
'BITCOIN 23,250.77 42,731.767 1.00000000',
|
|
'BULGARIAN LEV 0.54 1.000 0.00002340',
|
|
])
|
|
|
|
def ticker18(self):
|
|
return self.ticker(
|
|
['10'],
|
|
[
|
|
r'1\) BITCOIN 444.33652 23,250.77 21,848.7527 1.00000000 \+18.96 \+15.61 \+11.15 \+0.89',
|
|
r'33\) ALGORAND 2.30691 0.33 0.3120 0.00001428 \+16.47 \+13.57 \+9.69 \-0.82'
|
|
],
|
|
add_opts = ['--widest', '--add-columns=eurusd=x'])
|
|
|
|
def ticker19(self):
|
|
return self.ticker(
|
|
['1-5'],
|
|
[
|
|
r'MarketCap\(B\) USD EURUSD=X BTC '
|
|
'---------------------------------------------------------- '
|
|
r'1\) BTC 444.33652 23250.77 21848.7527 1.00000000',
|
|
r'8\) ADA 17.11161 0.51 0.4764 0.00002180'
|
|
' ----------------------------------------------------------'
|
|
],
|
|
add_opts = ['--add-columns=eurusd=x'])
|
|
|
|
def ticker20(self):
|
|
return self.ticker(
|
|
['2-5'],
|
|
[
|
|
r'MarketCap\(B\) USD EURUSD=X BTC '
|
|
'---------------------------------------------------------- '
|
|
r'2\) ETH 202.15129 1659.66 1559.5846 0.07138094',
|
|
r'8\) ADA 17.11161 0.51 0.4764 0.00002180',
|
|
],
|
|
add_opts = ['--add-columns=eurusd=x'])
|
|
|
|
def ticker21(self):
|
|
return self.ticker(
|
|
['5-5'],
|
|
[
|
|
r'MarketCap\(B\) USD EURUSD=X BTC '
|
|
'--------------------------------------------------------- '
|
|
r'8\) ADA 17.11161 0.51 0.4764 0.00002180',
|
|
],
|
|
add_opts = ['--add-columns=eurusd=x'])
|
|
|
|
def ticker22(self):
|
|
self.copy_file('ticker-cfg-bad.yaml', 'ticker-cfg.yaml')
|
|
t = self.ticker(
|
|
[],
|
|
['MONERO', 'ETHEREUM', 'BITCOIN', 'SILVER', 'BRENT', 'GOLD'],
|
|
add_opts = ['--name-labels', '--sort=rp'])
|
|
self.rm_file('ticker-cfg.yaml')
|
|
return t
|
|
|
|
def ticker23(self):
|
|
return self.ticker(
|
|
[],
|
|
['MONERO', 'ETHEREUM', 'BITCOIN', 'SILVER', 'BRENT', 'GOLD'],
|
|
add_opts = ['--name-labels', '--sort=rp', 'xmr:10'])
|
|
|
|
def ticker24(self):
|
|
return self.ticker(
|
|
[],
|
|
['BITCOIN', 'ETHEREUM', 'MONERO', 'GOLD', 'BRENT', 'SILVER'],
|
|
add_opts = ['--name-labels', '--sort=p'])
|
|
|
|
def ticker25(self):
|
|
return self.ticker(
|
|
[],
|
|
[
|
|
r' 1\) BITCOIN',
|
|
r' 2\) ETHEREUM',
|
|
r'30\) MONERO',
|
|
r'23\) LITECOIN',
|
|
r' 8\) CARDANO',
|
|
r'33\) ALGORAND'
|
|
],
|
|
add_opts = ['--name-labels', '--sort=p', '200'])
|
|
|
|
def ticker26(self):
|
|
return self.ticker(
|
|
[],
|
|
['BITCOIN', 'ETHEREUM', 'MONERO', 'CARDANO', 'ALGORAND'],
|
|
add_opts = ['--name-labels', '--sort=c', '-r', 'ada,algo'])
|
|
|
|
def ticker27(self):
|
|
return self.ticker(
|
|
[],
|
|
['MONERO', 'ETHEREUM', 'BITCOIN', 'S&P', 'NASDAQ', 'DOW', 'ALGORAND', 'CARDANO'],
|
|
add_opts = ['--name-labels', '--sort=rp', '--add-rows=ada-cardano,algo-algorand'])
|
|
|
|
def ticker28(self):
|
|
return self.ticker(
|
|
[],
|
|
['ETHEREUM', 'MONERO', 'BITCOIN', 'NASDAQ', 'S&P', 'DOW', 'CARDANO', 'ALGORAND'],
|
|
add_opts = ['--widest', '--sort=d', '-r', 'ada,algo'])
|
|
|
|
def ticker29(self):
|
|
return self.ticker(
|
|
[],
|
|
['ETHEREUM', 'BITCOIN', 'MONERO', 'S&P', 'DOW', 'NASDAQ', 'CARDANO', 'ALGORAND'],
|
|
add_opts = ['--widest', '-s', 'y', '-r', 'ada,algo'])
|
|
|
|
def ticker30(self):
|
|
self.copy_file('ticker-cfg-sort-pchg.yaml', 'ticker-cfg.yaml')
|
|
t = self.ticker(add_opts=['--wide'])
|
|
chk1 = '\n'.join(t.read().splitlines()[5:-2])
|
|
self.rm_file('ticker-cfg.yaml')
|
|
|
|
self.copy_file('ticker-cfg-bad.yaml', 'ticker-cfg.yaml')
|
|
t = self.ticker(add_opts=['--wide', '--pchg-unit=btc', '--sort=d'], no_msg=True)
|
|
chk2 = '\n'.join(t.read().splitlines()[5:-2])
|
|
self.rm_file('ticker-cfg.yaml')
|
|
|
|
assert chk1 == chk2, f'\nOUTPUT 1\n{chk1}\n!= OUTPUT 2\n{chk2}\n'
|
|
return t
|
|
|
|
def ticker31(self):
|
|
t = self.ticker(add_opts=['--wide'])
|
|
chk1 = '\n'.join(t.read().splitlines()[5:-2])
|
|
|
|
t = self.ticker(add_opts=['--wide', '--pchg-unit=usd'], no_msg=True)
|
|
chk2 = '\n'.join(t.read().splitlines()[6:-2])
|
|
|
|
assert chk1 == chk2, f'\nOUTPUT 1\n{chk1}\n!= OUTPUT 2\n{chk2}\n'
|
|
return t
|
|
|
|
def ticker32(self):
|
|
return self.ticker(
|
|
[],
|
|
[
|
|
'BITCOIN', r'\+10.99', r'\+7.06', '-1.18', r'\+1.05',
|
|
'ETHEREUM',
|
|
'GOLD', r'\+0.00', r'\+0.00', r'\+0.00', r'\+0.00',
|
|
'SILVER'
|
|
],
|
|
add_opts = ['--widest', '--pchg-unit=gc=f', '--sort=c'])
|
|
|
|
def _ticker_cur(self, sort):
|
|
self.copy_file('ticker-cfg-usd.yaml', 'ticker-cfg.yaml')
|
|
t = self.ticker(
|
|
[],
|
|
[
|
|
'BITCOIN 23,250.77 1.00000000 \+0.00',
|
|
'US DOLLAR 1.00 0.00004301 -15.93',
|
|
],
|
|
add_opts = ['--widest', '--pchg-unit=btc', f'--sort={sort}'])
|
|
self.rm_file('ticker-cfg.yaml')
|
|
return t
|
|
|
|
def ticker33(self):
|
|
return self._ticker_cur(sort='c')
|
|
|
|
def ticker34(self):
|
|
return self._ticker_cur(sort='y')
|
|
|
|
def ticker35(self):
|
|
return self._ticker_cur(sort='p')
|