test_gnunet_arm.py.in 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!@PYTHON@
  2. import os
  3. import sys
  4. import shutil
  5. import re
  6. import subprocess
  7. import time
  8. # FIXME: There's too much repetition, move generally used parts into reusable modules.
  9. if os.name == "nt":
  10. tmp = os.getenv("TEMP")
  11. else:
  12. tmp = "/tmp"
  13. if os.name == 'nt':
  14. st = 'gnunet-statistics.exe'
  15. arm = './gnunet-arm.exe'
  16. else:
  17. st = 'gnunet-statistics'
  18. arm = './gnunet-arm'
  19. run_arm = [arm, '-c', 'test_arm_api_data.conf', '--no-stdout', '--no-stderr']
  20. debug = os.getenv('DEBUG')
  21. if debug:
  22. run_arm += [debug.split(' ')]
  23. def cleanup():
  24. shutil.rmtree(os.path.join(tmp, "test-gnunetd-arm"), True)
  25. def sub_run(args, want_stdo=True, want_stde=False, nofail=False):
  26. if want_stdo:
  27. stdo = subprocess.PIPE
  28. else:
  29. stdo = None
  30. if want_stde:
  31. stde = subprocess.PIPE
  32. else:
  33. stde = None
  34. p = subprocess.Popen(args, stdout=stdo, stderr=stde)
  35. stdo, stde = p.communicate()
  36. if not nofail:
  37. if p.returncode != 0:
  38. sys.exit(p.returncode)
  39. return (p.returncode, stdo, stde)
  40. def fail(result):
  41. print(result)
  42. r_arm(['-e'], want_stdo=False)
  43. sys.exit(1)
  44. def end_arm_failer(command, rc, stdo, stde, normal):
  45. if normal:
  46. if rc != 0:
  47. fail("FAIL: error running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
  48. else:
  49. if rc == 0:
  50. fail("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
  51. def print_only_failer(command, rc, stdo, stde, normal):
  52. if normal:
  53. if rc != 0:
  54. print("FAIL: error running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
  55. sys.exit(1)
  56. else:
  57. if rc == 0:
  58. print("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
  59. sys.exit(1)
  60. def r_something(to_run, extra_args, failer=None, normal=True, **kw):
  61. rc, stdo, stde = sub_run(to_run + extra_args, nofail=True, want_stde=True, **kw)
  62. if failer is not None:
  63. failer(to_run + extra_args, rc, stdo, stde, normal)
  64. return (rc, stdo, stde)
  65. def r_arm(extra_args, **kw):
  66. return r_something(run_arm, extra_args, **kw)
  67. cleanup()
  68. print("TEST: Bad argument checking...", end='')
  69. r_arm(['-x'], normal=False, failer=print_only_failer)
  70. print("PASS")
  71. print("TEST: Start ARM...", end='')
  72. r_arm(['-s'], failer=print_only_failer)
  73. time.sleep(1)
  74. print("PASS")
  75. print("TEST: Start another service...", end='')
  76. r_arm(['-i', 'resolver'], failer=end_arm_failer)
  77. time.sleep(1)
  78. print("PASS")
  79. print("TEST: Stop a service...", end='')
  80. r_arm(['-k', 'resolver'], failer=end_arm_failer)
  81. time.sleep(1)
  82. print("PASS")
  83. print("TEST: Stop ARM...", end='')
  84. r_arm(['-e'], failer=print_only_failer)
  85. time.sleep(1)
  86. print("PASS")
  87. cleanup()