clique test
[oweals/gnunet.git] / src / integration-tests / test_integration_reconnect.py.in
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
35 #
36 # This test tests if a fresh peer bootstraps from a hostlist server and then
37 # successfully connects to the server. When both peers are connected
38 # in transport, core, topology, fs, botth peers are shutdown and restarted 
39 #
40 # Conditions for successful exit:
41 # Both peers have 1 connected peer in transport, core, topology, fs after restart
42
43 #definitions
44
45
46 testname = "test_integration_restart"
47 verbose = True
48 check_timeout = 180
49
50 if os.name == "nt":
51   tmp = os.getenv ("TEMP")
52 else:
53   tmp = "/tmp"
54
55 def cleanup ():
56     retries = 10
57     path = os.path.join (tmp, "c_bootstrap_server")  
58     test.p ("Removing " + path)      
59     while ((os.path.exists(path)) and (retries > 0)):
60         shutil.rmtree ((path), False)
61         time.sleep (1)
62         retries -= 1
63     if (os.path.exists(path)):
64         test.p ("Failed to remove " + path) 
65         
66     
67     retries = 10
68     path = os.path.join (tmp, "c_no_nat_client")  
69     test.p ("Removing " + path)      
70     while ((os.path.exists(path)) and (retries > 0)):
71         shutil.rmtree ((path), False)
72         time.sleep (1)
73         retries -= 1
74     if (os.path.exists(path)):
75         test.p ("Failed to remove " + path) 
76
77 def success_restart_cont (check):
78         global success 
79         print 'Peers connected successfully after restart'
80         server.stop ()
81         client.stop ()
82         success = True; 
83
84
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     
91
92 def success_connect_cont (check):
93         print 'Peers connected successfully'
94         server.stop ()
95         client.stop ()
96         
97         time.sleep(5)
98         
99         test.p ('Restarting client & server')
100         server.start ()
101         client.start ()
102         
103         check = Check (test)
104         check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
105         check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',1))  
106         check.add (StatisticsCondition (client, 'core', '# peers connected',1))
107         check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
108         check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
109         
110         check.add (StatisticsCondition (server, 'transport', '# peers connected',1))
111         check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',1))  
112         check.add (StatisticsCondition (server, 'core', '# peers connected',1))
113         check.add (StatisticsCondition (server, 'topology', '# peers connected',1))
114         check.add (StatisticsCondition (server, 'fs', '# peers connected',1))  
115         
116         check.run_blocking (check_timeout, success_restart_cont, fail_restart_cont)
117
118
119 def fail_connect_cont (check):    
120         global success 
121         success= False;
122         print 'Peers failed to connect'
123         check.evaluate(True)
124
125
126 def check_connect ():
127         check = Check (test)
128         check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
129         check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',1))  
130         check.add (StatisticsCondition (client, 'core', '# peers connected',1))
131         check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
132         check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
133         
134         check.add (StatisticsCondition (server, 'transport', '# peers connected',1))
135         check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',1))  
136         check.add (StatisticsCondition (server, 'core', '# peers connected',1))
137         check.add (StatisticsCondition (server, 'topology', '# peers connected',1))
138         check.add (StatisticsCondition (server, 'fs', '# peers connected',1))  
139         
140         check.run_blocking (check_timeout, success_connect_cont, fail_connect_cont)
141
142
143 # Test execution
144
145
146
147 def SigHandler(signum = None, frame = None):
148         global success  
149         global server
150         global client  
151         
152         print 'Test was aborted!'
153         if (None != server):
154                 server.stop ()
155         if (None != client):            
156                 client.stop ()
157         cleanup ()
158         sys.exit(success)
159
160 def run ():
161         global success
162         global test
163         global server
164         global client
165         
166         success = False
167         server = None
168         client = None   
169
170         for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
171                 signal.signal(sig, SigHandler)
172         
173         
174         test = Test ('test_integration_disconnect', verbose)
175         cleanup ()
176         server = Peer(test, './confs/c_bootstrap_server.conf');
177         server.start();
178         
179         client = Peer(test, './confs/c_no_nat_client.conf');
180         client.start();
181         
182
183         if (True != server.start()):
184                 print 'Failed to start server'
185                 if (None != server):
186                         server.stop ()
187                 if (None != server):            
188                         client.stop ()
189                 cleanup ()
190                 sys.exit(success)
191         if (True != client.start()):
192                 print 'Failed to start client'
193                 if (None != server):
194                         server.stop ()
195                 if (None != server):            
196                         client.stop ()
197                 cleanup ()
198                 sys.exit(success)
199         
200         check_connect ()
201         
202         server.stop ()    
203         client.stop ()
204         cleanup ()
205         
206         if (success == False):
207                 print ('Test failed')
208                 return True
209         else:
210                 return False
211
212         
213 try:
214     run ()
215 except (KeyboardInterrupt, SystemExit):    
216     print 'Test interrupted'
217     server.stop ()
218     client.stop ()
219     cleanup ()
220 if (success == False):
221         sys.exit(1)   
222 else:
223         sys.exit(0)    
224
225