if start fails, clean up and exit
[oweals/gnunet.git] / src / integration-tests / test_integration_clique.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 #
22 # This test starts 3 peers and expects bootstrap and a connected clique
23 #
24 # Conditions for successful exit:
25 # Both peers have 1 connected peer in transport, core, topology, fs 
26
27 import sys
28 import os
29 import subprocess
30 import re
31 import shutil
32 import time
33 from gnunet_testing import Peer
34 from gnunet_testing import Test
35 from gnunet_testing import Check
36 from gnunet_testing import Condition
37 from gnunet_testing import * 
38  
39 if os.name == "nt":
40   tmp = os.getenv ("TEMP")
41 else:
42   tmp = "/tmp"
43
44 #definitions
45
46 testname = "test_integration_clique"
47 verbose = True
48 check_timeout = 180
49
50
51 def cleanup ():
52     retries = 10
53     path = os.path.join (tmp, "c_bootstrap_server")  
54     test.p ("Removing " + path)      
55     while ((os.path.exists(path)) and (retries > 0)):
56         shutil.rmtree ((path), False)
57         time.sleep (1)
58         retries -= 1
59     if (os.path.exists(path)):
60         test.p ("Failed to remove " + path) 
61     retries = 10
62     path = os.path.join (tmp, "c_no_nat_client")  
63     test.p ("Removing " + path)      
64     while ((os.path.exists(path)) and (retries > 0)):
65         shutil.rmtree ((path), False)
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_2")  
72     test.p ("Removing " + path)      
73     while ((os.path.exists(path)) and (retries > 0)):
74         shutil.rmtree ((path), False)
75         time.sleep (1)
76         retries -= 1
77     if (os.path.exists(path)):
78         test.p ("Failed to remove " + path) 
79
80
81 def success_cont (check):
82     global success 
83     success = True;
84
85 def fail_cont (check):    
86     global success 
87     success= False;
88     check.evaluate(True)        
89
90
91 def check_disconnect_client ():
92   test.p ('Shutting down bootstrap client')
93   client.stop ()
94   check = Check (test)
95   
96   check.add (StatisticsCondition (client2, 'transport', '# peers connected',0))
97   check.add (StatisticsCondition (client2, 'core', '# neighbour entries allocated',0))  
98   check.add (StatisticsCondition (client2, 'core', '# peers connected',0))
99   check.add (StatisticsCondition (client2, 'topology', '# peers connected',0))
100   check.add (StatisticsCondition (client2, 'fs', '# peers connected',0))
101   
102   check.run_blocking (check_timeout, success_cont, fail_cont)
103
104                 
105 def success_disconnect_server_cont (check):
106     check_disconnect_client ()
107
108
109 def fail_disconnect_server_cont (check):    
110     global success 
111     success= False;
112     check.evaluate(True)        
113
114         
115 def check_disconnect_server ():
116   test.p ('Shutting down bootstrap server')
117   server.stop ()
118   check = Check (test)
119   check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
120   check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',1))  
121   check.add (StatisticsCondition (client, 'core', '# peers connected',1))
122   check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
123   check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
124   
125   check.add (StatisticsCondition (client2, 'transport', '# peers connected',1))
126   check.add (StatisticsCondition (client2, 'core', '# neighbour entries allocated',1))  
127   check.add (StatisticsCondition (client2, 'core', '# peers connected',1))
128   check.add (StatisticsCondition (client2, 'topology', '# peers connected',1))
129   check.add (StatisticsCondition (client2, 'fs', '# peers connected',1))
130   
131   check.run_blocking (check_timeout, success_disconnect_server_cont, fail_disconnect_server_cont)
132
133
134 def success_connect_cont (check):
135     check_disconnect_server ()
136
137
138 def fail_connect_cont (check):    
139     global success 
140     success= False;
141     check.evaluate(True)
142
143
144 def check_connect ():
145   check = Check (test)
146   check.add (StatisticsCondition (client, 'transport', '# peers connected',2))
147   check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',2))  
148   check.add (StatisticsCondition (client, 'core', '# peers connected',2))
149   check.add (StatisticsCondition (client, 'topology', '# peers connected',2))
150   check.add (StatisticsCondition (client, 'fs', '# peers connected',2))
151
152   check.add (StatisticsCondition (client2, 'transport', '# peers connected',2))
153   check.add (StatisticsCondition (client2, 'core', '# neighbour entries allocated',2))  
154   check.add (StatisticsCondition (client2, 'core', '# peers connected',2))
155   check.add (StatisticsCondition (client2, 'topology', '# peers connected',2))
156   check.add (StatisticsCondition (client2, 'fs', '# peers connected',2))
157   
158   check.add (StatisticsCondition (server, 'transport', '# peers connected',2))
159   check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',2))  
160   check.add (StatisticsCondition (server, 'core', '# peers connected',2))
161   check.add (StatisticsCondition (server, 'topology', '# peers connected',2))
162   check.add (StatisticsCondition (server, 'fs', '# peers connected',2))  
163   
164   check.run_blocking (check_timeout, success_connect_cont, fail_connect_cont)
165
166
167 # Test execution
168
169 def run ():
170         global success
171         global test
172         global server
173         global client
174         global client2  
175         
176         success = False
177         test = Test ('test_integration_disconnect', verbose)
178         cleanup ()
179         
180         server = Peer(test, './confs/c_bootstrap_server.conf');
181         server.start();
182         
183         client = Peer(test, './confs/c_no_nat_client.conf');
184         client.start();
185         
186         client2 = Peer(test, './confs/c_no_nat_client_2.conf');
187         client2.start();
188         
189         if ((client.started == True) and (client2.started == True) and (server.started == True)):
190             test.p ('Peers started, running check')
191             check_connect ()
192             
193         server.stop ()    
194         client.stop ()
195         client2.stop ()
196         
197         cleanup ()
198         
199         if (success == False):
200                 print ('Test failed')
201                 return False 
202         else:
203                 return True
204
205     
206 try:
207     run ()
208 except (KeyboardInterrupt, SystemExit):    
209     print 'Test interrupted'
210     server.stop ()
211     client.stop ()
212     client2.stop ()
213     cleanup ()
214 if (success == False):
215         sys.exit(1)   
216 else:
217         sys.exit(0)    
218             
219