ut_daemon.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python3
  2. """
  3. test/unit_tests_d/ut_daemon.py: unit test for the MMGen suite's Daemon class
  4. """
  5. from subprocess import run,DEVNULL
  6. from mmgen.common import *
  7. from mmgen.daemon import *
  8. from mmgen.protocol import init_proto
  9. def test_flags():
  10. d = CoinDaemon('eth')
  11. vmsg(f'Available opts: {fmt_list(d.avail_opts,fmt="bare")}')
  12. vmsg(f'Available flags: {fmt_list(d.avail_flags,fmt="bare")}')
  13. vals = namedtuple('vals',['online','no_daemonize','keep_cfg_file'])
  14. def gen():
  15. for opts,flags,val in (
  16. (None,None, vals(False,False,False)),
  17. (None,['keep_cfg_file'], vals(False,False,True)),
  18. (['online'],['keep_cfg_file'], vals(True,False,True)),
  19. (['online','no_daemonize'],['keep_cfg_file'], vals(True,True,True)),
  20. ):
  21. d = CoinDaemon('eth',opts=opts,flags=flags)
  22. assert d.flag.keep_cfg_file == val.keep_cfg_file
  23. assert d.opt.online == val.online
  24. assert d.opt.no_daemonize == val.no_daemonize
  25. d.flag.keep_cfg_file = not val.keep_cfg_file
  26. d.flag.keep_cfg_file = val.keep_cfg_file
  27. yield d
  28. return tuple(gen())
  29. def test_flags_err(ut,d):
  30. def bad1(): d[0].flag.foo = False
  31. def bad2(): d[0].opt.foo = False
  32. def bad3(): d[0].opt.no_daemonize = True
  33. def bad4(): d[0].flag.keep_cfg_file = 'x'
  34. def bad5(): d[0].opt.no_daemonize = 'x'
  35. def bad6(): d[0].flag.keep_cfg_file = False
  36. def bad7(): d[1].flag.keep_cfg_file = True
  37. ut.process_bad_data((
  38. ('flag (1)', 'ClassFlagsError', 'unrecognized flag', bad1 ),
  39. ('opt (1)', 'ClassFlagsError', 'unrecognized opt', bad2 ),
  40. ('opt (2)', 'AttributeError', 'is read-only', bad3 ),
  41. ('flag (2)', 'AssertionError', 'not boolean', bad4 ),
  42. ('opt (3)', 'AttributeError', 'is read-only', bad5 ),
  43. ('flag (3)', 'ClassFlagsError', 'not set', bad6 ),
  44. ('flag (4)', 'ClassFlagsError', 'already set', bad7 ),
  45. ))
  46. arm_skip_daemons = ('openethereum','parity')
  47. def test_cmds(op):
  48. network_ids = CoinDaemon.get_network_ids()
  49. import mmgen.daemon as daemon_mod
  50. for test_suite in [True,False] if op == 'print' else [True]:
  51. vmsg(orange(f'Start commands (op={op}, test_suite={test_suite}):'))
  52. for coin,data in CoinDaemon.coins.items():
  53. for daemon_id in data.daemon_ids:
  54. if daemon_id in arm_skip_daemons:
  55. continue
  56. for network in data.networks:
  57. if opt.no_altcoin_deps and coin != 'BTC':
  58. continue
  59. d = CoinDaemon(
  60. proto=init_proto(coin=coin,network=network),
  61. daemon_id = daemon_id,
  62. test_suite = test_suite )
  63. if op == 'print':
  64. for cmd in d.start_cmds:
  65. vmsg(' '.join(cmd))
  66. elif op == 'check':
  67. try:
  68. cp = run([d.exec_fn,'--help'],stdout=PIPE,stderr=PIPE)
  69. except:
  70. die(2,f'Unable to execute {d.exec_fn}')
  71. if cp.returncode:
  72. die(2,f'Unable to execute {d.exec_fn}')
  73. else:
  74. vmsg('{:16} {}'.format(
  75. d.exec_fn+':',
  76. cp.stdout.decode().splitlines()[0] ))
  77. else:
  78. if opt.quiet:
  79. msg_r('.')
  80. if op == 'stop' and hasattr(d,'rpc'):
  81. run_session(d.rpc.stop_daemon(quiet=opt.quiet))
  82. else:
  83. getattr(d,op)(silent=opt.quiet)
  84. class unit_tests:
  85. win_skip = ('start','status','stop')
  86. def flags(self,name,ut):
  87. qmsg_r('Testing flags and opts...')
  88. vmsg('')
  89. daemons = test_flags()
  90. qmsg('OK')
  91. qmsg_r('Testing error handling for flags and opts...')
  92. vmsg('')
  93. test_flags_err(ut,daemons)
  94. qmsg('OK')
  95. return True
  96. def cmds(self,name,ut):
  97. qmsg_r('Testing start commands for coin daemons...')
  98. vmsg('')
  99. test_cmds('print')
  100. qmsg('OK')
  101. return True
  102. def exec(self,name,ut):
  103. qmsg_r('Testing availability of coin daemons...')
  104. vmsg('')
  105. test_cmds('check')
  106. qmsg('OK')
  107. return True
  108. def start(self,name,ut):
  109. msg_r('Starting coin daemons...')
  110. qmsg('')
  111. test_cmds('start')
  112. msg('OK')
  113. return True
  114. def status(self,name,ut):
  115. msg_r('Checking status of coin daemons...')
  116. qmsg('')
  117. test_cmds('start')
  118. msg('OK')
  119. return True
  120. def stop(self,name,ut):
  121. msg_r('Stopping coin daemons...')
  122. qmsg('')
  123. test_cmds('stop')
  124. msg('OK')
  125. return True