-handle new error code from enum
[oweals/gnunet.git] / src / integration-tests / test_integration_bootstrap_and_connect.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 import signal
22 import sys
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 
38 #
39 # Conditions for successful exit:
40 # Both peers have 1 connected peer in transport, core, topology, fs
41
42 #
43 # This test tests if a fresh peer bootstraps from a hostlist server and then
44 # successfully connects to the server 
45 #
46 # Conditions for successful exit:
47 # Both peers have 1 connected peer in transport, core, topology, fs
48
49 #definitions
50
51 testname = "test_integration_bootstrap_and_connect"
52 verbose = False
53 check_timeout = 180
54
55 if os.name == "nt":
56   tmp = os.getenv ("TEMP")
57 else:
58   tmp = "/tmp"
59
60 def cleanup ():
61     retries = 10
62     path = os.path.join (tmp, "c_bootstrap_server")  
63     test.p ("Removing " + path)      
64     while ((os.path.exists(path)) and (retries > 0)):
65         shutil.rmtree ((path), False)
66         time.sleep (1)
67         retries -= 1
68     if (os.path.exists(path)):
69         test.p ("Failed to remove " + path) 
70     
71     
72     retries = 10
73     path = os.path.join (tmp, "c_no_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 def success_cont (check):
83     global success
84     success = True;
85     print 'Peers connected successfully'
86     
87 def fail_cont (check):    
88     global success 
89     success = False;
90     print 'Peers did not connect'
91     check.evaluate(True)
92
93 def check ():
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, 'dht', '# peers connected',1))  
100   check.add (StatisticsCondition (client, 'fs', '# peers connected',1))
101   
102   check.add (StatisticsCondition (server, 'transport', '# peers connected',1))
103   check.add (StatisticsCondition (server, 'core', '# neighbour entries allocated',1))  
104   check.add (StatisticsCondition (server, 'core', '# peers connected',1))
105   check.add (StatisticsCondition (server, 'topology', '# peers connected',1))
106   check.add (StatisticsCondition (server, 'dht', '# peers connected',1))  
107   check.add (StatisticsCondition (server, 'fs', '# peers connected',1))  
108   
109   check.run_blocking (check_timeout, success_cont, fail_cont)
110
111
112 # Test execution
113
114
115 def SigHandler(signum = None, frame = None):
116         global success  
117         global server
118         global client  
119         
120         print 'Test was aborted!'
121         if (None != server):
122                 server.stop ()
123         if (None != client):            
124                 client.stop ()
125         cleanup ()
126         sys.exit(success)
127
128 def run ():
129         global success
130         global test
131         global server
132         global client    
133         
134         server = None
135         client = None
136         success = False  
137         
138         for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
139                 signal.signal(sig, SigHandler)
140
141         test = Test ('test_integration_bootstrap_and_connect.py', verbose)
142         cleanup ()
143         
144         server = Peer(test, './confs/c_bootstrap_server.conf');
145         client = Peer(test, './confs/c_no_nat_client.conf');
146         
147         if (True != server.start()):
148                 print 'Failed to start server'
149                 if (None != server):
150                         server.stop ()
151                 if (None != server):            
152                         client.stop ()
153                 cleanup ()
154                 sys.exit(success)
155                 
156         # Give the server time to start
157         time.sleep(5)
158         
159         if (True != client.start()):
160                 print 'Failed to start client'
161                 if (None != server):
162                         server.stop ()
163                 if (None != server):            
164                         client.stop ()
165                 cleanup ()
166                 sys.exit(success)
167         
168         if ((client.started == True) and (server.started == True)):
169                 test.p ('Peers started, running check')
170                 time.sleep(5)
171                 check ()
172         server.stop ()
173         client.stop ()
174         
175         cleanup ()
176         
177         if (success == False):
178                 print ('Test failed')
179                 return False 
180         else:
181                 return True
182
183 try:
184         run ()
185 except (KeyboardInterrupt, SystemExit):    
186         print 'Test interrupted'
187         server.stop ()
188         client.stop ()
189         cleanup ()
190 if (success == False):
191         sys.exit(1)   
192 else:
193         sys.exit(0)    
194