attempt to fix 5560, not fixed yet. see log at https://gnunet.org/bugs/view.php?id...
[oweals/gnunet.git] / src / integration-tests / test_integration_reconnect_nat.py.in
1 #!@PYTHON@
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 from __future__ import print_function
22 import sys
23 import os
24 import subprocess
25 import re
26 import shutil
27 import time
28 import signal
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 #
37 # This test tests if a fresh peer bootstraps from a hostlist server and then
38 # successfully connects to the server. When both peers are connected
39 # in transport, core, topology, fs, botth peers are shutdown and restarted
40 #
41 # Conditions for successful exit:
42 # Both peers have 1 connected peer in transport, core, topology, fs after restart
43
44 # definitions
45
46
47 testname = "test_integration_restart"
48 verbose = True
49 check_timeout = 180
50
51 if os.name == "nt":
52     tmp = os.getenv("TEMP")
53     signals = [signal.SIGTERM, signal.SIGINT]
54 else:
55     tmp = "/tmp"
56     signals = [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]
57
58
59 def cleanup_onerror(function, path, excinfo):
60     import stat
61     if not os.path.exists(path):
62         pass
63     elif not os.access(path, os.W_OK):
64         # Is the error an access error ?
65         os.chmod(path, stat.S_IWUSR)
66         function(path)
67     else:
68         raise
69
70
71 def cleanup():
72     retries = 10
73     path = os.path.join(tmp, "c_bootstrap_server")
74     test.p("Removing " + path)
75     while ((os.path.exists(path)) and (retries > 0)):
76         shutil.rmtree((path), False, cleanup_onerror)
77         time.sleep(1)
78         retries -= 1
79     if (os.path.exists(path)):
80         test.p("Failed to remove " + path)
81
82     retries = 10
83     path = os.path.join(tmp, "c_nat_client")
84     test.p("Removing " + path)
85     while((os.path.exists(path)) and(retries > 0)):
86         shutil.rmtree((path), False, cleanup_onerror)
87         time.sleep(1)
88         retries -= 1
89     if (os.path.exists(path)):
90         test.p("Failed to remove " + path)
91
92
93 def success_restart_cont(check):
94         global success
95         print('Peers connected successfully after restart')
96         server.stop()
97         client.stop()
98         success = True
99
100
101 def fail_restart_cont(check):
102         global success
103         success = False
104         print('Peers failed to connect after restart')
105         check.evaluate(True)
106
107
108 def success_connect_cont(check):
109     print('Peers connected successfully')
110     server.stop()
111     client.stop()
112
113     time.sleep(5)
114
115     test.p('Restarting client & server')
116     server.start()
117     client.start()
118
119     check = Check(test)
120     check.add(StatisticsCondition(client, 'transport', '# peers connected', 1))
121     check.add(StatisticsCondition(client, 'core', '# peers connected', 1))
122     check.add(StatisticsCondition(client, 'topology', '# peers connected', 1))
123     check.add(StatisticsCondition(client, 'fs', '# peers connected', 1))
124
125     check.add(StatisticsCondition(server, 'transport', '# peers connected', 1))
126     check.add(StatisticsCondition(server, 'core', '# peers connected', 1))
127     check.add(StatisticsCondition(server, 'topology', '# peers connected', 1))
128     check.add(StatisticsCondition(server, 'fs', '# peers connected', 1))
129
130     check.run_blocking(check_timeout, success_restart_cont, fail_restart_cont)
131
132
133 def fail_connect_cont(check):
134     global success
135     success = False
136     print('Peers failed to connect')
137     check.evaluate(True)
138
139
140 def check_connect():
141     check = Check(test)
142     check.add(StatisticsCondition(client, 'transport', '# peers connected', 1))
143     check.add(StatisticsCondition(client, 'core', '# peers connected', 1))
144     check.add(StatisticsCondition(client, 'topology', '# peers connected', 1))
145     check.add(StatisticsCondition(client, 'fs', '# peers connected', 1))
146
147     check.add(StatisticsCondition(server, 'transport', '# peers connected', 1))
148     check.add(StatisticsCondition(server, 'core', '# peers connected', 1))
149     check.add(StatisticsCondition(server, 'topology', '# peers connected', 1))
150     check.add(StatisticsCondition(server, 'fs', '# peers connected', 1))
151
152     check.run_blocking(check_timeout, success_connect_cont, fail_connect_cont)
153
154 #
155 # Test execution
156 #
157
158
159 def SigHandler(signum=None, frame=None):
160     global success
161     global server
162     global client
163
164     print('Test was aborted!')
165     if (None != server):
166         server.stop()
167     if (None != client):
168         client.stop()
169     cleanup()
170     sys.exit(success)
171
172
173 def run():
174     global success
175     global test
176     global server
177     global client
178
179     success = False
180     server = None
181     client = None
182
183     for sig in signals:
184         signal.signal(sig, SigHandler)
185
186     test = Test('test_integration_disconnect', verbose)
187     cleanup()
188     server = Peer(test, './confs/c_bootstrap_server.conf')
189     server.start()
190
191     client = Peer(test, './confs/c_nat_client.conf')
192     client.start()
193
194     if (True != server.start()):
195         print('Failed to start server')
196         if (None != server):
197             server.stop()
198         if (None != server):
199             client.stop()
200         cleanup()
201         sys.exit(success)
202
203     # Give the server time to start
204     time.sleep(5)
205
206     if (True != client.start()):
207         print('Failed to start client')
208         if (None != server):
209             server.stop()
210         if (None != server):
211             client.stop()
212         cleanup()
213         sys.exit(success)
214
215     check_connect()
216
217     server.stop()
218     client.stop()
219     cleanup()
220
221     if (success == False):
222         print('Test failed')
223         return True
224     else:
225         return False
226
227
228 try:
229     run()
230 except(KeyboardInterrupt, SystemExit):
231     print('Test interrupted')
232     server.stop()
233     client.stop()
234     cleanup()
235 if (success == False):
236     sys.exit(1)
237 else:
238     sys.exit(0)