ut_daemon.py 3.9 KB

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