test_dht_tools.py.in 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!@PYTHON@
  2. from __future__ import print_function
  3. import os
  4. import sys
  5. import shutil
  6. import re
  7. import subprocess
  8. import time
  9. import tempfile
  10. if os.name == "nt":
  11. tmp = os.getenv ("TEMP")
  12. else:
  13. tmp = "/tmp"
  14. if os.name == 'nt':
  15. pif = 'gnunet-peerinfo.exe'
  16. get = './gnunet-dht-get.exe'
  17. put = './gnunet-dht-put.exe'
  18. arm = 'gnunet-arm.exe'
  19. else:
  20. pif = 'gnunet-peerinfo'
  21. get = './gnunet-dht-get'
  22. put = './gnunet-dht-put'
  23. arm = 'gnunet-arm'
  24. tf, tempcfg = tempfile.mkstemp (prefix='test_dht_api_peer1.')
  25. os.close (tf)
  26. run_pif = [pif, '-c', tempcfg, '-sq']
  27. run_get = [get, '-c', tempcfg]
  28. run_put = [put, '-c', tempcfg]
  29. run_arm = [arm, '-c', tempcfg]
  30. debug = os.getenv ('DEBUG')
  31. if debug:
  32. run_arm += [debug.split (' ')]
  33. def cleanup (exitcode):
  34. os.remove (tempcfg)
  35. sys.exit (exitcode)
  36. def sub_run (args, want_stdo = True, want_stde = False, nofail = False):
  37. if want_stdo:
  38. stdo = subprocess.PIPE
  39. else:
  40. stdo = None
  41. if want_stde:
  42. stde = subprocess.PIPE
  43. else:
  44. stde = None
  45. p = subprocess.Popen (args, stdout = stdo, stderr = stde)
  46. stdo, stde = p.communicate ()
  47. if not nofail:
  48. if p.returncode != 0:
  49. sys.exit (p.returncode)
  50. return (p.returncode, stdo, stde)
  51. def fail (result):
  52. print (result)
  53. r_arm (['-e'], want_stdo = False)
  54. cleanup (1)
  55. def r_something (to_run, extra_args, failer = None, normal = True, **kw):
  56. rc, stdo, stde = sub_run (to_run + extra_args, nofail = True, **kw)
  57. if failer is not None:
  58. failer (to_run + extra_args, rc, stdo, stde, normal)
  59. return (rc, stdo, stde)
  60. def r_arm (extra_args, **kw):
  61. return r_something (run_arm, extra_args, **kw)
  62. def r_pif (extra_args, **kw):
  63. return r_something (run_pif, extra_args, **kw)
  64. def r_get (extra_args, **kw):
  65. return r_something (run_get, extra_args, **kw)
  66. def r_put (extra_args, **kw):
  67. return r_something (run_put, extra_args, **kw)
  68. def end_arm_failer (command, rc, stdo, stde, normal):
  69. if normal:
  70. if rc != 0:
  71. fail ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
  72. else:
  73. if rc == 0:
  74. fail ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
  75. def print_only_failer (command, rc, stdo, stde, normal):
  76. if normal:
  77. if rc != 0:
  78. print ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
  79. cleanup (1)
  80. else:
  81. if rc == 0:
  82. print ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
  83. cleanup (1)
  84. shutil.copyfile ('test_dht_api_peer1.conf', tempcfg)
  85. print ("TEST: Generating hostkey...", end='')
  86. r_pif ([], failer = print_only_failer)
  87. print ("PASS")
  88. print ("TEST: Starting ARM...", end='')
  89. r_arm (['-s'], failer = end_arm_failer, want_stdo = False, want_stde = False)
  90. print ("PASS")
  91. time.sleep (1)
  92. print ("TEST: Testing put...", end='')
  93. r_put (['-k', 'testkey', '-d', 'testdata', '-t', '8'], failer = end_arm_failer)
  94. print ("PASS")
  95. time.sleep (1)
  96. print ("TEST: Testing get...", end='')
  97. rc, stdo, stde = r_get (['-k', 'testkey', '-T', '5 ms', '-t', '8'], want_stdo = True, failer = end_arm_failer)
  98. stdo = stdo.replace ('\r', '').splitlines ()
  99. expect = "Result 0, type 8:\ntestdata".splitlines()
  100. if len (stdo) != 2 or len (expect) != 2 or stdo[0] != expect[0] or stdo[1] != expect[1]:
  101. fail ("output `{}' differs from expected `{}'".format (stdo, expect))
  102. print ("PASS")
  103. r_arm (['-e', '-d'], failer = print_only_failer)