test_integration_disconnect.py.in 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!@PYTHON@
  2. # This file is part of GNUnet.
  3. # (C) 2010, 2017 Christian Grothoff (and other contributing authors)
  4. #
  5. # GNUnet is free software: you can redistribute it and/or modify it
  6. # under the terms of the GNU Affero General Public License as published
  7. # by the Free Software Foundation, either version 3 of the License,
  8. # or (at your 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. # Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # SPDX-License-Identifier: AGPL3.0-or-later
  19. #
  20. #
  21. import sys
  22. import signal
  23. import os
  24. import subprocess
  25. import re
  26. import shutil
  27. import time
  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, the server is shutdown
  37. #
  38. # Conditions for successful exit:
  39. # Client peer has 0 connected peer in transport, core, topology, dht, fs
  40. #definitions
  41. testname = "test_integration_disconnect"
  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. shutil.rmtree (os.path.join (tmp, "c_bootstrap_server"), False, cleanup_onerror)
  62. shutil.rmtree (os.path.join (tmp, "c_no_nat_client"), False, cleanup_onerror)
  63. def success_disconnect_cont (check):
  64. print('Peers disconnected successfully')
  65. global success
  66. success = True;
  67. def fail_disconnect_cont (check):
  68. global success
  69. success = False;
  70. print('Peers failed to disconnect')
  71. check.evaluate(True)
  72. def check_disconnect ():
  73. test.p ('Shutting down bootstrap server')
  74. server.stop ()
  75. check = Check (test)
  76. check.add (StatisticsCondition (client, 'transport', '# peers connected',0))
  77. check.add (StatisticsCondition (client, 'core', '# peers connected',0))
  78. check.add (StatisticsCondition (client, 'topology', '# peers connected',0))
  79. check.add (StatisticsCondition (client, 'dht', '# peers connected',0))
  80. check.add (StatisticsCondition (client, 'fs', '# peers connected',0))
  81. check.run_blocking (check_timeout, success_disconnect_cont, fail_disconnect_cont)
  82. def success_connect_cont (check):
  83. print('Peers connected successfully')
  84. check_disconnect ()
  85. def fail_connect_cont (check):
  86. global success
  87. success= False
  88. print('Peers failed to connected!')
  89. check.evaluate(True)
  90. def check_connect ():
  91. check = Check (test)
  92. check.add (StatisticsCondition (server, 'transport', '# peers connected',1))
  93. check.add (StatisticsCondition (server, 'core', '# peers connected',1))
  94. check.add (StatisticsCondition (server, 'topology', '# peers connected',1))
  95. check.add (StatisticsCondition (server, 'dht', '# peers connected',1))
  96. check.add (StatisticsCondition (server, 'fs', '# peers connected',1))
  97. check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
  98. check.add (StatisticsCondition (client, 'core', '# peers connected',1))
  99. check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
  100. check.add (StatisticsCondition (client, 'dht', '# peers connected',1))
  101. check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
  102. check.run_blocking (check_timeout, success_connect_cont, fail_connect_cont)
  103. #
  104. # Test execution
  105. #
  106. def SigHandler(signum = None, frame = None):
  107. global success
  108. global server
  109. global client
  110. print('Test was aborted!')
  111. if (None != server):
  112. server.stop ()
  113. if (None != client):
  114. client.stop ()
  115. cleanup ()
  116. sys.exit(success)
  117. def run ():
  118. global success
  119. global test
  120. global server
  121. global client
  122. server = None
  123. client = None
  124. success = False
  125. for sig in signals:
  126. signal.signal(sig, SigHandler)
  127. test = Test ('test_integration_bootstrap_and_connect.py', verbose)
  128. cleanup ()
  129. server = Peer(test, './confs/c_bootstrap_server.conf');
  130. client = Peer(test, './confs/c_no_nat_client.conf');
  131. if (True != server.start()):
  132. print('Failed to start server')
  133. if (None != server):
  134. server.stop ()
  135. cleanup ()
  136. sys.exit(success)
  137. # Give the server time to start
  138. time.sleep(5)
  139. if (True != client.start()):
  140. print('Failed to start client')
  141. if (None != server):
  142. server.stop ()
  143. if (None != client):
  144. client.stop ()
  145. cleanup ()
  146. sys.exit(success)
  147. if ((client.started == True) and (server.started == True)):
  148. test.p ('Peers started, running check')
  149. time.sleep(5)
  150. check_connect ()
  151. server.stop ()
  152. client.stop ()
  153. cleanup ()
  154. if (success == False):
  155. print ('Test failed')
  156. return False
  157. else:
  158. return True
  159. try:
  160. run ()
  161. except (KeyboardInterrupt, SystemExit):
  162. print('Test interrupted')
  163. server.stop ()
  164. client.stop ()
  165. cleanup ()
  166. if (success == False):
  167. sys.exit(1)
  168. else:
  169. sys.exit(0)