exec.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python3
  2. """
  3. test.daemontest_d.exec: unit test for the MMGen suite's Daemon class
  4. """
  5. from subprocess import run, PIPE
  6. from collections import namedtuple
  7. from mmgen.color import orange, red
  8. from mmgen.util import fmt_list
  9. from mmgen.daemon import CoinDaemon
  10. from ..include.common import cfg, qmsg, qmsg_r, vmsg, msg, msg_r
  11. def test_flags(coin):
  12. d = CoinDaemon(cfg, network_id=coin)
  13. vmsg(f'Available opts: {fmt_list(d.avail_opts, fmt="bare")}')
  14. vmsg(f'Available flags: {fmt_list(d.avail_flags, fmt="bare")}')
  15. vals = namedtuple('vals', ['online', 'no_daemonize', 'keep_cfg_file'])
  16. def gen():
  17. for opts, flags, val in (
  18. (None, None, vals(False, False, False)),
  19. (None, ['keep_cfg_file'], vals(False, False, True)),
  20. (['online'], ['keep_cfg_file'], vals(True, False, True)),
  21. (['online', 'no_daemonize'], ['keep_cfg_file'], vals(True, True, True)),
  22. ):
  23. d = CoinDaemon(cfg, network_id=coin, opts=opts, flags=flags)
  24. assert d.flag.keep_cfg_file == val.keep_cfg_file
  25. assert d.opt.online == val.online
  26. assert d.opt.no_daemonize == val.no_daemonize
  27. d.flag.keep_cfg_file = not val.keep_cfg_file
  28. d.flag.keep_cfg_file = val.keep_cfg_file
  29. yield d
  30. return tuple(gen())
  31. def test_flags_err(ut, d):
  32. def bad1(): d[0].flag.foo = False
  33. def bad2(): d[0].opt.foo = False
  34. def bad3(): d[0].opt.no_daemonize = True
  35. def bad4(): d[0].flag.keep_cfg_file = 'x'
  36. def bad5(): d[0].opt.no_daemonize = 'x'
  37. def bad6(): d[0].flag.keep_cfg_file = False
  38. def bad7(): d[1].flag.keep_cfg_file = True
  39. ut.process_bad_data((
  40. ('flag (1)', 'ClassFlagsError', 'unrecognized flag', bad1),
  41. ('opt (1)', 'ClassFlagsError', 'unrecognized opt', bad2),
  42. ('opt (2)', 'AttributeError', 'is read-only', bad3),
  43. ('flag (2)', 'AssertionError', 'not boolean', bad4),
  44. ('opt (3)', 'AttributeError', 'is read-only', bad5),
  45. ('flag (3)', 'ClassFlagsError', 'not set', bad6),
  46. ('flag (4)', 'ClassFlagsError', 'already set', bad7),
  47. ))
  48. class unit_tests:
  49. win_skip = ('start', 'status', 'stop')
  50. altcoin_deps = ('flags_eth',)
  51. def _pre(self):
  52. self.daemon_ctrl_args = ['btc', 'btc_tn', 'btc_rt'] if cfg.no_altcoin_deps else ['all']
  53. def _test_cmd(self, args_in, network_ids=[], ok=True):
  54. args = (
  55. ['python3', f'test/{args_in[0]}-coin-daemons.py']
  56. + list(args_in[1:])
  57. + (network_ids or self.daemon_ctrl_args))
  58. vmsg('\n' + orange(f"Running '{' '.join(args)}':"))
  59. cp = run(args, stdout=PIPE, stderr=PIPE, text=True)
  60. if cp.returncode != 0:
  61. if cp.stdout:
  62. msg(cp.stdout)
  63. if cp.stderr:
  64. msg(red(cp.stderr))
  65. return False
  66. if cfg.verbose:
  67. msg_r(cp.stderr.strip())
  68. if ok:
  69. vmsg('')
  70. qmsg('OK')
  71. return True
  72. def flags(self, name, ut):
  73. qmsg_r('Testing flags and opts (BTC)...')
  74. vmsg('')
  75. daemons = test_flags(coin='btc')
  76. qmsg('OK')
  77. qmsg_r('Testing error handling for flags and opts...')
  78. vmsg('')
  79. test_flags_err(ut, daemons)
  80. qmsg('OK')
  81. return True
  82. def flags_eth(self, name, ut):
  83. qmsg_r('Testing flags and opts (ETH)...')
  84. vmsg('')
  85. daemons = test_flags(coin='eth')
  86. qmsg('OK')
  87. qmsg_r('Testing error handling for flags and opts...')
  88. vmsg('')
  89. test_flags_err(ut, daemons)
  90. qmsg('OK')
  91. return True
  92. def avail(self, name, ut):
  93. qmsg_r('Testing availability of coin daemons...')
  94. return self._test_cmd(['start', '--print-version', '--mainnet-only'])
  95. def versions(self, name, ut):
  96. qmsg_r('Displaying coin daemon versions...')
  97. ret1 = self._test_cmd(['start', '--print-version'], ok=False)
  98. ret2 = self._test_cmd(['start', '--print-version', '--mainnet-only'])
  99. return ret1 and ret2
  100. def cmds(self, name, ut):
  101. qmsg_r('Testing start commands for coin daemons...')
  102. return self._test_cmd(['start', '--testing'])
  103. def start(self, name, ut):
  104. qmsg_r('Starting coin daemons...')
  105. return self._test_cmd(['start'])
  106. def status(self, name, ut):
  107. qmsg_r('Checking status of coin daemons...')
  108. return self._test_cmd(['start'])
  109. def stop(self, name, ut):
  110. qmsg_r('Stopping coin daemons...')
  111. return self._test_cmd(['stop', '--remove-datadir'])