error handling
[oweals/gnunet.git] / src / integration-tests / test_integration_bootstrap_and_connect.py.in
1 #!@PYTHONEXE@
2 #    This file is part of GNUnet.
3 #    (C) 2010, 2018 Christian Grothoff (and other contributing authors)
4 #
5 #    GNUnet is free software: you can redistribute it and/or modify it
6 #    under the terms of the GNU Affero General Public License as published
7 #    by the Free Software Foundation, either version 3 of the License,
8 #    or (at your 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 #    Affero General Public License for more details.
14 #
15 #    You should have received a copy of the GNU Affero General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 #    SPDX-License-Identifier: AGPL3.0-or-later
19 #
20 #
21
22 import signal
23 import sys
24 import os
25 import subprocess
26 import re
27 import shutil
28 import time
29 from gnunet_testing import Peer
30 from gnunet_testing import Test
31 from gnunet_testing import Check
32 from gnunet_testing import Condition
33 from gnunet_testing import *
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     signals = [signal.SIGTERM, signal.SIGINT]
58 else:
59     tmp = "/tmp"
60     signals = [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]
61
62
63 def cleanup_onerror(function, path, excinfo):
64     import stat
65     if not os.path.exists(path):
66         pass
67     elif not os.access(path, os.W_OK):
68         # Is the error an access error ?
69         os.chmod(path, stat.S_IWUSR)
70         function(path)
71     else:
72         raise
73
74
75 def cleanup():
76     retries = 10
77     path = os.path.join(tmp, "c_bootstrap_server")
78     test.p("Removing " + path)
79     while ((os.path.exists(path)) and (retries > 0)):
80         shutil.rmtree((path), False, cleanup_onerror)
81         time.sleep(1)
82         retries -= 1
83     if (os.path.exists(path)):
84         test.p("Failed to remove " + path)
85
86     retries = 10
87     path = os.path.join(tmp, "c_no_nat_client")
88     test.p("Removing " + path)
89     while ((os.path.exists(path)) and (retries > 0)):
90         shutil.rmtree((path), False, cleanup_onerror)
91         time.sleep(1)
92         retries -= 1
93     if (os.path.exists(path)):
94         test.p("Failed to remove " + path)
95
96
97 def success_cont(check):
98     global success
99     success = True
100     print('Peers connected successfully')
101
102
103 def fail_cont(check):
104     global success
105     success = False
106     print('Peers did not connect')
107     check.evaluate(True)
108
109
110 def check():
111     check = Check(test)
112     check.add(StatisticsCondition(client, 'transport', '# peers connected', 1))
113     check.add(StatisticsCondition(client, 'core', '# peers connected', 1))
114     check.add(StatisticsCondition(client, 'topology', '# peers connected', 1))
115     check.add(StatisticsCondition(client, 'dht', '# peers connected', 1))
116     check.add(StatisticsCondition(client, 'fs', '# peers connected', 1))
117
118     check.add(StatisticsCondition(server, 'transport', '# peers connected', 1))
119     check.add(StatisticsCondition(server, 'core', '# peers connected', 1))
120     check.add(StatisticsCondition(server, 'topology', '# peers connected', 1))
121     check.add(StatisticsCondition(server, 'dht', '# peers connected', 1))
122     check.add(StatisticsCondition(server, 'fs', '# peers connected', 1))
123
124     check.run_blocking(check_timeout, success_cont, fail_cont)
125
126
127 #
128 # Test execution
129 #
130
131
132 def SigHandler(signum=None, frame=None):
133     global success
134     global server
135     global client
136
137     print('Test was aborted!')
138     if (None != server):
139         server.stop()
140     if (None != client):
141         client.stop()
142     cleanup()
143     sys.exit(success)
144
145
146 def run():
147     global success
148     global test
149     global server
150     global client
151
152     server = None
153     client = None
154     success = False
155
156     for sig in signals:
157         signal.signal(sig, SigHandler)
158
159     test = Test('test_integration_bootstrap_and_connect.py', verbose)
160     cleanup()
161
162     server = Peer(test, './confs/c_bootstrap_server.conf')
163     client = Peer(test, './confs/c_no_nat_client.conf')
164
165     if (True != server.start()):
166         print('Failed to start server')
167         if (None != server):
168             server.stop()
169         if (None != server):
170             client.stop()
171         cleanup()
172         sys.exit(success)
173
174     # Give the server time to start
175     time.sleep(5)
176
177     if (True != client.start()):
178         print('Failed to start client')
179         if (None != server):
180             server.stop()
181         if (None != server):
182             client.stop()
183         cleanup()
184         sys.exit(success)
185
186     if ((client.started == True) and (server.started == True)):
187         test.p('Peers started, running check')
188         time.sleep(5)
189         check()
190     server.stop()
191     client.stop()
192
193     cleanup()
194
195     if (success == False):
196         print('Test failed')
197         return False
198     else:
199         return True
200
201
202 try:
203     run()
204 except (KeyboardInterrupt, SystemExit):
205     print('Test interrupted')
206     server.stop()
207     client.stop()
208     cleanup()
209 if (success == False):
210     sys.exit(1)
211 else:
212     sys.exit(0)