ut_exec.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python3
  2. """
  3. test.daemontest_d.ut_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
  11. def test_flags():
  12. d = CoinDaemon(cfg, 'eth')
  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, 'eth', 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. def _pre(self):
  51. self.daemon_ctrl_args = ['btc', 'btc_tn', 'btc_rt'] if cfg.no_altcoin_deps else ['all']
  52. def _test_cmd(self, args_in, message):
  53. qmsg_r(message)
  54. args = ['python3', f'test/{args_in[0]}-coin-daemons.py'] + list(args_in[1:]) + self.daemon_ctrl_args
  55. vmsg('\n' + orange(f"Running '{' '.join(args)}':"))
  56. cp = run(args, stdout=PIPE, stderr=PIPE, text=True)
  57. if cp.returncode != 0:
  58. if cp.stdout:
  59. msg(cp.stdout)
  60. if cp.stderr:
  61. msg(red(cp.stderr))
  62. return False
  63. qmsg('OK')
  64. return True
  65. def flags(self, name, ut):
  66. qmsg_r('Testing flags and opts...')
  67. vmsg('')
  68. daemons = test_flags()
  69. qmsg('OK')
  70. qmsg_r('Testing error handling for flags and opts...')
  71. vmsg('')
  72. test_flags_err(ut, daemons)
  73. qmsg('OK')
  74. return True
  75. def avail(self, name, ut):
  76. return self._test_cmd(['start', '-Vm'], 'Testing availability of coin daemons...')
  77. def cmds(self, name, ut):
  78. return self._test_cmd(['start', '-t'], 'Testing start commands for coin daemons...')
  79. def start(self, name, ut):
  80. return self._test_cmd(['start'], 'Starting coin daemons...')
  81. def status(self, name, ut):
  82. return self._test_cmd(['start'], 'Checking status of coin daemons...')
  83. def stop(self, name, ut):
  84. return self._test_cmd(['stop'], 'Stopping coin daemons...')