error handling
[oweals/gnunet.git] / src / integration-tests / test_integration_clique.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 # This test starts 3 peers (nated, server, no nat)and expects bootstrap
23 # and a connected clique
24 #
25 # Conditions for successful exit:
26 # Both peers have 2 connected peers in transport, core, topology, fs and dht
27
28 import sys
29 import signal
30 import os
31 import subprocess
32 import re
33 import shutil
34 import time
35 from gnunet_testing import Peer
36 from gnunet_testing import Test
37 from gnunet_testing import Check
38 from gnunet_testing import Condition
39 from gnunet_testing import *
40
41 if os.name == "nt":
42     tmp = os.getenv("TEMP")
43 else:
44     tmp = "/tmp"
45
46 # definitions
47
48 testname = "test_integration_clique"
49 verbose = True
50 check_timeout = 180
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     retries = 10
76     path = os.path.join(tmp, "c_no_nat_client")
77     test.p("Removing " + path)
78     while ((os.path.exists(path)) and (retries > 0)):
79         shutil.rmtree((path), False, cleanup_onerror)
80         time.sleep(1)
81         retries -= 1
82     if (os.path.exists(path)):
83         test.p("Failed to remove " + path)
84         retries = 10
85     path = os.path.join(tmp, "c_nat_client")
86     test.p("Removing " + path)
87     while ((os.path.exists(path)) and (retries > 0)):
88         shutil.rmtree((path), False, cleanup_onerror)
89         time.sleep(1)
90         retries -= 1
91     if (os.path.exists(path)):
92         test.p("Failed to remove " + path)
93
94
95 def success_cont(check):
96     global success
97     success = True
98     print('Connected clique successfully')
99
100
101 def fail_cont(check):
102     global success
103     success = False
104     check.evaluate(True)
105     print('Failed to connect clique')
106
107
108 def check_connect():
109     check = Check(test)
110     check.add(StatisticsCondition(client, 'transport', '# peers connected', 2))
111     check.add(StatisticsCondition(client, 'core', '# peers connected', 2))
112     check.add(StatisticsCondition(client, 'topology', '# peers connected', 2))
113     check.add(StatisticsCondition(client, 'dht', '# peers connected', 2))
114     check.add(StatisticsCondition(client, 'fs', '# peers connected', 2))
115
116     check.add(
117         StatisticsCondition(client_nat, 'transport', '# peers connected', 2)
118     )
119     check.add(StatisticsCondition(client_nat, 'core', '# peers connected', 2))
120     check.add(
121         StatisticsCondition(client_nat, 'topology', '# peers connected', 2)
122     )
123     check.add(StatisticsCondition(client_nat, 'dht', '# peers connected', 2))
124     check.add(StatisticsCondition(client_nat, 'fs', '# peers connected', 2))
125
126     check.add(StatisticsCondition(server, 'transport', '# peers connected', 2))
127     check.add(StatisticsCondition(server, 'core', '# peers connected', 2))
128     check.add(StatisticsCondition(server, 'topology', '# peers connected', 2))
129     check.add(StatisticsCondition(server, 'dht', '# peers connected', 2))
130     check.add(StatisticsCondition(server, 'fs', '# peers connected', 2))
131
132     check.run_blocking(check_timeout, success_cont, fail_cont)
133
134
135 #
136 # Test execution
137 #
138
139
140 def SigHandler(signum=None, frame=None):
141     global success
142     global server
143     global client
144     global client_nat
145
146     print('Test was aborted!')
147     if (None != server):
148         server.stop()
149     if (None != client):
150         client.stop()
151     if (None != client_nat):
152         client_nat.stop()
153     cleanup()
154     sys.exit(success)
155
156
157 def run():
158     global success
159     global test
160     global server
161     global client
162     global client_nat
163
164     success = False
165     server = None
166     client = None
167     client_nat = None
168     test = Test('test_integration_clique', verbose)
169     cleanup()
170
171     server = Peer(test, './confs/c_bootstrap_server.conf')
172     if (True != server.start()):
173         print('Failed to start server')
174         if (None != server):
175             server.stop()
176         cleanup()
177         sys.exit(success)
178
179     # Server has to settle down
180     time.sleep(5)
181
182     client = Peer(test, './confs/c_no_nat_client.conf')
183     if (True != client.start()):
184         print('Failed to start client')
185         if (None != server):
186             server.stop()
187         if (None != client):
188             client.stop()
189         cleanup()
190         sys.exit(success)
191
192     # Server has to settle down
193     time.sleep(5)
194
195     client_nat = Peer(test, './confs/c_nat_client.conf')
196     if (True != client_nat.start()):
197         print('Failed to start client_nat')
198         if (None != server):
199             server.stop()
200         if (None != client):
201             client.stop()
202         if (None != client_nat):
203             client_nat.stop()
204         cleanup()
205         sys.exit(success)
206
207     if ((client.started == True) and (client_nat.started == True)
208         and (server.started == True)):
209         test.p('Peers started, running check')
210         check_connect()
211
212     server.stop()
213     client.stop()
214     client_nat.stop()
215
216     cleanup()
217
218     if (success == False):
219         print('Test failed')
220         return False
221     else:
222         return True
223
224
225 try:
226     run()
227 except (KeyboardInterrupt, SystemExit):
228     print('Test interrupted')
229     server.stop()
230     client.stop()
231     client_nat.stop()
232     cleanup()
233 if (success == False):
234     sys.exit(1)
235 else:
236     sys.exit(0)