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