test now checks if boths transports are connecting
[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
31
32 #
33 # This test tests if a fresh peer bootstraps from a hostlist server and then
34 # successfully connects to the server 
35 #
36
37 #definitions
38
39 testname = "test_integration_bootstrap_and_connect"
40 verbose = True
41 gnunetarm = ""
42 gnunetstatistics = ""
43 #fix this!
44 success = False
45 timeout = 10
46
47 #test conditions
48
49 #server
50 server_transport_connected = False
51 server_topology_connected = False
52 server_core_connected = False
53
54 client_transport_connected = False
55 client_topology_connected = False
56 client_core_connected = False
57
58
59 def vprintf (msg):
60     if verbose == True:
61         print msg
62
63 def setup ():
64   srcdir = "../.."
65   gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
66   if gnunet_pyexpect_dir not in sys.path:
67     sys.path.append (gnunet_pyexpect_dir)
68   from gnunet_pyexpect import pexpect
69   global gnunetarm      
70   global gnunetstatistics
71   if os.name == 'posix':
72     gnunetarm = 'gnunet-arm'
73     gnunetstatistics = 'gnunet-statistics'
74   elif os.name == 'nt':
75     gnunetarm = 'gnunet-arm.exe'
76     gnunetstatistics = 'gnunet-statistics.exe'    
77   if os.name == "nt":
78     shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
79   else:
80     shutil.rmtree ("/tmp/" + testname, True)
81
82 def start ():
83         vprintf ("Starting bootstrap server & client")
84         try:
85             server = subprocess.Popen ([gnunetarm, '-sq', '-c', './confs/c_bootstrap_server.conf'])
86             server.communicate ()    
87         except OSError:
88             print "Can not start bootstrap server, exiting..."
89             exit (1)
90         try:
91             client = subprocess.Popen ([gnunetarm, '-sq', '-c', 'confs/c_no_nat_client.conf'])
92             client.communicate ()    
93         except OSError:
94             print "Can not start bootstrap client, exiting..."
95             exit (1)
96         vprintf ("Bootstrap server & client started")
97
98 def stop ():
99         vprintf ("Shutting down bootstrap server")
100         try:
101             server = subprocess.Popen ([gnunetarm, '-eq', '-c', './confs/c_bootstrap_server.conf'])
102             server.communicate ()    
103         except OSError:
104             print "Can not stop bootstrap server, exiting..."
105             exit (1)
106         try:
107             client = subprocess.Popen ([gnunetarm, '-eq', '-c', 'confs/c_no_nat_client.conf'])
108             client.communicate ()    
109         except OSError:
110             print "Can not stop bootstrap client, exiting..."
111             exit (1)
112         vprintf ("Bootstrap server & client stopped")
113
114
115 def cleanup ():
116         if os.name == "nt":
117             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-fs-py-ns"), True)
118         else:
119             shutil.rmtree ("/tmp/gnunet-test-fs-py-ns", True)
120
121 def check_statistics (conf, subsystem, name, value):
122     from gnunet_pyexpect import pexpect
123     server = pexpect ()
124     server.spawn (None, [gnunetstatistics, '-c', conf ,'-q','-n', name, '-s', subsystem ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
125     #server.expect ("stdout", re.compile (r""))
126     test = server.read("stdout", 10240)
127     if (test.find(str(value)) == -1): 
128         return False
129     else:
130         return True 
131     
132     
133
134 def check ():
135   global success
136   global timeout
137   global publish
138
139   count = 1
140   print 'check'
141   while ((success == False) and (count <= timeout)):
142     if (True == check_statistics ('./confs/c_bootstrap_server.conf', 'transport', '# peers connected',1)):
143         vprintf ('Server transport services is connected')
144     if (True == check_statistics ('./confs/c_no_nat_client.conf', 'transport', '# peers connected',1)):
145         vprintf ('Client transport services is connected')           
146     time.sleep(1)
147     count += 1 
148
149
150 # Test execution
151
152
153 vprintf ("Running " + testname)
154 setup ()
155 start ()
156
157 check ()
158
159 #stop ()
160 cleanup ()
161
162 if (success == False):
163         print ('Test failed')
164         exit (1)
165 else:
166         print ('Test successful')
167         exit (0)
168
169
170