fix memleak
[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     shutil.rmtree (os.path.join (tmp, "c_bootstrap_server"), True)
53     shutil.rmtree (os.path.join (tmp, "c_no_nat_client"), True)
54     shutil.rmtree (os.path.join (tmp, "c_no_nat_client_2"), True)
55
56
57 def success_cont (check):
58     global success 
59     success = True;
60
61 def fail_cont (check):    
62     global success 
63     success= False;
64     check.evaluate(True)        
65
66
67 def check_disconnect_client ():
68   test.p ('Shutting down bootstrap client')
69   client.stop ()
70   check = Check (test)
71   
72   check.add (StatisticsCondition (client2, 'transport', '# peers connected',0))
73   check.add (StatisticsCondition (client2, 'core', '# neighbour entries allocated',0))  
74   check.add (StatisticsCondition (client2, 'core', '# peers connected',0))
75   check.add (StatisticsCondition (client2, 'topology', '# peers connected',0))
76   check.add (StatisticsCondition (client2, 'fs', '# peers connected',0))
77   
78   check.run_blocking (check_timeout, success_cont, fail_cont)
79
80                 
81 def success_disconnect_server_cont (check):
82     check_disconnect_client ()
83
84
85 def fail_disconnect_server_cont (check):    
86     global success 
87     success= False;
88     check.evaluate(True)        
89
90         
91 def check_disconnect_server ():
92   test.p ('Shutting down bootstrap server')
93   server.stop ()
94   check = Check (test)
95   check.add (StatisticsCondition (client, 'transport', '# peers connected',1))
96   check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',1))  
97   check.add (StatisticsCondition (client, 'core', '# peers connected',1))
98   check.add (StatisticsCondition (client, 'topology', '# peers connected',1))
99   check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
100   
101   check.add (StatisticsCondition (client2, 'transport', '# peers connected',1))
102   check.add (StatisticsCondition (client2, 'core', '# neighbour entries allocated',1))  
103   check.add (StatisticsCondition (client2, 'core', '# peers connected',1))
104   check.add (StatisticsCondition (client2, 'topology', '# peers connected',1))
105   check.add (StatisticsCondition (client2, 'fs', '# peers connected',1))
106   
107   check.run_blocking (check_timeout, success_disconnect_server_cont, fail_disconnect_server_cont)
108
109
110 def success_connect_cont (check):
111     check_disconnect_server ()
112
113
114 def fail_connect_cont (check):    
115     global success 
116     success= False;
117     check.evaluate(True)
118
119
120 def check_connect ():
121   check = Check (test)
122   check.add (StatisticsCondition (client, 'transport', '# peers connected',2))
123   check.add (StatisticsCondition (client, 'core', '# neighbour entries allocated',2))  
124   check.add (StatisticsCondition (client, 'core', '# peers connected',2))
125   check.add (StatisticsCondition (client, 'topology', '# peers connected',2))
126   check.add (StatisticsCondition (client, 'fs', '# peers connected',2))
127
128   check.add (StatisticsCondition (client2, 'transport', '# peers connected',2))
129   check.add (StatisticsCondition (client2, 'core', '# neighbour entries allocated',2))  
130   check.add (StatisticsCondition (client2, 'core', '# peers connected',2))
131   check.add (StatisticsCondition (client2, 'topology', '# peers connected',2))
132   check.add (StatisticsCondition (client2, 'fs', '# peers connected',2))
133   
134   check.add (StatisticsCondition (server, 'transport', '# peers connected',2))
135   check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',2))  
136   check.add (StatisticsCondition (server, 'core', '# peers connected',2))
137   check.add (StatisticsCondition (server, 'topology', '# peers connected',2))
138   check.add (StatisticsCondition (server, 'fs', '# peers connected',2))  
139   
140   check.run_blocking (check_timeout, success_connect_cont, fail_connect_cont)
141
142
143 # Test execution
144
145 def run ():
146         global success
147         global test
148         global server
149         global client
150         global client2  
151         
152         success = False
153         
154         test = Test ('test_integration_disconnect', verbose)
155         
156         server = Peer(test, './confs/c_bootstrap_server.conf');
157         server.start();
158         
159         client = Peer(test, './confs/c_no_nat_client.conf');
160         client.start();
161         
162         client2 = Peer(test, './confs/c_no_nat_client_2.conf');
163         client2.start();
164         
165         if ((client.started == True) and (client2.started == True) and (server.started == True)):
166             test.p ('Peers started, running check')
167             check_connect ()
168             
169         server.stop ()    
170         client.stop ()
171         client2.stop ()
172         
173         cleanup ()
174         
175         if (success == False):
176                 print ('Test failed')
177                 return False 
178         else:
179                 return True
180
181     
182 try:
183     run ()
184 except (KeyboardInterrupt, SystemExit):    
185     print 'Test interrupted'
186     server.stop ()
187     client.stop ()
188     client2.stop ()
189     cleanup ()
190 if (success == False):
191         sys.exit(1)   
192 else:
193         sys.exit(0)    
194             
195