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