disconnect test
[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 sys
22 import os
23 import subprocess
24 import re
25 import shutil
26 import time
27 import pexpect
28
29 #
30 # This test tests if a fresh peer bootstraps from a hostlist server and then
31 # successfully connects to the server 
32 #
33 # Conditions for successful exit:
34 # Both peers have 1 connected peer in transport, core, topology, fs
35
36 #definitions
37
38 testname = "test_integration_bootstrap_and_connect"
39 verbose = False
40 gnunetarm = ""
41 gnunetstatistics = ""
42 success = False
43 timeout = 100
44
45 #test conditions
46
47 #server
48 server_transport_connected = False
49 server_topology_connected = False
50 server_core_connected = False
51 server_core_session_map = False
52 server_fs_connected = False
53
54 client_transport_connected = False
55 client_topology_connected = False
56 client_core_connected = False
57 client_core_session_map = False
58 client_fs_connected = False
59
60 def vprintf (msg):
61     if verbose == True:
62         print msg
63
64 def setup ():
65   srcdir = "../.."
66   gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
67   if gnunet_pyexpect_dir not in sys.path:
68     sys.path.append (gnunet_pyexpect_dir)
69   from gnunet_pyexpect import pexpect
70   global gnunetarm      
71   global gnunetstatistics
72   if os.name == 'posix':
73     gnunetarm = 'gnunet-arm'
74     gnunetstatistics = 'gnunet-statistics'
75   elif os.name == 'nt':
76     gnunetarm = 'gnunet-arm.exe'
77     gnunetstatistics = 'gnunet-statistics.exe'    
78   if os.name == "nt":
79     shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
80   else:
81     shutil.rmtree ("/tmp/" + testname, True)
82
83 def start ():
84         vprintf ("Starting bootstrap server & client")
85         try:
86             server = subprocess.Popen ([gnunetarm, '-sq', '-c', './confs/c_bootstrap_server.conf'])
87             server.communicate ()    
88         except OSError:
89             print "Can not start bootstrap server, exiting..."
90             exit (1)
91         try:
92             client = subprocess.Popen ([gnunetarm, '-sq', '-c', 'confs/c_no_nat_client.conf'])
93             client.communicate ()    
94         except OSError:
95             print "Can not start bootstrap client, exiting..."
96             exit (1)
97         vprintf ("Bootstrap server & client started")
98
99 def stop ():
100         vprintf ("Shutting down bootstrap server")
101         try:
102             server = subprocess.Popen ([gnunetarm, '-eq', '-c', './confs/c_bootstrap_server.conf'])
103             server.communicate ()    
104         except OSError:
105             print "Can not stop bootstrap server, exiting..."
106             exit (1)
107         try:
108             client = subprocess.Popen ([gnunetarm, '-eq', '-c', 'confs/c_no_nat_client.conf'])
109             client.communicate ()    
110         except OSError:
111             print "Can not stop bootstrap client, exiting..."
112             exit (1)
113         vprintf ("Bootstrap server & client stopped")
114
115
116 def cleanup ():
117         if os.name == "nt":
118             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-fs-py-ns"), True)
119             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "c_no_nat_client"), True)
120         else:
121             shutil.rmtree ("/tmp/c_bootstrap_server/", True)
122             shutil.rmtree ("/tmp/c_no_nat_client/", True)
123
124 def check_statistics (conf, subsystem, name, value):
125     from gnunet_pyexpect import pexpect
126     server = pexpect ()
127     server.spawn (None, [gnunetstatistics, '-c', conf ,'-q','-n', name, '-s', subsystem ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
128     #server.expect ("stdout", re.compile (r""))
129     test = server.read("stdout", 10240)
130     if (test.find(str(value)) == -1): 
131         return False
132     else:
133         return True 
134     
135     
136
137 def check ():
138   global success
139   global timeout
140   global publish
141   global server_transport_connected
142   global server_topology_connected
143   global server_core_connected
144   global server_core_session_map
145   global server_fs_connected
146   
147   global client_transport_connected
148   global client_topology_connected
149   global client_core_connected
150   global client_core_session_map
151   global client_fs_connected
152
153   count = 1
154   while ((success == False) and (count <= timeout)):
155         # Perform checks        
156         if ((False == server_transport_connected) and (True == check_statistics ('./confs/c_bootstrap_server.conf', 'transport', '# peers connected',1))):
157           server_transport_connected = True
158           vprintf ('Server transport services is connected')
159           
160         if ((False == client_transport_connected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'transport', '# peers connected',1))):
161           client_transport_connected = True
162           vprintf ('Client transport services is connected')
163
164         if ((False == server_core_connected) and (True == check_statistics ('./confs/c_bootstrap_server.conf', 'core', '# neighbour entries allocated',1))):
165           server_core_connected = True
166           vprintf ('Server core services is connected')
167           
168         if ((False == client_core_connected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'core', '# neighbour entries allocated',1))):
169           client_core_connected = True
170           vprintf ('Client core services is connected')
171           
172         if ((False == server_core_session_map) and (True == check_statistics ('./confs/c_bootstrap_server.conf', 'core', '# entries in session map',1))):
173           server_core_session_map = True
174           vprintf ('Server core services is connected')
175           
176         if ((False == client_core_session_map) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'core', '# entries in session map',1))):
177           client_core_session_map = True
178           vprintf ('Client core notifies about connected')
179
180         if ((False == server_topology_connected) and (True == check_statistics ('./confs/c_bootstrap_server.conf', 'topology', '# peers connected',1))):
181           server_topology_connected = True
182           vprintf ('Server topology services is connected')
183           
184         if ((False == client_topology_connected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'topology', '# peers connected',1))):
185           client_topology_connected = True
186           vprintf ('Client topology services is connected')
187           
188         if ((False == client_fs_connected) and (True == check_statistics ('./confs/c_no_nat_client.conf', 'fs', '# peers connected',1))):
189           client_fs_connected = True
190           vprintf ('Client fs services is connected')
191         if ((False == server_fs_connected) and (True == check_statistics ('./confs/c_bootstrap_server.conf', 'fs', '# peers connected',1))):
192           server_fs_connected = True
193           vprintf ('Server fs services is connected')     
194             
195         # Check if conditions fulfilled
196         if ((True == client_transport_connected) and (True == server_transport_connected) and 
197         (True == client_topology_connected) and (True == server_topology_connected) and 
198         (True == client_core_connected) and (True == server_core_connected) and
199         (True == client_core_session_map) and (True == server_core_session_map) and
200         (True == client_fs_connected) and (True == server_fs_connected)):
201                 success = True
202                 break 
203         print '.',
204         time.sleep(1)
205         count += 1
206   if (success == False):
207                 print ''
208                 if (client_transport_connected == False):
209                         print ('Client transport was NOT connected')
210                 if (server_transport_connected == False):
211                         print ('Server transport was NOT connected')
212                 if (client_topology_connected == False):
213                         print ('Client topology was NOT connected')
214                 if (server_topology_connected == False):
215                         print ('Server topology was NOT connected')
216                 if (client_core_connected == False):
217                         print ('Client core was NOT connected')
218                 if (server_core_connected == False):
219                         print ('Server core was NOT connected')
220                 if (client_core_session_map == False):
221                         print ('Client core sessions did NOT increase')
222                 if (server_core_session_map == False):
223                         print ('Server core sessions did NOT increase')
224
225
226 # Test execution
227
228
229 vprintf ("Running " + testname)
230 setup ()
231 start ()
232
233 check ()
234
235 stop ()
236 cleanup ()
237
238 if (success == False):
239         print ('Test failed')
240         exit (1)
241 else:
242         exit (0)
243
244
245