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