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