fix whitespace
[oweals/gnunet.git] / src / integration-tests / test_integration_disconnect_nat.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., 51 Franklin Street, Fifth Floor,
18 #    Boston, MA 02110-1301, USA.
19 #
20
21 import sys
22 import signal
23 import os
24 import subprocess
25 import re
26 import shutil
27 import time
28 from gnunet_testing import Peer
29 from gnunet_testing import Test
30 from gnunet_testing import Check
31 from gnunet_testing import Condition
32 from gnunet_testing import * 
33  
34
35 #
36 # This test tests if a fresh peer bootstraps from a hostlist server and then
37 # successfully connects to the server. When both peers are connected
38 # in transport, core, topology, fs, the server is shutdown
39 #
40 # Conditions for successful exit:
41 # Client peer has 0 connected peer in transport, core, topology, dht, fs
42
43 #definitions
44
45 testname = "test_integration_disconnect"
46 verbose = True
47 check_timeout = 180
48
49 if os.name == "nt":
50   tmp = os.getenv ("TEMP")
51   signals = [signal.SIGTERM, signal.SIGINT]
52 else:
53   tmp = "/tmp"
54   signals = [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]
55
56 def cleanup_onerror (function, path, excinfo):
57   import stat
58   if not os.path.exists (path):
59     pass
60   elif not os.access(path, os.W_OK):
61     # Is the error an access error ?
62     os.chmod (path, stat.S_IWUSR)
63     function (path)
64   else:
65     raise
66
67 def cleanup ():
68   shutil.rmtree (os.path.join (tmp, "c_bootstrap_server"), False, cleanup_onerror)
69   shutil.rmtree (os.path.join (tmp, "c_nat_client"), False, cleanup_onerror)
70
71
72 def success_disconnect_cont (check):
73         print 'Peers disconnected successfully'
74         global success 
75         success = True;
76
77
78 def fail_disconnect_cont (check):    
79         global success 
80         success = False;
81         print 'Peers failed to disconnect'
82         check.evaluate(True)   
83   
84 def check_disconnect ():
85   global server
86   global nat_client  
87   test.p ('Shutting down nat client')
88   nat_client.stop ()
89   check = Check (test)
90   check.add (StatisticsCondition (server, 'transport', '# peers connected',0))
91   check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',0))  
92   check.add (StatisticsCondition (server, 'core', '# peers connected',0))
93   check.add (StatisticsCondition (server, 'topology', '# peers connected',0))
94   check.add (StatisticsCondition (server, 'dht', '# peers connected',0))
95   check.add (StatisticsCondition (server, 'fs', '# peers connected',0))
96   check.run_blocking (check_timeout, success_disconnect_cont, fail_disconnect_cont)
97
98
99 def success_connect_cont (check):
100         print 'Peers connected successfully'
101         check_disconnect ()
102
103
104 def fail_connect_cont (check):    
105   global success 
106   success= False
107   print 'Peers failed to connected!'    
108   check.evaluate(True)
109
110
111 def check_connect ():
112   global server
113   global nat_client  
114   check = Check (test)
115   check.add (StatisticsCondition (nat_client, 'transport', '# peers connected',1))
116   check.add (StatisticsCondition (nat_client, 'core', '# neighbour entries allocated',1))  
117   check.add (StatisticsCondition (nat_client, 'core', '# peers connected',1))
118   check.add (StatisticsCondition (nat_client, 'topology', '# peers connected',1))
119   check.add (StatisticsCondition (nat_client, 'dht', '# peers connected',1))
120   check.add (StatisticsCondition (nat_client, 'fs', '# peers connected',1))
121   
122   check.add (StatisticsCondition (server, 'transport', '# peers connected',1))
123   check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',1))  
124   check.add (StatisticsCondition (server, 'core', '# peers connected',1))
125   check.add (StatisticsCondition (server, 'topology', '# peers connected',1))
126   check.add (StatisticsCondition (server, 'dht', '# peers connected',1))
127   check.add (StatisticsCondition (server, 'fs', '# peers connected',1))  
128   
129   check.run_blocking (check_timeout, success_connect_cont, fail_connect_cont)
130
131
132 # Test execution
133
134
135 def SigHandler(signum = None, frame = None):
136         global success  
137         global server
138         global nat_client  
139         
140         print 'Test was aborted!'
141         if (None != server):
142                 server.stop ()
143         if (None != nat_client):                
144                 nat_client.stop ()
145         cleanup ()
146         sys.exit(success)
147
148 def run ():
149         global success
150         global test
151         global server
152         global nat_client    
153         
154         server = None
155         nat_client = None
156         success = False  
157         
158         for sig in signals:
159                 signal.signal(sig, SigHandler)
160
161         test = Test ('test_integration_bootstrap_and_connect.py', verbose)
162         cleanup ()
163         
164         server = Peer(test, './confs/c_bootstrap_server.conf');
165         nat_client = Peer(test, './confs/c_nat_client.conf');
166         
167         if (True != server.start()):
168                 print 'Failed to start server'
169                 if (None != server):
170                         server.stop ()
171                 cleanup ()
172                 sys.exit(success)
173                 
174         # Give the server time to start
175         time.sleep(5)
176                 
177         if (True != nat_client.start()):
178                 print 'Failed to start nat_client'
179                 if (None != server):
180                         server.stop ()
181                 if (None != nat_client):                
182                         nat_client.stop ()
183                 cleanup ()
184                 sys.exit(success)
185         
186         if ((nat_client.started == True) and (server.started == True)):
187                 test.p ('Peers started, running check')
188                 time.sleep(5)
189                 check_connect ()
190         server.stop ()
191         nat_client.stop ()
192         
193         cleanup ()
194         
195         if (success == False):
196                 print ('Test failed')
197                 return False 
198         else:
199                 return True
200
201 try:
202         run ()
203 except (KeyboardInterrupt, SystemExit):    
204         print 'Test interrupted'
205         server.stop ()
206         nat_client.stop ()
207         cleanup ()
208 if (success == False):
209         sys.exit(1)   
210 else:
211         sys.exit(0)    
212      
213         
214      
215