test_dht_tools.py.in 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!@PYTHONEXE@
  2. #
  3. # This testcase simply checks that the DHT command-line tools work.
  4. # It launches a single peer, stores a value "testdata" under "testkey",
  5. # and then gives the system 50 ms to fetch it.
  6. #
  7. # This could fail if
  8. # - command line tool interfaces fail
  9. # - DHT plugins for storage are not installed / working
  10. # - block plugins for verification (the test plugin) is not installed
  11. #
  12. # The code does NOT depend on DHT routing or any actual P2P functionality.
  13. #
  14. import os
  15. import sys
  16. import shutil
  17. import re
  18. import subprocess
  19. import time
  20. import tempfile
  21. os.environ["PATH"] = "@bindirectory@" + ":" + os.environ["PATH"]
  22. if os.name == "nt":
  23. tmp = os.getenv("TEMP")
  24. else:
  25. tmp = "/tmp"
  26. if os.name == 'nt':
  27. get = './gnunet-dht-get.exe'
  28. put = './gnunet-dht-put.exe'
  29. arm = 'gnunet-arm.exe'
  30. else:
  31. get = './gnunet-dht-get'
  32. put = './gnunet-dht-put'
  33. arm = 'gnunet-arm'
  34. cfgfile = 'test_dht_api_peer1.conf'
  35. run_get = [get, '-c', cfgfile]
  36. run_put = [put, '-c', cfgfile]
  37. run_arm = [arm, '-c', cfgfile]
  38. debug = os.getenv('DEBUG')
  39. if debug:
  40. run_arm += [debug.split(' ')]
  41. def cleanup(exitcode):
  42. sys.exit(exitcode)
  43. def sub_run(args, want_stdo=True, want_stde=False, nofail=False):
  44. if want_stdo:
  45. stdo = subprocess.PIPE
  46. else:
  47. stdo = None
  48. if want_stde:
  49. stde = subprocess.PIPE
  50. else:
  51. stde = None
  52. p = subprocess.Popen(args, stdout=stdo, stderr=stde)
  53. stdo, stde = p.communicate()
  54. if not nofail:
  55. if p.returncode != 0:
  56. sys.exit(p.returncode)
  57. return (p.returncode, stdo, stde)
  58. def fail(result):
  59. print(result)
  60. r_arm(['-e'], want_stdo=False)
  61. cleanup(1)
  62. def r_something(to_run, extra_args, failure=None, normal=True, **kw):
  63. rc, stdo, stde = sub_run(to_run + extra_args, nofail=True, **kw)
  64. if failure is not None:
  65. failure(to_run + extra_args, rc, stdo, stde, normal)
  66. return (rc, stdo, stde)
  67. def r_arm(extra_args, **kw):
  68. return r_something(run_arm, extra_args, **kw)
  69. def r_get(extra_args, **kw):
  70. return r_something(run_get, extra_args, **kw)
  71. def r_put(extra_args, **kw):
  72. return r_something(run_put, extra_args, **kw)
  73. def end_arm_failure(command, rc, stdo, stde, normal):
  74. if normal:
  75. if rc != 0:
  76. fail(
  77. "FAIL: error running {}\nCommand output was:\n{}\n{}".format(
  78. command, stdo, stde
  79. )
  80. )
  81. else:
  82. if rc == 0:
  83. fail(
  84. "FAIL: expected error while running {}\nCommand output was:\n{}\n{}"
  85. .format(command, stdo, stde)
  86. )
  87. def print_only_failure(command, rc, stdo, stde, normal):
  88. if normal:
  89. if rc != 0:
  90. print(
  91. "FAIL: error running {}\nCommand output was:\n{}\n{}".format(
  92. command, stdo, stde
  93. )
  94. )
  95. cleanup(1)
  96. else:
  97. if rc == 0:
  98. print(
  99. "FAIL: expected error while running {}\nCommand output was:\n{}\n{}"
  100. .format(command, stdo, stde)
  101. )
  102. cleanup(1)
  103. print("TEST: Starting ARM...", end='')
  104. r_arm(['-s'], failure=end_arm_failure, want_stdo=False, want_stde=False)
  105. print("PASS")
  106. time.sleep(1)
  107. print("TEST: Testing put...", end='')
  108. r_put(['-k', 'testkey', '-d', 'testdata', '-t', '8'], failure=end_arm_failure)
  109. print("PASS")
  110. time.sleep(1)
  111. print("TEST: Testing get...", end='')
  112. rc, stdo, stde = r_get(['-k', 'testkey', '-T', '50 ms', '-t', '8'],
  113. want_stdo=True,
  114. failure=end_arm_failure)
  115. stdo = stdo.decode('utf-8').replace('\r', '').splitlines()
  116. expect = "Result 0, type 8:\ntestdata".splitlines()
  117. if len(stdo) != 2 or len(expect
  118. ) != 2 or stdo[0] != expect[0] or stdo[1] != expect[1]:
  119. fail("output `{}' differs from expected `{}'".format(stdo, expect))
  120. print("PASS")
  121. r_arm(['-e', '-d'], failure=print_only_failure)