error handling
[oweals/gnunet.git] / src / integration-tests / test_integration_disconnect_nat.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 sys
23 import signal
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. When both peers are connected
38 # in transport, core, topology, fs, the server is shutdown
39 #
40 # Conditions for successful exit:
41 # Client peer has 0 connected peer in transport, core, topology, dht, fs
42
43 # definitions
44
45 testname = "test_integration_disconnect"
46 verbose = True
47 check_timeout = 180
48
49 if os.name == "nt":
50     tmp = os.getenv("TEMP")
51     signals = [signal.SIGTERM, signal.SIGINT]
52 else:
53     tmp = "/tmp"
54     signals = [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]
55
56
57 def cleanup_onerror(function, path, excinfo):
58     import stat
59     if not os.path.exists(path):
60         pass
61     elif not os.access(path, os.W_OK):
62         # Is the error an access error ?
63         os.chmod(path, stat.S_IWUSR)
64         function(path)
65     else:
66         raise
67
68
69 def cleanup():
70     shutil.rmtree(
71         os.path.join(tmp, "c_bootstrap_server"), False, cleanup_onerror
72     )
73     shutil.rmtree(os.path.join(tmp, "c_nat_client"), False, cleanup_onerror)
74
75
76 def success_disconnect_cont(check):
77     print('Peers disconnected successfully')
78     global success
79     success = True
80
81
82 def fail_disconnect_cont(check):
83     global success
84     success = False
85     print('Peers failed to disconnect')
86     check.evaluate(True)
87
88
89 def check_disconnect():
90     global server
91     global nat_client
92     test.p('Shutting down nat client')
93     nat_client.stop()
94     check = Check(test)
95     check.add(StatisticsCondition(server, 'transport', '# peers connected', 0))
96     check.add(StatisticsCondition(server, 'core', '# peers connected', 0))
97     check.add(StatisticsCondition(server, 'topology', '# peers connected', 0))
98     check.add(StatisticsCondition(server, 'dht', '# peers connected', 0))
99     check.add(StatisticsCondition(server, 'fs', '# peers connected', 0))
100     check.run_blocking(
101         check_timeout, success_disconnect_cont, fail_disconnect_cont
102     )
103
104
105 def success_connect_cont(check):
106     print('Peers connected successfully')
107     check_disconnect()
108
109
110 def fail_connect_cont(check):
111     global success
112     success = False
113     print('Peers failed to connected!')
114     check.evaluate(True)
115
116
117 def check_connect():
118     global server
119     global nat_client
120     check = Check(test)
121     check.add(
122         StatisticsCondition(nat_client, 'transport', '# peers connected', 1)
123     )
124     check.add(StatisticsCondition(nat_client, 'core', '# peers connected', 1))
125     check.add(
126         StatisticsCondition(nat_client, 'topology', '# peers connected', 1)
127     )
128     check.add(StatisticsCondition(nat_client, 'dht', '# peers connected', 1))
129     check.add(StatisticsCondition(nat_client, 'fs', '# peers connected', 1))
130
131     check.add(StatisticsCondition(server, 'transport', '# peers connected', 1))
132     check.add(StatisticsCondition(server, 'core', '# peers connected', 1))
133     check.add(StatisticsCondition(server, 'topology', '# peers connected', 1))
134     check.add(StatisticsCondition(server, 'dht', '# peers connected', 1))
135     check.add(StatisticsCondition(server, 'fs', '# peers connected', 1))
136
137     check.run_blocking(check_timeout, success_connect_cont, fail_connect_cont)
138
139
140 #
141 # Test execution
142 #
143
144
145 def SigHandler(signum=None, frame=None):
146     global success
147     global server
148     global nat_client
149
150     print('Test was aborted!')
151     if (None != server):
152         server.stop()
153     if (None != nat_client):
154         nat_client.stop()
155     cleanup()
156     sys.exit(success)
157
158
159 def run():
160     global success
161     global test
162     global server
163     global nat_client
164
165     server = None
166     nat_client = None
167     success = False
168
169     for sig in signals:
170         signal.signal(sig, SigHandler)
171
172     test = Test('test_integration_bootstrap_and_connect.py', verbose)
173     cleanup()
174
175     server = Peer(test, './confs/c_bootstrap_server.conf')
176     nat_client = Peer(test, './confs/c_nat_client.conf')
177
178     if (True != server.start()):
179         print('Failed to start server')
180         if (None != server):
181             server.stop()
182         cleanup()
183         sys.exit(success)
184
185     # Give the server time to start
186     time.sleep(5)
187
188     if (True != nat_client.start()):
189         print('Failed to start nat_client')
190         if (None != server):
191             server.stop()
192         if (None != nat_client):
193             nat_client.stop()
194         cleanup()
195         sys.exit(success)
196
197     if ((nat_client.started == True) and (server.started == True)):
198         test.p('Peers started, running check')
199         time.sleep(5)
200         check_connect()
201     server.stop()
202     nat_client.stop()
203
204     cleanup()
205
206     if (success == False):
207         print('Test failed')
208         return False
209     else:
210         return True
211
212
213 try:
214     run()
215 except (KeyboardInterrupt, SystemExit):
216     print('Test interrupted')
217     server.stop()
218     nat_client.stop()
219     cleanup()
220 if (success == False):
221     sys.exit(1)
222 else:
223     sys.exit(0)