ut_daemon.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_daemon: 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. from ..include.common import cfg,qmsg,qmsg_r,vmsg
  10. def test_flags():
  11. d = CoinDaemon(cfg,'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(cfg,'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_cmd(args_in,message):
  48. qmsg_r(message)
  49. args = ['python3', f'test/{args_in[0]}-coin-daemons.py'] + list(args_in[1:])
  50. vmsg('\n' + orange(f"Running '{' '.join(args)}':"))
  51. pipe = None if cfg.verbose else PIPE
  52. cp = run( args, stdout=pipe, stderr=pipe, check=True )
  53. qmsg('OK')
  54. return True
  55. class unit_tests:
  56. win_skip = ('start','status','stop')
  57. def flags(self,name,ut):
  58. qmsg_r('Testing flags and opts...')
  59. vmsg('')
  60. daemons = test_flags()
  61. qmsg('OK')
  62. qmsg_r('Testing error handling for flags and opts...')
  63. vmsg('')
  64. test_flags_err(ut,daemons)
  65. qmsg('OK')
  66. return True
  67. def exec(self,name,ut):
  68. return test_cmd(['start','-Vm','all'], 'Testing availability of coin daemons...')
  69. def cmds(self,name,ut):
  70. return test_cmd(['start','-t','all'], 'Testing start commands for coin daemons...')
  71. def start(self,name,ut):
  72. return test_cmd(['start','all'], 'Starting coin daemons...')
  73. def status(self,name,ut):
  74. return test_cmd(['start','all'], 'Checking status of coin daemons...')
  75. def stop(self,name,ut):
  76. return test_cmd(['stop','all'], 'Stopping coin daemons...')