paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / integration-tests / test_integration_reconnect.py.in
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
19 import sys
20 import os
21 import subprocess
22 import re
23 import shutil
24 import time
25 import signal
26 from gnunet_testing import Peer
27 from gnunet_testing import Test
28 from gnunet_testing import Check
29 from gnunet_testing import Condition
30 from gnunet_testing import * 
31  
32
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
41 #definitions
42
43
44 testname = "test_integration_restart"
45 verbose = True
46 check_timeout = 180
47
48 if os.name == "nt":
49   tmp = os.getenv ("TEMP")
50   signals = [signal.SIGTERM, signal.SIGINT]
51 else:
52   tmp = "/tmp"
53   signals = [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]
54
55 def cleanup_onerror (function, path, excinfo):
56   import stat
57   if not os.path.exists (path):
58     pass
59   elif not os.access(path, os.W_OK):
60     # Is the error an access error ?
61     os.chmod (path, stat.S_IWUSR)
62     function (path)
63   else:
64     raise
65
66 def cleanup ():
67     retries = 10
68     path = os.path.join (tmp, "c_bootstrap_server")  
69     test.p ("Removing " + path)      
70     while ((os.path.exists(path)) and (retries > 0)):
71         shutil.rmtree ((path), False, cleanup_onerror)
72         time.sleep (1)
73         retries -= 1
74     if (os.path.exists(path)):
75         test.p ("Failed to remove " + path) 
76         
77     
78     retries = 10
79     path = os.path.join (tmp, "c_no_nat_client")  
80     test.p ("Removing " + path)      
81     while ((os.path.exists(path)) and (retries > 0)):
82         shutil.rmtree ((path), False, cleanup_onerror)
83         time.sleep (1)
84         retries -= 1
85     if (os.path.exists(path)):
86         test.p ("Failed to remove " + path) 
87
88 def success_restart_cont (check):
89         global success 
90         print('Peers connected successfully after restart')
91         server.stop ()
92         client.stop ()
93         success = True; 
94
95
96 def fail_restart_cont (check):    
97         global success 
98         success = False;
99         print('Peers failed to connect after restart')
100         check.evaluate(True)   
101     
102
103 def success_connect_cont (check):
104         print('Peers connected successfully')
105         server.stop ()
106         client.stop ()
107         
108         time.sleep(5)
109         
110         test.p ('Restarting client & server')
111         server.start ()
112         client.start ()
113         
114         check = Check (test)
115         check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
116         check.add (StatisticsCondition (client, 'core', '# peers connected',1))
117         check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
118         check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
119         
120         check.add (StatisticsCondition (server, 'transport', '# peers connected',1))
121         check.add (StatisticsCondition (server, 'core', '# peers connected',1))
122         check.add (StatisticsCondition (server, 'topology', '# peers connected',1))
123         check.add (StatisticsCondition (server, 'fs', '# peers connected',1))  
124         
125         check.run_blocking (check_timeout, success_restart_cont, fail_restart_cont)
126
127
128 def fail_connect_cont (check):    
129         global success 
130         success= False;
131         print('Peers failed to connect')
132         check.evaluate(True)
133
134
135 def check_connect ():
136         check = Check (test)
137         check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
138         check.add (StatisticsCondition (client, 'core', '# peers connected',1))
139         check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
140         check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
141         
142         check.add (StatisticsCondition (server, 'transport', '# peers connected',1))
143         check.add (StatisticsCondition (server, 'core', '# peers connected',1))
144         check.add (StatisticsCondition (server, 'topology', '# peers connected',1))
145         check.add (StatisticsCondition (server, 'fs', '# peers connected',1))  
146         
147         check.run_blocking (check_timeout, success_connect_cont, fail_connect_cont)
148
149
150 # Test execution
151
152
153
154 def SigHandler(signum = None, frame = None):
155         global success  
156         global server
157         global client  
158         
159         print('Test was aborted!')
160         if (None != server):
161                 server.stop ()
162         if (None != client):            
163                 client.stop ()
164         cleanup ()
165         sys.exit(success)
166
167 def run ():
168         global success
169         global test
170         global server
171         global client
172         
173         success = False
174         server = None
175         client = None   
176
177         for sig in signals:
178                 signal.signal(sig, SigHandler)
179         
180         
181         test = Test ('test_integration_disconnect', verbose)
182         cleanup ()
183         server = Peer(test, './confs/c_bootstrap_server.conf');
184         server.start();
185         
186         client = Peer(test, './confs/c_no_nat_client.conf');
187         client.start();
188         
189
190         if (True != server.start()):
191                 print('Failed to start server')
192                 if (None != server):
193                         server.stop ()
194                 if (None != server):            
195                         client.stop ()
196                 cleanup ()
197                 sys.exit(success)
198                 
199         # Give the server time to start
200         time.sleep(5)
201                         
202         if (True != client.start()):
203                 print('Failed to start client')
204                 if (None != server):
205                         server.stop ()
206                 if (None != server):            
207                         client.stop ()
208                 cleanup ()
209                 sys.exit(success)
210         
211         check_connect ()
212         
213         server.stop ()    
214         client.stop ()
215         cleanup ()
216         
217         if (success == False):
218                 print ('Test failed')
219                 return True
220         else:
221                 return False
222
223         
224 try:
225     run ()
226 except (KeyboardInterrupt, SystemExit):    
227     print('Test interrupted')
228     server.stop ()
229     client.stop ()
230     cleanup ()
231 if (success == False):
232         sys.exit(1)   
233 else:
234         sys.exit(0)    
235
236