e1856ba2b5f8723d5acd7aeeb7ed204fdf869907
[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         client = Peer(test, './confs/c_no_nat_client.conf');
162         if (True != client.start()):
163                 print 'Failed to start client'
164                 if (None != server):
165                         server.stop ()
166                 if (None != client):
167                         client.stop ()                  
168                 cleanup ()
169                 sys.exit(success)
170         
171         client_nat = Peer(test, './confs/c_nat_client.conf');
172         if (True != client_nat.start()):
173                 print 'Failed to start client_nat'
174                 if (None != server):
175                         server.stop ()
176                 if (None != client):
177                         client.stop ()
178                 if (None != client_nat):
179                         client_nat.stop ()                                                              
180                 cleanup ()
181                 sys.exit(success)       
182         
183         if ((client.started == True) and (client_nat.started == True) and (server.started == True)):
184             test.p ('Peers started, running check')
185             check_connect ()
186             
187         server.stop ()    
188         client.stop ()
189         client_nat.stop ()
190         
191         cleanup ()
192         
193         if (success == False):
194                 print ('Test failed')
195                 return False 
196         else:
197                 return True
198
199     
200 try:
201     run ()
202 except (KeyboardInterrupt, SystemExit):    
203     print 'Test interrupted'
204     server.stop ()
205     client.stop ()
206     client_nat.stop ()
207     cleanup ()
208 if (success == False):
209         sys.exit(1)   
210 else:
211         sys.exit(0)    
212             
213