test_integration_reconnect.py.in 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!@PYTHON@
  2. # This file is part of GNUnet.
  3. # (C) 2010 Christian Grothoff (and other contributing authors)
  4. #
  5. # GNUnet is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published
  7. # by the Free Software Foundation; either version 2, or (at your
  8. # option) any later version.
  9. #
  10. # GNUnet is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with GNUnet; see the file COPYING. If not, write to the
  17. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. # Boston, MA 02111-1307, USA.
  19. #
  20. #
  21. import sys
  22. import os
  23. import subprocess
  24. import re
  25. import shutil
  26. import time
  27. import signal
  28. from gnunet_testing import Peer
  29. from gnunet_testing import Test
  30. from gnunet_testing import Check
  31. from gnunet_testing import Condition
  32. from gnunet_testing import *
  33. #
  34. # This test tests if a fresh peer bootstraps from a hostlist server and then
  35. # successfully connects to the server. When both peers are connected
  36. # in transport, core, topology, fs, botth peers are shutdown and restarted
  37. #
  38. # Conditions for successful exit:
  39. # Both peers have 1 connected peer in transport, core, topology, fs after restart
  40. #definitions
  41. testname = "test_integration_restart"
  42. verbose = True
  43. check_timeout = 180
  44. if os.name == "nt":
  45. tmp = os.getenv ("TEMP")
  46. signals = [signal.SIGTERM, signal.SIGINT]
  47. else:
  48. tmp = "/tmp"
  49. signals = [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]
  50. def cleanup_onerror (function, path, excinfo):
  51. import stat
  52. if not os.path.exists (path):
  53. pass
  54. elif not os.access(path, os.W_OK):
  55. # Is the error an access error ?
  56. os.chmod (path, stat.S_IWUSR)
  57. function (path)
  58. else:
  59. raise
  60. def cleanup ():
  61. retries = 10
  62. path = os.path.join (tmp, "c_bootstrap_server")
  63. test.p ("Removing " + path)
  64. while ((os.path.exists(path)) and (retries > 0)):
  65. shutil.rmtree ((path), False, cleanup_onerror)
  66. time.sleep (1)
  67. retries -= 1
  68. if (os.path.exists(path)):
  69. test.p ("Failed to remove " + path)
  70. retries = 10
  71. path = os.path.join (tmp, "c_no_nat_client")
  72. test.p ("Removing " + path)
  73. while ((os.path.exists(path)) and (retries > 0)):
  74. shutil.rmtree ((path), False, cleanup_onerror)
  75. time.sleep (1)
  76. retries -= 1
  77. if (os.path.exists(path)):
  78. test.p ("Failed to remove " + path)
  79. def success_restart_cont (check):
  80. global success
  81. print 'Peers connected successfully after restart'
  82. server.stop ()
  83. client.stop ()
  84. success = True;
  85. def fail_restart_cont (check):
  86. global success
  87. success = False;
  88. print 'Peers failed to connect after restart'
  89. check.evaluate(True)
  90. def success_connect_cont (check):
  91. print 'Peers connected successfully'
  92. server.stop ()
  93. client.stop ()
  94. time.sleep(5)
  95. test.p ('Restarting client & server')
  96. server.start ()
  97. client.start ()
  98. check = Check (test)
  99. check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
  100. check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',1))
  101. check.add (StatisticsCondition (client, 'core', '# peers connected',1))
  102. check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
  103. check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
  104. check.add (StatisticsCondition (server, 'transport', '# peers connected',1))
  105. check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',1))
  106. check.add (StatisticsCondition (server, 'core', '# peers connected',1))
  107. check.add (StatisticsCondition (server, 'topology', '# peers connected',1))
  108. check.add (StatisticsCondition (server, 'fs', '# peers connected',1))
  109. check.run_blocking (check_timeout, success_restart_cont, fail_restart_cont)
  110. def fail_connect_cont (check):
  111. global success
  112. success= False;
  113. print 'Peers failed to connect'
  114. check.evaluate(True)
  115. def check_connect ():
  116. check = Check (test)
  117. check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
  118. check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',1))
  119. check.add (StatisticsCondition (client, 'core', '# peers connected',1))
  120. check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
  121. check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
  122. check.add (StatisticsCondition (server, 'transport', '# peers connected',1))
  123. check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',1))
  124. check.add (StatisticsCondition (server, 'core', '# peers connected',1))
  125. check.add (StatisticsCondition (server, 'topology', '# peers connected',1))
  126. check.add (StatisticsCondition (server, 'fs', '# peers connected',1))
  127. check.run_blocking (check_timeout, success_connect_cont, fail_connect_cont)
  128. #
  129. # Test execution
  130. #
  131. def SigHandler(signum = None, frame = None):
  132. global success
  133. global server
  134. global client
  135. print 'Test was aborted!'
  136. if (None != server):
  137. server.stop ()
  138. if (None != client):
  139. client.stop ()
  140. cleanup ()
  141. sys.exit(success)
  142. def run ():
  143. global success
  144. global test
  145. global server
  146. global client
  147. success = False
  148. server = None
  149. client = None
  150. for sig in signals:
  151. signal.signal(sig, SigHandler)
  152. test = Test ('test_integration_disconnect', verbose)
  153. cleanup ()
  154. server = Peer(test, './confs/c_bootstrap_server.conf');
  155. server.start();
  156. client = Peer(test, './confs/c_no_nat_client.conf');
  157. client.start();
  158. if (True != server.start()):
  159. print 'Failed to start server'
  160. if (None != server):
  161. server.stop ()
  162. if (None != server):
  163. client.stop ()
  164. cleanup ()
  165. sys.exit(success)
  166. # Give the server time to start
  167. time.sleep(5)
  168. if (True != client.start()):
  169. print 'Failed to start client'
  170. if (None != server):
  171. server.stop ()
  172. if (None != server):
  173. client.stop ()
  174. cleanup ()
  175. sys.exit(success)
  176. check_connect ()
  177. server.stop ()
  178. client.stop ()
  179. cleanup ()
  180. if (success == False):
  181. print ('Test failed')
  182. return True
  183. else:
  184. return False
  185. try:
  186. run ()
  187. except (KeyboardInterrupt, SystemExit):
  188. print 'Test interrupted'
  189. server.stop ()
  190. client.stop ()
  191. cleanup ()
  192. if (success == False):
  193. sys.exit(1)
  194. else:
  195. sys.exit(0)