e88c2988a9d5667ad7872b08fa27e7ead88954c2
[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
28 #
29 # This test tests if a fresh peer bootstraps from a hostlist server and then
30 # successfully connects to the server 
31 #
32
33 #definitions
34
35 testname = "test_integration_bootstrap_and_connect"
36 verbose = True
37 gnunetarm = ""
38 #fix this!
39 success = True
40 timeout = 2
41
42 def vprintf (msg):
43     if verbose == True:
44         print msg
45
46 def setup ():
47         srcdir = "../.."
48         gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
49         if gnunet_pyexpect_dir not in sys.path:
50           sys.path.append (gnunet_pyexpect_dir)
51         
52         from gnunet_pyexpect import pexpect
53
54         global gnunetarm        
55         if os.name == 'posix':
56           gnunetarm = 'gnunet-arm'
57         elif os.name == 'nt':
58           gnunetarm = 'gnunet-arm.exe'
59         
60         if os.name == "nt":
61           shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
62         else:
63           shutil.rmtree ("/tmp/" + testname, True)
64
65 def start ():
66         vprintf ("Starting bootstrap server & client")
67         try:
68             server = subprocess.Popen ([gnunetarm, '-sq', '-c', './confs/c_bootstrap_server.conf'])
69             server.communicate ()    
70         except OSError:
71             print "Can not start bootstrap server, exiting..."
72             exit (1)
73         try:
74             client = subprocess.Popen ([gnunetarm, '-sq', '-c', 'confs/c_no_nat_client.conf'])
75             client.communicate ()    
76         except OSError:
77             print "Can not start bootstrap client, exiting..."
78             exit (1)
79         vprintf ("Bootstrap server & client started")
80
81 def stop ():
82         vprintf ("Shutting down bootstrap server")
83         try:
84             server = subprocess.Popen ([gnunetarm, '-eq', '-c', './confs/c_bootstrap_server.conf'])
85             server.communicate ()    
86         except OSError:
87             print "Can not stop bootstrap server, exiting..."
88             exit (1)
89         try:
90             client = subprocess.Popen ([gnunetarm, '-eq', '-c', 'confs/c_no_nat_client.conf'])
91             client.communicate ()    
92         except OSError:
93             print "Can not stop bootstrap client, exiting..."
94             exit (1)
95         vprintf ("Bootstrap server & client stopped")
96
97
98 def cleanup ():
99         if os.name == "nt":
100             shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-fs-py-ns"), True)
101         else:
102             shutil.rmtree ("/tmp/gnunet-test-fs-py-ns", True)
103
104 def check ():
105         global success
106         global timeout
107         count = 1
108
109         while ((success == False) and (count < timeout)):
110                 time.sleep(1)
111                 count += 1 
112
113
114 # Test execution
115
116
117 vprintf ("Running " + testname)
118 setup ()
119 start ()
120
121 check ()
122
123 stop ()
124 cleanup ()
125
126 if (success == False):
127         print ('Test failed')
128         exit (1)
129 else:
130         print ('Test successful')
131         exit (0)
132
133